feat(orders): amendments, family imports, and the ingest API

Closes the three gaps left after the parser rebuild.

Refund amendments. ue-05 is a real refund: 'Previous total $49.94 / Refund
-$4.21 / New Total $45.73'. Uber reuses the order UUID across the receipt and
the amendment, so the two can be matched. The transaction is reduced in place
rather than offset with a second row — the order is one event whose cost
changed, and a compensating row would misreport both the meal count and the
merchant's spend. When the original was never ingested, nothing is invented.

[Family] orders now import instead of parking. Their payment line names the
payer, not an instrument ('Payments Siddharth LKR 3,783.20'), so no split is
recoverable and there is no card leg to reconcile against — they would have sat
pending forever, which fails the actual requirement to import and tag them.
Treated as credits, flagged as an assumption. Safe because the family tag
removes them from every budget regardless of instrument, and the LKR amount is
preserved with amount_aud left NULL rather than asserting an FX rate.

Ingest API. n8n now POSTs each message to /api/orders/ingest instead of parsing
in a Code node — the n8n sandbox has no require or fs, so a parser there cannot
be tested against the fixture corpus, which is the one thing that makes this
parser trustworthy. Auth is a shared secret, since machine callers have no
Traefik session header. Rejections return 422 and record nothing.

60 unit + 45 integration green on three consecutive runs; 63/65 corpus holds.
This commit is contained in:
2026-07-27 00:48:20 +10:00
parent c82a22767f
commit a9e251d969
4 changed files with 328 additions and 6 deletions
+62
View File
@@ -306,3 +306,65 @@ export function resolveCategory(order: ParsedOrder): string {
if (/woolworths|aldi|coles|glomark|keells|cargills|iga|costco/.test(m)) return "groceries";
return "dining";
}
/**
* Applies a refund / total-adjustment notice to an already-ingested order.
*
* The order it amends is matched by the UUID Uber reuses across the original
* receipt and the amendment. The recorded transaction is reduced to the new
* total rather than a compensating negative row being added: the order is one
* event and its cost changed, and a second row would misreport both the meal
* count and the merchant's spend.
*
* If the original has not been ingested (amendment arrived first, or the
* receipt predates the cutover), nothing is invented — the amendment is
* recorded as unmatched for a later pass.
*/
export async function applyOrderAmendment(a: {
order_reference: string | null;
new_total: number;
refund_amount: number | null;
previous_total: number | null;
order_datetime: string;
messageId: string;
}): Promise<{ matched: boolean; transactionId: number | null; adjusted: number | null }> {
if (!a.order_reference) return { matched: false, transactionId: null, adjusted: null };
const meta = await queryRow<{ id: number; transaction_id: number | null; amount: string }>(
`SELECT id, transaction_id, amount::text FROM expense_metadata
WHERE source = 'email' AND order_reference = $1`,
[a.order_reference]
);
if (!meta) return { matched: false, transactionId: null, adjusted: null };
await queryRaw(
`UPDATE expense_metadata
SET amount = $2,
flags = flags || $3::jsonb
WHERE id = $1`,
[
meta.id,
a.new_total,
JSON.stringify([
`amended_from_${Number(meta.amount).toFixed(2)}`,
a.refund_amount !== null ? `refund_${a.refund_amount.toFixed(2)}` : "amended",
]),
]
);
if (meta.transaction_id === null) {
// Card-settled or still pending: no transaction of ours to reduce. The
// refund will show on the statement in its own right.
return { matched: true, transactionId: null, adjusted: null };
}
await queryRaw(
`UPDATE transactions
SET amount = $2,
amount_aud = CASE WHEN foreign_currency_code IS NULL THEN $2 ELSE amount_aud END
WHERE id = $1`,
[meta.transaction_id, a.new_total]
);
return { matched: true, transactionId: meta.transaction_id, adjusted: a.new_total };
}