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