Add linked native Rihla account and quote access

This commit is contained in:
unimatrixgit 2026-11-28 15:56:15 +01:00
commit aeec2ef52c

View file

@ -1,5 +1,6 @@
import {createHash} from 'node:crypto';
import {identity} from './auth.mjs';
import {gameAuthorized,json,validUuid} from './game-link.mjs';
import {money,transferAmount} from './postbank-catalogue.mjs';
export const RIHLA_SCHEMA=[
`CREATE TABLE IF NOT EXISTS ria_accounts(owner_id CHAR(36) PRIMARY KEY,account_number VARCHAR(40) NOT NULL UNIQUE,balance_minor BIGINT NOT NULL DEFAULT 0,reserved_minor BIGINT NOT NULL DEFAULT 0,created_at BIGINT NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
@ -21,3 +22,56 @@ export function createRihlaAPI(central,bank,{authenticate=r=>identity(r,central)
await ensureRihlaAccount(bank,user.id,{clock});const account=await bank.first('SELECT account_number,balance_minor,reserved_minor FROM ria_accounts WHERE owner_id=?',[user.id]);const rows=await bank.query('SELECT transaction_number,sender_name,recipient_name,amount_minor,state FROM ria_transfers WHERE sender_id=? OR recipient_id=? ORDER BY created_at DESC,id DESC LIMIT 26 OFFSET '+((page-1)*25),[user.id,user.id]);
return reply({account:{number:account.account_number,balance:money(account.balance_minor),available:money(BigInt(account.balance_minor)-BigInt(account.reserved_minor)),currency:'DM'},page,hasMore:rows.length>25,transactions:rows.slice(0,25).map(r=>({number:r.transaction_number,sender:r.sender_name,recipient:r.recipient_name,amount:money(r.amount_minor),status:r.state}))});
}catch{return reply({message:'Rihla records are temporarily unavailable.'},503);}};}
export function createGameRihlaAPI(central,bank,settings={},deps={}){
const clock=deps.clock??Date.now;
const resolve=deps.resolve??(async uuid=>{
const rows=await central.query(`SELECT a.id,a.username FROM site_accounts a
JOIN site_game_links l ON l.user_id=a.id
WHERE LOWER(l.minecraft_uuid)=? AND a.password_hash NOT LIKE '!%'
AND a.role NOT IN ('system_account','official_account','memorial')`,[uuid]);
return rows.length===1?rows[0]:null;
});
return async request=>{
if(request.method!=='POST')return json({message:'Method not allowed.'},405);
if(!gameAuthorized(request,settings))return json({message:'Unauthorized.'},401);
let input;
try{
const body=await request.text();
if(body.length>2048)return json({message:'Rihla request is too large.'},413);
input=JSON.parse(body);
}catch{return json({message:'Invalid Rihla request.'},400);}
if(!input||!validUuid(input.uuid))return json({message:'Invalid Rihla request.'},400);
const fields={read:['action','uuid'],quote:['action','uuid','amount','recipientUuid']};
if(!Object.hasOwn(fields,input.action)||Object.keys(input).some(key=>!fields[input.action].includes(key)))
return json({message:'Invalid Rihla request.'},400);
try{
const actor=await resolve(input.uuid);
if(!actor)return json({message:'Link your active personal website account first.'},403);
if(!bank)return json({message:'Rihla records are temporarily unavailable.'},503);
if(input.action==='quote'){
if(!validUuid(input.recipientUuid)||input.recipientUuid===input.uuid)
return json({message:'Choose another linked player.'},400);
let quote;
try{quote=rihlaQuote(input.amount);}catch(error){return json({message:error.message},400);}
const recipient=await resolve(input.recipientUuid);
if(!recipient||recipient.id===actor.id)return json({message:'Choose another linked player.'},400);
return json({...quote,recipientName:recipient.username});
}
return json(await bank.transaction(async tx=>{
await ensureRihlaAccount(tx,actor.id,{clock});
const account=await tx.first('SELECT account_number,balance_minor,reserved_minor FROM ria_accounts WHERE owner_id=?',[actor.id]);
const rows=await tx.query(`SELECT transaction_number,sender_id,sender_name,recipient_name,amount_minor,tax_minor,state,created_at
FROM ria_transfers WHERE sender_id=? OR recipient_id=? ORDER BY created_at DESC,id DESC LIMIT 25`,[actor.id,actor.id]);
const pending=await tx.first("SELECT COALESCE(SUM(amount_minor),0) AS total_minor FROM ria_transfers WHERE recipient_id=? AND state='created'",[actor.id]);
return {accountNo:account.account_number,balanceMinor:String(account.balance_minor),reservedMinor:String(account.reserved_minor),
collectableMinor:String(pending.total_minor),transfers:rows.map(row=>{
const sent=row.sender_id===actor.id;
return {number:row.transaction_number,direction:sent?'sent':'received',
counterparty:sent?row.recipient_name:row.sender_name,amountMinor:String(row.amount_minor),taxMinor:String(row.tax_minor),
state:row.state,createdAt:Number(row.created_at)};
})};
}));
}catch{return json({message:'Rihla records are temporarily unavailable.'},503);}
};
}