web/website/node/postbank-client-management.mjs

38 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {PostbankService,BankError} from './postbank-service.mjs';
// Reuse customer product rules, while retaining the real staff actor. This
// adapter is never handed to a payment service or exposed as customer authority.
export class PostbankClientManagement{
constructor(db,authority,options={}){this.db=db;this.authority=authority;this.options=options;}
async scope(user,entityId,store=this.db){
if(typeof entityId!=='string'||! /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(entityId))throw new BankError('Select a banking client.');
const rights=await this.authority(user,{bank:store});
if(!rights.readAdmin)throw new BankError('Bank client access is required.',403);
const entity=await store.first('SELECT id,kind,external_key,display_name,active FROM pb_entities WHERE id=?',[entityId]);
if(!entity||Number(entity.active)!==1)throw new BankError('The banking client is unavailable.',404);
const official=['house','office','staff'].includes(entity.kind);
return {entity,canManage:!!rights.manageAdmin||!official&&!!rights.manageClients};
}
async detail(user,entityId){
const scope=await this.scope(user,entityId);
const authority=async()=>({entities:[this.entity(scope.entity,false)],readAdmin:true,manageAdmin:false});
const view=await new PostbankService(this.db,authority,this.options).overview(user);
return {...view,canManage:scope.canManage};
}
entity(row,write){return {id:row.id,kind:row.kind,key:row.external_key,name:row.display_name,canView:true,canActivate:write,canPay:write};}
async change(user,input,action,{revalidate=async()=>user}={}){
if(!input||!['package','options'].includes(action)||typeof input.reason!=='string'||input.reason.trim().length<5||input.reason.trim().length>200||/[\u0000-\u001f]/.test(input.reason))throw new BankError('Give a client service reason of 5–200 characters.');
const {reason,entityId,...fields}=input;
const scope=await this.scope(user,entityId);if(!scope.canManage)throw new BankError('Client management permission is required.',403);
if(action==='options'){
const account=await this.db.first('SELECT c.entity_id FROM pb_accounts a JOIN pb_clients c ON c.client_number=a.client_number WHERE a.id=?',[fields.accountId]);
if(account?.entity_id!==entityId)throw new BankError('The account does not belong to this client.',403);
}
const authority=async(actor,{bank=this.db}={})=>{
const current=await this.scope(actor,entityId,bank);
if(!current.canManage)throw new BankError('Your client management permission changed.',403);
return {entities:[this.entity(current.entity,true)],readAdmin:true,manageAdmin:false};
};
const service=new PostbankService(this.db,authority,{...this.options,staffAudit:{reason:reason.trim()}});
return action==='package'?service.changePackage(user,{...fields,entityId},{revalidate}):service.setAccountOptions(user,fields,{revalidate});
}
}