فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A04): verify restored posted invoices and treasury transactions link to valid journals
* fix(A04): validate company-restored posted invoices, cheques and treasury journal links * fix(A04): reject missing cheque status journal references before IndexedDB lookup * test(A04): enforce legitimate finance-posted invoice, cheque and treasury journal references
davoodmehraban89 committed
Oct 16, 2026 at 17:38 UTC
9295a2308de066ccab16d4b35f388b39d52a6ec7
2 files changed
+157
src/core/CompanyBackupService.js
+49
@@ -124,6 +124,55 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
124
}
125
}
126
127
+ // A restored row labelled as finance-posted must point to the real
128
+ // posted journal produced by its source command. Otherwise a partial
129
+ // backup could fabricate a "paid/posted" invoice without any ledger.
130
+ for (const [store, kind] of [
131
+ ['invoices', 'invoice'], ['expenses', 'expense'], ['cheques', 'cheque']
132
+ ]) {
133
+ for (const row of data[store] || []) {
134
+ if (!row.financePosted) continue;
135
+ if (!isId(row.journalId)) {
136
+ throw Error('سند مالی قطعی بازیابیشده فاقد شناسه دفتر کل است');
137
+ }
138
+ const journal = await tx.get('journal_entries', row.journalId);
139
+ if (!journal || journal.companyId !== companyId ||
140
+ journal.status !== 'posted' ||
141
+ journal.sourceType !== kind || journal.sourceId !== row.id) {
142
+ throw Error('ارجاع دفتر کل سند مالی بازیابیشده نامعتبر است');
143
+ }
144
+ if (store === 'cheques' && row.statusVersion === 2) {
145
+ if (!isId(row.statusJournalId)) {
146
+ throw Error('شناسه سند تغییر وضعیت چک بازیابیشده نامعتبر است');
147
+ }
148
+ const statusJournal = await tx.get('journal_entries', row.statusJournalId);
149
+ if (!statusJournal || statusJournal.companyId !== companyId ||
150
+ statusJournal.status !== 'posted' ||
151
+ statusJournal.sourceType !== 'cheque_status' ||
152
+ statusJournal.sourceId !== row.id) {
153
+ throw Error('ارجاع سند تغییر وضعیت چک بازیابیشده نامعتبر است');
154
+ }
155
+ }
156
+ }
157
+ }
158
+ for (const row of data.treasury || []) {
159
+ if (row.recordType !== 'transaction' || !row.financePosted) continue;
160
+ if (!isId(row.journalId)) {
161
+ throw Error('تراکنش مالی بازیابیشده فاقد سند دفتر کل است');
162
+ }
163
+ const journal = await tx.get('journal_entries', row.journalId);
164
+ const journalTypes = new Set(['treasury', 'expense', 'cheque_status']);
165
+ if (!journal || journal.companyId !== companyId ||
166
+ journal.status !== 'posted' || !journalTypes.has(journal.sourceType)) {
167
+ throw Error('تراکنش خزانه به سند دفتر کل معتبر اشاره ندارد');
168
+ }
169
+ if (journal.sourceType === 'treasury' && journal.sourceId !== row.id ||
170
+ journal.sourceType === 'expense' && journal.sourceId !== row.refExpenseId ||
171
+ journal.sourceType === 'cheque_status' && journal.sourceId !== row.refChequeId) {
172
+ throw Error('سند دفتر کل تراکنش خزانه به عملیات دیگری تعلق دارد');
173
+ }
174
+ }
175
+
176
if (COMPANY_BACKUP_STORES.filter(s => V2_STORES.has(s)).some(s => (data[s] || []).length)) {
177
const accounts = await tx.getAllByIndex('account_chart', 'companyId', companyId);
178
const periods = await tx.getAllByIndex('fiscal_periods', 'companyId', companyId);
tests/a04-company-restore-journal-references.test.mjs
new
+108
@@ -0,0 +1,108 @@
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
+import { Auth } from '../src/core/Auth.js';
6
+import { restoreCompanyBackup } from '../src/core/CompanyBackupService.js';
7
+
8
+const C = 'references-c1';
9
+const opts = { companyId: C, userId: 'manager', mode: 'merge' };
10
+
11
+async function reset() {
12
+ await StorageService.clearAll();
13
+ Auth.currentUser = { id: 'manager', username: 'manager', role: 'admin' };
14
+ await StorageService.put('companies', { id: C, name: 'Example company' });
15
+ await StorageService.put('companies', { id: 'references-c2', name: 'Foreign company' });
16
+}
17
+
18
+test('A04: a posted invoice without a journal is rejected before any import writes', async () => {
19
+ await reset();
20
+ await assert.rejects(restoreCompanyBackup({
21
+ contacts: [{ id: 'should-roll-back', companyId: C, name: 'Must not persist' }],
22
+ invoices: [{ id: 'fake-posted', companyId: C, financePosted: true,
23
+ kind: 'sale', journalId: 'no-journal', grandTotal: 1000 }]
24
+ }, opts), /ارجاع دفتر کل/);
25
+ assert.equal(await StorageService.get('contacts', 'should-roll-back'), undefined);
26
+ assert.equal(await StorageService.get('invoices', 'fake-posted'), undefined);
27
+ assert.equal((await StorageService.getAll('audit_events')).length, 0);
28
+});
29
+
30
+test('A04: a posted invoice cannot cite another company or another source journal', async () => {
31
+ await reset();
32
+ await StorageService.put('journal_entries', {
33
+ id: 'foreign-journal', companyId: 'references-c2',
34
+ status: 'posted', sourceType: 'invoice', sourceId: 'fake-posted'
35
+ });
36
+ await StorageService.put('journal_entries', {
37
+ id: 'wrong-source', companyId: C,
38
+ status: 'posted', sourceType: 'invoice', sourceId: 'unrelated-invoice'
39
+ });
40
+ const row = { id: 'fake-posted', companyId: C, financePosted: true,
41
+ kind: 'sale', journalId: 'foreign-journal', grandTotal: 100 };
42
+ await assert.rejects(restoreCompanyBackup({ invoices: [row] }, opts),
43
+ /ارجاع دفتر کل/);
44
+ await assert.rejects(restoreCompanyBackup({
45
+ invoices: [{ ...row, journalId: 'wrong-source' }]
46
+ }, opts), /ارجاع دفتر کل/);
47
+ assert.equal(await StorageService.get('invoices', 'fake-posted'), undefined);
48
+});
49
+
50
+test('A04: a complete posted invoice referencing the correct journal may be recovered', async () => {
51
+ await reset();
52
+ await StorageService.put('journal_entries', {
53
+ id: 'verified-source', companyId: C, status: 'posted',
54
+ sourceType: 'invoice', sourceId: 'valid-invoice'
55
+ });
56
+ const restored = await restoreCompanyBackup({
57
+ invoices: [{ id: 'valid-invoice', companyId: C,
58
+ financePosted: true, journalId: 'verified-source',
59
+ kind: 'sale', grandTotal: 1000 }]
60
+ }, opts);
61
+ assert.equal(restored.added, 1);
62
+ assert.equal((await StorageService.get('invoices', 'valid-invoice')).journalId,
63
+ 'verified-source');
64
+ assert.equal((await StorageService.getAll('audit_events')).length, 1);
65
+});
66
+
67
+test('A04: posted treasury transactions cannot cite a ledger event from another command', async () => {
68
+ await reset();
69
+ await StorageService.put('journal_entries', {
70
+ id: 'posted-treasury', companyId: C, status: 'posted',
71
+ sourceType: 'treasury', sourceId: 'other-payment'
72
+ });
73
+ const item = { id: 'payment-1', companyId: C, recordType: 'transaction',
74
+ financePosted: true, type: 'receipt', journalId: 'posted-treasury', amount: 100 };
75
+ await assert.rejects(restoreCompanyBackup({ treasury: [item] }, opts),
76
+ /به عملیات دیگری/);
77
+ assert.equal(await StorageService.get('treasury', 'payment-1'), undefined);
78
+
79
+ await StorageService.put('journal_entries', {
80
+ id: 'right-treasury', companyId: C, status: 'posted',
81
+ sourceType: 'treasury', sourceId: 'payment-1'
82
+ });
83
+ const result = await restoreCompanyBackup({
84
+ treasury: [{ ...item, journalId: 'right-treasury' }]
85
+ }, opts);
86
+ assert.equal(result.added, 1);
87
+ assert.equal((await StorageService.get('treasury', 'payment-1')).journalId, 'right-treasury');
88
+});
89
+
90
+test('A04: cheque status restore cannot reference a foreign or missing status journal', async () => {
91
+ await reset();
92
+ await StorageService.put('journal_entries', {
93
+ id: 'cheque-created', companyId: C, status: 'posted',
94
+ sourceType: 'cheque', sourceId: 'ch-1'
95
+ });
96
+ const row = { id: 'ch-1', companyId: C, financePosted: true,
97
+ journalId: 'cheque-created', status: 'cleared', statusVersion: 2 };
98
+ await assert.rejects(restoreCompanyBackup({ cheques: [row] }, opts),
99
+ /شناسه سند تغییر وضعیت/);
100
+ await StorageService.put('journal_entries', {
101
+ id: 'status-from-other', companyId: 'references-c2',
102
+ status: 'posted', sourceType: 'cheque_status', sourceId: 'ch-1'
103
+ });
104
+ await assert.rejects(restoreCompanyBackup({
105
+ cheques: [{ ...row, statusJournalId: 'status-from-other' }]
106
+ }, opts), /ارجاع سند تغییر وضعیت/);
107
+ assert.equal(await StorageService.get('cheques', 'ch-1'), undefined);
108
+});