import {createHash,randomUUID} from 'node:crypto'; import {entityId} from './postbank-authority.mjs'; const reset='uart-claims-reset-2026-11-17'; export const CLAIM_RESET_SCHEMA=`CREATE TABLE IF NOT EXISTS pb_claim_test_resets (id VARCHAR(64) PRIMARY KEY,entity_id CHAR(36) NOT NULL,created_at BIGINT NOT NULL)`; // Explicitly authorised test reset. Never debit Tax, erase posted history, // issue a reusable bearer key, or repeat a credit after a restart. export async function resetOwnerTestClaims(central,bank){ if(!bank)return {applied:false}; const clan=await central.prepare("SELECT c.id,c.owner_id FROM site_clans c JOIN site_accounts a ON a.id=c.owner_id WHERE c.tag='UART' AND a.username='unimatrix999' AND c.active=1").first(); if(!clan)return {applied:false}; const entity=entityId('clan',clan.id); return bank.transaction(async tx=>{ await tx.first('SELECT id FROM pb_entities WHERE id=? FOR UPDATE',[entity]); if(await tx.first('SELECT id FROM pb_claim_test_resets WHERE id=? FOR UPDATE',[reset]))return {applied:false}; const purchases=await tx.query("SELECT p.request_id,p.amount_minor,o.source_id FROM pb_claim_purchases p JOIN pb_orders o ON o.id=p.request_id WHERE p.entity_id=? AND o.state='posted' AND o.mode='claims' ORDER BY p.created_at,p.request_id",[entity]); const groups=new Map();for(const p of purchases)groups.set(String(p.source_id),(groups.get(String(p.source_id))||0n)+BigInt(p.amount_minor)); const issuerMeta=await tx.first("SELECT id FROM pb_accounts WHERE system_key='sys_bank_capital_issuer'"); if(groups.size&&!issuerMeta)throw Error('Claim reset reserve unavailable'); const ids=[...groups.keys(),...(issuerMeta?[String(issuerMeta.id)]:[])].sort((a,b)=>Number(a)-Number(b)); const accounts=ids.length?await tx.query('SELECT a.*,c.entity_id FROM pb_accounts a LEFT JOIN pb_clients c ON c.client_number=a.client_number WHERE a.id IN ('+ids.map(()=>'?').join(',')+') ORDER BY a.id FOR UPDATE',ids):[]; const issuer=accounts.find(a=>a.system_key==='sys_bank_capital_issuer');let balance=BigInt(issuer?.balance_minor||0),now=Date.now(); for(const [id,amount]of groups){ const target=accounts.find(a=>String(a.id)===id);if(!target||target.entity_id!==entity||target.state!=='active'||target.system_key||issuer.state!=='active'||amount<=0n)throw Error('Claim reset account requires review'); const credit=BigInt(target.balance_minor)+amount;balance-=amount;if(credit>9223372036854775807n||balance< -9223372036854775808n)throw Error('Claim correction exceeds balance limit'); const order=randomUUID(),fingerprint=createHash('sha256').update(reset+':'+id).digest('hex'); await tx.query("INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at,posted_at) VALUES(?,?,?,?,?,?,?,'grant','posted',?,?)",[order,clan.owner_id,issuer.id,target.id,fingerprint,String(amount),'Claim test reset ยท refund from bank capital; Tax unchanged',now,now]); await tx.query('UPDATE pb_accounts SET balance_minor=? WHERE id=?',[String(balance),issuer.id]);await tx.query('UPDATE pb_accounts SET balance_minor=? WHERE id=?',[String(credit),target.id]); for(const [account,value,after]of [[issuer,-amount,balance],[target,amount,credit]])await tx.query('INSERT INTO pb_entries(order_id,account_id,amount_minor,balance_after_minor,created_at) VALUES(?,?,?,?,?)',[order,account.id,String(value),String(after),now]); } // A later reset may not release new reservations or undo unrelated purchases. const pending=await tx.first("SELECT o.id FROM pb_claim_batches b JOIN pb_orders o ON o.id=b.order_id WHERE b.entity_id=? AND o.state='queued'",[entity]);if(pending)throw Error('Claim reset has pending purchases; review required'); await tx.query("UPDATE pb_claim_entitlements SET tier='BC_T0',revision=revision+1,updated_at=? WHERE entity_id=?",[now,entity]); await tx.query('INSERT INTO pb_claim_test_resets(id,entity_id,created_at) VALUES(?,?,?)',[reset,entity,now]); await tx.query('INSERT INTO pb_audit(id,actor_id,action,entity_id,details,created_at) VALUES(?,?,?,?,?,?)',[randomUUID(),clan.owner_id,'claim_test_reset',entity,JSON.stringify({source:'bank-capital',taxUntouched:true,purchases:purchases.map(p=>p.request_id)}),now]); return {applied:true}; }); }