Some checks failed
CI / Frontend Lint (pull_request) Failing after 8s
CI / Frontend Type Check (pull_request) Failing after 6s
CI / Frontend Build (pull_request) Failing after 5s
CI / Frontend E2E Tests (pull_request) Failing after 6s
CI / Orchestrator Build (pull_request) Failing after 6s
CI / Contracts Compile (pull_request) Failing after 6s
CI / Contracts Test (pull_request) Failing after 5s
Code Quality / SonarQube Analysis (pull_request) Failing after 18s
Code Quality / Code Quality Checks (pull_request) Failing after 5s
Security Scan / Dependency Vulnerability Scan (pull_request) Failing after 3s
Security Scan / OWASP ZAP Scan (pull_request) Failing after 4s
Closes gap-analysis v2 §4 partial (canonical "Execution Reference Set") and §10.6 SWIFT message ID persistence. - Migration 006 adds swift_message_id + swift_message_type columns to executions, with a partial index on swift_message_id for acknowledgment ingest (camt.025/054 -> original MT760/MT202 lookup). - db/executions.ts: recordExecution() UPSERT helper, getExecution(), findBySwiftMessageId() — the three queries the bank-instruction phase and SWIFT gateway need. - services/bank.ts.commitBankInstruction now emits a SWIFT reference alongside the ISO-20022 envelope: MT760 for plans carrying an issueInstrument step (real generateMt760 output, messageReference field), MT202 for payment-only plans (synthetic ref). - services/execution.ts persists the reference set at bank_instruction complete-time via recordExecution (best-effort; logs on failure, does not abort the leg). - 5 unit tests covering MT760 vs MT202 branching, reference uniqueness across calls, and SQL shape of the UPSERT + SELECT.
140 lines
4.3 KiB
TypeScript
140 lines
4.3 KiB
TypeScript
/**
|
|
* SWIFT message-id persistence (arch §4 Execution Reference Set,
|
|
* gap v2 §4 partial, §10.6).
|
|
*
|
|
* Commands tested:
|
|
* - commitBankInstruction returns swiftMessageId + swiftMessageType
|
|
* depending on whether the plan has an issueInstrument step
|
|
* - MT760 reference for instrument legs; MT202 synthetic ref for
|
|
* payment-only legs
|
|
* - db/executions.recordExecution upserts the SWIFT fields
|
|
*/
|
|
|
|
import { describe, it, expect, jest } from "@jest/globals";
|
|
import type { Plan } from "../../src/types/plan";
|
|
import { commitBankInstruction } from "../../src/services/bank";
|
|
|
|
jest.mock("../../src/db/postgres", () => {
|
|
const calls: Array<{ sql: string; params?: unknown[] }> = [];
|
|
return {
|
|
query: jest.fn(async (sql: string, params?: unknown[]) => {
|
|
calls.push({ sql, params });
|
|
return [];
|
|
}),
|
|
__calls: calls,
|
|
};
|
|
});
|
|
|
|
jest.mock("../../src/services/compliance", () => ({
|
|
getComplianceData: jest.fn(async () => ({ lei: "TEST-LEI", status: "ok" })),
|
|
}));
|
|
|
|
import { recordExecution, getExecution } from "../../src/db/executions";
|
|
|
|
function basePlan(overrides: Partial<Plan> = {}): Plan {
|
|
return {
|
|
plan_id: "plan-test-1",
|
|
schema_version: 1,
|
|
creator: "0xabc",
|
|
nonce: 1,
|
|
created_at: new Date().toISOString(),
|
|
steps: [
|
|
{
|
|
type: "pay",
|
|
from: "acct-a",
|
|
to: "acct-b",
|
|
amount: 100,
|
|
currency: "USD",
|
|
} as any,
|
|
],
|
|
...overrides,
|
|
} as Plan;
|
|
}
|
|
|
|
function instrumentPlan(): Plan {
|
|
return basePlan({
|
|
steps: [
|
|
{
|
|
type: "issueInstrument",
|
|
instrument: {
|
|
instrumentType: "SBLC",
|
|
amount: 1000000,
|
|
currency: "USD",
|
|
issuingBankBIC: "EIBIAEAD",
|
|
beneficiaryBankBIC: "ADCBAEAA",
|
|
beneficiaryName: "ACME TRADING LLC",
|
|
beneficiaryAccount: "AE12 3456 7890 1234",
|
|
expiryDate: "2026-12-31",
|
|
placeOfPresentation: "DUBAI",
|
|
governingLaw: "URDG 758",
|
|
applicant: "APPLICANT INC",
|
|
templateRef: "EI-SBLC-v1",
|
|
templateHash: "a".repeat(64),
|
|
tenor: "12M",
|
|
},
|
|
} as any,
|
|
{
|
|
type: "pay",
|
|
from: "acct-a",
|
|
to: "acct-b",
|
|
amount: 1000000,
|
|
currency: "USD",
|
|
} as any,
|
|
],
|
|
});
|
|
}
|
|
|
|
describe("commitBankInstruction SWIFT reference output", () => {
|
|
it("issues an MT760 reference when the plan contains issueInstrument", async () => {
|
|
const result = await commitBankInstruction(instrumentPlan());
|
|
expect(result.success).toBe(true);
|
|
expect(result.swiftMessageType).toBe("MT760");
|
|
expect(result.swiftMessageId).toMatch(/^MT760-/);
|
|
expect(result.isoMessageId).toMatch(/^MSG-/);
|
|
});
|
|
|
|
it("issues an MT202 reference when no issueInstrument step is present", async () => {
|
|
const result = await commitBankInstruction(basePlan());
|
|
expect(result.success).toBe(true);
|
|
expect(result.swiftMessageType).toBe("MT202");
|
|
expect(result.swiftMessageId).toMatch(/^MT202-/);
|
|
});
|
|
|
|
it("returns different swiftMessageIds across successive calls", async () => {
|
|
const a = await commitBankInstruction(basePlan());
|
|
const b = await commitBankInstruction(basePlan());
|
|
expect(a.swiftMessageId).not.toBe(b.swiftMessageId);
|
|
});
|
|
});
|
|
|
|
describe("db/executions SQL wiring", () => {
|
|
it("recordExecution builds an UPSERT including swift_message_id fields", async () => {
|
|
const pg = require("../../src/db/postgres");
|
|
pg.__calls.length = 0;
|
|
await recordExecution("exec-1", "plan-1", {
|
|
phase: "bank_instruction",
|
|
isoMessageId: "iso-1",
|
|
swiftMessageId: "MT760-ABC",
|
|
swiftMessageType: "MT760",
|
|
});
|
|
expect(pg.query).toHaveBeenCalled();
|
|
const call = pg.__calls[0];
|
|
expect(call.sql).toMatch(/INSERT INTO executions/);
|
|
expect(call.sql).toMatch(/swift_message_id/);
|
|
expect(call.sql).toMatch(/swift_message_type/);
|
|
expect(call.sql).toMatch(/ON CONFLICT \(execution_id\) DO UPDATE/);
|
|
expect(call.params).toEqual(
|
|
expect.arrayContaining(["exec-1", "plan-1", "bank_instruction", "iso-1", "MT760-ABC", "MT760"]),
|
|
);
|
|
});
|
|
|
|
it("getExecution selects the swift_message_* columns", async () => {
|
|
const pg = require("../../src/db/postgres");
|
|
pg.__calls.length = 0;
|
|
await getExecution("exec-1");
|
|
const call = pg.__calls[0];
|
|
expect(call.sql).toMatch(/swift_message_id/);
|
|
expect(call.sql).toMatch(/swift_message_type/);
|
|
});
|
|
});
|