فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A04): preserve immutable financial history on complete backup restore
* fix(A04): protect finalized ledger, cash and stock during complete backup restore * test(A04): reject full-snapshot overwrites of posted invoices, journals, cash and stock
davoodmehraban89 committed
Oct 16, 2026 at 17:36 UTC
c4476025ac8f0c8dd74f89d3453a49cdd6c38cf8
2 files changed
+124
-1
src/core/StorageService.js
+40
-1
@@ -45,6 +45,32 @@ const INDEXES = {
45
const CREDENTIAL_FIELDS = ['pass', 'pwHash', 'pwSalt', 'pwIter', 'pwAlgo'];
46
47
const APPEND_ONLY_STORES = new Set(['audit_events', 'document_corrections']);
48
+const FINALIZED_COMMERCE_STORES = new Set([
49
+ 'stock_movements', 'settlements', 'transactions'
50
+]);
51
+
52
+async function existingFinalizedRecord(tx, store, incoming, existing) {
53
+ if (!existing) return false;
54
+ if (FINALIZED_COMMERCE_STORES.has(store)) return true;
55
+ if (store === 'journal_entries') return existing.status === 'posted' || incoming.status === 'posted';
56
+ if (store === 'invoices' || store === 'cheques' || store === 'expenses') {
57
+ return Boolean(existing.financePosted || incoming.financePosted);
58
+ }
59
+ if (store === 'treasury') {
60
+ return existing.recordType === 'transaction' || incoming.recordType === 'transaction';
61
+ }
62
+ if (store === 'journal_lines' || store === 'invoice_items') {
63
+ const parentStore = store === 'journal_lines' ? 'journal_entries' : 'invoices';
64
+ const relation = store === 'journal_lines' ? 'journalId' : 'invoiceId';
65
+ const a = await tx.get(parentStore, existing[relation]);
66
+ const b = existing[relation] === incoming[relation] ? a :
67
+ await tx.get(parentStore, incoming[relation]);
68
+ return parentStore === 'journal_entries'
69
+ ? a?.status === 'posted' || b?.status === 'posted'
70
+ : Boolean(a?.financePosted || b?.financePosted);
71
+ }
72
+ return false;
73
+}
74
75
// Compare snapshot metadata without depending on JSON property insertion order.
76
function snapshotComparable(value) {
@@ -383,7 +409,20 @@ class StorageServiceImpl {
409
}
410
}
411
} else {
386
- await Promise.all(items.map(item => tx.putRaw(store, item)));
412
+ for (const item of items) {
413
+ // A full backup is not an authorization to rewrite posted invoices,
414
+ // journals, settlements, cheques, money or inventory movements.
415
+ // Older snapshots may add missing rows but conflicting historical
416
+ // financial facts fail the complete restore transaction.
417
+ const previous = item.id === undefined ? null : await tx.get(store, item.id);
418
+ if (previous && await existingFinalizedRecord(tx, store, item, previous)) {
419
+ if (!sameSnapshotRecord(previous, item)) {
420
+ throw new Error('بازنویسی سند قطعی مالی در بازیابی کامل مجاز نیست');
421
+ }
422
+ continue;
423
+ }
424
+ await tx.putRaw(store, item);
425
+ }
426
}
427
}
428
});
tests/a04-full-restore-finalized-records.test.mjs
new
+84
@@ -0,0 +1,84 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import 'fake-indexeddb/auto';
4
+import { StorageService } from '../src/core/StorageService.js';
5
+
6
+const backup = data => ({ app: 'Factor-easyPro', version: 7, data });
7
+const C = 'historic-company';
8
+
9
+test('A04: full backup cannot mutate posted invoices and must roll back earlier master changes', async () => {
10
+ await StorageService.clearAll();
11
+ await StorageService.put('invoices', {
12
+ id: 'posted-sale', companyId: C, financePosted: true,
13
+ kind: 'sale', grandTotal: 1000, paidAmount: 200, remaining: 800
14
+ });
15
+ const original = await StorageService.get('invoices', 'posted-sale');
16
+ await assert.rejects(StorageService.importAll(backup({
17
+ contacts: [{ id: 'rollback-contact', companyId: C, name: 'Must not remain' }],
18
+ invoices: [{ ...original, grandTotal: 100 }]
19
+ })), /بازنویسی سند قطعی/);
20
+ assert.equal(await StorageService.get('contacts', 'rollback-contact'), undefined);
21
+ assert.deepEqual(await StorageService.get('invoices', 'posted-sale'), original);
22
+
23
+ await StorageService.importAll(backup({ invoices: [{ ...original }] }));
24
+ assert.deepEqual(await StorageService.get('invoices', 'posted-sale'), original);
25
+});
26
+
27
+test('A04: full restore rejects mutation of original posted ledger lines', async () => {
28
+ await StorageService.clearAll();
29
+ await StorageService.put('journal_entries', {
30
+ id: 'historic-journal', companyId: C, status: 'posted',
31
+ date: '1405/01/01', periodId: 'old-period'
32
+ });
33
+ await StorageService.put('journal_lines', {
34
+ id: 'historic-line', companyId: C, journalId: 'historic-journal',
35
+ accountId: 'account-id', debit: 400, credit: 0
36
+ });
37
+ const original = await StorageService.get('journal_lines', 'historic-line');
38
+ await assert.rejects(StorageService.importAll(backup({
39
+ journal_lines: [{ ...original, debit: 99 }]
40
+ })), /بازنویسی سند قطعی/);
41
+ assert.deepEqual(await StorageService.get('journal_lines', 'historic-line'), original);
42
+});
43
+
44
+test('A04: full restore protects historical treasury, stock and settlement records', async () => {
45
+ await StorageService.clearAll();
46
+ const cases = [
47
+ ['stock_movements', { id: 'stock-1', companyId: C, type: 'out', cost: 100 }, { cost: 999 }],
48
+ ['settlements', { id: 'settlement-1', companyId: C, amount: 100 }, { amount: 5 }],
49
+ ['treasury', { id: 'cash-1', companyId: C, recordType: 'transaction', amount: 100 }, { amount: 5 }],
50
+ ['cheques', { id: 'cheque-1', companyId: C, financePosted: true, status: 'pending' }, { status: 'cleared' }],
51
+ ['expenses', { id: 'expense-1', companyId: C, financePosted: true, amount: 100 }, { amount: 1 }]
52
+ ];
53
+ for (const [store, initial, changed] of cases) {
54
+ await StorageService.put(store, { ...initial });
55
+ const old = await StorageService.get(store, initial.id);
56
+ await assert.rejects(StorageService.importAll(backup({
57
+ [store]: [{ ...old, ...changed }]
58
+ })), /بازنویسی سند قطعی/);
59
+ assert.deepEqual(await StorageService.get(store, initial.id), old);
60
+ }
61
+});
62
+
63
+test('A04: original finalized actor cannot be switched to another company by global restore', async () => {
64
+ await StorageService.clearAll();
65
+ await StorageService.put('invoices', {
66
+ id: 'company-owned-sale', companyId: 'original-c1',
67
+ financePosted: true, grandTotal: 110
68
+ });
69
+ const old = await StorageService.get('invoices', 'company-owned-sale');
70
+ await assert.rejects(StorageService.importAll(backup({
71
+ invoices: [{ ...old, companyId: 'other-c2' }]
72
+ })), /بازنویسی سند قطعی/);
73
+ assert.deepEqual(await StorageService.get('invoices', old.id), old);
74
+});
75
+
76
+test('A04: legitimate master data changes remain permitted for explicit recovery', async () => {
77
+ await StorageService.clearAll();
78
+ await StorageService.put('contacts', { id: 'cust', companyId: C, name: 'Before' });
79
+ const before = await StorageService.get('contacts', 'cust');
80
+ await StorageService.importAll(backup({
81
+ contacts: [{ ...before, name: 'After' }]
82
+ }));
83
+ assert.equal((await StorageService.get('contacts', 'cust')).name, 'After');
84
+});