manicanldes-backend/test/offline-payments.spec.ts

162 lines
6.1 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { checkoutFixture } from './helpers/checkout';
import { activatePricing } from './helpers/pricing';
describe('cash on delivery and manual UPI orders', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const onHand = async (id: string) =>
(await ctx.db.stockItem.findUniqueOrThrow({ where: { id } })).onHand;
const staff = (path: string, body?: object) =>
ctx
.api()
.post(`/api/v1/admin/orders/${path}`)
.auth(ctx.token, auth)
.send(body ?? {});
async function checkout(payment: object) {
const f = await checkoutFixture(ctx);
const response = await ctx
.api()
.post('/api/v1/checkout')
.auth(f.actor.token, auth)
.send({ ...f.input, ...payment });
return { f, response };
}
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('refuses a paid checkout until tax and shipping give a payable total', async () => {
const { f, response } = await checkout({ paymentMethod: 'COD' });
expect(response.status).toBe(409);
expect(response.body.code).toBe('ORDER_PRICING_REQUIRED');
expect(await onHand(f.stock.id)).toBe(10);
await activatePricing(ctx);
});
it('confirms cash on delivery at once, ships, delivers and blocks late cancellation', async () => {
const { f, response } = await checkout({ paymentMethod: 'COD' });
expect(response.status).toBe(201);
await ctx
.api()
.post(`/api/v1/orders/${response.body.id}/payment-reference`)
.auth(f.actor.token, auth)
.send({ reference: 'UTR123456789' })
.expect(409);
expect(response.body).toMatchObject({
status: 'CONFIRMED',
paymentMethod: 'COD',
pricingStatus: 'FINALIZED',
history: [{ status: 'CONFIRMED', note: '' }],
});
expect(response.body.payableTotal).not.toBeNull();
expect(await onHand(f.stock.id)).toBe(8);
const id = response.body.id as string;
await staff(`${id}/deliver`).expect(409);
await staff(`${id}/ship`, { note: '' }).expect(400);
await staff(`${id}/ship`, { note: 'Delhivery AWB 1234567890' }).expect(201);
await staff(`${id}/ship`, { note: 'Delhivery AWB 1234567890' }).expect(201);
const mine = await ctx
.api()
.get(`/api/v1/orders/${id}`)
.auth(f.actor.token, auth)
.expect(200);
expect(mine.body.status).toBe('SHIPPED');
expect(mine.body.history.map((e: { status: string }) => e.status)).toEqual([
'CONFIRMED',
'SHIPPED',
]);
expect(mine.body.history[1].note).toBe('Delhivery AWB 1234567890');
await ctx
.api()
.post(`/api/v1/orders/${id}/cancel`)
.auth(f.actor.token, auth)
.expect(409);
await staff(`${id}/deliver`).expect(201);
expect(await onHand(f.stock.id)).toBe(8);
});
it('restocks a confirmed order cancelled before shipment', async () => {
const { f, response } = await checkout({ paymentMethod: 'COD' });
expect(await onHand(f.stock.id)).toBe(8);
const cancelled = await ctx
.api()
.post(`/api/v1/orders/${response.body.id}/cancel`)
.auth(f.actor.token, auth)
.expect(201);
expect(cancelled.body.status).toBe('CANCELLED');
expect(await onHand(f.stock.id)).toBe(10);
await staff(`${response.body.id}/ship`, { note: 'Too late' }).expect(409);
});
it('holds UPI orders until staff match the reference, then commits stock', async () => {
const { f, response } = await checkout({ paymentMethod: 'UPI_MANUAL' });
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
status: 'PENDING_PAYMENT',
paymentMethod: 'UPI_MANUAL',
history: [{ status: 'PENDING_PAYMENT', note: '' }],
});
const reference = (body: object, token = f.actor.token) =>
ctx
.api()
.post(`/api/v1/orders/${response.body.id}/payment-reference`)
.auth(token, auth)
.send(body);
await reference({ reference: 'bad ref!' }).expect(400);
await reference({ reference: 'UTR123456789' }, ctx.token).expect(404);
const reported = await reference({ reference: ' utr123456789 ' }).expect(
201,
);
await reference({ reference: 'UTR123456789' }).expect(201);
expect(reported.body.history).toHaveLength(2);
expect(reported.body.history[1].note).toBe('UPI reference UTR123456789');
expect(
new Date(response.body.expiresAt).getTime() - Date.now(),
).toBeGreaterThan(47 * 3600000);
expect(await onHand(f.stock.id)).toBe(10);
const id = response.body.id as string;
await ctx
.api()
.post(`/api/v1/admin/orders/${id}/confirm`)
.auth(f.actor.token, auth)
.expect(403);
await staff(`${id}/ship`, { note: 'Not paid yet' }).expect(409);
const confirmed = await staff(`${id}/confirm`).expect(201);
expect(confirmed.body.status).toBe('CONFIRMED');
expect(await onHand(f.stock.id)).toBe(8);
await staff(`${id}/confirm`).expect(201);
expect(await onHand(f.stock.id)).toBe(8);
expect(
await ctx.db.stockReservation.count({
where: { orderId: id, status: 'COMMITTED' },
}),
).toBe(1);
await reference({ reference: 'UTR999999999' }).expect(409);
await staff(`${randomUUID()}/confirm`).expect(404);
});
it('rejects confirming an expired UPI order and invalid direct status writes', async () => {
const { response } = await checkout({ paymentMethod: 'UPI_MANUAL' });
const id = response.body.id as string;
await expect(
ctx.db.order.update({ where: { id }, data: { status: 'DELIVERED' } }),
).rejects.toThrow();
await ctx.executeSql(
`ALTER TABLE orders DISABLE TRIGGER orders_snapshot_immutable;
UPDATE orders SET created_at = NOW() - INTERVAL '3 days', expires_at = NOW() - INTERVAL '1 day' WHERE id = '${id}';
ALTER TABLE orders ENABLE TRIGGER orders_snapshot_immutable;`,
);
expect((await staff(`${id}/confirm`).expect(409)).body.code).toBe(
'ORDER_EXPIRED',
);
});
});