@fctc/interface-logic 4.3.2 → 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/provider.js CHANGED
@@ -2922,6 +2922,13 @@ function cleanObject(obj) {
2922
2922
  }
2923
2923
  return result;
2924
2924
  }
2925
+ var extractIdFromDomain = (domain) => {
2926
+ if (!domain || !Array.isArray(domain)) return null;
2927
+ const idCond = domain.find(
2928
+ ([field, operator]) => field === "id" && operator === "="
2929
+ );
2930
+ return idCond ? Number(idCond[2]) : null;
2931
+ };
2925
2932
 
2926
2933
  // src/utils/storage/local-storage.ts
2927
2934
  var localStorageUtils = () => {
@@ -5208,116 +5215,633 @@ var getASessionService = (env) => {
5208
5215
 
5209
5216
  // src/services/pos-service/add-entity.ts
5210
5217
  var import_react14 = require("react");
5211
- var addEntityService = (env) => {
5212
- const addEntity = (0, import_react14.useCallback)(
5213
- ({
5214
- model,
5215
- values,
5216
- xNode,
5217
- service,
5218
- isCreateEndpoint = false
5219
- }) => {
5220
- const jsonData = {
5221
- model,
5222
- values
5223
- };
5224
- return env?.requests.post(
5225
- isCreateEndpoint ? "/create" /* CREATE_PATH */ : "/call" /* CALL_PATH */,
5226
- jsonData,
5227
- {
5228
- headers: {
5229
- "Content-Type": "application/json",
5230
- ...xNode ? { "X-Node": xNode } : {}
5231
- }
5232
- },
5233
- service
5234
- );
5235
- },
5236
- [env]
5237
- );
5238
- return {
5239
- addEntity
5240
- };
5241
- };
5242
5218
 
5243
- // src/services/pos-service/change-order-preparation-state.ts
5244
- var import_react15 = require("react");
5245
- var changOrderPreparationStateService = (env) => {
5246
- const changeOrderPreparationState = (0, import_react15.useCallback)(
5247
- ({
5248
- orderId,
5249
- stageId,
5250
- preparationDisplayId,
5251
- xNode,
5252
- service
5253
- }) => {
5254
- const jsonData = {
5255
- model: "pos_preparation_display.order" /* POS_PREPARATION_ORDER */,
5256
- method: "change_order_stage" /* CHANGE_ORDER_STAGE */,
5257
- ids: orderId,
5258
- kwargs: {
5259
- stage_id: stageId,
5260
- preparation_display_id: preparationDisplayId
5261
- }
5262
- };
5263
- return env?.requests.post(
5264
- "/call" /* CALL_PATH */,
5265
- jsonData,
5266
- {
5267
- headers: {
5268
- "Content-Type": "application/json",
5269
- ...xNode ? { "X-Node": xNode } : {}
5270
- }
5271
- },
5272
- service
5273
- );
5274
- },
5275
- [env]
5276
- );
5277
- return {
5278
- changeOrderPreparationState
5279
- };
5219
+ // src/services/filesystem-service/file-service.ts
5220
+ var import_filesystem = require("@capacitor/filesystem");
5221
+ var fileService = {
5222
+ read: async (path) => {
5223
+ try {
5224
+ const res = await import_filesystem.Filesystem.readFile({
5225
+ path,
5226
+ directory: import_filesystem.Directory.Data,
5227
+ encoding: import_filesystem.Encoding.UTF8
5228
+ });
5229
+ if (typeof res.data === "string") return res.data;
5230
+ if (res.data instanceof Blob) return await res.data.text();
5231
+ return null;
5232
+ } catch {
5233
+ return null;
5234
+ }
5235
+ },
5236
+ write: async (path, data) => {
5237
+ await import_filesystem.Filesystem.writeFile({
5238
+ path,
5239
+ data,
5240
+ directory: import_filesystem.Directory.Data,
5241
+ encoding: import_filesystem.Encoding.UTF8,
5242
+ recursive: true
5243
+ });
5244
+ },
5245
+ writeAtomic: async (path, data) => {
5246
+ const tempPath = path + ".tmp";
5247
+ await import_filesystem.Filesystem.writeFile({
5248
+ path: tempPath,
5249
+ data,
5250
+ directory: import_filesystem.Directory.Data,
5251
+ encoding: import_filesystem.Encoding.UTF8,
5252
+ recursive: true
5253
+ });
5254
+ try {
5255
+ await import_filesystem.Filesystem.deleteFile({
5256
+ path,
5257
+ directory: import_filesystem.Directory.Data
5258
+ });
5259
+ } catch {
5260
+ }
5261
+ await import_filesystem.Filesystem.rename({
5262
+ from: tempPath,
5263
+ to: path,
5264
+ directory: import_filesystem.Directory.Data
5265
+ });
5266
+ },
5267
+ delete: async (path) => {
5268
+ try {
5269
+ await import_filesystem.Filesystem.deleteFile({
5270
+ path,
5271
+ directory: import_filesystem.Directory.Data
5272
+ });
5273
+ } catch {
5274
+ }
5275
+ },
5276
+ exists: async (path) => {
5277
+ try {
5278
+ await import_filesystem.Filesystem.stat({
5279
+ path,
5280
+ directory: import_filesystem.Directory.Data
5281
+ });
5282
+ return true;
5283
+ } catch {
5284
+ return false;
5285
+ }
5286
+ },
5287
+ mkdir: async (path) => {
5288
+ try {
5289
+ await import_filesystem.Filesystem.mkdir({
5290
+ path,
5291
+ directory: import_filesystem.Directory.Data,
5292
+ recursive: true
5293
+ });
5294
+ } catch (e) {
5295
+ if (!String(e?.message).includes("Exists")) {
5296
+ throw e;
5297
+ }
5298
+ }
5299
+ },
5300
+ list: async (path) => {
5301
+ return import_filesystem.Filesystem.readdir({
5302
+ path,
5303
+ directory: import_filesystem.Directory.Data
5304
+ });
5305
+ },
5306
+ getUri: async (path) => {
5307
+ return import_filesystem.Filesystem.getUri({
5308
+ path,
5309
+ directory: import_filesystem.Directory.Data
5310
+ });
5311
+ }
5280
5312
  };
5281
5313
 
5282
- // src/services/pos-service/check-payment.ts
5283
- var import_react16 = require("react");
5284
- var checkPaymentService = (env) => {
5285
- const checkPayment = (0, import_react16.useCallback)(
5286
- ({
5287
- model,
5288
- ids,
5289
- withContext,
5290
- xNode,
5291
- service
5292
- }) => {
5293
- const jsonData = {
5294
- model,
5295
- method: "check" /* CHECK */,
5296
- ids,
5297
- with_context: withContext
5298
- };
5299
- return env?.requests.post(
5300
- "/call" /* CALL_PATH */,
5301
- jsonData,
5302
- {
5303
- headers: {
5304
- "Content-Type": "application/json",
5305
- ...xNode ? { "X-Node": xNode } : {}
5306
- }
5307
- },
5308
- service
5309
- );
5310
- },
5311
- [env]
5312
- );
5313
- return {
5314
- checkPayment
5315
- };
5316
- };
5314
+ // src/services/filesystem-service/json-worker.ts
5315
+ function createWorkerBlob() {
5316
+ const workerCode = `
5317
+ self.addEventListener("message", async (ev) => {
5318
+ const { id, cmd, payload } = ev.data;
5319
+ try {
5320
+ if (cmd === "parse") {
5321
+ const parsed = JSON.parse(payload);
5322
+ self.postMessage({ id, ok: true, result: parsed });
5323
+ } else if (cmd === "stringify") {
5324
+ const str = JSON.stringify(payload);
5325
+ self.postMessage({ id, ok: true, result: str });
5326
+ }
5327
+ } catch (err) {
5328
+ self.postMessage({ id, ok: false, error: err?.message || String(err) });
5329
+ }
5330
+ });
5331
+ `;
5332
+ const blob = new Blob([workerCode], { type: "application/javascript" });
5333
+ return URL.createObjectURL(blob);
5334
+ }
5335
+ function spawnParseWorker(raw) {
5336
+ return new Promise((resolve, reject) => {
5337
+ const workerUrl = createWorkerBlob();
5338
+ const worker = new Worker(workerUrl);
5339
+ const id = Math.random().toString(36).slice(2);
5340
+ worker.onmessage = (ev) => {
5341
+ const { ok, result, error } = ev.data;
5342
+ if (ok) {
5343
+ resolve(result);
5344
+ } else {
5345
+ reject(new Error(error));
5346
+ }
5347
+ URL.revokeObjectURL(workerUrl);
5348
+ worker.terminate();
5349
+ };
5350
+ worker.onerror = (err) => {
5351
+ reject(err);
5352
+ URL.revokeObjectURL(workerUrl);
5353
+ worker.terminate();
5354
+ };
5355
+ worker.postMessage({ id, cmd: "parse", payload: raw });
5356
+ });
5357
+ }
5358
+ function spawnStringifyWorker(obj) {
5359
+ return new Promise((resolve, reject) => {
5360
+ const workerUrl = createWorkerBlob();
5361
+ const worker = new Worker(workerUrl);
5362
+ worker.onmessage = (ev) => {
5363
+ const { ok, result, error } = ev.data;
5364
+ if (ok) resolve(result);
5365
+ else reject(new Error(error));
5366
+ URL.revokeObjectURL(workerUrl);
5367
+ worker.terminate();
5368
+ };
5369
+ worker.onerror = (err) => {
5370
+ reject(err);
5371
+ URL.revokeObjectURL(workerUrl);
5372
+ worker.terminate();
5373
+ };
5374
+ worker.postMessage({ cmd: "stringify", payload: obj });
5375
+ });
5376
+ }
5317
5377
 
5318
- // src/services/pos-service/create-e-invoice.ts
5319
- var import_react17 = require("react");
5320
- var createEInvoiceService = (env) => {
5378
+ // src/services/filesystem-service/manifest.ts
5379
+ var MANIFEST_PATH = "pos/manifest.json";
5380
+ var MANIFEST_BAK_PATH = "pos/manifest.bak.json";
5381
+ async function writeManifest(manifest) {
5382
+ const oldRaw = await fileService.read(MANIFEST_PATH);
5383
+ if (oldRaw !== null) {
5384
+ await fileService.writeAtomic(MANIFEST_BAK_PATH, oldRaw);
5385
+ }
5386
+ await fileService.writeAtomic(MANIFEST_PATH, JSON.stringify(manifest));
5387
+ try {
5388
+ await fileService.delete(MANIFEST_BAK_PATH);
5389
+ } catch {
5390
+ }
5391
+ }
5392
+
5393
+ // src/services/filesystem-service/import-snapshot.ts
5394
+ var DATA_DIR = "pos";
5395
+ var MODELS_DIR = `${DATA_DIR}/models`;
5396
+ var MODELS_META_DIR = `${DATA_DIR}/models_meta`;
5397
+ var importSnapshot = async ({ data, onProgress }) => {
5398
+ onProgress?.(1, "Parsing snapshot");
5399
+ const parsed = await spawnParseWorker(data);
5400
+ const modelNames = Object.keys(parsed);
5401
+ const total = modelNames.length;
5402
+ const manifest = { version: (/* @__PURE__ */ new Date()).toISOString(), models: {} };
5403
+ const TMP_PREFIX = `pos/data/tmp_import_${Date.now()}`;
5404
+ await fileService.writeAtomic(`${TMP_PREFIX}/.marker`, "1");
5405
+ let i = 0;
5406
+ for (const model of modelNames) {
5407
+ i++;
5408
+ onProgress?.(
5409
+ Math.round(i / total * 100),
5410
+ `Processing ${model} (${i}/${total})`
5411
+ );
5412
+ const block = parsed[model];
5413
+ const dataPart = block?.data ?? block ?? [];
5414
+ const fields = block?.fields ?? [];
5415
+ const relations = block?.relations ?? {};
5416
+ const serialized = await spawnStringifyWorker(dataPart);
5417
+ const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
5418
+ await fileService.writeAtomic(tmpModelPath, serialized);
5419
+ const meta = {
5420
+ fields,
5421
+ relations,
5422
+ count: Array.isArray(dataPart) ? dataPart.length : 0,
5423
+ writtenAt: (/* @__PURE__ */ new Date()).toISOString()
5424
+ };
5425
+ const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
5426
+ await fileService.writeAtomic(tmpMetaPath, JSON.stringify(meta));
5427
+ manifest.models[model] = {
5428
+ file: `${MODELS_DIR}/${encodeURIComponent(model)}.json`,
5429
+ metaFile: `${MODELS_META_DIR}/${encodeURIComponent(model)}.meta.json`,
5430
+ count: meta.count,
5431
+ updatedAt: meta.writtenAt
5432
+ };
5433
+ }
5434
+ onProgress?.(95, "Committing import (moving files)");
5435
+ for (const model of modelNames) {
5436
+ const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
5437
+ const finalModelPath = `${MODELS_DIR}/${encodeURIComponent(model)}.json`;
5438
+ const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
5439
+ const finalMetaPath = `${MODELS_META_DIR}/${encodeURIComponent(
5440
+ model
5441
+ )}.meta.json`;
5442
+ const tmpRaw = await fileService.read(tmpModelPath);
5443
+ if (tmpRaw !== null) await fileService.writeAtomic(finalModelPath, tmpRaw);
5444
+ const tmpMetaRaw = await fileService.read(tmpMetaPath);
5445
+ if (tmpMetaRaw !== null)
5446
+ await fileService.writeAtomic(finalMetaPath, tmpMetaRaw);
5447
+ onProgress?.(
5448
+ 95 + Math.round(
5449
+ (Object.keys(manifest.models).indexOf(model) + 1) / modelNames.length * 5
5450
+ ),
5451
+ `Committed ${model}`
5452
+ );
5453
+ }
5454
+ await writeManifest(manifest);
5455
+ try {
5456
+ for (const model of modelNames) {
5457
+ await fileService.delete(
5458
+ `${TMP_PREFIX}/${encodeURIComponent(model)}.json`
5459
+ );
5460
+ await fileService.delete(
5461
+ `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`
5462
+ );
5463
+ }
5464
+ await fileService.delete(`${TMP_PREFIX}/.marker`);
5465
+ } catch (e) {
5466
+ console.log("Failed to cleanup tmp import files:", e);
5467
+ }
5468
+ onProgress?.(100, "Import complete");
5469
+ return manifest;
5470
+ };
5471
+ var import_snapshot_default = importSnapshot;
5472
+
5473
+ // src/services/filesystem-service/memory-cache.ts
5474
+ var MemoryCache = class {
5475
+ map = /* @__PURE__ */ new Map();
5476
+ get(k) {
5477
+ const e = this.map.get(k);
5478
+ if (!e) return null;
5479
+ if (e.ttl && Date.now() - e.t > e.ttl) {
5480
+ this.map.delete(k);
5481
+ return null;
5482
+ }
5483
+ return e.value;
5484
+ }
5485
+ set(k, v, ttl = 5 * 60 * 1e3) {
5486
+ this.map.set(k, { value: v, t: Date.now(), ttl });
5487
+ }
5488
+ del(k) {
5489
+ this.map.delete(k);
5490
+ }
5491
+ clear() {
5492
+ this.map.clear();
5493
+ }
5494
+ };
5495
+ var memoryCache = new MemoryCache();
5496
+
5497
+ // src/services/filesystem-service/model-loader.ts
5498
+ var MODELS_DIR2 = "pos/models";
5499
+ var MODELS_META_DIR2 = "pos/models_meta";
5500
+ async function loadModelData(modelName, includeMeta = true) {
5501
+ const key = `model:${modelName}:meta:${includeMeta}`;
5502
+ const cached = memoryCache.get(key);
5503
+ if (cached) return cached;
5504
+ const dataPath = `${MODELS_DIR2}/${encodeURIComponent(modelName)}.json`;
5505
+ const metaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
5506
+ modelName
5507
+ )}.meta.json`;
5508
+ const rawData = await fileService.read(dataPath);
5509
+ if (!rawData) return null;
5510
+ const parsedData = await spawnParseWorker(rawData);
5511
+ const data = Array.isArray(parsedData) ? parsedData : [];
5512
+ if (!includeMeta) {
5513
+ const result2 = { data };
5514
+ memoryCache.set(key, result2, 1e3 * 60 * 60);
5515
+ return result2;
5516
+ }
5517
+ const rawMeta = await fileService.read(metaPath);
5518
+ let fields = [];
5519
+ let relations = {};
5520
+ if (rawMeta) {
5521
+ const parsedMeta = await spawnParseWorker(rawMeta);
5522
+ fields = parsedMeta?.fields ?? [];
5523
+ relations = parsedMeta?.relations ?? {};
5524
+ }
5525
+ const result = {
5526
+ data,
5527
+ fields,
5528
+ relations
5529
+ };
5530
+ memoryCache.set(key, result, 1e3 * 60 * 60);
5531
+ return result;
5532
+ }
5533
+ async function loadData(includeMeta = true) {
5534
+ try {
5535
+ const listResult = await fileService.list(MODELS_DIR2);
5536
+ if (!listResult || !Array.isArray(listResult.files)) {
5537
+ console.log("No models found");
5538
+ return {};
5539
+ }
5540
+ const result = {};
5541
+ for (const file of listResult.files) {
5542
+ if (file.type !== "file") continue;
5543
+ if (!file.name.endsWith(".json")) continue;
5544
+ const fileName = file.name;
5545
+ const modelName = fileName.replace(/\.json$/, "");
5546
+ const dataPath = `${MODELS_DIR2}/${fileName}`;
5547
+ const rawData = await fileService.read(dataPath);
5548
+ if (!rawData) continue;
5549
+ const parsedData = await spawnParseWorker(rawData);
5550
+ const data = Array.isArray(parsedData) ? parsedData : [];
5551
+ if (!includeMeta) {
5552
+ result[modelName] = { data };
5553
+ continue;
5554
+ }
5555
+ const metaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
5556
+ modelName
5557
+ )}.meta.json`;
5558
+ const rawMeta = await fileService.read(metaPath);
5559
+ let fields = [];
5560
+ let relations = {};
5561
+ if (rawMeta) {
5562
+ const parsedMeta = await spawnParseWorker(rawMeta);
5563
+ fields = parsedMeta?.fields ?? [];
5564
+ relations = parsedMeta?.relations ?? {};
5565
+ }
5566
+ result[modelName] = {
5567
+ data,
5568
+ fields,
5569
+ relations
5570
+ };
5571
+ }
5572
+ return result;
5573
+ } catch (error) {
5574
+ console.error("Error loading data:", error);
5575
+ throw error;
5576
+ }
5577
+ }
5578
+
5579
+ // src/services/filesystem-service/snapshot-helper.ts
5580
+ var createEmptySnapshot = () => {
5581
+ return {
5582
+ data: []
5583
+ };
5584
+ };
5585
+ var generateNextId = (existingIds, startFrom = 1) => {
5586
+ if (!existingIds || existingIds.length === 0) {
5587
+ return startFrom;
5588
+ }
5589
+ const maxId = Math.max(...existingIds, startFrom - 1);
5590
+ return maxId + 1;
5591
+ };
5592
+ var loadSnapshot = async ({
5593
+ modelName
5594
+ }) => {
5595
+ try {
5596
+ const snapshot = await loadModelData(modelName);
5597
+ if (!snapshot || typeof snapshot !== "object") {
5598
+ console.warn("invalid snapshot, creating new one");
5599
+ return createEmptySnapshot();
5600
+ }
5601
+ return {
5602
+ data: Array.isArray(snapshot.data) ? snapshot.data : []
5603
+ };
5604
+ } catch (error) {
5605
+ console.error("Failed to load snapshot:", error);
5606
+ return createEmptySnapshot();
5607
+ }
5608
+ };
5609
+ var getExistingIds = (snapshot) => {
5610
+ return snapshot.data.map((order) => order.id).filter((id) => typeof id === "number");
5611
+ };
5612
+ var saveSnapshot = async ({
5613
+ snapshot,
5614
+ modelName
5615
+ }) => {
5616
+ try {
5617
+ await import_snapshot_default({
5618
+ data: JSON.stringify({
5619
+ [modelName]: snapshot
5620
+ })
5621
+ });
5622
+ return true;
5623
+ } catch (error) {
5624
+ console.error("failed to save snapshot:", error);
5625
+ return false;
5626
+ }
5627
+ };
5628
+
5629
+ // src/services/filesystem-service/model-repository.ts
5630
+ var ModelRepository = class {
5631
+ getRecord = async ({
5632
+ id,
5633
+ modelName
5634
+ }) => {
5635
+ try {
5636
+ const snapshot = await loadSnapshot({
5637
+ modelName
5638
+ });
5639
+ return snapshot.data.find((record) => record.id === id) || null;
5640
+ } catch (error) {
5641
+ console.error("failed to get record:", error);
5642
+ return null;
5643
+ }
5644
+ };
5645
+ addRecord = async ({
5646
+ newRecord,
5647
+ modelName
5648
+ }) => {
5649
+ try {
5650
+ const snapshot = await loadSnapshot({
5651
+ modelName
5652
+ });
5653
+ const existingIds = getExistingIds(snapshot);
5654
+ const newId = generateNextId(existingIds, snapshot.data.length);
5655
+ snapshot.data.push({
5656
+ ...newRecord,
5657
+ id: newId
5658
+ });
5659
+ const saved = await saveSnapshot({
5660
+ snapshot,
5661
+ modelName
5662
+ });
5663
+ if (!saved) {
5664
+ console.error("failed to add new record");
5665
+ return [];
5666
+ }
5667
+ console.log(`\u2705 ${snapshot.data.length} records saved`);
5668
+ return snapshot.data;
5669
+ } catch (error) {
5670
+ console.error("failed to add new record:", error);
5671
+ return [];
5672
+ }
5673
+ };
5674
+ updateRecord = async ({
5675
+ id,
5676
+ update,
5677
+ modelName
5678
+ }) => {
5679
+ try {
5680
+ const snapshot = await loadSnapshot({
5681
+ modelName
5682
+ });
5683
+ const index = snapshot.data.findIndex((record) => record.id === id);
5684
+ if (index === -1) {
5685
+ console.error(`record with id ${id} not found`);
5686
+ return false;
5687
+ }
5688
+ snapshot.data[index] = {
5689
+ ...snapshot.data[index],
5690
+ ...update
5691
+ };
5692
+ return await saveSnapshot({
5693
+ snapshot,
5694
+ modelName
5695
+ });
5696
+ } catch (error) {
5697
+ console.error("error updating record:", error);
5698
+ return false;
5699
+ }
5700
+ };
5701
+ deleteRecord = async ({
5702
+ id,
5703
+ modelName
5704
+ }) => {
5705
+ try {
5706
+ const snapshot = await loadSnapshot({
5707
+ modelName
5708
+ });
5709
+ const before = snapshot.data.length;
5710
+ snapshot.data = snapshot.data.filter((record) => record.id !== id);
5711
+ if (snapshot.data.length === before) {
5712
+ console.error(`record with id ${id} not found`);
5713
+ return false;
5714
+ }
5715
+ return await saveSnapshot({
5716
+ snapshot,
5717
+ modelName
5718
+ });
5719
+ } catch (error) {
5720
+ console.error("error deleting record:", error);
5721
+ return false;
5722
+ }
5723
+ };
5724
+ };
5725
+
5726
+ // src/services/pos-service/add-entity.ts
5727
+ var addEntityService = (env) => {
5728
+ const isLocalMode = env?.isLocalMode;
5729
+ const repo = new ModelRepository();
5730
+ const addEntity = (0, import_react14.useCallback)(
5731
+ ({
5732
+ model,
5733
+ values,
5734
+ xNode,
5735
+ service,
5736
+ isCreateEndpoint = false
5737
+ }) => {
5738
+ if (isLocalMode) {
5739
+ return repo.addRecord({
5740
+ newRecord: values,
5741
+ modelName: model
5742
+ });
5743
+ }
5744
+ const jsonData = {
5745
+ model,
5746
+ values
5747
+ };
5748
+ return env?.requests.post(
5749
+ isCreateEndpoint ? "/create" /* CREATE_PATH */ : "/call" /* CALL_PATH */,
5750
+ jsonData,
5751
+ {
5752
+ headers: {
5753
+ "Content-Type": "application/json",
5754
+ ...xNode ? { "X-Node": xNode } : {}
5755
+ }
5756
+ },
5757
+ service
5758
+ );
5759
+ },
5760
+ [env, isLocalMode]
5761
+ );
5762
+ return {
5763
+ addEntity
5764
+ };
5765
+ };
5766
+
5767
+ // src/services/pos-service/change-order-preparation-state.ts
5768
+ var import_react15 = require("react");
5769
+ var changOrderPreparationStateService = (env) => {
5770
+ const changeOrderPreparationState = (0, import_react15.useCallback)(
5771
+ ({
5772
+ orderId,
5773
+ stageId,
5774
+ preparationDisplayId,
5775
+ xNode,
5776
+ service
5777
+ }) => {
5778
+ const jsonData = {
5779
+ model: "pos_preparation_display.order" /* POS_PREPARATION_ORDER */,
5780
+ method: "change_order_stage" /* CHANGE_ORDER_STAGE */,
5781
+ ids: orderId,
5782
+ kwargs: {
5783
+ stage_id: stageId,
5784
+ preparation_display_id: preparationDisplayId
5785
+ }
5786
+ };
5787
+ return env?.requests.post(
5788
+ "/call" /* CALL_PATH */,
5789
+ jsonData,
5790
+ {
5791
+ headers: {
5792
+ "Content-Type": "application/json",
5793
+ ...xNode ? { "X-Node": xNode } : {}
5794
+ }
5795
+ },
5796
+ service
5797
+ );
5798
+ },
5799
+ [env]
5800
+ );
5801
+ return {
5802
+ changeOrderPreparationState
5803
+ };
5804
+ };
5805
+
5806
+ // src/services/pos-service/check-payment.ts
5807
+ var import_react16 = require("react");
5808
+ var checkPaymentService = (env) => {
5809
+ const checkPayment = (0, import_react16.useCallback)(
5810
+ ({
5811
+ model,
5812
+ ids,
5813
+ withContext,
5814
+ xNode,
5815
+ service
5816
+ }) => {
5817
+ const jsonData = {
5818
+ model,
5819
+ method: "check" /* CHECK */,
5820
+ ids,
5821
+ with_context: withContext
5822
+ };
5823
+ return env?.requests.post(
5824
+ "/call" /* CALL_PATH */,
5825
+ jsonData,
5826
+ {
5827
+ headers: {
5828
+ "Content-Type": "application/json",
5829
+ ...xNode ? { "X-Node": xNode } : {}
5830
+ }
5831
+ },
5832
+ service
5833
+ );
5834
+ },
5835
+ [env]
5836
+ );
5837
+ return {
5838
+ checkPayment
5839
+ };
5840
+ };
5841
+
5842
+ // src/services/pos-service/create-e-invoice.ts
5843
+ var import_react17 = require("react");
5844
+ var createEInvoiceService = (env) => {
5321
5845
  const createEInvoice = (0, import_react17.useCallback)(
5322
5846
  async ({
5323
5847
  service,
@@ -5355,6 +5879,8 @@ var createEInvoiceService = (env) => {
5355
5879
  // src/services/pos-service/create-entity.ts
5356
5880
  var import_react18 = require("react");
5357
5881
  var createEntityService = (env) => {
5882
+ const isLocalMode = env?.isLocalMode;
5883
+ const repo = new ModelRepository();
5358
5884
  const createEntity = (0, import_react18.useCallback)(
5359
5885
  ({
5360
5886
  model,
@@ -5362,6 +5888,12 @@ var createEntityService = (env) => {
5362
5888
  xNode,
5363
5889
  service
5364
5890
  }) => {
5891
+ if (isLocalMode) {
5892
+ return repo.addRecord({
5893
+ newRecord: args,
5894
+ modelName: model
5895
+ });
5896
+ }
5365
5897
  const jsonData = {
5366
5898
  model,
5367
5899
  method: "create" /* CREATE */,
@@ -5379,7 +5911,7 @@ var createEntityService = (env) => {
5379
5911
  service
5380
5912
  );
5381
5913
  },
5382
- [env]
5914
+ [env, isLocalMode]
5383
5915
  );
5384
5916
  return {
5385
5917
  createEntity
@@ -5464,6 +5996,8 @@ var createSessionService = (env) => {
5464
5996
  // src/services/pos-service/delete-entity.ts
5465
5997
  var import_react21 = require("react");
5466
5998
  var deleteEntityService = (env) => {
5999
+ const isLocalMode = env?.isLocalMode;
6000
+ const repo = new ModelRepository();
5467
6001
  const deleteEntity = (0, import_react21.useCallback)(
5468
6002
  ({
5469
6003
  model,
@@ -5472,6 +6006,14 @@ var deleteEntityService = (env) => {
5472
6006
  service,
5473
6007
  method
5474
6008
  }) => {
6009
+ if (isLocalMode) {
6010
+ const id = ids[0];
6011
+ if (!id) return;
6012
+ return repo.deleteRecord({
6013
+ modelName: model,
6014
+ id
6015
+ });
6016
+ }
5475
6017
  const jsonData = {
5476
6018
  model,
5477
6019
  ids,
@@ -5489,7 +6031,7 @@ var deleteEntityService = (env) => {
5489
6031
  service
5490
6032
  );
5491
6033
  },
5492
- [env]
6034
+ [env, isLocalMode]
5493
6035
  );
5494
6036
  return {
5495
6037
  deleteEntity
@@ -5822,356 +6364,111 @@ var handleCloseSessionService = (env) => {
5822
6364
  const handleCloseSession = (0, import_react30.useCallback)(
5823
6365
  ({
5824
6366
  model,
5825
- ids,
5826
- xNode,
5827
- service,
5828
- method
5829
- }) => {
5830
- const jsonData = {
5831
- model,
5832
- ids,
5833
- method
5834
- };
5835
- return env?.requests.post(
5836
- "/call" /* CALL_PATH */,
5837
- jsonData,
5838
- {
5839
- headers: {
5840
- "Content-Type": "application/json",
5841
- ...xNode ? { "X-Node": xNode } : {}
5842
- }
5843
- },
5844
- service
5845
- );
5846
- },
5847
- [env]
5848
- );
5849
- return {
5850
- handleCloseSession
5851
- };
5852
- };
5853
-
5854
- // src/services/pos-service/handle-closing-detail-session.ts
5855
- var import_react31 = require("react");
5856
- var handleClosingDetailSessionService = (env) => {
5857
- const handleClosingDetailSession = (0, import_react31.useCallback)(
5858
- ({
5859
- model,
5860
- ids,
5861
- method,
5862
- xNode,
5863
- service,
5864
- kwargs
5865
- }) => {
5866
- const jsonData = {
5867
- model,
5868
- ids,
5869
- method,
5870
- kwargs
5871
- };
5872
- return env?.requests.post(
5873
- "/call" /* CALL_PATH */,
5874
- jsonData,
5875
- {
5876
- headers: {
5877
- "Content-Type": "application/json",
5878
- ...xNode ? { "X-Node": xNode } : {}
5879
- }
5880
- },
5881
- service
5882
- );
5883
- },
5884
- [env]
5885
- );
5886
- return {
5887
- handleClosingDetailSession
5888
- };
5889
- };
5890
-
5891
- // src/services/pos-service/handle-closing-session.ts
5892
- var import_react32 = require("react");
5893
- var handleClosingSessionService = (env) => {
5894
- const handleClosingSession = (0, import_react32.useCallback)(
5895
- ({
5896
- model,
5897
- method,
5898
- ids,
5899
- kwargs,
5900
- xNode,
5901
- service
5902
- }) => {
5903
- const jsonData = {
5904
- model,
5905
- method,
5906
- ids,
5907
- kwargs
5908
- };
5909
- return env?.requests.post(
5910
- "/call" /* CALL_PATH */,
5911
- jsonData,
5912
- {
5913
- headers: {
5914
- "Content-Type": "application/json",
5915
- ...xNode ? { "X-Node": xNode } : {}
5916
- }
5917
- },
5918
- service
5919
- );
5920
- },
5921
- [env]
5922
- );
5923
- return {
5924
- handleClosingSession
5925
- };
5926
- };
5927
-
5928
- // src/services/pos-service/load-data-pos-session.ts
5929
- var import_react33 = require("react");
5930
-
5931
- // src/services/filesystem-service/file-service.ts
5932
- var import_filesystem = require("@capacitor/filesystem");
5933
- var fileService = {
5934
- async read(path) {
5935
- try {
5936
- const res = await import_filesystem.Filesystem.readFile({
5937
- path,
5938
- directory: import_filesystem.Directory.Data,
5939
- encoding: import_filesystem.Encoding.UTF8
5940
- });
5941
- if (typeof res.data === "string") return res.data;
5942
- if (res.data instanceof Blob) return await res.data.text();
5943
- return null;
5944
- } catch {
5945
- return null;
5946
- }
5947
- },
5948
- async write(path, data) {
5949
- await import_filesystem.Filesystem.writeFile({
5950
- path,
5951
- data,
5952
- directory: import_filesystem.Directory.Data,
5953
- encoding: import_filesystem.Encoding.UTF8,
5954
- recursive: true
5955
- });
5956
- },
5957
- async writeAtomic(path, data) {
5958
- const tempPath = path + ".tmp";
5959
- await import_filesystem.Filesystem.writeFile({
5960
- path: tempPath,
5961
- data,
5962
- directory: import_filesystem.Directory.Data,
5963
- encoding: import_filesystem.Encoding.UTF8,
5964
- recursive: true
5965
- });
5966
- try {
5967
- await import_filesystem.Filesystem.deleteFile({
5968
- path,
5969
- directory: import_filesystem.Directory.Data
5970
- });
5971
- } catch {
5972
- }
5973
- await import_filesystem.Filesystem.rename({
5974
- from: tempPath,
5975
- to: path,
5976
- directory: import_filesystem.Directory.Data
5977
- });
5978
- },
5979
- async delete(path) {
5980
- try {
5981
- await import_filesystem.Filesystem.deleteFile({
5982
- path,
5983
- directory: import_filesystem.Directory.Data
5984
- });
5985
- } catch {
5986
- }
5987
- },
5988
- async exists(path) {
5989
- try {
5990
- await import_filesystem.Filesystem.stat({
5991
- path,
5992
- directory: import_filesystem.Directory.Data
5993
- });
5994
- return true;
5995
- } catch {
5996
- return false;
5997
- }
5998
- },
5999
- async mkdir(path) {
6000
- try {
6001
- await import_filesystem.Filesystem.mkdir({
6002
- path,
6003
- directory: import_filesystem.Directory.Data,
6004
- recursive: true
6005
- });
6006
- } catch (e) {
6007
- if (!String(e?.message).includes("Exists")) {
6008
- throw e;
6009
- }
6010
- }
6011
- },
6012
- async list(path) {
6013
- return import_filesystem.Filesystem.readdir({
6014
- path,
6015
- directory: import_filesystem.Directory.Data
6016
- });
6017
- },
6018
- async getUri(path) {
6019
- return import_filesystem.Filesystem.getUri({
6020
- path,
6021
- directory: import_filesystem.Directory.Data
6022
- });
6023
- }
6024
- };
6025
-
6026
- // src/services/filesystem-service/json-worker.ts
6027
- function createWorkerBlob() {
6028
- const workerCode = `
6029
- self.addEventListener("message", async (ev) => {
6030
- const { id, cmd, payload } = ev.data;
6031
- try {
6032
- if (cmd === "parse") {
6033
- const parsed = JSON.parse(payload);
6034
- self.postMessage({ id, ok: true, result: parsed });
6035
- } else if (cmd === "stringify") {
6036
- const str = JSON.stringify(payload);
6037
- self.postMessage({ id, ok: true, result: str });
6038
- }
6039
- } catch (err) {
6040
- self.postMessage({ id, ok: false, error: err?.message || String(err) });
6041
- }
6042
- });
6043
- `;
6044
- const blob = new Blob([workerCode], { type: "application/javascript" });
6045
- return URL.createObjectURL(blob);
6046
- }
6047
- function spawnParseWorker(raw) {
6048
- return new Promise((resolve, reject) => {
6049
- const workerUrl = createWorkerBlob();
6050
- const worker = new Worker(workerUrl);
6051
- const id = Math.random().toString(36).slice(2);
6052
- worker.onmessage = (ev) => {
6053
- const { ok, result, error } = ev.data;
6054
- if (ok) {
6055
- resolve(result);
6056
- } else {
6057
- reject(new Error(error));
6058
- }
6059
- URL.revokeObjectURL(workerUrl);
6060
- worker.terminate();
6061
- };
6062
- worker.onerror = (err) => {
6063
- reject(err);
6064
- URL.revokeObjectURL(workerUrl);
6065
- worker.terminate();
6066
- };
6067
- worker.postMessage({ id, cmd: "parse", payload: raw });
6068
- });
6069
- }
6070
- function spawnStringifyWorker(obj) {
6071
- return new Promise((resolve, reject) => {
6072
- const workerUrl = createWorkerBlob();
6073
- const worker = new Worker(workerUrl);
6074
- worker.onmessage = (ev) => {
6075
- const { ok, result, error } = ev.data;
6076
- if (ok) resolve(result);
6077
- else reject(new Error(error));
6078
- URL.revokeObjectURL(workerUrl);
6079
- worker.terminate();
6080
- };
6081
- worker.onerror = (err) => {
6082
- reject(err);
6083
- URL.revokeObjectURL(workerUrl);
6084
- worker.terminate();
6085
- };
6086
- worker.postMessage({ cmd: "stringify", payload: obj });
6087
- });
6088
- }
6367
+ ids,
6368
+ xNode,
6369
+ service,
6370
+ method
6371
+ }) => {
6372
+ const jsonData = {
6373
+ model,
6374
+ ids,
6375
+ method
6376
+ };
6377
+ return env?.requests.post(
6378
+ "/call" /* CALL_PATH */,
6379
+ jsonData,
6380
+ {
6381
+ headers: {
6382
+ "Content-Type": "application/json",
6383
+ ...xNode ? { "X-Node": xNode } : {}
6384
+ }
6385
+ },
6386
+ service
6387
+ );
6388
+ },
6389
+ [env]
6390
+ );
6391
+ return {
6392
+ handleCloseSession
6393
+ };
6394
+ };
6089
6395
 
6090
- // src/services/filesystem-service/memory-cache.ts
6091
- var MemoryCache = class {
6092
- map = /* @__PURE__ */ new Map();
6093
- get(k) {
6094
- const e = this.map.get(k);
6095
- if (!e) return null;
6096
- if (e.ttl && Date.now() - e.t > e.ttl) {
6097
- this.map.delete(k);
6098
- return null;
6099
- }
6100
- return e.value;
6101
- }
6102
- set(k, v, ttl = 5 * 60 * 1e3) {
6103
- this.map.set(k, { value: v, t: Date.now(), ttl });
6104
- }
6105
- del(k) {
6106
- this.map.delete(k);
6107
- }
6108
- clear() {
6109
- this.map.clear();
6110
- }
6396
+ // src/services/pos-service/handle-closing-detail-session.ts
6397
+ var import_react31 = require("react");
6398
+ var handleClosingDetailSessionService = (env) => {
6399
+ const handleClosingDetailSession = (0, import_react31.useCallback)(
6400
+ ({
6401
+ model,
6402
+ ids,
6403
+ method,
6404
+ xNode,
6405
+ service,
6406
+ kwargs
6407
+ }) => {
6408
+ const jsonData = {
6409
+ model,
6410
+ ids,
6411
+ method,
6412
+ kwargs
6413
+ };
6414
+ return env?.requests.post(
6415
+ "/call" /* CALL_PATH */,
6416
+ jsonData,
6417
+ {
6418
+ headers: {
6419
+ "Content-Type": "application/json",
6420
+ ...xNode ? { "X-Node": xNode } : {}
6421
+ }
6422
+ },
6423
+ service
6424
+ );
6425
+ },
6426
+ [env]
6427
+ );
6428
+ return {
6429
+ handleClosingDetailSession
6430
+ };
6111
6431
  };
6112
- var memoryCache = new MemoryCache();
6113
6432
 
6114
- // src/services/filesystem-service/model-loader.ts
6115
- var MODELS_DIR = "pos/models";
6116
- var MODELS_META_DIR = "pos/models_meta";
6117
- async function loadData(includeMeta = true) {
6118
- console.log("\u{1F50D} loadData START");
6119
- try {
6120
- const listResult = await fileService.list(MODELS_DIR);
6121
- console.log("\u{1F4CB} listResult:", listResult);
6122
- if (!listResult || !Array.isArray(listResult.files)) {
6123
- console.log("\u26A0\uFE0F No files found");
6124
- return {};
6125
- }
6126
- const result = {};
6127
- for (const file of listResult.files) {
6128
- console.log("\u{1F4C4} Processing file:", file.name);
6129
- if (file.type !== "file") continue;
6130
- if (!file.name.endsWith(".json")) continue;
6131
- const fileName = file.name;
6132
- const modelName = fileName.replace(/\.json$/, "");
6133
- const dataPath = `${MODELS_DIR}/${fileName}`;
6134
- console.log("\u{1F4C2} Reading data from:", dataPath);
6135
- const rawData = await fileService.read(dataPath);
6136
- console.log("\u2705 Data read, length:", rawData?.length);
6137
- if (!rawData) continue;
6138
- console.log("\u{1F504} Parsing data...");
6139
- const parsedData = await spawnParseWorker(rawData);
6140
- console.log("\u2705 Data parsed");
6141
- const data = Array.isArray(parsedData) ? parsedData : [];
6142
- if (!includeMeta) {
6143
- result[modelName] = { data };
6144
- continue;
6145
- }
6146
- const metaPath = `${MODELS_META_DIR}/${encodeURIComponent(
6147
- modelName
6148
- )}.meta.json`;
6149
- console.log("\u{1F4C2} Reading meta from:", metaPath);
6150
- const rawMeta = await fileService.read(metaPath);
6151
- let fields = [];
6152
- let relations = {};
6153
- if (rawMeta) {
6154
- console.log("\u{1F504} Parsing meta...");
6155
- const parsedMeta = await spawnParseWorker(rawMeta);
6156
- fields = parsedMeta?.fields ?? [];
6157
- relations = parsedMeta?.relations ?? {};
6158
- console.log("\u2705 Meta parsed");
6159
- }
6160
- result[modelName] = {
6161
- data,
6162
- fields,
6163
- relations
6433
+ // src/services/pos-service/handle-closing-session.ts
6434
+ var import_react32 = require("react");
6435
+ var handleClosingSessionService = (env) => {
6436
+ const handleClosingSession = (0, import_react32.useCallback)(
6437
+ ({
6438
+ model,
6439
+ method,
6440
+ ids,
6441
+ kwargs,
6442
+ xNode,
6443
+ service
6444
+ }) => {
6445
+ const jsonData = {
6446
+ model,
6447
+ method,
6448
+ ids,
6449
+ kwargs
6164
6450
  };
6165
- }
6166
- console.log("\u2705 loadData COMPLETE:", Object.keys(result));
6167
- return result;
6168
- } catch (error) {
6169
- console.error("\u274C loadData ERROR:", error);
6170
- throw error;
6171
- }
6172
- }
6451
+ return env?.requests.post(
6452
+ "/call" /* CALL_PATH */,
6453
+ jsonData,
6454
+ {
6455
+ headers: {
6456
+ "Content-Type": "application/json",
6457
+ ...xNode ? { "X-Node": xNode } : {}
6458
+ }
6459
+ },
6460
+ service
6461
+ );
6462
+ },
6463
+ [env]
6464
+ );
6465
+ return {
6466
+ handleClosingSession
6467
+ };
6468
+ };
6173
6469
 
6174
6470
  // src/services/pos-service/load-data-pos-session.ts
6471
+ var import_react33 = require("react");
6175
6472
  var loadDataPosSessionService = (env) => {
6176
6473
  const isLocalMode = env?.isLocalMode;
6177
6474
  const loadDataPosSession = (0, import_react33.useCallback)(
@@ -6184,10 +6481,8 @@ var loadDataPosSessionService = (env) => {
6184
6481
  modelsToLoad = [],
6185
6482
  searchParams
6186
6483
  }) => {
6187
- console.log("isLocalMode", isLocalMode);
6188
6484
  if (isLocalMode) {
6189
6485
  const data = await loadData();
6190
- console.log("\u2705 loadData resolved:", data);
6191
6486
  return data;
6192
6487
  }
6193
6488
  const jsonData = {
@@ -6441,6 +6736,8 @@ var updateClosedSessionService = (env) => {
6441
6736
  // src/services/pos-service/update-entity.ts
6442
6737
  var import_react40 = require("react");
6443
6738
  var updateEntityService = (env) => {
6739
+ const isLocalMode = env?.isLocalMode;
6740
+ const repo = new ModelRepository();
6444
6741
  const updateEntity = (0, import_react40.useCallback)(
6445
6742
  ({
6446
6743
  model,
@@ -6450,6 +6747,15 @@ var updateEntityService = (env) => {
6450
6747
  xNode,
6451
6748
  service
6452
6749
  }) => {
6750
+ if (isLocalMode) {
6751
+ const id = extractIdFromDomain(domain);
6752
+ if (!id) return;
6753
+ return repo.updateRecord({
6754
+ update: values,
6755
+ modelName: model,
6756
+ id
6757
+ });
6758
+ }
6453
6759
  const jsonData = {
6454
6760
  model,
6455
6761
  domain,
@@ -6467,7 +6773,7 @@ var updateEntityService = (env) => {
6467
6773
  service
6468
6774
  );
6469
6775
  },
6470
- [env]
6776
+ [env, isLocalMode]
6471
6777
  );
6472
6778
  return {
6473
6779
  updateEntity
@@ -6549,101 +6855,6 @@ var usePosService = () => {
6549
6855
  return service;
6550
6856
  };
6551
6857
 
6552
- // src/services/filesystem-service/manifest.ts
6553
- var MANIFEST_PATH = "pos/manifest.json";
6554
- var MANIFEST_BAK_PATH = "pos/manifest.bak.json";
6555
- async function writeManifest(manifest) {
6556
- const oldRaw = await fileService.read(MANIFEST_PATH);
6557
- if (oldRaw !== null) {
6558
- await fileService.writeAtomic(MANIFEST_BAK_PATH, oldRaw);
6559
- }
6560
- await fileService.writeAtomic(MANIFEST_PATH, JSON.stringify(manifest));
6561
- try {
6562
- await fileService.delete(MANIFEST_BAK_PATH);
6563
- } catch {
6564
- }
6565
- }
6566
-
6567
- // src/services/filesystem-service/import-snapshot.ts
6568
- var DATA_DIR = "pos";
6569
- var MODELS_DIR2 = `${DATA_DIR}/models`;
6570
- var MODELS_META_DIR2 = `${DATA_DIR}/models_meta`;
6571
- var importSnapshot = async ({ data, onProgress }) => {
6572
- onProgress?.(1, "Parsing snapshot");
6573
- const parsed = await spawnParseWorker(data);
6574
- const modelNames = Object.keys(parsed);
6575
- const total = modelNames.length;
6576
- const manifest = { version: (/* @__PURE__ */ new Date()).toISOString(), models: {} };
6577
- const TMP_PREFIX = `pos/data/tmp_import_${Date.now()}`;
6578
- await fileService.writeAtomic(`${TMP_PREFIX}/.marker`, "1");
6579
- let i = 0;
6580
- for (const model of modelNames) {
6581
- i++;
6582
- onProgress?.(
6583
- Math.round(i / total * 100),
6584
- `Processing ${model} (${i}/${total})`
6585
- );
6586
- const block = parsed[model];
6587
- const dataPart = block?.data ?? block ?? [];
6588
- const fields = block?.fields ?? [];
6589
- const relations = block?.relations ?? {};
6590
- const serialized = await spawnStringifyWorker(dataPart);
6591
- const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
6592
- await fileService.writeAtomic(tmpModelPath, serialized);
6593
- const meta = {
6594
- fields,
6595
- relations,
6596
- count: Array.isArray(dataPart) ? dataPart.length : 0,
6597
- writtenAt: (/* @__PURE__ */ new Date()).toISOString()
6598
- };
6599
- const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
6600
- await fileService.writeAtomic(tmpMetaPath, JSON.stringify(meta));
6601
- manifest.models[model] = {
6602
- file: `${MODELS_DIR2}/${encodeURIComponent(model)}.json`,
6603
- metaFile: `${MODELS_META_DIR2}/${encodeURIComponent(model)}.meta.json`,
6604
- count: meta.count,
6605
- updatedAt: meta.writtenAt
6606
- };
6607
- }
6608
- onProgress?.(95, "Committing import (moving files)");
6609
- for (const model of modelNames) {
6610
- const tmpModelPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.json`;
6611
- const finalModelPath = `${MODELS_DIR2}/${encodeURIComponent(model)}.json`;
6612
- const tmpMetaPath = `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`;
6613
- const finalMetaPath = `${MODELS_META_DIR2}/${encodeURIComponent(
6614
- model
6615
- )}.meta.json`;
6616
- const tmpRaw = await fileService.read(tmpModelPath);
6617
- if (tmpRaw !== null) await fileService.writeAtomic(finalModelPath, tmpRaw);
6618
- const tmpMetaRaw = await fileService.read(tmpMetaPath);
6619
- if (tmpMetaRaw !== null)
6620
- await fileService.writeAtomic(finalMetaPath, tmpMetaRaw);
6621
- onProgress?.(
6622
- 95 + Math.round(
6623
- (Object.keys(manifest.models).indexOf(model) + 1) / modelNames.length * 5
6624
- ),
6625
- `Committed ${model}`
6626
- );
6627
- }
6628
- await writeManifest(manifest);
6629
- try {
6630
- for (const model of modelNames) {
6631
- await fileService.delete(
6632
- `${TMP_PREFIX}/${encodeURIComponent(model)}.json`
6633
- );
6634
- await fileService.delete(
6635
- `${TMP_PREFIX}/${encodeURIComponent(model)}.meta.json`
6636
- );
6637
- }
6638
- await fileService.delete(`${TMP_PREFIX}/.marker`);
6639
- } catch (e) {
6640
- console.log("Failed to cleanup tmp import files:", e);
6641
- }
6642
- onProgress?.(100, "Import complete");
6643
- return manifest;
6644
- };
6645
- var import_snapshot_default = importSnapshot;
6646
-
6647
6858
  // src/services/filesystem-service/init-snapshot.ts
6648
6859
  var isSnapshotReady = async () => {
6649
6860
  try {