forked from unimatrix/web
Connect supplied Rihla ATM and recover interrupted cash actions
This commit is contained in:
parent
28b6955676
commit
be41df5726
2 changed files with 206 additions and 39 deletions
|
|
@ -5,12 +5,12 @@ import {NATIVE_SERVERS} from './native-server-scope.mjs';
|
|||
|
||||
export const RIHLA_CASH_SCHEMA=[
|
||||
`CREATE TABLE IF NOT EXISTS ria_cash_pieces(
|
||||
serial CHAR(36) PRIMARY KEY,denomination VARCHAR(24) NOT NULL,state VARCHAR(16) NOT NULL,operation_id CHAR(36) NOT NULL
|
||||
serial CHAR(36) PRIMARY KEY,denomination VARCHAR(24) NOT NULL,state VARCHAR(16) NOT NULL,operation_id CHAR(36) NOT NULL,INDEX ria_piece_operation(operation_id,state)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
`CREATE TABLE IF NOT EXISTS ria_cash_operations(
|
||||
id CHAR(36) PRIMARY KEY,owner_id CHAR(36) NOT NULL,player_uuid CHAR(36) NOT NULL,server VARCHAR(32) NOT NULL,
|
||||
direction VARCHAR(12) NOT NULL,terms VARCHAR(64) NOT NULL,fingerprint CHAR(64) NOT NULL,amount_minor BIGINT NOT NULL,
|
||||
pieces_json TEXT NOT NULL,state VARCHAR(16) NOT NULL,voucher VARCHAR(40) UNIQUE NULL,created_at BIGINT NOT NULL,expires_at BIGINT NOT NULL
|
||||
pieces_json TEXT NOT NULL,state VARCHAR(16) NOT NULL,voucher VARCHAR(40) UNIQUE NULL,created_at BIGINT NOT NULL,expires_at BIGINT NOT NULL,INDEX ria_cash_owner_operation(owner_id,state,created_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
||||
`CREATE TABLE IF NOT EXISTS ria_cash_redemptions(
|
||||
id CHAR(36) PRIMARY KEY,voucher VARCHAR(40) NOT NULL UNIQUE,owner_id CHAR(36) NOT NULL,player_uuid CHAR(36) NOT NULL,
|
||||
|
|
@ -78,6 +78,8 @@ export function createRihlaCashLedger(bank,{readReceipt,clock=Date.now}={}){
|
|||
await tx.query('UPDATE ria_cash_operations SET expires_at=? WHERE id=?',[expires_at,previous.id]);
|
||||
return permit({...previous,expires_at});
|
||||
}
|
||||
const pendingOperation=await tx.first("SELECT id FROM ria_cash_operations WHERE owner_id=? AND state='prepared' ORDER BY created_at,id LIMIT 1 FOR UPDATE",[actor.id]);
|
||||
if(pendingOperation)fail('A previous cash operation must be recovered first.');
|
||||
const pieces=deposit?submitted:sorted(submitted.map(denomination=>({serial:randomUUID(),denomination})));
|
||||
const minor=pieces.reduce((total,piece)=>total+BigInt(values[piece.denomination]),0n);
|
||||
if(deposit){
|
||||
|
|
@ -114,6 +116,13 @@ export function createRihlaCashLedger(bank,{readReceipt,clock=Date.now}={}){
|
|||
});
|
||||
}
|
||||
|
||||
async function pending(actor,input){
|
||||
const row=await bank.first("SELECT * FROM ria_cash_operations WHERE owner_id=? AND player_uuid=? AND server=? AND state='prepared' ORDER BY created_at,id LIMIT 1",
|
||||
[actor.id,input.uuid,input.server]);
|
||||
if(!row)return {ok:true,pending:false};
|
||||
return {...await prepareAbort(actor,{...input,requestId:row.id}),pending:true};
|
||||
}
|
||||
|
||||
async function prepareAbort(actor,input){
|
||||
return bank.transaction(async tx=>{
|
||||
const op=await tx.first('SELECT * FROM ria_cash_operations WHERE id=? FOR UPDATE',[input.requestId]);
|
||||
|
|
@ -166,7 +175,7 @@ export function createRihlaCashLedger(bank,{readReceipt,clock=Date.now}={}){
|
|||
if(input.action==='cash_lookup'){
|
||||
const row=await bank.first(`SELECT o.amount_minor FROM ria_cash_operations o
|
||||
LEFT JOIN ria_cash_redemptions r ON r.voucher=o.voucher
|
||||
WHERE o.voucher=? AND o.direction='deposit' AND o.state='committed' AND r.id IS NULL`,[input.voucher]);
|
||||
WHERE o.voucher=? AND o.direction='deposit' AND o.state='committed' AND (r.id IS NULL OR (r.mode='cash' AND r.state='prepared' AND r.owner_id=? AND r.player_uuid=?))`,[input.voucher,actor.id,input.uuid]);
|
||||
if(!row)fail('Cash voucher is unavailable.');
|
||||
return {ok:true,voucher:input.voucher,amountMinor:String(row.amount_minor)};
|
||||
}
|
||||
|
|
@ -176,9 +185,11 @@ export function createRihlaCashLedger(bank,{readReceipt,clock=Date.now}={}){
|
|||
const deposit=await tx.first("SELECT amount_minor FROM ria_cash_operations WHERE voucher=? AND direction='deposit' AND state='committed' FOR UPDATE",
|
||||
[input.voucher]);
|
||||
if(!deposit)fail('Cash voucher is unavailable.');
|
||||
const previous=await tx.first('SELECT * FROM ria_cash_redemptions WHERE id=? OR voucher=? FOR UPDATE',[input.requestId,input.voucher]);
|
||||
const reference=await tx.first('SELECT voucher FROM ria_cash_redemptions WHERE id=? FOR UPDATE',[input.requestId]);
|
||||
if(reference&&reference.voucher!==input.voucher)fail('Cash voucher is unavailable.');
|
||||
const previous=await tx.first('SELECT * FROM ria_cash_redemptions WHERE voucher=? FOR UPDATE',[input.voucher]);
|
||||
if(previous){
|
||||
if(previous.id!==input.requestId||previous.voucher!==input.voucher||previous.owner_id!==actor.id||previous.player_uuid!==input.uuid||
|
||||
if(previous.voucher!==input.voucher||previous.owner_id!==actor.id||previous.player_uuid!==input.uuid||
|
||||
previous.mode!=='bank'||previous.state!=='redeemed')
|
||||
fail('Cash voucher is unavailable.');
|
||||
return {ok:true,requestId:previous.id,state:'redeemed',amountMinor:String(previous.amount_minor)};
|
||||
|
|
@ -195,7 +206,9 @@ export function createRihlaCashLedger(bank,{readReceipt,clock=Date.now}={}){
|
|||
return async (actor,input)=>{
|
||||
if(!validUuid(input.uuid))fail('Invalid native cash identity.');
|
||||
if(input.action==='cash_lookup')return voucher(actor,input);
|
||||
if(!validUuid(input.uuid)||!validUuid(input.requestId)||!NATIVE_SERVERS.includes(input.server))fail('Invalid native cash request.');
|
||||
if(!NATIVE_SERVERS.includes(input.server))fail('Invalid native cash server.');
|
||||
if(input.action==='cash_pending')return pending(actor,input);
|
||||
if(!validUuid(input.requestId))fail('Invalid native cash request.');
|
||||
if(input.action==='cash_prepare'||input.action==='cash_redeem_cash')return prepare(actor,input);
|
||||
if(input.action==='cash_settle')return settle(actor,input);
|
||||
if(input.action==='cash_prepare_abort')return prepareAbort(actor,input);
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ export function createGameRihlaAPI(central,bank,settings={},deps={}){
|
|||
read:['action','uuid'],quote:['action','uuid','amount','recipientUuid'],
|
||||
create:['action','uuid','amount','recipientUuid','quoteReference','requestId'],
|
||||
cancel:['action','uuid','number'],collect:['action','uuid','number'],collectAll:['action','uuid'],
|
||||
cash_prepare:['action','uuid','server','requestId','direction','pieces'],
|
||||
cash_prepare:['action','uuid','server','requestId','direction','pieces'],cash_pending:['action','uuid','server'],
|
||||
cash_settle:['action','uuid','server','requestId'],cash_prepare_abort:['action','uuid','server','requestId'],cash_lookup:['action','uuid','voucher'],
|
||||
cash_redeem_bank:['action','uuid','server','requestId','voucher'],
|
||||
cash_redeem_cash:['action','uuid','server','requestId','voucher','direction','pieces']
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff