Files
fabric_trade_server/dist/customers/customers.service.js
T
mohmdsdgandCursor 345a4a4708 Add SMS OTP, harden auth, and fix order/checkout server bugs.
Ship 4-digit OTP with rate-limit attempts, Tehran-safe expiry/rass math, larger JSON body limit for check images, and safer order pricing/status guards. Include rebuilt dist for prebuilt deploy.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 14:55:26 +03:30

196 lines
7.9 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomersService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const prisma_service_1 = require("../prisma/prisma.service");
const phone_1 = require("../common/utils/phone");
const name_1 = require("../common/utils/name");
let CustomersService = class CustomersService {
prisma;
constructor(prisma) {
this.prisma = prisma;
}
async findAll(q) {
const customers = await this.prisma.customer.findMany({
where: q
? {
OR: [
{ firstName: { contains: q, mode: 'insensitive' } },
{ lastName: { contains: q, mode: 'insensitive' } },
{ phone: { contains: (0, phone_1.toEnglishDigits)(q) } },
{ nationalId: { contains: (0, phone_1.toEnglishDigits)(q) } },
],
}
: undefined,
orderBy: { createdAt: 'desc' },
include: {
company: { select: { name: true } },
orders: { select: { totalMeterage: true } },
},
});
return customers.map((c) => this.map(c));
}
async findOne(id) {
const customer = await this.prisma.customer.findUnique({
where: { id },
include: {
company: { select: { name: true } },
orders: { select: { totalMeterage: true } },
},
});
if (!customer)
throw new common_1.NotFoundException('مشتری یافت نشد');
return this.map(customer);
}
async create(dto) {
const phone = (0, phone_1.normalizePhone)((0, phone_1.toEnglishDigits)(dto.phone));
if (!(0, phone_1.isValidPhone)(phone)) {
throw new common_1.BadRequestException('شماره موبایل نامعتبر است');
}
const { firstName, lastName } = this.resolveNames(dto);
if (dto.companyId) {
const company = await this.prisma.company.findUnique({
where: { id: dto.companyId },
});
if (!company)
throw new common_1.NotFoundException('شرکت یافت نشد');
}
try {
const customer = await this.prisma.customer.create({
data: {
firstName,
lastName,
phone,
nationalId: dto.nationalId?.trim() || null,
customerType: dto.customerType ?? client_1.CustomerType.wholesale,
companyId: dto.companyId || null,
isActive: dto.isActive ?? true,
},
include: {
company: { select: { name: true } },
orders: { select: { totalMeterage: true } },
},
});
return this.map(customer);
}
catch (e) {
if (e instanceof client_1.Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002') {
throw new common_1.ConflictException('این شماره قبلاً ثبت شده است');
}
throw e;
}
}
async update(id, dto) {
await this.ensure(id);
const phone = dto.phone !== undefined
? (0, phone_1.normalizePhone)((0, phone_1.toEnglishDigits)(dto.phone))
: undefined;
if (phone !== undefined && !(0, phone_1.isValidPhone)(phone)) {
throw new common_1.BadRequestException('شماره موبایل نامعتبر است');
}
let firstName;
let lastName;
if (dto.name !== undefined ||
dto.firstName !== undefined ||
dto.lastName !== undefined) {
const names = this.resolveNames(dto);
firstName = names.firstName;
lastName = names.lastName;
}
try {
const customer = await this.prisma.customer.update({
where: { id },
data: {
firstName,
lastName,
phone,
nationalId: dto.nationalId === undefined
? undefined
: dto.nationalId?.trim() || null,
customerType: dto.customerType,
companyId: dto.companyId === undefined ? undefined : dto.companyId || null,
isActive: dto.isActive,
},
include: {
company: { select: { name: true } },
orders: { select: { totalMeterage: true } },
},
});
return this.map(customer);
}
catch (e) {
if (e instanceof client_1.Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002') {
throw new common_1.ConflictException('این شماره قبلاً ثبت شده است');
}
throw e;
}
}
async setActive(id, isActive) {
await this.ensure(id);
const customer = await this.prisma.customer.update({
where: { id },
data: { isActive },
include: {
company: { select: { name: true } },
orders: { select: { totalMeterage: true } },
},
});
return this.map(customer);
}
resolveNames(dto) {
if (dto.name?.trim()) {
return (0, name_1.splitFullName)(dto.name);
}
const firstName = dto.firstName?.trim();
const lastName = dto.lastName?.trim() ?? '';
if (!firstName) {
throw new common_1.BadRequestException('نام مشتری الزامی است');
}
return { firstName, lastName };
}
async ensure(id) {
const customer = await this.prisma.customer.findUnique({ where: { id } });
if (!customer)
throw new common_1.NotFoundException('مشتری یافت نشد');
return customer;
}
map(customer) {
const totalMeterage = customer.orders.reduce((sum, o) => sum + o.totalMeterage, 0);
const name = (0, name_1.joinFullName)(customer.firstName, customer.lastName);
return {
id: customer.id,
firstName: customer.firstName,
lastName: customer.lastName,
name,
phone: customer.phone,
nationalId: customer.nationalId ?? '',
customerType: customer.customerType,
companyId: customer.companyId,
companyName: customer.company?.name ?? '',
orderCount: customer.orders.length,
totalMeterage,
isActive: customer.isActive,
registeredAt: customer.createdAt.toISOString(),
createdAt: customer.createdAt.toISOString(),
updatedAt: customer.updatedAt.toISOString(),
};
}
};
exports.CustomersService = CustomersService;
exports.CustomersService = CustomersService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
], CustomersService);
//# sourceMappingURL=customers.service.js.map