@fctc/interface-logic 4.3.1 → 4.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/services.js CHANGED
@@ -1653,9 +1653,9 @@ function applyBinaryOp(ast, context) {
1653
1653
  var DICT = {
1654
1654
  get(...args) {
1655
1655
  const { key, defValue } = parseArgs(args, ["key", "defValue"]);
1656
- const self2 = this;
1657
- if (key in self2) {
1658
- return self2[key];
1656
+ const self = this;
1657
+ if (key in self) {
1658
+ return self[key];
1659
1659
  } else if (defValue !== void 0) {
1660
1660
  return defValue;
1661
1661
  }
@@ -2225,6 +2225,13 @@ function cleanObject(obj) {
2225
2225
  }
2226
2226
  return result;
2227
2227
  }
2228
+ var extractIdFromDomain = (domain) => {
2229
+ if (!domain || !Array.isArray(domain)) return null;
2230
+ const idCond = domain.find(
2231
+ ([field, operator]) => field === "id" && operator === "="
2232
+ );
2233
+ return idCond ? Number(idCond[2]) : null;
2234
+ };
2228
2235
 
2229
2236
  // src/provider/react-query-provider.tsx
2230
2237
  var import_react2 = require("react");
@@ -3188,130 +3195,647 @@ var getASessionService = (env) => {
3188
3195
 
3189
3196
  // src/services/pos-service/add-entity.ts
3190
3197
  var import_react7 = require("react");
3191
- var addEntityService = (env) => {
3192
- const addEntity = (0, import_react7.useCallback)(
3193
- ({
3194
- model,
3195
- values,
3196
- xNode,
3197
- service,
3198
- isCreateEndpoint = false
3199
- }) => {
3200
- const jsonData = {
3201
- model,
3202
- values
3203
- };
3204
- return env?.requests.post(
3205
- isCreateEndpoint ? "/create" /* CREATE_PATH */ : "/call" /* CALL_PATH */,
3206
- jsonData,
3207
- {
3208
- headers: {
3209
- "Content-Type": "application/json",
3210
- ...xNode ? { "X-Node": xNode } : {}
3211
- }
3212
- },
3213
- service
3214
- );
3215
- },
3216
- [env]
3217
- );
3218
- return {
3219
- addEntity
3220
- };
3198
+
3199
+ // src/services/filesystem-service/file-service.ts
3200
+ var import_filesystem = require("@capacitor/filesystem");
3201
+ var fileService = {
3202
+ read: async (path) => {
3203
+ try {
3204
+ const res = await import_filesystem.Filesystem.readFile({
3205
+ path,
3206
+ directory: import_filesystem.Directory.Data,
3207
+ encoding: import_filesystem.Encoding.UTF8
3208
+ });
3209
+ if (typeof res.data === "string") return res.data;
3210
+ if (res.data instanceof Blob) return await res.data.text();
3211
+ return null;
3212
+ } catch {
3213
+ return null;
3214
+ }
3215
+ },
3216
+ write: async (path, data) => {
3217
+ await import_filesystem.Filesystem.writeFile({
3218
+ path,
3219
+ data,
3220
+ directory: import_filesystem.Directory.Data,
3221
+ encoding: import_filesystem.Encoding.UTF8,
3222
+ recursive: true
3223
+ });
3224
+ },
3225
+ writeAtomic: async (path, data) => {
3226
+ const tempPath = path + ".tmp";
3227
+ await import_filesystem.Filesystem.writeFile({
3228
+ path: tempPath,
3229
+ data,
3230
+ directory: import_filesystem.Directory.Data,
3231
+ encoding: import_filesystem.Encoding.UTF8,
3232
+ recursive: true
3233
+ });
3234
+ try {
3235
+ await import_filesystem.Filesystem.deleteFile({
3236
+ path,
3237
+ directory: import_filesystem.Directory.Data
3238
+ });
3239
+ } catch {
3240
+ }
3241
+ await import_filesystem.Filesystem.rename({
3242
+ from: tempPath,
3243
+ to: path,
3244
+ directory: import_filesystem.Directory.Data
3245
+ });
3246
+ },
3247
+ delete: async (path) => {
3248
+ try {
3249
+ await import_filesystem.Filesystem.deleteFile({
3250
+ path,
3251
+ directory: import_filesystem.Directory.Data
3252
+ });
3253
+ } catch {
3254
+ }
3255
+ },
3256
+ exists: async (path) => {
3257
+ try {
3258
+ await import_filesystem.Filesystem.stat({
3259
+ path,
3260
+ directory: import_filesystem.Directory.Data
3261
+ });
3262
+ return true;
3263
+ } catch {
3264
+ return false;
3265
+ }
3266
+ },
3267
+ mkdir: async (path) => {
3268
+ try {
3269
+ await import_filesystem.Filesystem.mkdir({
3270
+ path,
3271
+ directory: import_filesystem.Directory.Data,
3272
+ recursive: true
3273
+ });
3274
+ } catch (e) {
3275
+ if (!String(e?.message).includes("Exists")) {
3276
+ throw e;
3277
+ }
3278
+ }
3279
+ },
3280
+ list: async (path) => {
3281
+ return import_filesystem.Filesystem.readdir({
3282
+ path,
3283
+ directory: import_filesystem.Directory.Data
3284
+ });
3285
+ },
3286
+ getUri: async (path) => {
3287
+ return import_filesystem.Filesystem.getUri({
3288
+ path,
3289
+ directory: import_filesystem.Directory.Data
3290
+ });
3291
+ }
3221
3292
  };
3222
3293
 
3223
- // src/services/pos-service/change-order-preparation-state.ts
3224
- var import_react8 = require("react");
3225
- var changOrderPreparationStateService = (env) => {
3226
- const changeOrderPreparationState = (0, import_react8.useCallback)(
3227
- ({
3228
- orderId,
3229
- stageId,
3230
- preparationDisplayId,
3231
- xNode,
3232
- service
3233
- }) => {
3234
- const jsonData = {
3235
- model: "pos_preparation_display.order" /* POS_PREPARATION_ORDER */,
3236
- method: "change_order_stage" /* CHANGE_ORDER_STAGE */,
3237
- ids: orderId,
3238
- kwargs: {
3239
- stage_id: stageId,
3240
- preparation_display_id: preparationDisplayId
3294
+ // src/services/filesystem-service/json-worker.ts
3295
+ function createWorkerBlob() {
3296
+ const workerCode = `
3297
+ self.addEventListener("message", async (ev) => {
3298
+ const { id, cmd, payload } = ev.data;
3299
+ try {
3300
+ if (cmd === "parse") {
3301
+ const parsed = JSON.parse(payload);
3302
+ self.postMessage({ id, ok: true, result: parsed });
3303
+ } else if (cmd === "stringify") {
3304
+ const str = JSON.stringify(payload);
3305
+ self.postMessage({ id, ok: true, result: str });
3241
3306
  }
3242
- };
3243
- return env?.requests.post(
3244
- "/call" /* CALL_PATH */,
3245
- jsonData,
3246
- {
3247
- headers: {
3248
- "Content-Type": "application/json",
3249
- ...xNode ? { "X-Node": xNode } : {}
3250
- }
3251
- },
3252
- service
3253
- );
3254
- },
3255
- [env]
3256
- );
3257
- return {
3258
- changeOrderPreparationState
3259
- };
3260
- };
3307
+ } catch (err) {
3308
+ self.postMessage({ id, ok: false, error: err?.message || String(err) });
3309
+ }
3310
+ });
3311
+ `;
3312
+ const blob = new Blob([workerCode], { type: "application/javascript" });
3313
+ return URL.createObjectURL(blob);
3314
+ }
3315
+ function spawnParseWorker(raw) {
3316
+ return new Promise((resolve, reject) => {
3317
+ const workerUrl = createWorkerBlob();
3318
+ const worker = new Worker(workerUrl);
3319
+ const id = Math.random().toString(36).slice(2);
3320
+ worker.onmessage = (ev) => {
3321
+ const { ok, result, error } = ev.data;
3322
+ if (ok) {
3323
+ resolve(result);
3324
+ } else {
3325
+ reject(new Error(error));
3326
+ }
3327
+ URL.revokeObjectURL(workerUrl);
3328
+ worker.terminate();
3329
+ };
3330
+ worker.onerror = (err) => {
3331
+ reject(err);
3332
+ URL.revokeObjectURL(workerUrl);
3333
+ worker.terminate();
3334
+ };
3335
+ worker.postMessage({ id, cmd: "parse", payload: raw });
3336
+ });
3337
+ }
3338
+ function spawnStringifyWorker(obj) {
3339
+ return new Promise((resolve, reject) => {
3340
+ const workerUrl = createWorkerBlob();
3341
+ const worker = new Worker(workerUrl);
3342
+ worker.onmessage = (ev) => {
3343
+ const { ok, result, error } = ev.data;
3344
+ if (ok) resolve(result);
3345
+ else reject(new Error(error));
3346
+ URL.revokeObjectURL(workerUrl);
3347
+ worker.terminate();
3348
+ };
3349
+ worker.onerror = (err) => {
3350
+ reject(err);
3351
+ URL.revokeObjectURL(workerUrl);
3352
+ worker.terminate();
3353
+ };
3354
+ worker.postMessage({ cmd: "stringify", payload: obj });
3355
+ });
3356
+ }
3261
3357
 
3262
- // src/services/pos-service/check-payment.ts
3263
- var import_react9 = require("react");
3264
- var checkPaymentService = (env) => {
3265
- const checkPayment = (0, import_react9.useCallback)(
3266
- ({
3267
- model,
3268
- ids,
3269
- withContext,
3270
- xNode,
3271
- service
3272
- }) => {
3273
- const jsonData = {
3274
- model,
3275
- method: "check" /* CHECK */,
3276
- ids,
3277
- with_context: withContext
3278
- };
3279
- return env?.requests.post(
3280
- "/call" /* CALL_PATH */,
3281
- jsonData,
3282
- {
3283
- headers: {
3284
- "Content-Type": "application/json",
3285
- ...xNode ? { "X-Node": xNode } : {}
3286
- }
3287
- },
3288
- service
3289
- );
3290
- },
3291
- [env]
3292
- );
3293
- return {
3294
- checkPayment
3295
- };
3296
- };
3358
+ // src/services/filesystem-service/manifest.ts
3359
+ var MANIFEST_PATH = "pos/manifest.json";
3360
+ var MANIFEST_BAK_PATH = "pos/manifest.bak.json";
3361
+ async function writeManifest(manifest) {
3362
+ const oldRaw = await fileService.read(MANIFEST_PATH);
3363
+ if (oldRaw !== null) {
3364
+ await fileService.writeAtomic(MANIFEST_BAK_PATH, oldRaw);
3365
+ }
3366
+ await fileService.writeAtomic(MANIFEST_PATH, JSON.stringify(manifest));
3367
+ try {
3368
+ await fileService.delete(MANIFEST_BAK_PATH);
3369
+ } catch {
3370
+ }
3371
+ }
3297
3372
 
3298
- // src/services/pos-service/create-e-invoice.ts
3299
- var import_react10 = require("react");
3300
- var createEInvoiceService = (env) => {
3301
- const createEInvoice = (0, import_react10.useCallback)(
3302
- async ({
3303
- service,
3304
- xNode,
3305
- kwargs,
3306
- ids,
3307
- withContext
3308
- }) => {
3309
- const body = {
3310
- model: "pos.order" /* POS_ORDER */,
3311
- method: "create_e_invoice" /* CREATE_E_INVOICE */,
3312
- kwargs,
3313
- ids,
3314
- with_context: withContext
3373
+ // src/services/filesystem-service/import-snapshot.ts
3374
+ var DATA_DIR = "pos";
3375
+ var MODELS_DIR = `${DATA_DIR}/models`;
3376
+ var MODELS_META_DIR = `${DATA_DIR}/models_meta`;
3377
+ var importSnapshot = async ({ data, onProgress }) => {
3378
+ onProgress?.(1, "Parsing snapshot");
3379
+ const parsed = await spawnParseWorker(data);
3380
+ const modelNames = Object.keys(parsed);
3381
+ const total = modelNames.length;
3382
+ const manifest = { version: (/* @__PURE__ */ new Date()).toISOString(), models: {} };
3383
+ const TMP_PREFIX = `pos/data/tmp_import_${Date.now()}`;
3384
+ await fileService.writeAtomic(`${TMP_PREFIX}/.marker`, "1");
3385
+ let i = 0;
3386
+ for (const model of modelNames) {
3387
+ i++;
3388
+ onProgress?.(
3389
+ Math.round(i / total * 100),
3390
+ `Processing ${model} (${i}/${total})`
3391
+ );
3392
+ const block = parsed[model];
3393
+ const dataPart = block?.data ?? block ?? [];
3394
+ const fields = block?.fields ?? [];
3395
+ const relations = block?.relations ?? {};
3396
+ const serialized = await spawnStringifyWorker(dataPart);
3397
+ const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
3398
+ await fileService.writeAtomic(tmpModelPath, serialized);
3399
+ const meta = {
3400
+ fields,
3401
+ relations,
3402
+ count: Array.isArray(dataPart) ? dataPart.length : 0,
3403
+ writtenAt: (/* @__PURE__ */ new Date()).toISOString()
3404
+ };
3405
+ const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
3406
+ await fileService.writeAtomic(tmpMetaPath, JSON.stringify(meta));
3407
+ manifest.models[model] = {
3408
+ file: `${MODELS_DIR}/${encodeURIComponent(model)}.json`,
3409
+ metaFile: `${MODELS_META_DIR}/${encodeURIComponent(model)}.meta.json`,
3410
+ count: meta.count,
3411
+ updatedAt: meta.writtenAt
3412
+ };
3413
+ }
3414
+ onProgress?.(95, "Committing import (moving files)");
3415
+ for (const model of modelNames) {
3416
+ const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
3417
+ const finalModelPath = `${MODELS_DIR}/${encodeURIComponent(model)}.json`;
3418
+ const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
3419
+ const finalMetaPath = `${MODELS_META_DIR}/${encodeURIComponent(
3420
+ model
3421
+ )}.meta.json`;
3422
+ const tmpRaw = await fileService.read(tmpModelPath);
3423
+ if (tmpRaw !== null) await fileService.writeAtomic(finalModelPath, tmpRaw);
3424
+ const tmpMetaRaw = await fileService.read(tmpMetaPath);
3425
+ if (tmpMetaRaw !== null)
3426
+ await fileService.writeAtomic(finalMetaPath, tmpMetaRaw);
3427
+ onProgress?.(
3428
+ 95 + Math.round(
3429
+ (Object.keys(manifest.models).indexOf(model) + 1) / modelNames.length * 5
3430
+ ),
3431
+ `Committed ${model}`
3432
+ );
3433
+ }
3434
+ await writeManifest(manifest);
3435
+ try {
3436
+ for (const model of modelNames) {
3437
+ await fileService.delete(
3438
+ `${TMP_PREFIX}/${encodeURIComponent(model)}.json`
3439
+ );
3440
+ await fileService.delete(
3441
+ `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`
3442
+ );
3443
+ }
3444
+ await fileService.delete(`${TMP_PREFIX}/.marker`);
3445
+ } catch (e) {
3446
+ console.log("Failed to cleanup tmp import files:", e);
3447
+ }
3448
+ onProgress?.(100, "Import complete");
3449
+ return manifest;
3450
+ };
3451
+ var import_snapshot_default = importSnapshot;
3452
+
3453
+ // src/services/filesystem-service/memory-cache.ts
3454
+ var MemoryCache = class {
3455
+ map = /* @__PURE__ */ new Map();
3456
+ get(k) {
3457
+ const e = this.map.get(k);
3458
+ if (!e) return null;
3459
+ if (e.ttl && Date.now() - e.t > e.ttl) {
3460
+ this.map.delete(k);
3461
+ return null;
3462
+ }
3463
+ return e.value;
3464
+ }
3465
+ set(k, v, ttl = 5 * 60 * 1e3) {
3466
+ this.map.set(k, { value: v, t: Date.now(), ttl });
3467
+ }
3468
+ del(k) {
3469
+ this.map.delete(k);
3470
+ }
3471
+ clear() {
3472
+ this.map.clear();
3473
+ }
3474
+ };
3475
+ var memoryCache = new MemoryCache();
3476
+
3477
+ // src/services/filesystem-service/model-loader.ts
3478
+ var MODELS_DIR2 = "pos/models";
3479
+ var MODELS_META_DIR2 = "pos/models_meta";
3480
+ async function loadModelData(modelName, includeMeta = true) {
3481
+ const key = `model:${modelName}:meta:${includeMeta}`;
3482
+ const cached = memoryCache.get(key);
3483
+ if (cached) return cached;
3484
+ const dataPath = `${MODELS_DIR2}/${encodeURIComponent(modelName)}.json`;
3485
+ const metaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
3486
+ modelName
3487
+ )}.meta.json`;
3488
+ const rawData = await fileService.read(dataPath);
3489
+ if (!rawData) return null;
3490
+ const parsedData = await spawnParseWorker(rawData);
3491
+ const data = Array.isArray(parsedData) ? parsedData : [];
3492
+ if (!includeMeta) {
3493
+ const result2 = { data };
3494
+ memoryCache.set(key, result2, 1e3 * 60 * 60);
3495
+ return result2;
3496
+ }
3497
+ const rawMeta = await fileService.read(metaPath);
3498
+ let fields = [];
3499
+ let relations = {};
3500
+ if (rawMeta) {
3501
+ const parsedMeta = await spawnParseWorker(rawMeta);
3502
+ fields = parsedMeta?.fields ?? [];
3503
+ relations = parsedMeta?.relations ?? {};
3504
+ }
3505
+ const result = {
3506
+ data,
3507
+ fields,
3508
+ relations
3509
+ };
3510
+ memoryCache.set(key, result, 1e3 * 60 * 60);
3511
+ return result;
3512
+ }
3513
+ async function loadData(includeMeta = true) {
3514
+ try {
3515
+ const listResult = await fileService.list(MODELS_DIR2);
3516
+ if (!listResult || !Array.isArray(listResult.files)) {
3517
+ console.log("No models found");
3518
+ return {};
3519
+ }
3520
+ const result = {};
3521
+ for (const file of listResult.files) {
3522
+ if (file.type !== "file") continue;
3523
+ if (!file.name.endsWith(".json")) continue;
3524
+ const fileName = file.name;
3525
+ const modelName = fileName.replace(/\.json$/, "");
3526
+ const dataPath = `${MODELS_DIR2}/${fileName}`;
3527
+ const rawData = await fileService.read(dataPath);
3528
+ if (!rawData) continue;
3529
+ const parsedData = await spawnParseWorker(rawData);
3530
+ const data = Array.isArray(parsedData) ? parsedData : [];
3531
+ if (!includeMeta) {
3532
+ result[modelName] = { data };
3533
+ continue;
3534
+ }
3535
+ const metaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
3536
+ modelName
3537
+ )}.meta.json`;
3538
+ const rawMeta = await fileService.read(metaPath);
3539
+ let fields = [];
3540
+ let relations = {};
3541
+ if (rawMeta) {
3542
+ const parsedMeta = await spawnParseWorker(rawMeta);
3543
+ fields = parsedMeta?.fields ?? [];
3544
+ relations = parsedMeta?.relations ?? {};
3545
+ }
3546
+ result[modelName] = {
3547
+ data,
3548
+ fields,
3549
+ relations
3550
+ };
3551
+ }
3552
+ return result;
3553
+ } catch (error) {
3554
+ console.error("Error loading data:", error);
3555
+ throw error;
3556
+ }
3557
+ }
3558
+
3559
+ // src/services/filesystem-service/snapshot-helper.ts
3560
+ var createEmptySnapshot = () => {
3561
+ return {
3562
+ data: []
3563
+ };
3564
+ };
3565
+ var generateNextId = (existingIds, startFrom = 1) => {
3566
+ if (!existingIds || existingIds.length === 0) {
3567
+ return startFrom;
3568
+ }
3569
+ const maxId = Math.max(...existingIds, startFrom - 1);
3570
+ return maxId + 1;
3571
+ };
3572
+ var loadSnapshot = async ({
3573
+ modelName
3574
+ }) => {
3575
+ try {
3576
+ const snapshot = await loadModelData(modelName);
3577
+ if (!snapshot || typeof snapshot !== "object") {
3578
+ console.warn("invalid snapshot, creating new one");
3579
+ return createEmptySnapshot();
3580
+ }
3581
+ return {
3582
+ data: Array.isArray(snapshot.data) ? snapshot.data : []
3583
+ };
3584
+ } catch (error) {
3585
+ console.error("Failed to load snapshot:", error);
3586
+ return createEmptySnapshot();
3587
+ }
3588
+ };
3589
+ var getExistingIds = (snapshot) => {
3590
+ return snapshot.data.map((order) => order.id).filter((id) => typeof id === "number");
3591
+ };
3592
+ var saveSnapshot = async ({
3593
+ snapshot,
3594
+ modelName
3595
+ }) => {
3596
+ try {
3597
+ await import_snapshot_default({
3598
+ data: JSON.stringify({
3599
+ [modelName]: snapshot
3600
+ })
3601
+ });
3602
+ return true;
3603
+ } catch (error) {
3604
+ console.error("failed to save snapshot:", error);
3605
+ return false;
3606
+ }
3607
+ };
3608
+
3609
+ // src/services/filesystem-service/model-repository.ts
3610
+ var ModelRepository = class {
3611
+ getRecord = async ({
3612
+ id,
3613
+ modelName
3614
+ }) => {
3615
+ try {
3616
+ const snapshot = await loadSnapshot({
3617
+ modelName
3618
+ });
3619
+ return snapshot.data.find((record) => record.id === id) || null;
3620
+ } catch (error) {
3621
+ console.error("failed to get record:", error);
3622
+ return null;
3623
+ }
3624
+ };
3625
+ addRecord = async ({
3626
+ newRecord,
3627
+ modelName
3628
+ }) => {
3629
+ try {
3630
+ const snapshot = await loadSnapshot({
3631
+ modelName
3632
+ });
3633
+ const existingIds = getExistingIds(snapshot);
3634
+ const newId = generateNextId(existingIds, snapshot.data.length);
3635
+ snapshot.data.push({
3636
+ ...newRecord,
3637
+ id: newId
3638
+ });
3639
+ const saved = await saveSnapshot({
3640
+ snapshot,
3641
+ modelName
3642
+ });
3643
+ if (!saved) {
3644
+ console.error("failed to add new record");
3645
+ return [];
3646
+ }
3647
+ console.log(`\u2705 ${snapshot.data.length} records saved`);
3648
+ return snapshot.data;
3649
+ } catch (error) {
3650
+ console.error("failed to add new record:", error);
3651
+ return [];
3652
+ }
3653
+ };
3654
+ updateRecord = async ({
3655
+ id,
3656
+ update,
3657
+ modelName
3658
+ }) => {
3659
+ try {
3660
+ const snapshot = await loadSnapshot({
3661
+ modelName
3662
+ });
3663
+ const index = snapshot.data.findIndex((record) => record.id === id);
3664
+ if (index === -1) {
3665
+ console.error(`record with id ${id} not found`);
3666
+ return false;
3667
+ }
3668
+ snapshot.data[index] = {
3669
+ ...snapshot.data[index],
3670
+ ...update
3671
+ };
3672
+ return await saveSnapshot({
3673
+ snapshot,
3674
+ modelName
3675
+ });
3676
+ } catch (error) {
3677
+ console.error("error updating record:", error);
3678
+ return false;
3679
+ }
3680
+ };
3681
+ deleteRecord = async ({
3682
+ id,
3683
+ modelName
3684
+ }) => {
3685
+ try {
3686
+ const snapshot = await loadSnapshot({
3687
+ modelName
3688
+ });
3689
+ const before = snapshot.data.length;
3690
+ snapshot.data = snapshot.data.filter((record) => record.id !== id);
3691
+ if (snapshot.data.length === before) {
3692
+ console.error(`record with id ${id} not found`);
3693
+ return false;
3694
+ }
3695
+ return await saveSnapshot({
3696
+ snapshot,
3697
+ modelName
3698
+ });
3699
+ } catch (error) {
3700
+ console.error("error deleting record:", error);
3701
+ return false;
3702
+ }
3703
+ };
3704
+ };
3705
+
3706
+ // src/services/pos-service/add-entity.ts
3707
+ var addEntityService = (env) => {
3708
+ const isLocalMode = env?.isLocalMode;
3709
+ const repo = new ModelRepository();
3710
+ const addEntity = (0, import_react7.useCallback)(
3711
+ ({
3712
+ model,
3713
+ values,
3714
+ xNode,
3715
+ service,
3716
+ isCreateEndpoint = false
3717
+ }) => {
3718
+ if (isLocalMode) {
3719
+ return repo.addRecord({
3720
+ newRecord: values,
3721
+ modelName: model
3722
+ });
3723
+ }
3724
+ const jsonData = {
3725
+ model,
3726
+ values
3727
+ };
3728
+ return env?.requests.post(
3729
+ isCreateEndpoint ? "/create" /* CREATE_PATH */ : "/call" /* CALL_PATH */,
3730
+ jsonData,
3731
+ {
3732
+ headers: {
3733
+ "Content-Type": "application/json",
3734
+ ...xNode ? { "X-Node": xNode } : {}
3735
+ }
3736
+ },
3737
+ service
3738
+ );
3739
+ },
3740
+ [env, isLocalMode]
3741
+ );
3742
+ return {
3743
+ addEntity
3744
+ };
3745
+ };
3746
+
3747
+ // src/services/pos-service/change-order-preparation-state.ts
3748
+ var import_react8 = require("react");
3749
+ var changOrderPreparationStateService = (env) => {
3750
+ const changeOrderPreparationState = (0, import_react8.useCallback)(
3751
+ ({
3752
+ orderId,
3753
+ stageId,
3754
+ preparationDisplayId,
3755
+ xNode,
3756
+ service
3757
+ }) => {
3758
+ const jsonData = {
3759
+ model: "pos_preparation_display.order" /* POS_PREPARATION_ORDER */,
3760
+ method: "change_order_stage" /* CHANGE_ORDER_STAGE */,
3761
+ ids: orderId,
3762
+ kwargs: {
3763
+ stage_id: stageId,
3764
+ preparation_display_id: preparationDisplayId
3765
+ }
3766
+ };
3767
+ return env?.requests.post(
3768
+ "/call" /* CALL_PATH */,
3769
+ jsonData,
3770
+ {
3771
+ headers: {
3772
+ "Content-Type": "application/json",
3773
+ ...xNode ? { "X-Node": xNode } : {}
3774
+ }
3775
+ },
3776
+ service
3777
+ );
3778
+ },
3779
+ [env]
3780
+ );
3781
+ return {
3782
+ changeOrderPreparationState
3783
+ };
3784
+ };
3785
+
3786
+ // src/services/pos-service/check-payment.ts
3787
+ var import_react9 = require("react");
3788
+ var checkPaymentService = (env) => {
3789
+ const checkPayment = (0, import_react9.useCallback)(
3790
+ ({
3791
+ model,
3792
+ ids,
3793
+ withContext,
3794
+ xNode,
3795
+ service
3796
+ }) => {
3797
+ const jsonData = {
3798
+ model,
3799
+ method: "check" /* CHECK */,
3800
+ ids,
3801
+ with_context: withContext
3802
+ };
3803
+ return env?.requests.post(
3804
+ "/call" /* CALL_PATH */,
3805
+ jsonData,
3806
+ {
3807
+ headers: {
3808
+ "Content-Type": "application/json",
3809
+ ...xNode ? { "X-Node": xNode } : {}
3810
+ }
3811
+ },
3812
+ service
3813
+ );
3814
+ },
3815
+ [env]
3816
+ );
3817
+ return {
3818
+ checkPayment
3819
+ };
3820
+ };
3821
+
3822
+ // src/services/pos-service/create-e-invoice.ts
3823
+ var import_react10 = require("react");
3824
+ var createEInvoiceService = (env) => {
3825
+ const createEInvoice = (0, import_react10.useCallback)(
3826
+ async ({
3827
+ service,
3828
+ xNode,
3829
+ kwargs,
3830
+ ids,
3831
+ withContext
3832
+ }) => {
3833
+ const body = {
3834
+ model: "pos.order" /* POS_ORDER */,
3835
+ method: "create_e_invoice" /* CREATE_E_INVOICE */,
3836
+ kwargs,
3837
+ ids,
3838
+ with_context: withContext
3315
3839
  };
3316
3840
  return env?.requests?.post(
3317
3841
  `${"/call" /* CALL_PATH */}`,
@@ -3335,6 +3859,8 @@ var createEInvoiceService = (env) => {
3335
3859
  // src/services/pos-service/create-entity.ts
3336
3860
  var import_react11 = require("react");
3337
3861
  var createEntityService = (env) => {
3862
+ const isLocalMode = env?.isLocalMode;
3863
+ const repo = new ModelRepository();
3338
3864
  const createEntity = (0, import_react11.useCallback)(
3339
3865
  ({
3340
3866
  model,
@@ -3342,6 +3868,12 @@ var createEntityService = (env) => {
3342
3868
  xNode,
3343
3869
  service
3344
3870
  }) => {
3871
+ if (isLocalMode) {
3872
+ return repo.addRecord({
3873
+ newRecord: args,
3874
+ modelName: model
3875
+ });
3876
+ }
3345
3877
  const jsonData = {
3346
3878
  model,
3347
3879
  method: "create" /* CREATE */,
@@ -3359,7 +3891,7 @@ var createEntityService = (env) => {
3359
3891
  service
3360
3892
  );
3361
3893
  },
3362
- [env]
3894
+ [env, isLocalMode]
3363
3895
  );
3364
3896
  return {
3365
3897
  createEntity
@@ -3444,6 +3976,8 @@ var createSessionService = (env) => {
3444
3976
  // src/services/pos-service/delete-entity.ts
3445
3977
  var import_react14 = require("react");
3446
3978
  var deleteEntityService = (env) => {
3979
+ const isLocalMode = env?.isLocalMode;
3980
+ const repo = new ModelRepository();
3447
3981
  const deleteEntity = (0, import_react14.useCallback)(
3448
3982
  ({
3449
3983
  model,
@@ -3452,6 +3986,14 @@ var deleteEntityService = (env) => {
3452
3986
  service,
3453
3987
  method
3454
3988
  }) => {
3989
+ if (isLocalMode) {
3990
+ const id = ids[0];
3991
+ if (!id) return;
3992
+ return repo.deleteRecord({
3993
+ modelName: model,
3994
+ id
3995
+ });
3996
+ }
3455
3997
  const jsonData = {
3456
3998
  model,
3457
3999
  ids,
@@ -3469,7 +4011,7 @@ var deleteEntityService = (env) => {
3469
4011
  service
3470
4012
  );
3471
4013
  },
3472
- [env]
4014
+ [env, isLocalMode]
3473
4015
  );
3474
4016
  return {
3475
4017
  deleteEntity
@@ -3802,341 +4344,111 @@ var handleCloseSessionService = (env) => {
3802
4344
  const handleCloseSession = (0, import_react23.useCallback)(
3803
4345
  ({
3804
4346
  model,
3805
- ids,
3806
- xNode,
3807
- service,
3808
- method
3809
- }) => {
3810
- const jsonData = {
3811
- model,
3812
- ids,
3813
- method
3814
- };
3815
- return env?.requests.post(
3816
- "/call" /* CALL_PATH */,
3817
- jsonData,
3818
- {
3819
- headers: {
3820
- "Content-Type": "application/json",
3821
- ...xNode ? { "X-Node": xNode } : {}
3822
- }
3823
- },
3824
- service
3825
- );
3826
- },
3827
- [env]
3828
- );
3829
- return {
3830
- handleCloseSession
3831
- };
3832
- };
3833
-
3834
- // src/services/pos-service/handle-closing-detail-session.ts
3835
- var import_react24 = require("react");
3836
- var handleClosingDetailSessionService = (env) => {
3837
- const handleClosingDetailSession = (0, import_react24.useCallback)(
3838
- ({
3839
- model,
3840
- ids,
3841
- method,
3842
- xNode,
3843
- service,
3844
- kwargs
3845
- }) => {
3846
- const jsonData = {
3847
- model,
3848
- ids,
3849
- method,
3850
- kwargs
3851
- };
3852
- return env?.requests.post(
3853
- "/call" /* CALL_PATH */,
3854
- jsonData,
3855
- {
3856
- headers: {
3857
- "Content-Type": "application/json",
3858
- ...xNode ? { "X-Node": xNode } : {}
3859
- }
3860
- },
3861
- service
3862
- );
3863
- },
3864
- [env]
3865
- );
3866
- return {
3867
- handleClosingDetailSession
3868
- };
3869
- };
3870
-
3871
- // src/services/pos-service/handle-closing-session.ts
3872
- var import_react25 = require("react");
3873
- var handleClosingSessionService = (env) => {
3874
- const handleClosingSession = (0, import_react25.useCallback)(
3875
- ({
3876
- model,
3877
- method,
3878
- ids,
3879
- kwargs,
3880
- xNode,
3881
- service
3882
- }) => {
3883
- const jsonData = {
3884
- model,
3885
- method,
3886
- ids,
3887
- kwargs
3888
- };
3889
- return env?.requests.post(
3890
- "/call" /* CALL_PATH */,
3891
- jsonData,
3892
- {
3893
- headers: {
3894
- "Content-Type": "application/json",
3895
- ...xNode ? { "X-Node": xNode } : {}
3896
- }
3897
- },
3898
- service
3899
- );
3900
- },
3901
- [env]
3902
- );
3903
- return {
3904
- handleClosingSession
3905
- };
3906
- };
3907
-
3908
- // src/services/pos-service/load-data-pos-session.ts
3909
- var import_react26 = require("react");
3910
-
3911
- // src/services/filesystem-service/file-service.ts
3912
- var import_filesystem = require("@capacitor/filesystem");
3913
- var fileService = {
3914
- async read(path) {
3915
- try {
3916
- const res = await import_filesystem.Filesystem.readFile({
3917
- path,
3918
- directory: import_filesystem.Directory.Data,
3919
- encoding: import_filesystem.Encoding.UTF8
3920
- });
3921
- if (typeof res.data === "string") return res.data;
3922
- if (res.data instanceof Blob) return await res.data.text();
3923
- return null;
3924
- } catch {
3925
- return null;
3926
- }
3927
- },
3928
- async write(path, data) {
3929
- await import_filesystem.Filesystem.writeFile({
3930
- path,
3931
- data,
3932
- directory: import_filesystem.Directory.Data,
3933
- encoding: import_filesystem.Encoding.UTF8,
3934
- recursive: true
3935
- });
3936
- },
3937
- async writeAtomic(path, data) {
3938
- const tempPath = path + ".tmp";
3939
- await import_filesystem.Filesystem.writeFile({
3940
- path: tempPath,
3941
- data,
3942
- directory: import_filesystem.Directory.Data,
3943
- encoding: import_filesystem.Encoding.UTF8,
3944
- recursive: true
3945
- });
3946
- try {
3947
- await import_filesystem.Filesystem.deleteFile({
3948
- path,
3949
- directory: import_filesystem.Directory.Data
3950
- });
3951
- } catch {
3952
- }
3953
- await import_filesystem.Filesystem.rename({
3954
- from: tempPath,
3955
- to: path,
3956
- directory: import_filesystem.Directory.Data
3957
- });
3958
- },
3959
- async delete(path) {
3960
- try {
3961
- await import_filesystem.Filesystem.deleteFile({
3962
- path,
3963
- directory: import_filesystem.Directory.Data
3964
- });
3965
- } catch {
3966
- }
3967
- },
3968
- async exists(path) {
3969
- try {
3970
- await import_filesystem.Filesystem.stat({
3971
- path,
3972
- directory: import_filesystem.Directory.Data
3973
- });
3974
- return true;
3975
- } catch {
3976
- return false;
3977
- }
3978
- },
3979
- async mkdir(path) {
3980
- try {
3981
- await import_filesystem.Filesystem.mkdir({
3982
- path,
3983
- directory: import_filesystem.Directory.Data,
3984
- recursive: true
3985
- });
3986
- } catch (e) {
3987
- if (!String(e?.message).includes("Exists")) {
3988
- throw e;
3989
- }
3990
- }
3991
- },
3992
- async list(path) {
3993
- return import_filesystem.Filesystem.readdir({
3994
- path,
3995
- directory: import_filesystem.Directory.Data
3996
- });
3997
- },
3998
- async getUri(path) {
3999
- return import_filesystem.Filesystem.getUri({
4000
- path,
4001
- directory: import_filesystem.Directory.Data
4002
- });
4003
- }
4004
- };
4005
-
4006
- // src/services/filesystem-service/json-worker.ts
4007
- var import_meta = {};
4008
- self.addEventListener("message", async (ev) => {
4009
- const { id, cmd, payload } = ev.data;
4010
- try {
4011
- if (cmd === "parse") {
4012
- const parsed = JSON.parse(payload);
4013
- self.postMessage({ id, ok: true, result: parsed });
4014
- } else if (cmd === "stringify") {
4015
- const str = JSON.stringify(payload);
4016
- self.postMessage({ id, ok: true, result: str });
4017
- }
4018
- } catch (err) {
4019
- self.postMessage({ id, ok: false, error: err?.message || String(err) });
4020
- }
4021
- });
4022
- function spawnParseWorker(raw) {
4023
- return new Promise((resolve, reject) => {
4024
- const worker = new Worker(new URL("./json-worker.ts", import_meta.url), {
4025
- type: "module"
4026
- });
4027
- const id = Math.random().toString(36).slice(2);
4028
- worker.onmessage = (ev) => {
4029
- const { ok, result, error } = ev.data;
4030
- if (ok) {
4031
- resolve(result);
4032
- } else {
4033
- reject(new Error(error));
4034
- }
4035
- worker.terminate();
4036
- };
4037
- worker.postMessage({ id, cmd: "parse", payload: raw });
4038
- });
4039
- }
4040
- function spawnStringifyWorker(obj) {
4041
- return new Promise((resolve, reject) => {
4042
- const worker = new Worker(new URL("./json-worker.ts", import_meta.url), {
4043
- type: "module"
4044
- });
4045
- worker.onmessage = (ev) => {
4046
- const { ok, result, error } = ev.data;
4047
- if (ok) resolve(result);
4048
- else reject(new Error(error));
4049
- worker.terminate();
4050
- };
4051
- worker.postMessage({ cmd: "stringify", payload: obj });
4052
- });
4053
- }
4347
+ ids,
4348
+ xNode,
4349
+ service,
4350
+ method
4351
+ }) => {
4352
+ const jsonData = {
4353
+ model,
4354
+ ids,
4355
+ method
4356
+ };
4357
+ return env?.requests.post(
4358
+ "/call" /* CALL_PATH */,
4359
+ jsonData,
4360
+ {
4361
+ headers: {
4362
+ "Content-Type": "application/json",
4363
+ ...xNode ? { "X-Node": xNode } : {}
4364
+ }
4365
+ },
4366
+ service
4367
+ );
4368
+ },
4369
+ [env]
4370
+ );
4371
+ return {
4372
+ handleCloseSession
4373
+ };
4374
+ };
4054
4375
 
4055
- // src/services/filesystem-service/memory-cache.ts
4056
- var MemoryCache = class {
4057
- map = /* @__PURE__ */ new Map();
4058
- get(k) {
4059
- const e = this.map.get(k);
4060
- if (!e) return null;
4061
- if (e.ttl && Date.now() - e.t > e.ttl) {
4062
- this.map.delete(k);
4063
- return null;
4064
- }
4065
- return e.value;
4066
- }
4067
- set(k, v, ttl = 5 * 60 * 1e3) {
4068
- this.map.set(k, { value: v, t: Date.now(), ttl });
4069
- }
4070
- del(k) {
4071
- this.map.delete(k);
4072
- }
4073
- clear() {
4074
- this.map.clear();
4075
- }
4376
+ // src/services/pos-service/handle-closing-detail-session.ts
4377
+ var import_react24 = require("react");
4378
+ var handleClosingDetailSessionService = (env) => {
4379
+ const handleClosingDetailSession = (0, import_react24.useCallback)(
4380
+ ({
4381
+ model,
4382
+ ids,
4383
+ method,
4384
+ xNode,
4385
+ service,
4386
+ kwargs
4387
+ }) => {
4388
+ const jsonData = {
4389
+ model,
4390
+ ids,
4391
+ method,
4392
+ kwargs
4393
+ };
4394
+ return env?.requests.post(
4395
+ "/call" /* CALL_PATH */,
4396
+ jsonData,
4397
+ {
4398
+ headers: {
4399
+ "Content-Type": "application/json",
4400
+ ...xNode ? { "X-Node": xNode } : {}
4401
+ }
4402
+ },
4403
+ service
4404
+ );
4405
+ },
4406
+ [env]
4407
+ );
4408
+ return {
4409
+ handleClosingDetailSession
4410
+ };
4076
4411
  };
4077
- var memoryCache = new MemoryCache();
4078
4412
 
4079
- // src/services/filesystem-service/model-loader.ts
4080
- var MODELS_DIR = "pos/models";
4081
- var MODELS_META_DIR = "pos/models_meta";
4082
- async function loadData(includeMeta = true) {
4083
- console.log("\u{1F50D} loadData START");
4084
- try {
4085
- const listResult = await fileService.list(MODELS_DIR);
4086
- console.log("\u{1F4CB} listResult:", listResult);
4087
- if (!listResult || !Array.isArray(listResult.files)) {
4088
- console.log("\u26A0\uFE0F No files found");
4089
- return {};
4090
- }
4091
- const result = {};
4092
- for (const file of listResult.files) {
4093
- console.log("\u{1F4C4} Processing file:", file.name);
4094
- if (file.type !== "file") continue;
4095
- if (!file.name.endsWith(".json")) continue;
4096
- const fileName = file.name;
4097
- const modelName = fileName.replace(/\.json$/, "");
4098
- const dataPath = `${MODELS_DIR}/${fileName}`;
4099
- console.log("\u{1F4C2} Reading data from:", dataPath);
4100
- const rawData = await fileService.read(dataPath);
4101
- console.log("\u2705 Data read, length:", rawData?.length);
4102
- if (!rawData) continue;
4103
- console.log("\u{1F504} Parsing data...");
4104
- const parsedData = await spawnParseWorker(rawData);
4105
- console.log("\u2705 Data parsed");
4106
- const data = Array.isArray(parsedData) ? parsedData : [];
4107
- if (!includeMeta) {
4108
- result[modelName] = { data };
4109
- continue;
4110
- }
4111
- const metaPath = `${MODELS_META_DIR}/${encodeURIComponent(
4112
- modelName
4113
- )}.meta.json`;
4114
- console.log("\u{1F4C2} Reading meta from:", metaPath);
4115
- const rawMeta = await fileService.read(metaPath);
4116
- let fields = [];
4117
- let relations = {};
4118
- if (rawMeta) {
4119
- console.log("\u{1F504} Parsing meta...");
4120
- const parsedMeta = await spawnParseWorker(rawMeta);
4121
- fields = parsedMeta?.fields ?? [];
4122
- relations = parsedMeta?.relations ?? {};
4123
- console.log("\u2705 Meta parsed");
4124
- }
4125
- result[modelName] = {
4126
- data,
4127
- fields,
4128
- relations
4413
+ // src/services/pos-service/handle-closing-session.ts
4414
+ var import_react25 = require("react");
4415
+ var handleClosingSessionService = (env) => {
4416
+ const handleClosingSession = (0, import_react25.useCallback)(
4417
+ ({
4418
+ model,
4419
+ method,
4420
+ ids,
4421
+ kwargs,
4422
+ xNode,
4423
+ service
4424
+ }) => {
4425
+ const jsonData = {
4426
+ model,
4427
+ method,
4428
+ ids,
4429
+ kwargs
4129
4430
  };
4130
- }
4131
- console.log("\u2705 loadData COMPLETE:", Object.keys(result));
4132
- return result;
4133
- } catch (error) {
4134
- console.error("\u274C loadData ERROR:", error);
4135
- throw error;
4136
- }
4137
- }
4431
+ return env?.requests.post(
4432
+ "/call" /* CALL_PATH */,
4433
+ jsonData,
4434
+ {
4435
+ headers: {
4436
+ "Content-Type": "application/json",
4437
+ ...xNode ? { "X-Node": xNode } : {}
4438
+ }
4439
+ },
4440
+ service
4441
+ );
4442
+ },
4443
+ [env]
4444
+ );
4445
+ return {
4446
+ handleClosingSession
4447
+ };
4448
+ };
4138
4449
 
4139
4450
  // src/services/pos-service/load-data-pos-session.ts
4451
+ var import_react26 = require("react");
4140
4452
  var loadDataPosSessionService = (env) => {
4141
4453
  const isLocalMode = env?.isLocalMode;
4142
4454
  const loadDataPosSession = (0, import_react26.useCallback)(
@@ -4149,10 +4461,8 @@ var loadDataPosSessionService = (env) => {
4149
4461
  modelsToLoad = [],
4150
4462
  searchParams
4151
4463
  }) => {
4152
- console.log("isLocalMode", isLocalMode);
4153
4464
  if (isLocalMode) {
4154
4465
  const data = await loadData();
4155
- console.log("\u2705 loadData resolved:", data);
4156
4466
  return data;
4157
4467
  }
4158
4468
  const jsonData = {
@@ -4406,6 +4716,8 @@ var updateClosedSessionService = (env) => {
4406
4716
  // src/services/pos-service/update-entity.ts
4407
4717
  var import_react33 = require("react");
4408
4718
  var updateEntityService = (env) => {
4719
+ const isLocalMode = env?.isLocalMode;
4720
+ const repo = new ModelRepository();
4409
4721
  const updateEntity = (0, import_react33.useCallback)(
4410
4722
  ({
4411
4723
  model,
@@ -4415,6 +4727,15 @@ var updateEntityService = (env) => {
4415
4727
  xNode,
4416
4728
  service
4417
4729
  }) => {
4730
+ if (isLocalMode) {
4731
+ const id = extractIdFromDomain(domain);
4732
+ if (!id) return;
4733
+ return repo.updateRecord({
4734
+ update: values,
4735
+ modelName: model,
4736
+ id
4737
+ });
4738
+ }
4418
4739
  const jsonData = {
4419
4740
  model,
4420
4741
  domain,
@@ -4432,7 +4753,7 @@ var updateEntityService = (env) => {
4432
4753
  service
4433
4754
  );
4434
4755
  },
4435
- [env]
4756
+ [env, isLocalMode]
4436
4757
  );
4437
4758
  return {
4438
4759
  updateEntity
@@ -6764,101 +7085,6 @@ function useDashboardService() {
6764
7085
  return { readGroup, getDataChart };
6765
7086
  }
6766
7087
 
6767
- // src/services/filesystem-service/manifest.ts
6768
- var MANIFEST_PATH = "pos/manifest.json";
6769
- var MANIFEST_BAK_PATH = "pos/manifest.bak.json";
6770
- async function writeManifest(manifest) {
6771
- const oldRaw = await fileService.read(MANIFEST_PATH);
6772
- if (oldRaw !== null) {
6773
- await fileService.writeAtomic(MANIFEST_BAK_PATH, oldRaw);
6774
- }
6775
- await fileService.writeAtomic(MANIFEST_PATH, JSON.stringify(manifest));
6776
- try {
6777
- await fileService.delete(MANIFEST_BAK_PATH);
6778
- } catch {
6779
- }
6780
- }
6781
-
6782
- // src/services/filesystem-service/import-snapshot.ts
6783
- var DATA_DIR = "pos";
6784
- var MODELS_DIR2 = `${DATA_DIR}/models`;
6785
- var MODELS_META_DIR2 = `${DATA_DIR}/models_meta`;
6786
- var importSnapshot = async ({ data, onProgress }) => {
6787
- onProgress?.(1, "Parsing snapshot");
6788
- const parsed = await spawnParseWorker(data);
6789
- const modelNames = Object.keys(parsed);
6790
- const total = modelNames.length;
6791
- const manifest = { version: (/* @__PURE__ */ new Date()).toISOString(), models: {} };
6792
- const TMP_PREFIX = `pos/data/tmp_import_${Date.now()}`;
6793
- await fileService.writeAtomic(`${TMP_PREFIX}/.marker`, "1");
6794
- let i = 0;
6795
- for (const model of modelNames) {
6796
- i++;
6797
- onProgress?.(
6798
- Math.round(i / total * 100),
6799
- `Processing ${model} (${i}/${total})`
6800
- );
6801
- const block = parsed[model];
6802
- const dataPart = block?.data ?? block ?? [];
6803
- const fields = block?.fields ?? [];
6804
- const relations = block?.relations ?? {};
6805
- const serialized = await spawnStringifyWorker(dataPart);
6806
- const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
6807
- await fileService.writeAtomic(tmpModelPath, serialized);
6808
- const meta = {
6809
- fields,
6810
- relations,
6811
- count: Array.isArray(dataPart) ? dataPart.length : 0,
6812
- writtenAt: (/* @__PURE__ */ new Date()).toISOString()
6813
- };
6814
- const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
6815
- await fileService.writeAtomic(tmpMetaPath, JSON.stringify(meta));
6816
- manifest.models[model] = {
6817
- file: `${MODELS_DIR2}/${encodeURIComponent(model)}.json`,
6818
- metaFile: `${MODELS_META_DIR2}/${encodeURIComponent(model)}.meta.json`,
6819
- count: meta.count,
6820
- updatedAt: meta.writtenAt
6821
- };
6822
- }
6823
- onProgress?.(95, "Committing import (moving files)");
6824
- for (const model of modelNames) {
6825
- const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
6826
- const finalModelPath = `${MODELS_DIR2}/${encodeURIComponent(model)}.json`;
6827
- const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
6828
- const finalMetaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
6829
- model
6830
- )}.meta.json`;
6831
- const tmpRaw = await fileService.read(tmpModelPath);
6832
- if (tmpRaw !== null) await fileService.writeAtomic(finalModelPath, tmpRaw);
6833
- const tmpMetaRaw = await fileService.read(tmpMetaPath);
6834
- if (tmpMetaRaw !== null)
6835
- await fileService.writeAtomic(finalMetaPath, tmpMetaRaw);
6836
- onProgress?.(
6837
- 95 + Math.round(
6838
- (Object.keys(manifest.models).indexOf(model) + 1) / modelNames.length * 5
6839
- ),
6840
- `Committed ${model}`
6841
- );
6842
- }
6843
- await writeManifest(manifest);
6844
- try {
6845
- for (const model of modelNames) {
6846
- await fileService.delete(
6847
- `${TMP_PREFIX}/${encodeURIComponent(model)}.json`
6848
- );
6849
- await fileService.delete(
6850
- `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`
6851
- );
6852
- }
6853
- await fileService.delete(`${TMP_PREFIX}/.marker`);
6854
- } catch (e) {
6855
- console.log("Failed to cleanup tmp import files:", e);
6856
- }
6857
- onProgress?.(100, "Import complete");
6858
- return manifest;
6859
- };
6860
- var import_snapshot_default = importSnapshot;
6861
-
6862
7088
  // src/services/filesystem-service/init-snapshot.ts
6863
7089
  var isSnapshotReady = async () => {
6864
7090
  try {