@freshpointcz/fresh-core 0.0.13 → 0.0.15

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/index.js CHANGED
@@ -452,6 +452,14 @@ __export(index_exports, {
452
452
  TransactionType: () => TransactionType,
453
453
  createDeferred: () => createDeferred,
454
454
  freshEslintConfig: () => eslint_config_default,
455
+ hasOwn: () => hasOwn,
456
+ isEnumValue: () => isEnumValue,
457
+ isFlag01: () => isFlag01,
458
+ isMaybe: () => isMaybe,
459
+ isNumber: () => isNumber,
460
+ isNumberInRange: () => isNumberInRange,
461
+ isObject: () => isObject,
462
+ isString: () => isString,
455
463
  isValidCron: () => isValidCron,
456
464
  logger: () => logger
457
465
  });
@@ -746,14 +754,38 @@ __name(isValidCron, "isValidCron");
746
754
 
747
755
  // src/common/patterns/singleton.ts
748
756
  var _Singleton = class _Singleton {
749
- constructor() {
757
+ /**
758
+ * Creates or returns the singleton instance for the subclass.
759
+ *
760
+ * If an instance already exists for the subclass, that instance is
761
+ * returned instead of creating a new one.
762
+ *
763
+ * @param callOnInit - Whether to call {@link onInit} for a newly
764
+ * created instance. Ignored if the instance already exists.
765
+ */
766
+ constructor(callOnInit = true) {
750
767
  const ctor = this.constructor;
751
768
  const existing = _Singleton.instances.get(ctor);
752
769
  if (existing) {
753
770
  return existing;
754
771
  }
755
772
  _Singleton.instances.set(ctor, this);
773
+ if (callOnInit) {
774
+ void this.onInit();
775
+ }
756
776
  }
777
+ /**
778
+ * Retrieves the singleton instance for the calling subclass.
779
+ *
780
+ * If no instance exists, a new one is created using the provided
781
+ * constructor arguments.
782
+ *
783
+ * Intended to be used by subclasses as a protected factory method.
784
+ *
785
+ * @typeParam T - The concrete subclass type.
786
+ * @param args - Arguments forwarded to the subclass constructor.
787
+ * @returns The singleton instance of the subclass.
788
+ */
757
789
  static getInstance(...args) {
758
790
  let instance = _Singleton.instances.get(this);
759
791
  if (!instance) {
@@ -764,6 +796,10 @@ var _Singleton = class _Singleton {
764
796
  }
765
797
  };
766
798
  __name(_Singleton, "Singleton");
799
+ /**
800
+ * Internal registry of singleton instances.
801
+ * One instance per subclass constructor.
802
+ */
767
803
  __publicField(_Singleton, "instances", /* @__PURE__ */ new Map());
768
804
  var Singleton = _Singleton;
769
805
 
@@ -1112,6 +1148,7 @@ var PaymentMethod = /* @__PURE__ */ (function(PaymentMethod2) {
1112
1148
  PaymentMethod2[PaymentMethod2["CODE"] = 0] = "CODE";
1113
1149
  PaymentMethod2[PaymentMethod2["CARD"] = 1] = "CARD";
1114
1150
  PaymentMethod2[PaymentMethod2["NONE"] = 2] = "NONE";
1151
+ PaymentMethod2[PaymentMethod2["CREDIT"] = 3] = "CREDIT";
1115
1152
  return PaymentMethod2;
1116
1153
  })({});
1117
1154
 
@@ -1170,6 +1207,9 @@ _ts_decorate3([
1170
1207
 
1171
1208
  // src/database/dao/fresh-dao.ts
1172
1209
  var _FreshDao = class _FreshDao {
1210
+ getRepo(manager, entity = this.entity) {
1211
+ return manager ? manager.getRepository(entity) : this.repo;
1212
+ }
1173
1213
  };
1174
1214
  __name(_FreshDao, "FreshDao");
1175
1215
  var FreshDao = _FreshDao;
@@ -1258,6 +1298,56 @@ Subcategory = _ts_decorate8([
1258
1298
  (0, import_typeorm9.Entity)()
1259
1299
  ], Subcategory);
1260
1300
 
1301
+ // src/common/typeguards/enums.ts
1302
+ function isEnumValue(enumObj, v) {
1303
+ if (!enumObj) {
1304
+ return typeof v === "string" || typeof v === "number";
1305
+ }
1306
+ const values = new Set(Object.values(enumObj));
1307
+ return (typeof v === "string" || typeof v === "number") && values.has(v);
1308
+ }
1309
+ __name(isEnumValue, "isEnumValue");
1310
+
1311
+ // src/common/typeguards/objects.ts
1312
+ function isObject(v) {
1313
+ return typeof v === "object" && v !== null;
1314
+ }
1315
+ __name(isObject, "isObject");
1316
+ function hasOwn(obj, key) {
1317
+ return Object.prototype.hasOwnProperty.call(obj, key);
1318
+ }
1319
+ __name(hasOwn, "hasOwn");
1320
+
1321
+ // src/common/typeguards/primitives.ts
1322
+ function isNumber(v) {
1323
+ return typeof v === "number" && Number.isFinite(v);
1324
+ }
1325
+ __name(isNumber, "isNumber");
1326
+ function isString(v) {
1327
+ return typeof v === "string";
1328
+ }
1329
+ __name(isString, "isString");
1330
+ function isFlag01(v) {
1331
+ return v === 0 || v === 1;
1332
+ }
1333
+ __name(isFlag01, "isFlag01");
1334
+ function isNumberInRange(v, min, max, options) {
1335
+ var _a, _b;
1336
+ if (!isNumber(v)) {
1337
+ return false;
1338
+ }
1339
+ const includeMin = (_a = options == null ? void 0 : options.includeMin) != null ? _a : true;
1340
+ const includeMax = (_b = options == null ? void 0 : options.includeMax) != null ? _b : true;
1341
+ if (includeMin ? v < min : v <= min) {
1342
+ return false;
1343
+ }
1344
+ if (includeMax ? v > max : v >= max) {
1345
+ return false;
1346
+ }
1347
+ return true;
1348
+ }
1349
+ __name(isNumberInRange, "isNumberInRange");
1350
+
1261
1351
  // src/core/data-helper.ts
1262
1352
  var _DataHelper = class _DataHelper {
1263
1353
  constructor(startDataRetrieve = true, deviceIds) {
@@ -1299,13 +1389,34 @@ var DataHelper = _DataHelper;
1299
1389
  var import_node_schedule = require("node-schedule");
1300
1390
  var _FreshJob = class _FreshJob extends Singleton {
1301
1391
  /**
1302
- * @param cronExpression must be cron of just numbers ex.: `0 5 * * 4`
1303
- * By default timezone is added " Europe/Prague"
1392
+ * Creates and optionally schedules a singleton cron job.
1393
+ *
1394
+ * @param jobName - Logical name of the job (used in logs and errors).
1395
+ * @param cronExpression - Cron expression consisting of numeric fields only.
1396
+ * Example: `0 5 * * 4`
1397
+ * The timezone `"Europe/Prague"` is applied automatically.
1398
+ *
1399
+ * @param jobEnabled - Whether the job should be scheduled immediately.
1400
+ * If `false`, the instance exists but no cron trigger is registered.
1401
+ *
1402
+ * @throws Error If the cron expression is invalid.
1403
+ * @throws Error If the job cannot be scheduled.
1304
1404
  */
1305
1405
  constructor(jobName, cronExpression, jobEnabled) {
1306
- super();
1406
+ super(false);
1407
+ /** Human-readable job identifier (used for logs and errors). */
1307
1408
  __publicField(this, "_jobName");
1409
+ /**
1410
+ * Cron expression defining when the job runs.
1411
+ *
1412
+ * Must contain numeric cron fields only.
1413
+ * Example: `0 5 * * 4`
1414
+ *
1415
+ * Timezone is always forced to `"Europe/Prague"`.
1416
+ * If you need another timezone, this class is not your friend.
1417
+ */
1308
1418
  __publicField(this, "_cronExpression");
1419
+ /** Scheduled job instance, or `null` if the job is disabled. */
1309
1420
  __publicField(this, "_job", null);
1310
1421
  this._jobName = jobName;
1311
1422
  if (!isValidCron(cronExpression)) {
@@ -1325,28 +1436,68 @@ var _FreshJob = class _FreshJob extends Singleton {
1325
1436
  }
1326
1437
  this.onInit();
1327
1438
  }
1328
- /** Just logging */
1439
+ /**
1440
+ * Initialization hook.
1441
+ *
1442
+ * Called once during construction and used primarily for logging.
1443
+ * Can also be reused by subclasses if manual rescheduling is implemented.
1444
+ *
1445
+ * @param rescheduled - Indicates whether the job was reconfigured
1446
+ * after initial creation.
1447
+ */
1329
1448
  onInit(rescheduled) {
1330
1449
  console.log(`Job ${this.jobName} ${rescheduled ? "rescheduled" : "initialized"} with cron rule: "${this.cronExpression}"`);
1331
1450
  }
1451
+ /** @returns The logical name of the job. */
1332
1452
  get jobName() {
1333
1453
  return this._jobName;
1334
1454
  }
1455
+ /** Allows subclasses to rename the job if absolutely necessary. */
1335
1456
  set jobName(value) {
1336
1457
  this._jobName = value;
1337
1458
  }
1459
+ /** @returns The cron expression used to schedule the job. */
1338
1460
  get cronExpression() {
1339
1461
  return this._cronExpression;
1340
1462
  }
1463
+ /**
1464
+ * Allows subclasses to update the cron expression internally.
1465
+ * Does reschedule of job if exists
1466
+ */
1341
1467
  set cronExpression(value) {
1468
+ if (!isValidCron(value)) {
1469
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${value}"`);
1470
+ }
1342
1471
  this._cronExpression = value;
1343
1472
  }
1473
+ /** @returns The underlying scheduled job instance, or `null` if disabled. */
1344
1474
  get job() {
1345
1475
  return this._job;
1346
1476
  }
1477
+ /** Allows subclasses to manage the scheduled job instance. */
1347
1478
  set job(value) {
1348
1479
  this._job = value;
1349
1480
  }
1481
+ reschedule(newCronExpression) {
1482
+ let result = false;
1483
+ if (!isValidCron(newCronExpression)) {
1484
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${newCronExpression}"`);
1485
+ }
1486
+ this._cronExpression = newCronExpression;
1487
+ if (this._job) {
1488
+ const rescheduleSuccess = this._job.reschedule({
1489
+ rule: this._cronExpression,
1490
+ tz: "Europe/Prague"
1491
+ });
1492
+ if (!rescheduleSuccess) {
1493
+ throw new Error(`Job ${this._jobName} could not be re-scheduled`);
1494
+ } else {
1495
+ this.onInit(true);
1496
+ }
1497
+ result = rescheduleSuccess;
1498
+ }
1499
+ return result;
1500
+ }
1350
1501
  };
1351
1502
  __name(_FreshJob, "FreshJob");
1352
1503
  var FreshJob = _FreshJob;
@@ -1370,6 +1521,12 @@ var _ApiError = class _ApiError extends Error {
1370
1521
  __name(_ApiError, "ApiError");
1371
1522
  var ApiError = _ApiError;
1372
1523
 
1524
+ // src/types/maybe-type.ts
1525
+ function isMaybe(v, inner) {
1526
+ return v === null || inner(v);
1527
+ }
1528
+ __name(isMaybe, "isMaybe");
1529
+
1373
1530
  // src/config/eslint-config.ts
1374
1531
  var import_typescript_eslint = __toESM(require("typescript-eslint"));
1375
1532
  var import_eslint_plugin_prettier = __toESM(require("eslint-plugin-prettier"));
@@ -1581,6 +1738,14 @@ var datasource_default = PG_DATA_SOURCE_OPTIONS;
1581
1738
  TransactionType,
1582
1739
  createDeferred,
1583
1740
  freshEslintConfig,
1741
+ hasOwn,
1742
+ isEnumValue,
1743
+ isFlag01,
1744
+ isMaybe,
1745
+ isNumber,
1746
+ isNumberInRange,
1747
+ isObject,
1748
+ isString,
1584
1749
  isValidCron,
1585
1750
  logger
1586
1751
  });