206 lines
6.1 KiB
TypeScript
206 lines
6.1 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
Res,
|
|
UseGuards,
|
|
NotFoundException,
|
|
} from "@nestjs/common";
|
|
import type { Response } from "express";
|
|
import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger";
|
|
import { JwtAuthGuard } from "../auth/jwt-auth.guard";
|
|
import { RolesGuard } from "../common/guards/roles.guard";
|
|
import { Roles } from "../common/decorators/roles.decorator";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import {
|
|
CreateB2BLeadDto,
|
|
UpdateB2BLeadDto,
|
|
QueryB2BLeadDto,
|
|
} from "./dto/b2b-lead.dto";
|
|
import { generateCsv } from "../common/utils/csv-helper";
|
|
|
|
@ApiTags("Admin - B2B Leads")
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles("Admin", "SUPER_ADMIN")
|
|
@Controller("admin/b2b/leads")
|
|
export class B2BLeadsController {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: "لیست لیدهای سازمانی و کلینیکها" })
|
|
async findAll(@Query() query: QueryB2BLeadDto) {
|
|
const page = Number(query.page) || 1;
|
|
const limit = Number(query.limit) || 10;
|
|
const skip = (page - 1) * limit;
|
|
|
|
const where: any = {};
|
|
if (query.city) {
|
|
where.city = { contains: query.city, mode: "insensitive" };
|
|
}
|
|
if (query.tier) {
|
|
where.tier = query.tier;
|
|
}
|
|
if (query.status) {
|
|
where.status = query.status;
|
|
}
|
|
if (query.search) {
|
|
where.OR = [
|
|
{ clinicName: { contains: query.search, mode: "insensitive" } },
|
|
{ contactPerson: { contains: query.search, mode: "insensitive" } },
|
|
{ phone: { contains: query.search } },
|
|
];
|
|
}
|
|
|
|
const [total, items] = await Promise.all([
|
|
this.prisma.b2BLead.count({ where }),
|
|
this.prisma.b2BLead.findMany({
|
|
where,
|
|
skip,
|
|
take: limit,
|
|
orderBy: { [query.sortBy || "createdAt"]: query.sortOrder || "desc" },
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
success: true,
|
|
data: items,
|
|
meta: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
},
|
|
};
|
|
}
|
|
|
|
@Get("export/csv")
|
|
@ApiOperation({ summary: "خروجی CSV لیدهای B2B" })
|
|
async exportCsv(@Query() query: QueryB2BLeadDto, @Res() res: Response) {
|
|
const where: any = {};
|
|
if (query.city) {
|
|
where.city = { contains: query.city, mode: "insensitive" };
|
|
}
|
|
if (query.tier) {
|
|
where.tier = query.tier;
|
|
}
|
|
if (query.status) {
|
|
where.status = query.status;
|
|
}
|
|
|
|
const items: any[] = await this.prisma.b2BLead.findMany({
|
|
where,
|
|
orderBy: { createdAt: "desc" },
|
|
});
|
|
|
|
const rows = items.map((item: any) => ({
|
|
id: item.id,
|
|
clinicName: item.clinicName,
|
|
contactPerson: item.contactPerson,
|
|
phone: item.phone,
|
|
city: item.city,
|
|
district: item.district || "",
|
|
tier: item.tier,
|
|
specialties: (item.specialties || []).join(" | "),
|
|
status: item.status,
|
|
notes: item.notes || "",
|
|
createdAt: item.createdAt.toISOString(),
|
|
updatedAt: item.updatedAt.toISOString(),
|
|
}));
|
|
|
|
const csvData = generateCsv(rows, [
|
|
{ key: "id", header: "ID" },
|
|
{ key: "clinicName", header: "Clinic Name" },
|
|
{ key: "contactPerson", header: "Contact Person" },
|
|
{ key: "phone", header: "Phone" },
|
|
{ key: "city", header: "City" },
|
|
{ key: "district", header: "District" },
|
|
{ key: "tier", header: "Tier" },
|
|
{ key: "specialties", header: "Specialties" },
|
|
{ key: "status", header: "Status" },
|
|
{ key: "notes", header: "Notes" },
|
|
{ key: "createdAt", header: "Created At" },
|
|
{ key: "updatedAt", header: "Updated At" },
|
|
]);
|
|
|
|
res.setHeader("Content-Type", "text/csv; charset=utf-8");
|
|
res.setHeader(
|
|
"Content-Disposition",
|
|
`attachment; filename="b2b-leads-${Date.now()}.csv"`,
|
|
);
|
|
return res.status(200).send(csvData);
|
|
}
|
|
|
|
@Get(":id")
|
|
@ApiOperation({ summary: "دریافت جزییات یک لید B2B" })
|
|
async findOne(@Param("id") id: string) {
|
|
const item = await this.prisma.b2BLead.findUnique({
|
|
where: { id },
|
|
});
|
|
if (!item) {
|
|
throw new NotFoundException(`B2B Lead with ID ${id} not found`);
|
|
}
|
|
return { success: true, data: item };
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: "ثبت لید سازمانی جدید" })
|
|
async create(@Body() dto: CreateB2BLeadDto) {
|
|
const item = await this.prisma.b2BLead.create({
|
|
data: {
|
|
clinicName: dto.clinicName,
|
|
contactPerson: dto.contactPerson,
|
|
phone: dto.phone,
|
|
city: dto.city,
|
|
district: dto.district,
|
|
tier: dto.tier || "STANDARD",
|
|
specialties: dto.specialties || [],
|
|
status: dto.status || "NEW",
|
|
notes: dto.notes,
|
|
},
|
|
});
|
|
return { success: true, data: item };
|
|
}
|
|
|
|
@Put(":id")
|
|
@ApiOperation({ summary: "ویرایش لید سازمانی" })
|
|
async update(@Param("id") id: string, @Body() dto: UpdateB2BLeadDto) {
|
|
const exists = await this.prisma.b2BLead.findUnique({ where: { id } });
|
|
if (!exists) {
|
|
throw new NotFoundException(`B2B Lead with ID ${id} not found`);
|
|
}
|
|
|
|
const updated = await this.prisma.b2BLead.update({
|
|
where: { id },
|
|
data: {
|
|
...(dto.clinicName && { clinicName: dto.clinicName }),
|
|
...(dto.contactPerson && { contactPerson: dto.contactPerson }),
|
|
...(dto.phone && { phone: dto.phone }),
|
|
...(dto.city && { city: dto.city }),
|
|
...(dto.district !== undefined && { district: dto.district }),
|
|
...(dto.tier && { tier: dto.tier }),
|
|
...(dto.specialties !== undefined && { specialties: dto.specialties }),
|
|
...(dto.status && { status: dto.status }),
|
|
...(dto.notes !== undefined && { notes: dto.notes }),
|
|
},
|
|
});
|
|
return { success: true, data: updated };
|
|
}
|
|
|
|
@Delete(":id")
|
|
@ApiOperation({ summary: "حذف لید سازمانی" })
|
|
async remove(@Param("id") id: string) {
|
|
const exists = await this.prisma.b2BLead.findUnique({ where: { id } });
|
|
if (!exists) {
|
|
throw new NotFoundException(`B2B Lead with ID ${id} not found`);
|
|
}
|
|
await this.prisma.b2BLead.delete({ where: { id } });
|
|
return { success: true, message: "Deleted successfully" };
|
|
}
|
|
}
|