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

213 lines
5.8 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
import { seedSupplierInvoice } from './helpers/procurement';
describe('supplier payment API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
const payment = (
amount: string,
reference: string,
idempotencyKey = randomUUID(),
) => ({
idempotencyKey,
reference,
method: 'BANK_TRANSFER',
amount,
paidDate: '2026-09-28',
});
it('records partial payments, replays retries once, and prevents overpayment', async () => {
const { invoice } = await seedSupplierInvoice(ctx);
const path = `/api/v1/supplier-invoices/${invoice.id}/payments`;
const firstInput = payment('50.00', 'utr-001');
const first = await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(firstInput)
.expect(201);
expect(first.body).toMatchObject({
reference: 'UTR-001',
amount: '50',
currency: 'INR',
});
expect(
(
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(firstInput)
.expect(201)
).body.id,
).toBe(first.body.id);
expect(
(
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send({ ...firstInput, amount: '60.00' })
.expect(409)
).body.code,
).toBe('PAYMENT_REPLAY_CONFLICT');
expect(
(
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(payment('1.00', 'UTR-001'))
.expect(409)
).body.code,
).toBe('PAYMENT_REFERENCE_CONFLICT');
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(payment('70.00', 'UTR-002'))
.expect(201);
expect(
(
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(payment('0.01', 'UTR-003'))
.expect(409)
).body.code,
).toBe('PAYMENT_AMOUNT_LIMIT');
const list = await ctx
.api()
.get(`${path}?limit=1&offset=1`)
.auth(ctx.token, auth)
.expect(200);
expect(list.body).toMatchObject({
payableTotal: '120.00',
paidTotal: '120.00',
outstandingTotal: '0.00',
});
expect(list.body.payments).toHaveLength(1);
expect(
await ctx.db.auditEvent.count({
where: { targetId: first.body.id, action: 'supplier_payment.recorded' },
}),
).toBe(1);
});
it('reverses once, releases balance, and protects invoice voiding', async () => {
const { invoice } = await seedSupplierInvoice(ctx);
const path = `/api/v1/supplier-invoices/${invoice.id}/payments`;
const input = payment('60.00', 'UTR-VOID');
const created = await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(input)
.expect(201);
const voidPath = `/api/v1/supplier-invoices/${invoice.id}/void`;
expect(
(
await ctx
.api()
.post(voidPath)
.auth(ctx.token, auth)
.send({ reason: 'Wrong invoice' })
.expect(409)
).body.code,
).toBe('INVOICE_PAYMENT_EXISTS');
const reversePath = `/api/v1/supplier-payments/${created.body.id}/reverse`;
const reversed = await ctx
.api()
.post(reversePath)
.auth(ctx.token, auth)
.send({ reason: 'Bank transfer returned' })
.expect(201);
expect(reversed.body.reversal.reason).toBe('Bank transfer returned');
expect(
(
await ctx
.api()
.post(reversePath)
.auth(ctx.token, auth)
.send({ reason: 'Again' })
.expect(409)
).body.code,
).toBe('PAYMENT_ALREADY_REVERSED');
expect(
(await ctx.api().get(path).auth(ctx.token, auth).expect(200)).body
.outstandingTotal,
).toBe('120.00');
await ctx
.api()
.post(voidPath)
.auth(ctx.token, auth)
.send({ reason: 'Wrong invoice' })
.expect(201);
expect(
(
await ctx
.api()
.post(path)
.auth(ctx.token, auth)
.send(payment('1.00', 'UTR-AFTER'))
.expect(409)
).body.code,
).toBe('PAYMENT_INVOICE_VOID');
expect(
(await ctx.api().post(path).auth(ctx.token, auth).send(input).expect(201))
.body.id,
).toBe(created.body.id);
});
it('requires payment permission and isolates organizations', async () => {
const { invoice } = await seedSupplierInvoice(ctx);
const path = `/api/v1/supplier-invoices/${invoice.id}/payments`;
const reader = await secondActor(ctx, true, ['procurement.read']);
await ctx.api().get(path).auth(reader.token, auth).expect(200);
await ctx
.api()
.post(path)
.auth(reader.token, auth)
.send(payment('1.00', 'DENIED'))
.expect(403);
const payer = await secondActor(ctx, true, ['supplier_payment.manage']);
const created = await ctx
.api()
.post(path)
.auth(payer.token, auth)
.send(payment('1.00', 'ALLOWED'))
.expect(201);
const foreign = await secondActor(ctx, false, [
'procurement.read',
'supplier_payment.manage',
]);
expect(
(
await ctx
.api()
.get(`/api/v1/supplier-payments/${created.body.id}`)
.auth(foreign.token, auth)
.expect(404)
).body.code,
).toBe('PAYMENT_NOT_FOUND');
await ctx
.api()
.post(`/api/v1/supplier-payments/${created.body.id}/reverse`)
.auth(reader.token, auth)
.send({ reason: 'Denied' })
.expect(403);
});
});