api-server/src/modules/admin-conversation/admin-conversation.service.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { randomUUID } from 'crypto';
@Injectable()
export class AdminConversationService {
constructor(private readonly prisma: PrismaService) {}
async list(adminUserId: string) {
return this.prisma.adminConversation.findMany({
where: { adminUserId, deletedAt: null },
orderBy: { updatedAt: 'desc' },
select: { id: true, title: true, createdAt: true, updatedAt: true },
});
}
async create(adminUserId: string, title?: string) {
const hermesSessionId = randomUUID();
return this.prisma.adminConversation.create({
data: { adminUserId, hermesSessionId, title: title || '新对话' },
select: { id: true, title: true, hermesSessionId: true, createdAt: true },
});
}
async update(id: string, adminUserId: string, title: string) {
return this.prisma.adminConversation.updateMany({
where: { id, adminUserId, deletedAt: null },
data: { title },
});
}
async delete(id: string, adminUserId: string) {
return this.prisma.adminConversation.updateMany({
where: { id, adminUserId, deletedAt: null },
data: { deletedAt: new Date() },
});
}
async getSessionId(conversationId: string, adminUserId: string) {
const conv = await this.prisma.adminConversation.findFirst({
where: { id: conversationId, adminUserId, deletedAt: null },
select: { id: true, hermesSessionId: true },
});
return conv?.hermesSessionId ?? null;
}
}