feat: Add contact form submission and message management functionality.
This commit is contained in:
42
src/contact/contact.service.ts
Normal file
42
src/contact/contact.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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 };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user