feat: connect chatbot to conversation database
This commit is contained in:
@@ -2,8 +2,11 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Conversation } from './entities/conversation.entity';
|
||||
import { Message } from './entities/message.entity';
|
||||
import { ConversationService } from './providers/conversation.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Conversation, Message])],
|
||||
providers: [ConversationService],
|
||||
exports: [ConversationService],
|
||||
})
|
||||
export class ConversationModule {}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpStatus, 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';
|
||||
import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { MessageRole } from '../enums/message-role.enum';
|
||||
|
||||
@Injectable()
|
||||
export class ConversationService {
|
||||
@@ -38,7 +40,72 @@ export class ConversationService {
|
||||
}
|
||||
}
|
||||
|
||||
// public async findConversationById(id: number, user: User): Promise<Conversation> {
|
||||
// const
|
||||
// }
|
||||
public async findConversationById(
|
||||
id: number,
|
||||
user: User,
|
||||
): Promise<Conversation> {
|
||||
let conversation: Conversation | null = null;
|
||||
|
||||
try {
|
||||
conversation = await this.conversationRepository.findOne({
|
||||
where: {
|
||||
id,
|
||||
user: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
if (!conversation) {
|
||||
throw new AppException(
|
||||
'common.errors.entityNotFound',
|
||||
HttpStatus.NOT_FOUND,
|
||||
{ entity: '$t(conversations.fields.conversation)' },
|
||||
);
|
||||
}
|
||||
|
||||
return conversation;
|
||||
}
|
||||
|
||||
public async addMessage(
|
||||
conversation: Conversation,
|
||||
role: MessageRole,
|
||||
content: string,
|
||||
): Promise<Message> {
|
||||
const message = this.messageRepository.create({
|
||||
conversation,
|
||||
role,
|
||||
content,
|
||||
});
|
||||
|
||||
try {
|
||||
return await this.messageRepository.save(message);
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async getMessages(conversation: Conversation): Promise<Message[]> {
|
||||
let messages: Array<Message> = [];
|
||||
|
||||
try {
|
||||
messages = await this.messageRepository.find({
|
||||
where: {
|
||||
conversation: {
|
||||
id: conversation.id,
|
||||
},
|
||||
},
|
||||
order: {
|
||||
createdAt: 'ASC',
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
throw new AppTimeoutException(err);
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user