mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
91deedae8b
decodeJson/decodeJsonResult/encodeJson read and write a JSON-in-TEXT column against the schema and fallback declared for it in db/json-columns.ts, and log a row that does not decode. reservations.metadata, the OAuth scope and redirect lists, consents, API token scopes, MFA backup codes, journey entry tags and pros/cons and poll options go through it. Readers that used to throw on a broken row (a hotel update, the ICS export, the price mirror, the OAuth token and session reads, a consent, a journey entry, a poll) now take the tolerant fallback the other readers of the same column already used.
54 lines
2.6 KiB
TypeScript
54 lines
2.6 KiB
TypeScript
import { RESERVATION_METADATA } from '../../db/json-columns';
|
|
import { decodeJsonResult, logJsonFailure } from '../../utils/json-column';
|
|
|
|
/**
|
|
* Metadata keys on a reservation that the booking forms do not own.
|
|
*
|
|
* `price` and `priceCurrency` are written by the expense side
|
|
* (BudgetService.syncReservationPrice) and by the booking importer, never by a
|
|
* booking form. The forms rebuild metadata from their own fields on every save
|
|
* and carry only `transit` and `airtrail_ids` across, so an ordinary edit used
|
|
* to drop the price and the card lost it until the expense was saved again
|
|
* (#2233). The same happened unattended on the AirTrail poll, which updates a
|
|
* reservation with a mapped metadata object that never contains a price.
|
|
*
|
|
* Kept out of the reservations service so it can be exercised on its own.
|
|
*/
|
|
|
|
/** Read a stored metadata column into an object, or null if it is not one. */
|
|
function parseStored(stored: string | null | undefined): Record<string, unknown> | null {
|
|
const decoded = decodeJsonResult(RESERVATION_METADATA, stored);
|
|
if (decoded.ok) return decoded.value;
|
|
if (decoded.reason !== 'empty') logJsonFailure(RESERVATION_METADATA, decoded.reason);
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Carry `price` / `priceCurrency` from the stored metadata into an incoming
|
|
* object that does not name them.
|
|
*
|
|
* Everything else passes through untouched, which is what keeps the caller in
|
|
* control: `undefined` (no metadata on the payload) leaves the row alone, `null`
|
|
* clears the column, and naming the key — including setting it to null — is
|
|
* taken at face value, so a price can still be removed deliberately.
|
|
*
|
|
* There is deliberately no "only while an expense is linked" condition. The
|
|
* importer stamps a price onto the booking whether or not the Costs addon is
|
|
* enabled, and an MCP booking created with a price of 0 gets no linked item
|
|
* either, so that condition would drop a real price on the first edit. What
|
|
* stops a stale price from outliving its expense is the other end:
|
|
* BudgetService.deleteBudgetItem clears the mirror when the expense goes away.
|
|
*/
|
|
export function keepMirroredPrice(incoming: unknown, stored: string | null | undefined): unknown {
|
|
if (!incoming || typeof incoming !== 'object' || Array.isArray(incoming)) return incoming;
|
|
const next = incoming as Record<string, unknown>;
|
|
if ('price' in next) return incoming;
|
|
|
|
const prev = parseStored(stored);
|
|
if (!prev || prev.price === undefined) return incoming;
|
|
|
|
const kept: Record<string, unknown> = { ...next, price: prev.price };
|
|
if (prev.priceCurrency !== undefined && !('priceCurrency' in next)) kept.priceCurrency = prev.priceCurrency;
|
|
return kept;
|
|
}
|