feat(contact): implement contact form submission, admin management and dynamic contact info

This commit is contained in:
parsa aghaei committed 2026-08-01 18:28:19 +03:30
1 parent 49049a7f78
commit 2aea3bded4
11 files changed
+961 -77

No files matched your search

+2
View File
@@ -20,10 +20,12 @@ import { CmsModule } from './cms/cms.module';
import { WholesaleModule } from './wholesale/wholesale.module';
import { VideosModule } from './videos/videos.module';
import { SmsModule } from './common/sms.module';
import { ContactModule } from './contact/contact.module';
@Module({
imports: [
SmsModule,
ContactModule,
PrismaModule,
RedisModule,
ProductsModule,
+86
View File
@@ -0,0 +1,86 @@
import {
Controller,
Get,
Post,
Put,
Body,
Param,
Query,
UseGuards,
} from '@nestjs/common';
import { ContactService } from './contact.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../auth/roles.guard';
import { Roles } from '../auth/roles.decorator';
@Controller('contact')
export class ContactController {
constructor(private readonly contactService: ContactService) {}
@Post()
async submitContact(
@Body()
body: {
name: string;
phone: string;
email?: string;
subject?: string;
message: string;
},
) {
return this.contactService.submitContactForm(body);
}
@Get('info')
async getContactInfo() {
return this.contactService.getContactInfo();
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin', 'SuperAdmin')
@Get('submissions')
async getAllSubmissions(
@Query('page') page?: string,
@Query('limit') limit?: string,
@Query('status') status?: string,
) {
return this.contactService.getAllSubmissions(
page ? parseInt(page, 10) : 1,
limit ? parseInt(limit, 10) : 20,
status,
);
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin', 'SuperAdmin')
@Put('submissions/:id')
async updateSubmissionStatus(
@Param('id') id: string,
@Body() body: { status: string; adminNotes?: string },
) {
return this.contactService.updateSubmissionStatus(
id,
body.status,
body.adminNotes,
);
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin', 'SuperAdmin')
@Put('info')
async updateContactInfo(
@Body()
body: {
items: Array<{
key: string;
title: string;
value: string;
icon?: string;
order?: number;
}>;
},
) {
return this.contactService.updateContactInfoItems(body.items);
}
}
+13
View File
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { ContactController } from './contact.controller';
import { ContactService } from './contact.service';
import { SmsModule } from '../common/sms.module';
@Module({
imports: [SmsModule],
controllers: [ContactController],
providers: [ContactService],
exports: [ContactService],
})
export class ContactModule {}
+166
View File
@@ -0,0 +1,166 @@
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 to User & notification to Admin
try {
const userPatternId = parseInt(process.env.MELIPAYAMAK_CONTACT_USER_BODY_ID || '508081', 10);
await this.smsService.sendPatternSms({
to: dto.phone,
bodyId: userPatternId,
args: [dto.name, 'فرم تماس'],
});
const adminPhone = process.env.ADMIN_MOBILE || '09364100228';
const adminPatternId = parseInt(process.env.MELIPAYAMAK_CONTACT_ADMIN_BODY_ID || '508083', 10);
await this.smsService.sendPatternSms({
to: adminPhone,
bodyId: adminPatternId,
args: [dto.name, dto.phone],
});
} catch (err) {
this.logger.error(`SMS trigger error on contact submission: ${err.message}`);
}
return {
success: true,
message: 'پیام شما با موفقیت ثبت شد و به‌زودی کارشناسان ما با شما تماس خواهند گرفت.',
submissionId: submission.id,
};
}
async getContactInfo() {
let items = await this.prisma.contactInfo.findMany({
orderBy: { order: 'asc' },
});
if (items.length === 0) {
// Seed default items if empty
const defaultItems = [
{
key: 'branch_info',
title: 'اطلاعات نمایندگی',
value: 'تلفن‌های تماس\n۰۲۱-۸۸۸۸ ۴۴۴۴\n\nشنبه تا چهارشنبه ۹:۰۰ الی ۱۸:۰۰',
icon: 'phone',
order: 1,
},
{
key: 'email_info',
title: 'پست الکترونیک',
value: 'info@canina-iran.com\n\nپاسخگویی در کمتر از ۲۴ ساعت کاری',
icon: 'mail',
order: 2,
},
{
key: 'address_info',
title: 'نشانی دفتر مرکزی',
value: 'تهران، جردن، خیابان سعیدی، ساختمان کانینا، طبقه ۵، واحد ۱۹',
icon: 'map-pin',
order: 3,
},
];
for (const item of defaultItems) {
await this.prisma.contactInfo.upsert({
where: { key: item.key },
update: {},
create: item,
});
}
items = await this.prisma.contactInfo.findMany({
orderBy: { order: 'asc' },
});
}
return items;
}
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();
}
}