فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
feat(A02): add zero-write explicit mapped Holoo extract preflight
davoodmehraban89 committed
Oct 17, 2026 at 01:44 UTC
6ad879aa18c33dc2a984f2bb9c5d29970c8f27f0
1 file changed
+125
src/core/HolooMigrationPreflight.mjs
new
+125
@@ -0,0 +1,125 @@
1
+// A02: format-independent, zero-write profiler for user-authorized Holoo exports.
2
+// This does NOT decode proprietary backup files or migrate/post financial records.
3
+const TABLES = Object.freeze({
4
+ contacts: { keys: ['sourceId'], required: ['sourceId', 'name'], money: ['receivable', 'payable'] },
5
+ products: { keys: ['sourceId'], required: ['sourceId', 'name'], money: [] },
6
+ invoices: { keys: ['sourceId'], required: ['sourceId', 'grandTotal', 'paidAmount'], money: ['grandTotal', 'paidAmount'] },
7
+ invoiceLines: { keys: [], required: ['invoiceId', 'productId'], money: [] },
8
+ cheques: { keys: ['sourceId'], required: ['sourceId', 'amount'], money: ['amount'] }
9
+});
10
+const own = (o, key) => Object.prototype.hasOwnProperty.call(o, key);
11
+const nonempty = value => (typeof value === 'string' && value.trim().length > 0) ||
12
+ (Number.isSafeInteger(value) && value >= 0);
13
+const identity = value => nonempty(value) ? String(value).trim() : null;
14
+const money = value => typeof value === 'number' &&
15
+ Number.isSafeInteger(value) && value >= 0;
16
+// Numeric parsing, currency scaling, identifier normalization and enum mapping
17
+// belong to an explicit versioned source adapter, NOT this generic validator.
18
+
19
+export function previewHolooExtract(input = {}, { maxIssues = 100 } = {}) {
20
+ if (!Number.isSafeInteger(maxIssues) || maxIssues < 1 || maxIssues > 1000) {
21
+ throw new RangeError('حداکثر مغایرتها باید بین ۱ تا ۱۰۰۰ باشد');
22
+ }
23
+ if (!input || typeof input !== 'object' || Array.isArray(input) ||
24
+ input.sourceSystem !== 'holoo') {
25
+ throw new TypeError('فرمت منبع باید صراحتاً Holoo و توسط مالک تأیید شده باشد');
26
+ }
27
+ const datasets = input.datasets || {};
28
+ const mappings = input.columns || {};
29
+ if (!datasets || typeof datasets !== 'object' || Array.isArray(datasets) ||
30
+ !mappings || typeof mappings !== 'object' || Array.isArray(mappings)) {
31
+ throw new TypeError('ساختار جدولها یا نگاشت ستونها نامعتبر است');
32
+ }
33
+
34
+ const issues = [], counts = {};
35
+ let issueCount = 0;
36
+ const add = (code, table, rowIndex = null) => {
37
+ issueCount++;
38
+ if (issues.length < maxIssues) {
39
+ // No names, account/cheque numbers, raw IDs, source values or PII.
40
+ issues.push(Object.freeze({ code, table, rowIndex }));
41
+ }
42
+ };
43
+ const field = (table, record, canonical) => {
44
+ const key = mappings[table]?.[canonical];
45
+ return typeof key === 'string' && key.length && own(record, key)
46
+ ? record[key] : undefined;
47
+ };
48
+ const sets = {};
49
+ let rowTotal = 0;
50
+ for (const table of Object.keys(TABLES)) {
51
+ const rows = datasets[table] ?? [];
52
+ if (!Array.isArray(rows)) throw new TypeError('جدول ورودی باید آرایه باشد: ' + table);
53
+ if (rows.length > 100000) throw new RangeError('اندازه جدول برای پیشبررسی محدود است: ' + table);
54
+ sets[table] = new Set();
55
+ counts[table] = rows.length;
56
+ rowTotal += rows.length;
57
+ if (!rows.length) continue;
58
+ const rules = TABLES[table];
59
+ const columns = mappings[table];
60
+ if (!columns || typeof columns !== 'object' || Array.isArray(columns)) {
61
+ add('MAPPING_REQUIRED', table);
62
+ continue;
63
+ }
64
+ const requiredFields = [...new Set([...rules.required, ...rules.money])];
65
+ const missing = requiredFields.filter(c => typeof columns[c] !== 'string' || !columns[c]);
66
+ for (const ignored of missing) add('MAPPING_FIELD_REQUIRED', table);
67
+ if (missing.length) continue;
68
+ const ids = new Set();
69
+ rows.forEach((record, i) => {
70
+ if (!record || typeof record !== 'object' || Array.isArray(record)) {
71
+ add('INVALID_ROW', table, i);
72
+ return;
73
+ }
74
+ for (const key of rules.required) {
75
+ if (!nonempty(field(table, record, key))) add('REQUIRED_FIELD_EMPTY', table, i);
76
+ }
77
+ for (const key of rules.money) {
78
+ if (!money(field(table, record, key))) add('AMOUNT_NOT_INTEGER_RIAL', table, i);
79
+ }
80
+ if (table === 'invoices' &&
81
+ money(field(table, record, 'paidAmount')) &&
82
+ money(field(table, record, 'grandTotal')) &&
83
+ field(table, record, 'paidAmount') > field(table, record, 'grandTotal')) {
84
+ add('PAID_EXCEEDS_GROSS', table, i);
85
+ }
86
+ for (const key of rules.keys) {
87
+ const id = identity(field(table, record, key));
88
+ if (!id) continue;
89
+ if (ids.has(id)) add('DUPLICATE_SOURCE_ID', table, i);
90
+ else ids.add(id);
91
+ }
92
+ });
93
+ sets[table] = ids;
94
+ }
95
+ if (rowTotal === 0) add('SOURCE_HAS_NO_MAPPED_ROWS', 'source');
96
+
97
+ // Referential checks run only when both involved key maps are present.
98
+ // Missing datasets do not prove missing source objects; this is not a
99
+ // completeness certificate.
100
+ const references = [
101
+ { table: 'invoices', key: 'contactId', parent: 'contacts' },
102
+ { table: 'invoiceLines', key: 'invoiceId', parent: 'invoices' },
103
+ { table: 'invoiceLines', key: 'productId', parent: 'products' }
104
+ ];
105
+ for (const { table, key, parent } of references) {
106
+ const rows = datasets[table] || [];
107
+ const mapped = mappings[table]?.[key];
108
+ if (!rows.length || !mapped || !counts[parent] || !mappings[parent]?.sourceId) continue;
109
+ rows.forEach((record, i) => {
110
+ if (!record || typeof record !== 'object' || Array.isArray(record)) return;
111
+ const id = identity(field(table, record, key));
112
+ if (id && !sets[parent].has(id)) add('REFERENCE_NOT_FOUND', table, i);
113
+ });
114
+ }
115
+
116
+ const details = Object.freeze({ ...counts });
117
+ return Object.freeze({
118
+ sourceSystem: 'holoo',
119
+ inputType: 'explicitly-mapped-tabular-extract',
120
+ status: issueCount ? 'blocked' : 'requires-source-and-accountant-review',
121
+ counts: details, inspectedRows: rowTotal, issueCount,
122
+ truncated: issueCount > issues.length, issues: Object.freeze(issues),
123
+ writePerformed: false, importPerformed: false, canCommit: false
124
+ });
125
+}