17 lines
1.0 KiB
JavaScript
17 lines
1.0 KiB
JavaScript
export class WorkshopStockOperation {
|
|
constructor(api){this.api=api;this.pending=false;this.uncertain=false;this.operation=null;}
|
|
async send(path,body,key){
|
|
if(this.pending||this.uncertain)throw new Error('Recupere primeiro a operação pendente.');
|
|
this.operation={path,body:JSON.parse(JSON.stringify(body)),key};return this.execute();
|
|
}
|
|
async retry(){if(!this.uncertain||this.pending)throw new Error('Não existe uma operação incerta para recuperar.');return this.execute();}
|
|
async execute(){
|
|
this.pending=true;const op=this.operation;
|
|
try{const result=await this.api(op.path,'POST',op.body,undefined,op.key);
|
|
if(!result||!Number.isInteger(result.id??result.movement_id??result.claim_id))throw new Error('Resposta incompleta; conserve a decisão para recuperação.');
|
|
this.uncertain=false;return result;
|
|
}catch(e){this.uncertain=!Number.isInteger(e.status)||e.status>=500||e.status===429;throw e;}
|
|
finally{this.pending=false;}
|
|
}
|
|
}
|