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>
This commit is contained in:
Vendored
+58
-20
@@ -28,6 +28,19 @@ const orderInclude = {
|
||||
user: true,
|
||||
company: true,
|
||||
};
|
||||
function roundMoney(value) {
|
||||
return Math.round(value * 100) / 100;
|
||||
}
|
||||
const CHECK_EDITABLE_STATUSES = [
|
||||
client_1.OrderStatus.awaiting_review,
|
||||
client_1.OrderStatus.pricing,
|
||||
client_1.OrderStatus.needs_correction,
|
||||
client_1.OrderStatus.awaiting_settlement_fix,
|
||||
client_1.OrderStatus.check_in_progress,
|
||||
client_1.OrderStatus.awaiting_check_review,
|
||||
client_1.OrderStatus.check_needs_fix,
|
||||
client_1.OrderStatus.check_rejected,
|
||||
];
|
||||
let OrdersService = class OrdersService {
|
||||
prisma;
|
||||
constructor(prisma) {
|
||||
@@ -140,8 +153,7 @@ let OrdersService = class OrdersService {
|
||||
const cashInvoiceExpiresAt = settlementType === client_1.SettlementType.cash
|
||||
? (0, cash_expiry_1.computeCashInvoiceExpiry)(company)
|
||||
: null;
|
||||
const orderNumber = await this.nextOrderNumber();
|
||||
const order = await this.prisma.$transaction(async (tx) => {
|
||||
const order = await this.createWithUniqueOrderNumber((orderNumber) => this.prisma.$transaction(async (tx) => {
|
||||
const created = await tx.order.create({
|
||||
data: {
|
||||
orderNumber,
|
||||
@@ -188,7 +200,7 @@ let OrdersService = class OrdersService {
|
||||
});
|
||||
}
|
||||
return created;
|
||||
});
|
||||
}));
|
||||
return this.mapOrder(order);
|
||||
}
|
||||
async adminCreate(adminId, dto) {
|
||||
@@ -207,7 +219,9 @@ let OrdersService = class OrdersService {
|
||||
if (!(0, phone_1.isValidPhone)(phone)) {
|
||||
throw new common_1.BadRequestException('شماره موبایل نامعتبر است');
|
||||
}
|
||||
let customer = await this.prisma.customer.findUnique({ where: { phone } });
|
||||
let customer = await this.prisma.customer.findUnique({
|
||||
where: { phone },
|
||||
});
|
||||
if (!customer) {
|
||||
const names = (0, name_1.splitFullName)(dto.customerName || 'مشتری جدید');
|
||||
customer = await this.prisma.customer.create({
|
||||
@@ -223,12 +237,12 @@ let OrdersService = class OrdersService {
|
||||
if (!userId) {
|
||||
throw new common_1.BadRequestException('شناسه یا شماره مشتری الزامی است');
|
||||
}
|
||||
const orderNumber = await this.nextOrderNumber();
|
||||
const totalMeterage = dto.meterage ?? 0;
|
||||
const order = await this.prisma.order.create({
|
||||
const ownerId = userId;
|
||||
const order = await this.createWithUniqueOrderNumber((orderNumber) => this.prisma.order.create({
|
||||
data: {
|
||||
orderNumber,
|
||||
userId,
|
||||
userId: ownerId,
|
||||
companyId: company.id,
|
||||
companyName: company.name,
|
||||
settlementType,
|
||||
@@ -245,33 +259,39 @@ let OrdersService = class OrdersService {
|
||||
},
|
||||
},
|
||||
include: orderInclude,
|
||||
});
|
||||
}));
|
||||
return this.mapOrder(order, true);
|
||||
}
|
||||
async updatePricing(id, adminId, dto) {
|
||||
const order = await this.getOrder(id);
|
||||
const unknown = dto.items.find((item) => !order.items.some((i) => i.id === item.id));
|
||||
if (unknown) {
|
||||
throw new common_1.BadRequestException(`آیتم ${unknown.id} یافت نشد`);
|
||||
}
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
let totalAmount = 0;
|
||||
let totalMeterage = 0;
|
||||
for (const item of dto.items) {
|
||||
const existing = order.items.find((i) => i.id === item.id);
|
||||
if (!existing) {
|
||||
throw new common_1.BadRequestException(`آیتم ${item.id} یافت نشد`);
|
||||
}
|
||||
const unitPrice = item.unitPrice !== undefined
|
||||
for (const existing of order.items) {
|
||||
const item = dto.items.find((i) => i.id === existing.id);
|
||||
const unitPrice = item?.unitPrice !== undefined
|
||||
? item.unitPrice
|
||||
: item.supplyPrice !== undefined
|
||||
: item?.supplyPrice !== undefined
|
||||
? item.supplyPrice
|
||||
: (existing.unitPrice ?? 0);
|
||||
const unitDiscount = item.unitDiscount ?? existing.unitDiscount ?? 0;
|
||||
const confirmed = item.confirmedMeterage ??
|
||||
const unitDiscount = item?.unitDiscount ?? existing.unitDiscount ?? 0;
|
||||
if (unitDiscount > unitPrice) {
|
||||
throw new common_1.BadRequestException('تخفیف واحد نمیتواند از قیمت واحد بیشتر باشد');
|
||||
}
|
||||
const confirmed = item?.confirmedMeterage ??
|
||||
existing.confirmedMeterage ??
|
||||
existing.requestedMeterage;
|
||||
const lineTotal = Math.max(0, (unitPrice - unitDiscount) * confirmed);
|
||||
const lineTotal = roundMoney(Math.max(0, (unitPrice - unitDiscount) * confirmed));
|
||||
totalAmount += lineTotal;
|
||||
totalMeterage += confirmed;
|
||||
if (!item)
|
||||
continue;
|
||||
await tx.orderItem.update({
|
||||
where: { id: item.id },
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
confirmedMeterage: confirmed,
|
||||
unitPrice,
|
||||
@@ -280,9 +300,10 @@ let OrdersService = class OrdersService {
|
||||
},
|
||||
});
|
||||
}
|
||||
totalAmount = roundMoney(totalAmount);
|
||||
const shippingCost = dto.shippingCost ?? order.shippingCost ?? 0;
|
||||
const totalDiscount = dto.totalDiscount ?? order.totalDiscount ?? 0;
|
||||
const finalAmount = Math.max(0, totalAmount + shippingCost - totalDiscount);
|
||||
const finalAmount = roundMoney(Math.max(0, totalAmount + shippingCost - totalDiscount));
|
||||
const nextStatus = order.status === client_1.OrderStatus.awaiting_review ||
|
||||
order.status === client_1.OrderStatus.pricing
|
||||
? client_1.OrderStatus.pricing
|
||||
@@ -354,6 +375,9 @@ let OrdersService = class OrdersService {
|
||||
if (order.settlementType !== client_1.SettlementType.check) {
|
||||
throw new common_1.BadRequestException('این سفارش تسویه چکی نیست');
|
||||
}
|
||||
if (!CHECK_EDITABLE_STATUSES.includes(order.status)) {
|
||||
throw new common_1.BadRequestException('در وضعیت فعلی سفارش امکان ویرایش چکها وجود ندارد');
|
||||
}
|
||||
const checkMode = dto.checkMode ?? order.checkMode ?? client_1.CheckRegistrationMode.full;
|
||||
const rass = (0, check_rass_1.calculateCheckRass)(dto.checks.map((c) => ({ amount: c.amount, dueDate: c.dueDate })), order.createdAt);
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
@@ -418,6 +442,20 @@ let OrdersService = class OrdersService {
|
||||
}
|
||||
return `${(0, order_number_1.generateOrderNumber)()}-${Date.now().toString().slice(-3)}`;
|
||||
}
|
||||
async createWithUniqueOrderNumber(create) {
|
||||
for (let attempt = 0;; attempt++) {
|
||||
const orderNumber = await this.nextOrderNumber();
|
||||
try {
|
||||
return await create(orderNumber);
|
||||
}
|
||||
catch (e) {
|
||||
const isDuplicate = e instanceof client_1.Prisma.PrismaClientKnownRequestError &&
|
||||
e.code === 'P2002';
|
||||
if (!isDuplicate || attempt >= 4)
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
async getOrder(id) {
|
||||
const order = await this.prisma.order.findUnique({
|
||||
where: { id },
|
||||
|
||||
Reference in New Issue
Block a user