43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class ContactService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async createMessage(data: { name: string; email: string; phone?: string; message: string }) {
|
|
return this.prisma.contactMessage.create({ data });
|
|
}
|
|
|
|
async getAllMessages(isRead?: boolean) {
|
|
const where = isRead !== undefined ? { isRead } : {};
|
|
return this.prisma.contactMessage.findMany({
|
|
where,
|
|
orderBy: { createdAt: 'desc' },
|
|
});
|
|
}
|
|
|
|
async markAsRead(id: string) {
|
|
const msg = await this.prisma.contactMessage.findUnique({ where: { id } });
|
|
if (!msg) throw new NotFoundException('Contact message not found');
|
|
return this.prisma.contactMessage.update({
|
|
where: { id },
|
|
data: { isRead: true },
|
|
});
|
|
}
|
|
|
|
async deleteMessage(id: string) {
|
|
const msg = await this.prisma.contactMessage.findUnique({ where: { id } });
|
|
if (!msg) throw new NotFoundException('Contact message not found');
|
|
return this.prisma.contactMessage.delete({ where: { id } });
|
|
}
|
|
|
|
async getCounts() {
|
|
const [total, unread] = await Promise.all([
|
|
this.prisma.contactMessage.count(),
|
|
this.prisma.contactMessage.count({ where: { isRead: false } }),
|
|
]);
|
|
return { total, unread };
|
|
}
|
|
}
|