feat: add conversation module
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Conversation } from './entities/conversation.entity';
|
||||
import { Message } from './entities/message.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Conversation, Message])],
|
||||
})
|
||||
export class ConversationModule {}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { User } from '@/modules/users/entities/user.entity';
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
import { Message } from './message.entity';
|
||||
|
||||
@Entity()
|
||||
export class Conversation {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@Column({ type: 'varchar', nullable: true })
|
||||
title!: string | null;
|
||||
|
||||
@ManyToOne(() => User, { onDelete: 'CASCADE' })
|
||||
user!: User;
|
||||
|
||||
@OneToMany(() => Message, (message) => message.conversation)
|
||||
messages!: Message[];
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt!: Date;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
import { Conversation } from './conversation.entity';
|
||||
import { MessageRole } from '../enums/message-role.enum';
|
||||
|
||||
@Entity()
|
||||
export class Message {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
|
||||
@ManyToOne(() => Conversation, (conversation) => conversation.messages, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
conversation!: Conversation;
|
||||
|
||||
@Column({
|
||||
type: 'enum',
|
||||
enum: MessageRole,
|
||||
nullable: false,
|
||||
})
|
||||
role!: MessageRole;
|
||||
|
||||
@Column({ type: 'text' })
|
||||
content!: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum ConversationType {
|
||||
AI = 'ai',
|
||||
DOCTOR = 'doctor',
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum MessageRole {
|
||||
USER = 'user',
|
||||
ASSISTANT = 'assistant',
|
||||
DOCTOR = 'doctor',
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Conversation } from '../entities/conversation.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Message } from '../entities/message.entity';
|
||||
import { User } from '@/modules/users/entities/user.entity';
|
||||
import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception';
|
||||
|
||||
@Injectable()
|
||||
export class ConversationService {
|
||||
constructor(
|
||||
/**
|
||||
* Inject Conversation Repository
|
||||
*/
|
||||
@InjectRepository(Conversation)
|
||||
private readonly conversationRepository: Repository<Conversation>,
|
||||
|
||||
/**
|
||||
* Inject Message Repository
|
||||
*/
|
||||
@InjectRepository(Message)
|
||||
private readonly messageRepository: Repository<Message>,
|
||||
) {}
|
||||
|
||||
public async createConversation(
|
||||
user: User,
|
||||
title?: string,
|
||||
): Promise<Conversation> {
|
||||
const conversation = this.conversationRepository.create({
|
||||
user,
|
||||
title: title ?? null,
|
||||
});
|
||||
|
||||
try {
|
||||
return await this.conversationRepository.save(conversation);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
// public async findConversationById(id: number, user: User): Promise<Conversation> {
|
||||
// const
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user