140 lines
4.0 KiB
TypeScript
140 lines
4.0 KiB
TypeScript
import { Injectable, Logger } from "@nestjs/common";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { SmsService } from "../common/services/sms.service";
|
|
|
|
export interface CreateContactSubmissionDto {
|
|
name: string;
|
|
phone: string;
|
|
email?: string;
|
|
subject?: string;
|
|
message: string;
|
|
}
|
|
|
|
export interface UpdateContactInfoItemDto {
|
|
key: string;
|
|
title: string;
|
|
value: string;
|
|
icon?: string;
|
|
order?: number;
|
|
}
|
|
|
|
@Injectable()
|
|
export class ContactService {
|
|
private readonly logger = new Logger(ContactService.name);
|
|
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly smsService: SmsService,
|
|
) {}
|
|
|
|
async submitContactForm(dto: CreateContactSubmissionDto) {
|
|
const submission = await this.prisma.contactSubmission.create({
|
|
data: {
|
|
name: dto.name,
|
|
phone: dto.phone,
|
|
email: dto.email,
|
|
subject: dto.subject,
|
|
message: dto.message,
|
|
},
|
|
});
|
|
|
|
// Send SMS confirmation & notifications via triggerEvent
|
|
try {
|
|
await this.smsService.triggerEvent("CONTACT_SUBMITTED", {
|
|
senderName: dto.name,
|
|
customerName: dto.name,
|
|
senderPhone: dto.phone,
|
|
customerPhone: dto.phone,
|
|
email: dto.email || "",
|
|
subject: dto.subject || "پیام جدید",
|
|
message: dto.message || "",
|
|
date: new Date().toLocaleDateString("fa-IR"),
|
|
});
|
|
} catch (err) {
|
|
this.logger.error(
|
|
`SMS trigger error on contact submission: ${(err as Error).message}`,
|
|
);
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
message:
|
|
"پیام شما با موفقیت ثبت شد و بهزودی کارشناسان ما با شما تماس خواهند گرفت.",
|
|
submissionId: submission.id,
|
|
};
|
|
}
|
|
|
|
async getContactInfo() {
|
|
const settings = await this.prisma.setting.findMany({
|
|
where: { key: { in: ["CONTACT_PHONE", "contact_phone", "CONTACT_EMAIL", "contact_email", "CONTACT_ADDRESS", "contact_address"] } },
|
|
select: { key: true, value: true },
|
|
});
|
|
const values = new Map(settings.map((item) => [item.key, String(item.value ?? "")]));
|
|
const read = (...keys: string[]) => keys.map((key) => values.get(key)).find((value) => value?.trim()) || "";
|
|
const phone = read("CONTACT_PHONE", "contact_phone");
|
|
const email = read("CONTACT_EMAIL", "contact_email");
|
|
const address = read("CONTACT_ADDRESS", "contact_address");
|
|
return [
|
|
{ key: "phone", title: "تلفنهای تماس", value: phone, icon: "phone", order: 1 },
|
|
{ key: "email", title: "پست الکترونیک", value: email, icon: "mail", order: 2 },
|
|
{ key: "address", title: "نشانی دفتر مرکزی", value: address, icon: "map-pin", order: 3 },
|
|
].filter((item) => item.value.trim());
|
|
}
|
|
|
|
async getAllSubmissions(page = 1, limit = 20, status?: string) {
|
|
const skip = (page - 1) * limit;
|
|
const where = status ? { status } : {};
|
|
|
|
const [items, total] = await Promise.all([
|
|
this.prisma.contactSubmission.findMany({
|
|
where,
|
|
skip,
|
|
take: limit,
|
|
orderBy: { createdAt: "desc" },
|
|
}),
|
|
this.prisma.contactSubmission.count({ where }),
|
|
]);
|
|
|
|
return {
|
|
items,
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
};
|
|
}
|
|
|
|
async updateSubmissionStatus(
|
|
id: string,
|
|
status: string,
|
|
adminNotes?: string,
|
|
) {
|
|
return this.prisma.contactSubmission.update({
|
|
where: { id },
|
|
data: { status, adminNotes },
|
|
});
|
|
}
|
|
|
|
async updateContactInfoItems(items: UpdateContactInfoItemDto[]) {
|
|
for (const item of items) {
|
|
await this.prisma.contactInfo.upsert({
|
|
where: { key: item.key },
|
|
update: {
|
|
title: item.title,
|
|
value: item.value,
|
|
icon: item.icon,
|
|
order: item.order ?? 0,
|
|
},
|
|
create: {
|
|
key: item.key,
|
|
title: item.title,
|
|
value: item.value,
|
|
icon: item.icon,
|
|
order: item.order ?? 0,
|
|
},
|
|
});
|
|
}
|
|
return this.getContactInfo();
|
|
}
|
|
}
|