@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 5f1d848
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 4, 03:00 UTC

fix(accounting): reuse draft id across ambiguous RPC retries

davoodmehraban89 committed Nov 16, 2026 at 07:15 UTC 5f1d848a750bf459261c0d5510d19e91dd4a2f64
2 files changed +25 -2
js/journal-rpc.js
+9 -2
@@ -4,6 +4,7 @@
4 */
5 let jeRpcEffectiveStatuses=new Map();
6 let jeRpcStatusRefreshToken=0;
7 +let jeRpcPendingCreate=null;
8
9 async function jeRpcAssertContext(){
10 if(!currentUser?.id||!currentUser?.organizationId)throw new Error('سازمان فعال برای عملیات دفتر کل مشخص نیست.');
@@ -62,11 +63,17 @@ async function jeRpcCreateDraft(input){
63 const org=await jeRpcAssertContext(),companyId=afCompanyId(),date=jeRpcDate(input.date);
64 const fy=jeFiscalForDate(date);
65 if(!companyId||!fy||fy.status==='locked')throw new Error('شرکت یا سال مالی باز برای تاریخ سند پیدا نشد.');
65 - const id=afId('JV'),header=jeRpcHeader(input,companyId,fy.id);
66 + const header=jeRpcHeader(input,companyId,fy.id),lines=jeRpcLines(input.lines);
67 + const fingerprint=JSON.stringify({org,header,lines});
68 + if(jeRpcPendingCreate&&jeRpcPendingCreate.fingerprint!==fingerprint)
69 + throw new Error('وضعیت درخواست قبلی ثبت پیش‌نویس نامشخص است. ابتدا صفحه را تازه کنید و فهرست اسناد را بررسی کنید؛ درخواست جدید ارسال نشد.');
70 + const id=jeRpcPendingCreate?.id||afId('JV');
71 + jeRpcPendingCreate={id,fingerprint};
72 const data=await jeRpcCall('finora_journal_create_draft',{
67 - p_organization_id:org,p_voucher_id:id,p_header:header,p_lines:jeRpcLines(input.lines)
73 + p_organization_id:org,p_voucher_id:id,p_header:header,p_lines:lines
74 });
75 await jeRpcRefresh(org);
76 + jeRpcPendingCreate=null;
77 return data?.voucher||getMyJournalVouchers().find(x=>x.id===id)||data;
78 }
79 async function jeRpcUpdateDraft(voucherId,input){
scripts/journal-rpc-adapter.test.mjs
+16
@@ -44,6 +44,22 @@ test('manual draft create calls only the dedicated server RPC with active organi
44 assert.equal(result.id,'JV_1');
45 });
46
47 +test('ambiguous draft-create retry reuses the same idempotency key',async()=>{
48 + const h=harness();
49 + let loseFirst=true;
50 + const original=h.context.sb.rpc;
51 + h.context.sb.rpc=async(name,args)=>{
52 + if(name==='finora_journal_create_draft'&&loseFirst){loseFirst=false;h.calls.push({name,args});throw new Error('response lost after server commit');}
53 + return original(name,args);
54 + };
55 + const input={date:'1405/07/19',description:'manual',voucherType:'manual',lines:[{accountId:'A1',debit:100,credit:0},{accountId:'A2',debit:0,credit:100}]};
56 + await assert.rejects(()=>h.context.jeRpcCreateDraft(input),/response lost/);
57 + const firstId=h.calls[0].args.p_voucher_id;
58 + const result=await h.context.jeRpcCreateDraft(input);
59 + assert.equal(h.calls[1].args.p_voucher_id,firstId);
60 + assert.equal(result.id,firstId);
61 +});
62 +
63 test('draft update uses the server row version as compare-and-swap token',async()=>{
64 const h=harness({vouchers:[{id:'JV_D',status:'draft',sourceType:'manual',companyId:'CO_A',fiscalYearId:'FY_A',_serverVersion:7}]});
65 await h.context.jeRpcUpdateDraft('JV_D',{date:'1405/07/19',description:'edited',voucherType:'manual',lines:[{accountId:'A1',debit:100,credit:0},{accountId:'A2',debit:0,credit:100}]});