سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 03:00 UTC
fix(accounting): serialize journal RPC submissions
davoodmehraban89 committed
Nov 16, 2026 at 07:19 UTC
e37174cc29a543120ed944acb39b50f401f7e408
2 files changed
+27
-7
js/journal-rpc.js
+13
-7
@@ -5,6 +5,7 @@
5
let jeRpcEffectiveStatuses=new Map();
6
let jeRpcStatusRefreshToken=0;
7
let jeRpcPendingCreate=null;
8
+let jeRpcBusy=false;
9
10
async function jeRpcAssertContext(){
11
if(!currentUser?.id||!currentUser?.organizationId)throw new Error('سازمان فعال برای عملیات دفتر کل مشخص نیست.');
@@ -37,12 +38,17 @@ function jeRpcLines(lines){
38
}))}));
39
}
40
async function jeRpcCall(name,args){
40
- const activeOrg=await jeRpcAssertContext();
41
- if(args.p_organization_id&&args.p_organization_id!==activeOrg)throw new Error('سازمان فعال تغییر کرده است؛ عملیات برای جلوگیری از ثبت بینسازمانی متوقف شد.');
42
- if(syncPending||syncRunning)throw new Error('ابتدا همگامسازی تغییرات دیگر را کامل کنید؛ عملیات دفتر کل برای جلوگیری از بازنویسی داده معلق متوقف شد.');
43
- const {data,error}=await sb.rpc(name,args);
44
- if(error){const e=new Error(error.message||'عملیات دفتر کل در سرور انجام نشد.');e.code=error.code||'';throw e;}
45
- return data;
41
+ if(jeRpcBusy)throw new Error('یک عملیات دفتر کل در حال اجراست؛ از ارسال دوباره درخواست خودداری کنید.');
42
+ jeRpcBusy=true;
43
+ try{
44
+ const activeOrg=await jeRpcAssertContext();
45
+ if(args.p_organization_id&&args.p_organization_id!==activeOrg)throw new Error('سازمان فعال تغییر کرده است؛ عملیات برای جلوگیری از ثبت بینسازمانی متوقف شد.');
46
+ if(syncPending||syncRunning)throw new Error('ابتدا همگامسازی تغییرات دیگر را کامل کنید؛ عملیات دفتر کل برای جلوگیری از بازنویسی داده معلق متوقف شد.');
47
+ const {data,error}=await sb.rpc(name,args);
48
+ if(error){const e=new Error(error.message||'عملیات دفتر کل در سرور انجام نشد.');e.code=error.code||'';throw e;}
49
+ if(name==='finora_journal_read_graph')jeRpcBusy=false;
50
+ return data;
51
+ }catch(err){jeRpcBusy=false;throw err;}
52
}
53
async function jeRpcRefresh(expectedOrg){
54
try{
@@ -51,7 +57,7 @@ async function jeRpcRefresh(expectedOrg){
57
await pullAll();
58
if(currentUser?.organizationId!==expectedOrg)throw new Error('FINORA_ORGANIZATION_CONTEXT_CHANGED');
59
}catch(err){const e=new Error('عملیات در سرور ثبت شد، اما سازمان/اطلاعات صفحه تغییر کرد. صفحه را تازه کنید و پیش از ارسال مجدد، وضعیت سند را بررسی کنید.');e.code='FINORA_RPC_COMMITTED_REFRESH_FAILED';throw e;}
54
- await jeRpcRefreshEffectiveStatuses();
60
+ try{await jeRpcRefreshEffectiveStatuses();}finally{jeRpcBusy=false;}
61
}
62
async function jeRpcRefreshEffectiveStatuses(){
63
const token=++jeRpcStatusRefreshToken,org=await jeRpcAssertContext();
scripts/journal-rpc-adapter.test.mjs
+14
@@ -44,6 +44,20 @@ test('manual draft create calls only the dedicated server RPC with active organi
44
assert.equal(result.id,'JV_1');
45
});
46
47
+test('concurrent UI submissions are serialized and duplicate RPCs are blocked',async()=>{
48
+ const h=harness();
49
+ let release;
50
+ const wait=new Promise(resolve=>{release=resolve});
51
+ h.context.sb.rpc=async(name,args)=>{h.calls.push({name,args});await wait;return {data:{voucher:{id:args.p_voucher_id,version:1,status:'draft'}},error:null};};
52
+ const input={date:'1405/07/19',description:'manual',voucherType:'manual',lines:[{accountId:'A1',debit:100,credit:0},{accountId:'A2',debit:0,credit:100}]};
53
+ const first=h.context.jeRpcCreateDraft(input);
54
+ await new Promise(resolve=>setImmediate(resolve));
55
+ await assert.rejects(()=>h.context.jeRpcCreateDraft(input),/در حال اجراست/);
56
+ release();
57
+ await first;
58
+ assert.equal(h.calls.filter(x=>x.name==='finora_journal_create_draft').length,1);
59
+});
60
+
61
test('ambiguous draft-create retry reuses the same idempotency key',async()=>{
62
const h=harness();
63
let loseFirst=true;