File size: 4,261 Bytes
e9af8e8
 
 
 
dba6001
e9af8e8
ab3bd11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9af8e8
 
 
 
ab3bd11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9af8e8
 
ab3bd11
e9af8e8
ab3bd11
e9af8e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab3bd11
e9af8e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import fs from "node:fs/promises";
import crypto from "node:crypto";
import { FileBlob, SpreadsheetFile } from "@oai/artifact-tool";

const [inputPath, outputPath, password = "20302030"] = process.argv.slice(2);

const CITY_INFO = new Map([
  ["المدينة الصناعية الأولى بالدمام", { region: "منطقة الدمام", city: "الصناعية الأولى بالدمام" }],
  ["المدينة صناعية الثانية بالدمام", { region: "منطقة الدمام", city: "الصناعية الثانية بالدمام" }],
  ["المدينة صناعية الثالثة بالدمام", { region: "منطقة الدمام", city: "الصناعية الثالثة بالدمام" }],
  ["المدينة صناعية الأولى بالأحساء", { region: "منطقة الأحساء", city: "المدينة صناعية الأولى بالأحساء" }],
  ["المدينة الصناعية بحفر الباطن", { region: "منطقة حفر الباطن", city: "المدينة الصناعية بحفر الباطن" }],
  ['مدينة الملك سلمان "سبارك"', { region: "سبارك", city: "مدينة الملك سلمان" }],
]);

const REGIONS = ["منطقة الدمام", "منطقة الأحساء", "منطقة حفر الباطن", "سبارك"];

function normalizeHeader(value) {
  return String(value ?? "")
    .replace(/\s+/g, " ")
    .trim();
}

function getByHeader(row, headers, candidates) {
  for (const candidate of candidates) {
    const index = headers.findIndex((header) => normalizeHeader(header) === candidate);
    if (index !== -1) return row[index] ?? "";
  }
  return "";
}

function clean(value) {
  if (value === null || value === undefined) return "";
  return String(value).trim();
}

if (!inputPath || !outputPath) {
  console.error("Usage: node generate-data.mjs <input.xlsx> <output.js> [password]");
  process.exitCode = 1;
} else {
  const blob = await FileBlob.load(inputPath);
  const workbook = await SpreadsheetFile.importXlsx(blob);
  const rows = [];

  for (const sheet of workbook.worksheets.items) {
    const info = CITY_INFO.get(sheet.name);
    if (!info) continue;
    const { region, city } = info;

    const values = sheet.getUsedRange(true).values;
    if (!values?.length) continue;
    const headers = values[0].map(normalizeHeader);

    for (const sourceRow of values.slice(1)) {
      const establishmentName = clean(getByHeader(sourceRow, headers, ["إسم المنشأة", "اسم المنشأة"]));
      if (!establishmentName) continue;
      rows.push({
        region,
        city,
        establishmentName,
        commercialRecord: clean(getByHeader(sourceRow, headers, ["السجل التجاري من الاطار", "السجل التجاري"])),
        complianceStatus: clean(getByHeader(sourceRow, headers, ["حالة الاستيفاء"])),
        cityClarification: clean(getByHeader(sourceRow, headers, ["توضيح المدينة", "توضيح المدينة الصناعية"])),
        coordinates: clean(getByHeader(sourceRow, headers, ["الاحداثيات", "الإحداثية", "الإحداثيات"])),
      });
    }
  }

  const payload = JSON.stringify({
    version: 2,
    generatedAt: new Date().toISOString(),
    regions: REGIONS,
    rows,
  });

  const iterations = 310000;
  const salt = crypto.randomBytes(16);
  const iv = crypto.randomBytes(12);
  const key = crypto.pbkdf2Sync(password, salt, iterations, 32, "sha256");
  const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
  const encrypted = Buffer.concat([cipher.update(payload, "utf8"), cipher.final()]);
  const authTag = cipher.getAuthTag();
  const combined = Buffer.concat([encrypted, authTag]);

  const output = [
    "/* Generated encrypted data. Replace this file using generate-data.mjs when the workbook changes. */",
    "const ENCRYPTED_DATA = Object.freeze({",
    `  iterations: ${iterations},`,
    `  salt: "${salt.toString("base64")}",`,
    `  iv: "${iv.toString("base64")}",`,
    `  payload: "${combined.toString("base64")}",`,
    "});",
    "",
  ].join("\n");

  await fs.writeFile(outputPath, output, "utf8");
  console.log(JSON.stringify({ rows: rows.length, regions: REGIONS.length, outputPath }));
}