فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(A03): make full-snapshot restores append-only for audited financial history
davoodmehraban89 committed
Oct 15, 2026 at 21:04 UTC
bca229dd58226ddf5b3a6826af6b2afe03e7b891
1 file changed
+33
src/core/StorageService.js
+33
@@ -44,6 +44,22 @@ const INDEXES = {
44
// فیلدهای مربوط به رمز عبور کاربران؛ هرگز در فایل پشتیبان نمیروند
45
const CREDENTIAL_FIELDS = ['pass', 'pwHash', 'pwSalt', 'pwIter', 'pwAlgo'];
46
47
+const APPEND_ONLY_STORES = new Set(['audit_events', 'document_corrections']);
48
+
49
+// Compare snapshot metadata without depending on JSON property insertion order.
50
+function snapshotComparable(value) {
51
+ if (Array.isArray(value)) return value.map(snapshotComparable);
52
+ if (value && typeof value === 'object') {
53
+ const normalized = {};
54
+ for (const key of Object.keys(value).sort()) normalized[key] = snapshotComparable(value[key]);
55
+ return normalized;
56
+ }
57
+ return value;
58
+}
59
+const sameSnapshotRecord = (left, right) =>
60
+ JSON.stringify(snapshotComparable(left)) === JSON.stringify(snapshotComparable(right));
61
+
62
+
63
function stripCredentials(user) {
64
const copy = { ...user };
65
CREDENTIAL_FIELDS.forEach(f => delete copy[f]);
@@ -349,6 +365,23 @@ class StorageServiceImpl {
365
if (!items) continue;
366
if (store === 'users') {
367
await this._importUsers(tx, items);
368
+ } else if (APPEND_ONLY_STORES.has(store)) {
369
+ // Backup replay may re-add history but can never rewrite it.
370
+ // A conflicting record aborts the WHOLE multi-store restore.
371
+ for (const item of items) {
372
+ if (!item.id || typeof item.id !== 'string' ||
373
+ !item.companyId || typeof item.companyId !== 'string') {
374
+ throw new Error('رکورد تاریخچه ممیزی یا اصلاح سند نامعتبر است');
375
+ }
376
+ const old = await tx.get(store, item.id);
377
+ if (old) {
378
+ if (!sameSnapshotRecord(old, item)) {
379
+ throw new Error('بازنویسی سابقه ممیزی یا اصلاح سند از پشتیبان مجاز نیست');
380
+ }
381
+ } else {
382
+ await tx.add(store, { ...item });
383
+ }
384
+ }
385
} else {
386
await Promise.all(items.map(item => tx.putRaw(store, item)));
387
}