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

fix(A04): compare immutable company restore history independently of property order

davoodmehraban89 committed Oct 16, 2026 at 16:47 UTC cb16ca5ad888eba3ea4e1d878ba6de2ad37973d2
1 file changed +14 -1
src/core/CompanyBackupService.js
+14 -1
@@ -16,6 +16,19 @@ export const COMPANY_BACKUP_STORES = Object.freeze([
16 const V2_STORES = new Set(['account_chart', 'fiscal_periods', 'journal_entries', 'journal_lines']);
17 const isId = id => typeof id === 'string' && id.trim().length > 0;
18
19 +// Backups serialize object fields in arbitrary insertion order. Immutable
20 +// financial history equality must compare values, never field order.
21 +const stableRecord = value => {
22 + if (Array.isArray(value)) return value.map(stableRecord);
23 + if (value && typeof value === 'object') {
24 + return Object.fromEntries(Object.keys(value).sort()
25 + .map(key => [key, stableRecord(value[key])]));
26 + }
27 + return value;
28 +};
29 +const sameImmutableRecord = (a, b) =>
30 + JSON.stringify(stableRecord(a)) === JSON.stringify(stableRecord(b));
31 +
32 export async function restoreCompanyBackup(data, { companyId, userId, mode = 'merge' }) {
33 if (!Auth.isAdmin())throw Error('بازنشانی یا بازیابی اطلاعات حسابداری فقط با مدیر مجاز است');
34 if (!companyId || !userId || !['merge', 'replace'].includes(mode)) throw new Error('گزینه‌های بازیابی نامعتبر است');
@@ -55,7 +68,7 @@ export async function restoreCompanyBackup(data, { companyId, userId, mode = 'me
68 throw new Error(`شناسه رکورد ${store}/${row.id} متعلق به شرکت دیگر است؛ بازیابی متوقف شد`);
69 }
70 if (existing && immutable) {
58 - if (JSON.stringify(existing) !== JSON.stringify(row)) {
71 + if (!sameImmutableRecord(existing, row)) {
72 throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند مجاز نیست');
73 }
74 counts.skipped++;