2026-10-08 10:40:21 +03:30
|
|
|
import { Prisma } from '@prisma/client';
|
|
|
|
|
import { OrdersService } from './orders.service';
|
|
|
|
|
|
2026-10-12 10:24:46 +03:30
|
|
|
/** Order SMS is fire-and-forget (D20); these suites only assert order data. */
|
|
|
|
|
const smsStub = { send: jest.fn(async () => ({ ok: true })) };
|
|
|
|
|
|
2026-10-08 10:40:21 +03:30
|
|
|
/**
|
|
|
|
|
* Owner ruling 2026-10-08: an order is credited to the customer's expert AT
|
|
|
|
|
* THE MOMENT IT IS PLACED (legacy `orders.reagent_id`), and a later change of
|
|
|
|
|
* the customer's expert never moves it.
|
|
|
|
|
*/
|
|
|
|
|
describe('OrdersService.createOrder — sales expert credit', () => {
|
|
|
|
|
function build(referredByAgentId: string | null) {
|
|
|
|
|
const created: Record<string, unknown>[] = [];
|
|
|
|
|
const tx = {
|
|
|
|
|
order: {
|
|
|
|
|
create: jest.fn(async ({ data }: { data: Record<string, unknown> }) => {
|
|
|
|
|
created.push(data);
|
|
|
|
|
return { id: 'o1', ...data };
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const prisma = {
|
|
|
|
|
customer: {
|
|
|
|
|
findUnique: jest.fn(async () => ({
|
|
|
|
|
id: 'c1',
|
|
|
|
|
referredByAgentId,
|
|
|
|
|
goldDebtGrams: new Prisma.Decimal(0),
|
|
|
|
|
goldCreditLimitGrams: new Prisma.Decimal(1000),
|
|
|
|
|
})),
|
|
|
|
|
},
|
2026-10-12 10:24:46 +03:30
|
|
|
adminUser: { findUnique: jest.fn(async () => null) },
|
2026-10-08 10:40:21 +03:30
|
|
|
$transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)),
|
|
|
|
|
};
|
|
|
|
|
const catalog = { getVariantOrThrow: jest.fn(async () => ({ id: 'v1', rashWeightGrams: '10' })) };
|
|
|
|
|
const accounting = { orderStatusReached: jest.fn() };
|
2026-10-08 11:26:55 +03:30
|
|
|
const wage = { quote: jest.fn() };
|
2026-10-12 10:24:46 +03:30
|
|
|
const service = new OrdersService(prisma as never, catalog as never, {} as never, accounting as never, wage as never, smsStub as never);
|
2026-10-08 10:40:21 +03:30
|
|
|
return { service, created };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dto = { items: [{ productVariantId: 'v1', quantity: 1, wagePercent: 3 }] } as never;
|
|
|
|
|
|
|
|
|
|
it("records the customer's current expert on the order", async () => {
|
|
|
|
|
const { service, created } = build('agent-A');
|
|
|
|
|
await service.createOrder(dto, 'c1');
|
|
|
|
|
expect(created[0].salesAgentId).toBe('agent-A');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('records no expert for a customer without one', async () => {
|
|
|
|
|
const { service, created } = build(null);
|
|
|
|
|
await service.createOrder(dto, 'c1');
|
|
|
|
|
expect(created[0].salesAgentId).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-10-08 11:16:29 +03:30
|
|
|
|
2026-10-08 11:26:55 +03:30
|
|
|
/** A server quote of 10% for every variant. */
|
|
|
|
|
const wageStub = {
|
|
|
|
|
quote: jest.fn(async (_c: string, ids: string[]) => new Map(ids.map((id) => [id, { wagePercentSnapshot: '10', discountPercentSnapshot: '0' }]))),
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-08 11:16:29 +03:30
|
|
|
/** MON-05 (owner ruling 2026-10-08): «عدم سفارش» stops app orders only. */
|
|
|
|
|
describe('OrdersService.createOrder — order ban', () => {
|
|
|
|
|
function build(canPlaceOrders: boolean) {
|
|
|
|
|
const tx = { order: { create: jest.fn(async ({ data }: { data: object }) => ({ id: 'o1', ...data })) } };
|
|
|
|
|
const prisma = {
|
|
|
|
|
customer: {
|
|
|
|
|
findUnique: jest.fn(async () => ({
|
|
|
|
|
id: 'c1',
|
|
|
|
|
canPlaceOrders,
|
|
|
|
|
referredByAgentId: null,
|
|
|
|
|
goldDebtGrams: new Prisma.Decimal(0),
|
|
|
|
|
goldCreditLimitGrams: new Prisma.Decimal(1000),
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
$transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)),
|
|
|
|
|
};
|
|
|
|
|
const catalog = { getVariantOrThrow: jest.fn(async () => ({ id: 'v1', rashWeightGrams: '10' })) };
|
2026-10-12 10:24:46 +03:30
|
|
|
return new OrdersService(prisma as never, catalog as never, {} as never, { orderStatusReached: jest.fn() } as never, wageStub as never, smsStub as never);
|
2026-10-08 11:16:29 +03:30
|
|
|
}
|
|
|
|
|
const dto = { items: [{ productVariantId: 'v1', quantity: 1, wagePercent: 3 }] } as never;
|
|
|
|
|
|
|
|
|
|
it('refuses a suspended customer ordering from the app', async () => {
|
|
|
|
|
await expect(build(false).createOrder(dto, 'c1', { placedBy: 'customer' })).rejects.toThrow('غیرفعال');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('lets an operator place an order for a suspended customer', async () => {
|
|
|
|
|
await expect(build(false).createOrder(dto, 'c1', { placedBy: 'operator' })).resolves.toMatchObject({ id: 'o1' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('lets an allowed customer order from the app', async () => {
|
|
|
|
|
await expect(build(true).createOrder(dto, 'c1', { placedBy: 'customer' })).resolves.toMatchObject({ id: 'o1' });
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-10-08 11:26:55 +03:30
|
|
|
|
|
|
|
|
/** MON-08 (owner ruling 2026-10-08): an app order is priced by the server, never by the app. */
|
|
|
|
|
describe('OrdersService.createOrder — server-side making charge', () => {
|
|
|
|
|
function build() {
|
|
|
|
|
const created: Record<string, any>[] = [];
|
|
|
|
|
const tx = { order: { create: jest.fn(async ({ data }: { data: Record<string, any> }) => { created.push(data); return { id: 'o1', ...data }; }) } };
|
|
|
|
|
const prisma = {
|
|
|
|
|
customer: { findUnique: jest.fn(async () => ({ id: 'c1', canPlaceOrders: true, referredByAgentId: null, goldDebtGrams: new Prisma.Decimal(0), goldCreditLimitGrams: new Prisma.Decimal(1000) })) },
|
|
|
|
|
$transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)),
|
|
|
|
|
};
|
|
|
|
|
const catalog = { getVariantOrThrow: jest.fn(async () => ({ id: 'v1', rashWeightGrams: '10' })) };
|
2026-10-12 10:24:46 +03:30
|
|
|
const service = new OrdersService(prisma as never, catalog as never, {} as never, { orderStatusReached: jest.fn() } as never, wageStub as never, smsStub as never);
|
2026-10-08 11:26:55 +03:30
|
|
|
return { service, created };
|
|
|
|
|
}
|
|
|
|
|
// A tampered request: zero making charge, a 50% discount and a rial charge.
|
|
|
|
|
const tampered = { items: [{ productVariantId: 'v1', quantity: 1, wagePercent: 0, discountPercent: 50, flatRialCharge: 999 }] } as never;
|
|
|
|
|
|
|
|
|
|
it('ignores what the app sends and uses the server quote', async () => {
|
|
|
|
|
const { service, created } = build();
|
|
|
|
|
await service.createOrder(tampered, 'c1', { placedBy: 'customer' });
|
|
|
|
|
expect(created[0].totalGoldWeightGrams.toString()).toBe('11');
|
|
|
|
|
expect(created[0].flatRialCharge.toString()).toBe('0');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('keeps the numbers an operator entered in the panel', async () => {
|
|
|
|
|
const { service, created } = build();
|
|
|
|
|
await service.createOrder(tampered, 'c1', { placedBy: 'operator' });
|
|
|
|
|
expect(created[0].totalGoldWeightGrams.toString()).toBe('5');
|
|
|
|
|
expect(created[0].flatRialCharge.toString()).toBe('999');
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-10-12 10:24:46 +03:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Legacy ApiMobileController::setOrder:733-766 texted three audiences on every
|
|
|
|
|
* placed order. None of it was wired in the target until now.
|
|
|
|
|
*/
|
|
|
|
|
describe('OrdersService.createOrder — order SMS', () => {
|
|
|
|
|
function build(opts: { overLimit?: boolean; agentId?: string | null; agentMobile?: string | null } = {}) {
|
|
|
|
|
const sent: { to: string; body: string }[] = [];
|
|
|
|
|
const tx = { order: { create: jest.fn(async ({ data }: { data: object }) => ({ id: 'o1', ...data })) } };
|
|
|
|
|
const prisma = {
|
|
|
|
|
customer: {
|
|
|
|
|
findUnique: jest.fn(async () => ({
|
|
|
|
|
id: 'c1',
|
|
|
|
|
mobile: '09120000001',
|
|
|
|
|
firstName: 'علی',
|
|
|
|
|
lastName: 'رضایی',
|
|
|
|
|
canPlaceOrders: true,
|
|
|
|
|
referredByAgentId: opts.agentId ?? null,
|
|
|
|
|
goldDebtGrams: new Prisma.Decimal(0),
|
|
|
|
|
goldCreditLimitGrams: new Prisma.Decimal(opts.overLimit ? 1 : 1000),
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
adminUser: { findUnique: jest.fn(async () => ({ mobile: opts.agentMobile ?? null })) },
|
|
|
|
|
$transaction: jest.fn(async (fn: (t: typeof tx) => unknown) => fn(tx)),
|
|
|
|
|
};
|
|
|
|
|
const catalog = { getVariantOrThrow: jest.fn(async () => ({ id: 'v1', rashWeightGrams: '10' })) };
|
|
|
|
|
const sms = { send: jest.fn(async (to: string, body: string) => { sent.push({ to, body }); return { ok: true }; }) };
|
|
|
|
|
const service = new OrdersService(prisma as never, catalog as never, {} as never, { orderStatusReached: jest.fn() } as never, wageStub as never, sms as never);
|
|
|
|
|
return { service, sent };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dto = { items: [{ productVariantId: 'v1', quantity: 1, wagePercent: 3 }] } as never;
|
|
|
|
|
// SMS is deliberately not awaited (D20), so let the microtasks drain.
|
|
|
|
|
const settle = () => new Promise((r) => setImmediate(r));
|
|
|
|
|
|
|
|
|
|
afterEach(() => { delete process.env.ORDER_ADMIN_MOBILES; });
|
|
|
|
|
|
|
|
|
|
it('texts the customer that the order was placed', async () => {
|
|
|
|
|
const { service, sent } = build();
|
|
|
|
|
await service.createOrder(dto, 'c1', { placedBy: 'customer' });
|
|
|
|
|
await settle();
|
|
|
|
|
const toCustomer = sent.find((s) => s.to === '09120000001');
|
|
|
|
|
expect(toCustomer?.body).toContain('به ثبت رسید. منتزر');
|
|
|
|
|
expect(toCustomer?.body).not.toContain('معلق');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('tells the customer an over-limit order was suspended', async () => {
|
|
|
|
|
const { service, sent } = build({ overLimit: true });
|
|
|
|
|
await service.createOrder(dto, 'c1', { placedBy: 'customer' });
|
|
|
|
|
await settle();
|
|
|
|
|
const toCustomer = sent.find((s) => s.to === '09120000001');
|
|
|
|
|
expect(toCustomer?.body).toContain('عبور از سقف مجاز گرمی در حالت معلق قرار گرفت');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('texts every configured admin with the name and weight', async () => {
|
|
|
|
|
process.env.ORDER_ADMIN_MOBILES = '09121111111, 09122222222';
|
|
|
|
|
const { service, sent } = build();
|
|
|
|
|
await service.createOrder(dto, 'c1', { placedBy: 'customer' });
|
|
|
|
|
await settle();
|
|
|
|
|
const admins = sent.filter((s) => s.to.startsWith('0912') && s.to !== '09120000001');
|
|
|
|
|
expect(admins.map((a) => a.to)).toEqual(['09121111111', '09122222222']);
|
|
|
|
|
expect(admins[0].body).toContain('علی رضایی');
|
|
|
|
|
expect(admins[0].body).toContain('11');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('texts nobody extra when no admin numbers are configured', async () => {
|
|
|
|
|
const { service, sent } = build();
|
|
|
|
|
await service.createOrder(dto, 'c1', { placedBy: 'customer' });
|
|
|
|
|
await settle();
|
|
|
|
|
expect(sent).toHaveLength(1);
|
|
|
|
|
expect(sent[0].to).toBe('09120000001');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("texts the customer's sales expert", async () => {
|
|
|
|
|
const { service, sent } = build({ agentId: 'agent-A', agentMobile: '09123333333' });
|
|
|
|
|
await service.createOrder(dto, 'c1', { placedBy: 'customer' });
|
|
|
|
|
await settle();
|
|
|
|
|
const toAgent = sent.find((s) => s.to === '09123333333');
|
|
|
|
|
expect(toAgent?.body).toContain('سفارشی توسط علی رضایی');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('places the order even when the expert has no mobile', async () => {
|
|
|
|
|
const { service, sent } = build({ agentId: 'agent-A', agentMobile: null });
|
|
|
|
|
await expect(service.createOrder(dto, 'c1', { placedBy: 'customer' })).resolves.toMatchObject({ id: 'o1' });
|
|
|
|
|
await settle();
|
|
|
|
|
expect(sent).toHaveLength(1);
|
|
|
|
|
});
|
|
|
|
|
});
|