forked from unimatrix/web
38 lines
4.3 KiB
JavaScript
38 lines
4.3 KiB
JavaScript
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)<amount)throw new JarvisError('Insufficient available funds.',409);
|
|
const tax=await tx.first("SELECT id,state FROM pb_accounts WHERE system_key='sys_tax_treasury'");if(!tax||tax.state!=='active')throw new JarvisError('Tax treasury unavailable. No funds were reserved.',503);
|
|
const id=randomUUID();await tx.query("INSERT INTO pb_auction_holds(id,auction_id,user_id,source_id,amount_minor,state,created_at) VALUES(?,?,?,?,?,'reserved',?)",[id,auctionId,actor.id,source.id,String(amount),clock()]);
|
|
await tx.query('UPDATE pb_accounts SET reserved_minor=reserved_minor+? WHERE id=?',[String(amount),source.id]);return id;
|
|
},
|
|
async release(tx,{holdId,auctionId}){
|
|
const hold=await tx.first('SELECT * FROM pb_auction_holds WHERE id=? FOR UPDATE',[holdId]);
|
|
if(!hold||hold.auction_id!==auctionId||hold.state!=='reserved')throw Error('Auction reservation requires reconciliation');
|
|
const source=await tx.first('SELECT * FROM pb_accounts WHERE id=? FOR UPDATE',[hold.source_id]);
|
|
if(!source||BigInt(source.reserved_minor)<BigInt(hold.amount_minor))throw Error('Auction reserve mismatch');
|
|
await tx.query('UPDATE pb_accounts SET reserved_minor=reserved_minor-? WHERE id=?',[hold.amount_minor,source.id]);await tx.query("UPDATE pb_auction_holds SET state='released' WHERE id=?",[holdId]);
|
|
},
|
|
async settlePayment(tx,{holdId,auctionId,amountMinor,recipient}){
|
|
if(recipient!=='sys_tax_treasury')throw Error('Invalid office auction recipient');
|
|
const hold=await tx.first('SELECT * FROM pb_auction_holds WHERE id=? FOR UPDATE',[holdId]);
|
|
if(!hold||hold.state!=='reserved'||hold.auction_id!==auctionId||BigInt(hold.amount_minor)!==BigInt(amountMinor))throw Error('Auction settlement reserve mismatch');
|
|
const taxMeta=await tx.first("SELECT id FROM pb_accounts WHERE system_key='sys_tax_treasury'");if(!taxMeta)throw Error('Tax treasury unavailable');
|
|
const accounts=await tx.query(accountSQL+' WHERE a.id IN (?,?) ORDER BY a.id FOR UPDATE',[hold.source_id,taxMeta.id]),source=accounts.find(a=>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)<BigInt(hold.amount_minor))throw Error('Auction accounts require review');
|
|
const fingerprint=createHash('sha256').update('office-auction:'+auctionId+':'+holdId).digest('hex');
|
|
await tx.query("INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at) VALUES(?,?,?,?,?,? ,?,'auction','queued',?)",[holdId,hold.user_id,source.id,tax.id,fingerprint,hold.amount_minor,'Office auction · '+auctionId,clock()]);
|
|
const order=await tx.first('SELECT * FROM pb_orders WHERE id=?',[holdId]);await payments.post(tx,order,source,tax,true);await tx.query("UPDATE pb_auction_holds SET state='posted' WHERE id=?",[holdId]);
|
|
}
|
|
});
|
|
}
|

Staff