فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

fix(A03/A04): retain and protect audit history during company restores

davoodmehraban89 committed Oct 14, 2026 at 23:23 UTC 68086784dae99176dfd459be52974c7d03789b9a
1 file changed +15 -2
src/core/CompanyBackupService.js
+15 -2
@@ -9,7 +9,8 @@ export const COMPANY_BACKUP_STORES = Object.freeze([
9 'invoices', 'invoice_items', 'stock_movements', 'transactions',
10 'cheques', 'expenses', 'account_chart', 'fiscal_periods',
11 'journal_entries', 'journal_lines', 'warehouses', 'inventory_balances',
12 - 'stocktakes', 'stocktake_lines', 'settlements', 'account_mappings'
12 + 'stocktakes', 'stocktake_lines', 'settlements', 'account_mappings',
13 + 'audit_events', 'document_corrections'
14 ]);
15 const V2_STORES = new Set(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines']);
16 const isId = id => typeof id === 'string' && id.trim().length > 0;
@@ -38,8 +39,13 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
39 return StorageService.transaction([...COMPANY_BACKUP_STORES, 'settings'], async tx => {
40 for (const store of COMPANY_BACKUP_STORES) {
41 for (const incoming of data[store] || []) {
42 + const immutable = store === 'audit_events' || store === 'document_corrections';
43 + // Historical audit/correction events cannot be reassigned to a new company.
44 + if (immutable && incoming.companyId !== companyId) {
45 + throw new Error('رویداد ممیزی یا اصلاح سند متعلق به شرکت دیگری است');
46 + }
47 const row = { ...incoming, companyId };
42 - if (row.ownerUserId) row.ownerUserId = userId;
48 + if (row.ownerUserId && !immutable) row.ownerUserId = userId;
49 if (store === 'journal_entries' && row.sourceType && row.sourceId) {
50 row.sourceKey = journalSourceKey(companyId, row.sourceType, row.sourceId, row.revision || 1);
51 }
@@ -47,6 +53,13 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
53 if (existing && existing.companyId !== companyId) {
54 throw new Error(`شناسه رکورد ${store}/${row.id} متعلق به شرکت دیگر است؛ بازیابی متوقف شد`);
55 }
56 + if (existing && immutable) {
57 + if (JSON.stringify(existing) !== JSON.stringify(row)) {
58 + throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند مجاز نیست');
59 + }
60 + counts.skipped++;
61 + continue;
62 + }
63 if (existing && mode === 'merge') {
64 counts.skipped++;
65 continue;