import {randomUUID,createHash} from 'node:crypto'; import {canDebitAccount} from './postbank-authorisations.mjs'; import {ordinaryPaymentAccount} from './postbank-recipient-policy.mjs'; import {PostbankPayments} from './postbank-payments.mjs'; import {JarvisError} from './jarvis-listings.mjs'; export const AUCTION_HOLD_SCHEMA=`CREATE TABLE IF NOT EXISTS pb_auction_holds(id CHAR(36) PRIMARY KEY,auction_id CHAR(36) NOT NULL,user_id CHAR(36) NOT NULL,source_id BIGINT NOT NULL,amount_minor BIGINT NOT NULL,state VARCHAR(16) NOT NULL,created_at BIGINT NOT NULL)`; const accountSQL='SELECT a.*,c.entity_id,e.kind,e.external_key,e.active AS entity_active,p.state AS package_state FROM pb_accounts a LEFT JOIN pb_clients c ON c.client_number=a.client_number LEFT JOIN pb_entities e ON e.id=c.entity_id LEFT JOIN pb_packages p ON p.id=a.package_id'; export function createAuctionPayments(bank,authority,{clock=Date.now}={}){ const payments=new PostbankPayments(bank,authority,{clock}); return Object.freeze({ async reserve(tx,{actor,sourceAccount,amountMinor,auctionId}){ const rights=await authority(actor,{bank:tx}),source=await tx.first(accountSQL+' WHERE a.id=? FOR UPDATE',[sourceAccount]),amount=BigInt(amountMinor); if(!source||!ordinaryPaymentAccount(source)||!canDebitAccount(rights,source.entity_id,source.id))throw new JarvisError('Payment account authority required.',403); if(amount<=0n||BigInt(source.balance_minor)-BigInt(source.reserved_minor)String(a.id)===String(hold.source_id)),tax=accounts.find(a=>String(a.id)===String(taxMeta.id)); if(!source||!tax||source.id===tax.id||source.state!=='active'||tax.state!=='active'||BigInt(source.reserved_minor)