Files
petmaster/api/src/messages/messages.service.ts
T

42 lines
1.0 KiB
TypeScript
Raw Normal View History

import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { SendMessageDto } from './dto/message.dto';
@Injectable()
export class MessagesService {
constructor(private prisma: PrismaService) {}
async getConversation(clientId: string, staffId: string) {
await this.prisma.client.findUniqueOrThrow({ where: { id: clientId } });
return this.prisma.message.findMany({
where: {
clientId,
},
orderBy: { createdAt: 'asc' },
});
}
async getUnreadCount(clientId: string, staffId: string) {
const count = await this.prisma.message.count({
where: {
clientId,
readAt: null,
},
});
return { unreadCount: count };
}
async markAsRead(clientId: string, messageIds: string[]) {
await this.prisma.client.findUniqueOrThrow({ where: { id: clientId } });
return this.prisma.message.updateMany({
where: {
id: { in: messageIds },
clientId,
},
data: { readAt: new Date() },
});
}
}