From 507fc990659ac5fb0375ff225fb24fbd291ac99f Mon Sep 17 00:00:00 2001 From: safavimohadeseh78 Date: Tue, 11 Aug 2026 18:17:58 +0200 Subject: [PATCH] Accept customerId when admin creates an order from the customers list. Co-authored-by: Cursor --- src/orders/dto/order.dto.ts | 5 +++++ src/orders/orders.service.ts | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/orders/dto/order.dto.ts b/src/orders/dto/order.dto.ts index 4885e56..b3b541b 100644 --- a/src/orders/dto/order.dto.ts +++ b/src/orders/dto/order.dto.ts @@ -138,6 +138,11 @@ export class ChooseSettlementDto { } export class AdminCreateOrderDto { + /** Customer / user id — preferred when picking from customers list */ + @IsOptional() + @IsString() + customerId?: string; + @IsOptional() @IsString() userId?: string; diff --git a/src/orders/orders.service.ts b/src/orders/orders.service.ts index df0484a..cef3758 100644 --- a/src/orders/orders.service.ts +++ b/src/orders/orders.service.ts @@ -302,8 +302,15 @@ export class OrdersService { }); if (!company) throw new NotFoundException('شرکت یافت نشد'); - let userId = dto.userId; - if (!userId && dto.customerPhone) { + let userId = dto.customerId || dto.userId; + if (userId) { + const existing = await this.prisma.customer.findUnique({ + where: { id: userId }, + }); + if (!existing) { + throw new BadRequestException('مشتری انتخاب‌شده یافت نشد'); + } + } else if (dto.customerPhone) { const phone = normalizePhone(toEnglishDigits(dto.customerPhone)); if (!isValidPhone(phone)) { throw new BadRequestException('شماره موبایل نامعتبر است'); @@ -324,7 +331,7 @@ export class OrdersService { userId = customer.id; } if (!userId) { - throw new BadRequestException('شناسه یا شماره مشتری الزامی است'); + throw new BadRequestException('انتخاب مشتری الزامی است'); } const totalMeterage = dto.meterage ?? 0;