سامانه صدور فاکتور و حسابداری
test(stage2.2): fix real-stack lexical user check and prove journal sync never pulls all
davoodmehraban89 committed
Oct 18, 2026 at 06:39 UTC
6b7ab03ec16d82e89e9af2d836f86bb6c5a44bef
3 files changed
+36
-19
js/sync.js
+25
-15
@@ -119,6 +119,31 @@ async function runSync(){
119
}
120
async function handleSyncError(err){
121
const msg=String((err&&err.message)||''),code=String((err&&err.code)||'');
122
+ // Classify journal failures BEFORE generic RLS/CAS reconciliation.
123
+ // Never replace unrelated unsaved collections with a tenant-wide pullAll.
124
+ const journalError=!!err?.finoraJournalVoucherId ||
125
+ /FINORA_(SERVER_ONLY_JOURNAL_FIELDS|POSTED_JOURNAL|JOURNAL_|DRAFT_|CHILD_PARENT|INVALID_JOURNAL)/.test(msg);
126
+ if(journalError){
127
+ setSyncBadge('error');
128
+ const id=err?.finoraJournalVoucherId;
129
+ if(id&&typeof finoraManualReload==='function'){
130
+ try{
131
+ await finoraManualReload(id);
132
+ alert('فقط همین سند از سرور بازخوانی شد؛ تغییرات ذخیرهنشده بخشهای دیگر حفظ شدند.');
133
+ }catch(e){
134
+ console.error('targeted journal reconciliation failed',e);
135
+ syncPending=true;scheduleSync(5000);
136
+ return;
137
+ }
138
+ }else{
139
+ // No ID means no safe target. Fail closed, preserving ALL local edits.
140
+ alert('تعارض سند حسابداری بدون شناسه مشخص شد؛ بازخوانی کلی انجام نمیشود.');
141
+ }
142
+ const pending=computeDiff();syncPending=!!(pending.up.length||pending.del.length);
143
+ if(syncPending)scheduleSync(500);
144
+ return;
145
+ }
146
+
147
if(code==='42501'||/row-level security/i.test(msg)){
148
setSyncBadge('error');
149
syncPending=false;
@@ -128,21 +153,6 @@ async function handleSyncError(err){
153
return;
154
}
155
const financialConflict=code==='23505'||code==='40001'||/FINORA_VERSION_CONFLICT|FINORA_EXPECTED_VERSION_REQUIRED|FINORA_SERVER_ONLY_JOURNAL_FIELDS|immutable|قطعی|posted\/reversed|duplicate key|unique constraint/i.test(msg);
131
- if(financialConflict&&err?.finoraJournalVoucherId&&
132
- typeof finoraManualReload==='function'){
133
- try{
134
- await finoraManualReload(err.finoraJournalVoucherId);
135
- setSyncBadge('error');
136
- alert('نسخه سند دفترکل از سرور بازخوانی شد؛ سایر تغییرات ذخیرهنشده حفظ شدند.');
137
- const pending=computeDiff();
138
- syncPending=!!(pending.up.length||pending.del.length);
139
- if(syncPending)scheduleSync(500);
140
- }catch(e){
141
- console.error('targeted journal reconcile failed',e);
142
- syncPending=true;scheduleSync(5000);
143
- }
144
- return;
145
- }
156
if(financialConflict){
157
syncPending=false;syncFailCount=0;setSyncBadge('error');
158
alert('تغییرات با سابقه قطعی یا ثبت همزمان دیگری تعارض داشت و روی سرور ذخیره نشد. نسخه معتبر سرور دوباره بارگذاری میشود؛ عملیات را پس از بررسی تکرار کنید.');
scripts/stage22-manual-browser.mjs
+9
-2
@@ -201,11 +201,18 @@ if(retry.status!=='posted')throw Error('TIMEOUT_STATUS_QUERY_RETRY_FAILED');
201
evidence.scenarios.push({scenario:'offline-block-and-timeout-status-before-retry',result:'GREEN',sameId:draft3.id});
202
await one.evaluate(id=>{datastore.products.push({id:'UNSAVED_PRODUCT',ownerUserId:currentUser.id,title:'Keep my changes'});},draft2.id);
203
await one.evaluate(async id=>{
204
- await handleSyncError({code:'40001',message:'FINORA_VERSION_CONFLICT',finoraJournalVoucherId:id});
204
+ const originalPull=pullAll;
205
+ let globalCalls=0;
206
+ pullAll=async()=>{globalCalls++;throw Error('STAGE22_FORBIDDEN_GLOBAL_PULLALL');};
207
+ try{
208
+ await handleSyncError({code:'42501',message:'FINORA_SERVER_ONLY_JOURNAL_FIELDS',finoraJournalVoucherId:id});
209
+ await handleSyncError({code:'P0001',message:'FINORA_JOURNAL_NOT_DRAFT'});
210
+ if(globalCalls!==0)throw Error('STAGE22_JOURNAL_ERROR_CALLED_PULLALL');
211
+ }finally{pullAll=originalPull;}
212
},draft2.id);
213
const kept=await one.evaluate(()=>datastore.products.some(x=>x.id==='UNSAVED_PRODUCT'));
214
if(!kept)throw Error('UNRELATED_UNSAVED_DATA_LOST');
208
-evidence.scenarios.push({scenario:'targeted-sync-reload-preserves-unrelated-dirty-collection',result:'GREEN'});
215
+evidence.scenarios.push({scenario:'targeted-journal-sync-42501-and-unknown-ID-never-pullAll',result:'GREEN',unrelatedDirtyPreserved:true});
216
const final=JSON.parse(sql(
217
"SELECT jsonb_build_object('posted',(SELECT count(*) FROM public.records WHERE organization_id="+quote(ORG)+
218
"::uuid AND collection='journalVouchers' AND data->>'status'='posted'),"+
scripts/stage22-real-stack.mjs
+2
-2
@@ -83,7 +83,7 @@ try{
83
await page.locator('#auth-password').fill('Local-Stage22-Password!12345');
84
await page.locator('#auth-login-button').click();
85
try{
86
- await page.waitForFunction(()=>document.getElementById('auth-screen')?.style.display==='none'&&!!window.currentUser?.organizationId,null,{timeout:50000});
86
+ await page.waitForFunction(()=>document.getElementById('auth-screen')?.style.display==='none'&&typeof currentUser!=='undefined'&&!!currentUser?.organizationId,null,{timeout:50000});
87
}catch(error){
88
const stage=await page.evaluate(async()=>({
89
membershipLock:!!document.getElementById('finora-membership-lock'),
@@ -94,7 +94,7 @@ try{
94
authChecked:window.FinoraStartupStatus?.authChecked,
95
loginState:document.getElementById('auth-login-status')?.dataset?.state,
96
loginMessage:document.getElementById('auth-login-status')?.textContent?.slice(0,180),
97
- hasCurrentUser:!!window.currentUser,
97
+ hasCurrentUser:typeof currentUser!=='undefined'&&!!currentUser,
98
authSession:!!(await sb.auth.getSession()).data.session,
99
orgContextReady:!!window.FinoraOrganizationContextReady
100
}));