fix(procurement): guard purchase order snapshots in database
This commit is contained in:
parent
35d352434d
commit
467690eb2b
|
|
@ -16,3 +16,4 @@ Phase 1D appends checkout tables, reservation ownership, immutable snapshot guar
|
||||||
Phase 1E adds pricing policies, immutable final-price snapshots and commerce events with leased delivery state in two further timestamped migrations. All eleven preceding migrations remain unchanged.
|
Phase 1E adds pricing policies, immutable final-price snapshots and commerce events with leased delivery state in two further timestamped migrations. All eleven preceding migrations remain unchanged.
|
||||||
Phase 2A appends supplier, material and supplier-material compatibility tables. It grants procurement permissions to existing system roles; custom roles must be updated through the RBAC API.
|
Phase 2A appends supplier, material and supplier-material compatibility tables. It grants procurement permissions to existing system roles; custom roles must be updated through the RBAC API.
|
||||||
Phase 2B appends purchase-order headers and immutable line snapshots. No existing migration is modified.
|
Phase 2B appends purchase-order headers and immutable line snapshots. No existing migration is modified.
|
||||||
|
The subsequent purchase-order integrity migration adds database guards for header state changes and line snapshot edits; historical migrations remain unchanged.
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,6 @@
|
||||||
"20260911110906_pricing_events": "60a4a5a0a05821c2a9785496cd2e9bc0f839e5fb2ae3c59275655481c96eb66b",
|
"20260911110906_pricing_events": "60a4a5a0a05821c2a9785496cd2e9bc0f839e5fb2ae3c59275655481c96eb66b",
|
||||||
"20260911111403_operations_integrity": "f40bddf9e29d6518bc765cbaca7688c04cb95d5ff729c52e7dc775eefa1e521e",
|
"20260911111403_operations_integrity": "f40bddf9e29d6518bc765cbaca7688c04cb95d5ff729c52e7dc775eefa1e521e",
|
||||||
"20260913140451_supplier_materials": "c1f59e082ddf97443c17d52745068d9e6cbafb3d4e1088126a150f555c1cf0a1",
|
"20260913140451_supplier_materials": "c1f59e082ddf97443c17d52745068d9e6cbafb3d4e1088126a150f555c1cf0a1",
|
||||||
"20260913173146_purchase_orders": "1e226f158e57af94c03b8f749349d06a874a8e82c32094ebd638d0ec41dc762a"
|
"20260913173146_purchase_orders": "1e226f158e57af94c03b8f749349d06a874a8e82c32094ebd638d0ec41dc762a",
|
||||||
|
"20260923000116_purchase_order_integrity": "ed14ce76d1b29c8de890a89c8dd84216edd002e43732b75d0cdeab4fd8f51fb8"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
CREATE FUNCTION protect_purchase_order() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||||
|
BEGIN
|
||||||
|
IF TG_OP = 'DELETE' THEN
|
||||||
|
RAISE EXCEPTION 'Purchase orders cannot be deleted';
|
||||||
|
END IF;
|
||||||
|
IF OLD.status <> 'DRAFT' OR NEW.status NOT IN ('ISSUED', 'CANCELLED')
|
||||||
|
OR (to_jsonb(NEW) - 'status' - 'issued_at' - 'cancelled_at')
|
||||||
|
IS DISTINCT FROM (to_jsonb(OLD) - 'status' - 'issued_at' - 'cancelled_at') THEN
|
||||||
|
RAISE EXCEPTION 'Purchase order snapshot or state cannot be changed';
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$;
|
||||||
|
CREATE TRIGGER purchase_order_protected BEFORE UPDATE OR DELETE ON purchase_orders
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION protect_purchase_order();
|
||||||
|
|
||||||
|
CREATE FUNCTION protect_purchase_order_line() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||||
|
BEGIN
|
||||||
|
IF TG_OP <> 'INSERT' THEN
|
||||||
|
RAISE EXCEPTION 'Purchase order line snapshots cannot be changed';
|
||||||
|
END IF;
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM purchase_orders WHERE id = NEW.purchase_order_id
|
||||||
|
AND organization_id = NEW.organization_id AND status = 'DRAFT') THEN
|
||||||
|
RAISE EXCEPTION 'Purchase order lines require a draft order';
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$;
|
||||||
|
CREATE TRIGGER purchase_order_line_protected BEFORE INSERT OR UPDATE OR DELETE ON purchase_order_lines
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION protect_purchase_order_line();
|
||||||
|
|
@ -94,12 +94,40 @@ describe('purchase-order API', () => {
|
||||||
.auth(ctx.token, auth)
|
.auth(ctx.token, auth)
|
||||||
.expect(200);
|
.expect(200);
|
||||||
expect(detail.body.lines[0].unitPrice).toBe('120');
|
expect(detail.body.lines[0].unitPrice).toBe('120');
|
||||||
|
await expect(
|
||||||
|
ctx.db.purchaseOrderLine.update({
|
||||||
|
where: { id: detail.body.lines[0].id },
|
||||||
|
data: { unitPrice: '1.00' },
|
||||||
|
}),
|
||||||
|
).rejects.toThrow();
|
||||||
|
await expect(
|
||||||
|
ctx.db.purchaseOrder.update({
|
||||||
|
where: { id: created.body.id },
|
||||||
|
data: { merchandiseTotal: '1.00' },
|
||||||
|
}),
|
||||||
|
).rejects.toThrow();
|
||||||
const issued = await ctx
|
const issued = await ctx
|
||||||
.api()
|
.api()
|
||||||
.post(`/api/v1/purchase-orders/${created.body.id}/issue`)
|
.post(`/api/v1/purchase-orders/${created.body.id}/issue`)
|
||||||
.auth(ctx.token, auth)
|
.auth(ctx.token, auth)
|
||||||
.expect(201);
|
.expect(201);
|
||||||
expect(issued.body).toMatchObject({ status: 'ISSUED' });
|
expect(issued.body).toMatchObject({ status: 'ISSUED' });
|
||||||
|
await expect(
|
||||||
|
ctx.db.purchaseOrderLine.create({
|
||||||
|
data: {
|
||||||
|
purchaseOrderId: created.body.id,
|
||||||
|
organizationId: ctx.owner.organizationId,
|
||||||
|
materialId: first.material.id,
|
||||||
|
materialName: 'Late edit',
|
||||||
|
materialCode: 'LATE',
|
||||||
|
unit: 'KILOGRAM',
|
||||||
|
supplierSku: 'LATE',
|
||||||
|
quantity: '1.000',
|
||||||
|
unitPrice: '1.00',
|
||||||
|
lineTotal: '1.00',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
).rejects.toThrow();
|
||||||
const invalid = await ctx
|
const invalid = await ctx
|
||||||
.api()
|
.api()
|
||||||
.post(`/api/v1/purchase-orders/${created.body.id}/cancel`)
|
.post(`/api/v1/purchase-orders/${created.body.id}/cancel`)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue