fix: Kanban pulls and final approvals stop failing on stale or missing context

Sentry DTF-WEB-4..7 (12/11):
- Pulling a card with no machine asks which machine takes it (gestão and
  admins); an operator always pulls to the machine of their session.
- The alert before a pull only offers Feito/Adiar to the machine's own
  operator or management, as the machine sheet does.
- A final-file approval from a dialog opened before the same approval
  went through reloads and says it was already approved, instead of the
  409 "O pedido mudou".

Built by Prensa; machine cards shared between the two choosers.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-12 15:48:41 -03:00
parent 7abd215d13
commit d0ec629c4e
3 changed files with 110 additions and 36 deletions

View File

@@ -1886,13 +1886,34 @@ function readyFinals(order){
return {files,name:files[0].name,source:original?'original':'generated',label:(original?'O original (':'Gerado pelo sistema (')+names+')'};
});
}
// Sends final-files from a dialog that may have been opened before another
// screen approved the same files (DTF-WEB-7). A stale version gets 409 "O
// pedido mudou"; when the approval is already there, or the order left Arte
// recebida/Correção, reload and say so; true tells the caller to stop, and a
// form dialog then closes itself. Anything else is still an error, and the
// version is never resent blindly.
async function saveFinalFiles(order, files, note){
try{
await api('/orders/'+order.id+'/final-files',{version:order.version,files,note});
return false;
}catch(error){
if(error.status!==409||!String(error.message).startsWith('O pedido mudou'))throw error;
await load();
const fresh=(board.orders||[]).find(row=>row.id===order.id);
if(fresh&&(finalDone(fresh)||!['rec','cor'].includes(fresh.state))){
say('O arquivo final já estava aprovado.');
return true;
}
throw error;
}
}
async function approveReady(order,ready){
const many=ready.length>1;
if(!await ask('Aprovar o arquivo final?','Conferiu? É este arquivo que vai para a impressão: '+
ready.map((r,i)=>(many?'item '+(i+1)+', ':'')+r.label).join('; ')+'.','Aprovar arquivo final',{host:$('drawer')}))return;
const note='Aprovado como está: '+[...new Set(ready.map(r=>FINAL_SOURCE[r.source]))].join(', ');
await api('/orders/'+order.id+'/final-files',{version:order.version,
files:ready.flatMap((r,i)=>r.files.map(f=>({item_index:i,upload_id:f.upload_id}))),note});
if(await saveFinalFiles(order,
ready.flatMap((r,i)=>r.files.map(f=>({item_index:i,upload_id:f.upload_id}))),note))return;
await load();say(mayMove(order.state,'tra')?'Arquivo final aprovado. Agora é só mover para Arte conferida.':'Arquivo final aprovado. Falta mover para Arte conferida.');
}
// The file the operator prepared, per item; a ready file comes ticked.
@@ -1930,7 +1951,7 @@ function finalDialog(order,ready,{replace=false}={}){
refs.push({item_index:index,upload_id:uid});
}
}
await api('/orders/'+order.id+'/final-files',{version:order.version,files:refs,note:note.value});
if(await saveFinalFiles(order,refs,note.value))return;
await load();say('Arquivo final aprovado.');
}});
}
@@ -2151,9 +2172,9 @@ function isOk(order){
for(const [index,select] of selects.entries())
if(select.value!==contractionOf(order,index))
await api('/orders/'+order.id+'/contraction',{item_index:index,value:select.value});
if(!finalDone(order))
await api('/orders/'+order.id+'/final-files',{version:order.version,
files:ready.flatMap((r,i)=>r.files.map(f=>({item_index:i,upload_id:f.upload_id}))),note:'Conferido em Abrir e conferir'});
if(!finalDone(order)&&await saveFinalFiles(order,
ready.flatMap((r,i)=>r.files.map(f=>({item_index:i,upload_id:f.upload_id}))),
'Conferido em Abrir e conferir'))return;
await load();
await move(board.orders.find(o=>o.id===order.id)||order,'tra');
say('Pedido #'+order.number+' em Arte conferida.');
@@ -2210,7 +2231,7 @@ function treatDialog(order){
const uid=await dtfUpload(p.file,{api,startPath:'/orders/'+order.id+'/uploads',scope:'operator:'+order.id+':'+order.version,progress:text=>say(text)});
refs.push({item_index:index,upload_id:uid});
}
await api('/orders/'+order.id+'/final-files',{version:order.version,files:refs,note:TREAT_NOTE});
if(await saveFinalFiles(order,refs,TREAT_NOTE))return;
await load();
say(mayMove(order.state,'tra')?'Arte tratada aprovada como arquivo final. Agora é só mover para Arte conferida.':'Arte tratada aprovada como arquivo final.');
}});