@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.mjs CHANGED
@@ -711,14 +711,38 @@ __name(isValidCron, "isValidCron");
711
711
 
712
712
  // src/common/patterns/singleton.ts
713
713
  var _Singleton = class _Singleton {
714
- constructor() {
714
+ /**
715
+ * Creates or returns the singleton instance for the subclass.
716
+ *
717
+ * If an instance already exists for the subclass, that instance is
718
+ * returned instead of creating a new one.
719
+ *
720
+ * @param callOnInit - Whether to call {@link onInit} for a newly
721
+ * created instance. Ignored if the instance already exists.
722
+ */
723
+ constructor(callOnInit = true) {
715
724
  const ctor = this.constructor;
716
725
  const existing = _Singleton.instances.get(ctor);
717
726
  if (existing) {
718
727
  return existing;
719
728
  }
720
729
  _Singleton.instances.set(ctor, this);
730
+ if (callOnInit) {
731
+ void this.onInit();
732
+ }
721
733
  }
734
+ /**
735
+ * Retrieves the singleton instance for the calling subclass.
736
+ *
737
+ * If no instance exists, a new one is created using the provided
738
+ * constructor arguments.
739
+ *
740
+ * Intended to be used by subclasses as a protected factory method.
741
+ *
742
+ * @typeParam T - The concrete subclass type.
743
+ * @param args - Arguments forwarded to the subclass constructor.
744
+ * @returns The singleton instance of the subclass.
745
+ */
722
746
  static getInstance(...args) {
723
747
  let instance = _Singleton.instances.get(this);
724
748
  if (!instance) {
@@ -729,6 +753,10 @@ var _Singleton = class _Singleton {
729
753
  }
730
754
  };
731
755
  __name(_Singleton, "Singleton");
756
+ /**
757
+ * Internal registry of singleton instances.
758
+ * One instance per subclass constructor.
759
+ */
732
760
  __publicField(_Singleton, "instances", /* @__PURE__ */ new Map());
733
761
  var Singleton = _Singleton;
734
762
 
@@ -1077,6 +1105,7 @@ var PaymentMethod = /* @__PURE__ */ (function(PaymentMethod2) {
1077
1105
  PaymentMethod2[PaymentMethod2["CODE"] = 0] = "CODE";
1078
1106
  PaymentMethod2[PaymentMethod2["CARD"] = 1] = "CARD";
1079
1107
  PaymentMethod2[PaymentMethod2["NONE"] = 2] = "NONE";
1108
+ PaymentMethod2[PaymentMethod2["CREDIT"] = 3] = "CREDIT";
1080
1109
  return PaymentMethod2;
1081
1110
  })({});
1082
1111
 
@@ -1135,6 +1164,9 @@ _ts_decorate3([
1135
1164
 
1136
1165
  // src/database/dao/fresh-dao.ts
1137
1166
  var _FreshDao = class _FreshDao {
1167
+ getRepo(manager, entity = this.entity) {
1168
+ return manager ? manager.getRepository(entity) : this.repo;
1169
+ }
1138
1170
  };
1139
1171
  __name(_FreshDao, "FreshDao");
1140
1172
  var FreshDao = _FreshDao;
@@ -1223,6 +1255,56 @@ Subcategory = _ts_decorate8([
1223
1255
  Entity5()
1224
1256
  ], Subcategory);
1225
1257
 
1258
+ // src/common/typeguards/enums.ts
1259
+ function isEnumValue(enumObj, v) {
1260
+ if (!enumObj) {
1261
+ return typeof v === "string" || typeof v === "number";
1262
+ }
1263
+ const values = new Set(Object.values(enumObj));
1264
+ return (typeof v === "string" || typeof v === "number") && values.has(v);
1265
+ }
1266
+ __name(isEnumValue, "isEnumValue");
1267
+
1268
+ // src/common/typeguards/objects.ts
1269
+ function isObject(v) {
1270
+ return typeof v === "object" && v !== null;
1271
+ }
1272
+ __name(isObject, "isObject");
1273
+ function hasOwn(obj, key) {
1274
+ return Object.prototype.hasOwnProperty.call(obj, key);
1275
+ }
1276
+ __name(hasOwn, "hasOwn");
1277
+
1278
+ // src/common/typeguards/primitives.ts
1279
+ function isNumber(v) {
1280
+ return typeof v === "number" && Number.isFinite(v);
1281
+ }
1282
+ __name(isNumber, "isNumber");
1283
+ function isString(v) {
1284
+ return typeof v === "string";
1285
+ }
1286
+ __name(isString, "isString");
1287
+ function isFlag01(v) {
1288
+ return v === 0 || v === 1;
1289
+ }
1290
+ __name(isFlag01, "isFlag01");
1291
+ function isNumberInRange(v, min, max, options) {
1292
+ var _a, _b;
1293
+ if (!isNumber(v)) {
1294
+ return false;
1295
+ }
1296
+ const includeMin = (_a = options == null ? void 0 : options.includeMin) != null ? _a : true;
1297
+ const includeMax = (_b = options == null ? void 0 : options.includeMax) != null ? _b : true;
1298
+ if (includeMin ? v < min : v <= min) {
1299
+ return false;
1300
+ }
1301
+ if (includeMax ? v > max : v >= max) {
1302
+ return false;
1303
+ }
1304
+ return true;
1305
+ }
1306
+ __name(isNumberInRange, "isNumberInRange");
1307
+
1226
1308
  // src/core/data-helper.ts
1227
1309
  var _DataHelper = class _DataHelper {
1228
1310
  constructor(startDataRetrieve = true, deviceIds) {
@@ -1264,13 +1346,34 @@ var DataHelper = _DataHelper;
1264
1346
  import { scheduleJob } from "node-schedule";
1265
1347
  var _FreshJob = class _FreshJob extends Singleton {
1266
1348
  /**
1267
- * @param cronExpression must be cron of just numbers ex.: `0 5 * * 4`
1268
- * By default timezone is added " Europe/Prague"
1349
+ * Creates and optionally schedules a singleton cron job.
1350
+ *
1351
+ * @param jobName - Logical name of the job (used in logs and errors).
1352
+ * @param cronExpression - Cron expression consisting of numeric fields only.
1353
+ * Example: `0 5 * * 4`
1354
+ * The timezone `"Europe/Prague"` is applied automatically.
1355
+ *
1356
+ * @param jobEnabled - Whether the job should be scheduled immediately.
1357
+ * If `false`, the instance exists but no cron trigger is registered.
1358
+ *
1359
+ * @throws Error If the cron expression is invalid.
1360
+ * @throws Error If the job cannot be scheduled.
1269
1361
  */
1270
1362
  constructor(jobName, cronExpression, jobEnabled) {
1271
- super();
1363
+ super(false);
1364
+ /** Human-readable job identifier (used for logs and errors). */
1272
1365
  __publicField(this, "_jobName");
1366
+ /**
1367
+ * Cron expression defining when the job runs.
1368
+ *
1369
+ * Must contain numeric cron fields only.
1370
+ * Example: `0 5 * * 4`
1371
+ *
1372
+ * Timezone is always forced to `"Europe/Prague"`.
1373
+ * If you need another timezone, this class is not your friend.
1374
+ */
1273
1375
  __publicField(this, "_cronExpression");
1376
+ /** Scheduled job instance, or `null` if the job is disabled. */
1274
1377
  __publicField(this, "_job", null);
1275
1378
  this._jobName = jobName;
1276
1379
  if (!isValidCron(cronExpression)) {
@@ -1290,28 +1393,68 @@ var _FreshJob = class _FreshJob extends Singleton {
1290
1393
  }
1291
1394
  this.onInit();
1292
1395
  }
1293
- /** Just logging */
1396
+ /**
1397
+ * Initialization hook.
1398
+ *
1399
+ * Called once during construction and used primarily for logging.
1400
+ * Can also be reused by subclasses if manual rescheduling is implemented.
1401
+ *
1402
+ * @param rescheduled - Indicates whether the job was reconfigured
1403
+ * after initial creation.
1404
+ */
1294
1405
  onInit(rescheduled) {
1295
1406
  console.log(`Job ${this.jobName} ${rescheduled ? "rescheduled" : "initialized"} with cron rule: "${this.cronExpression}"`);
1296
1407
  }
1408
+ /** @returns The logical name of the job. */
1297
1409
  get jobName() {
1298
1410
  return this._jobName;
1299
1411
  }
1412
+ /** Allows subclasses to rename the job if absolutely necessary. */
1300
1413
  set jobName(value) {
1301
1414
  this._jobName = value;
1302
1415
  }
1416
+ /** @returns The cron expression used to schedule the job. */
1303
1417
  get cronExpression() {
1304
1418
  return this._cronExpression;
1305
1419
  }
1420
+ /**
1421
+ * Allows subclasses to update the cron expression internally.
1422
+ * Does reschedule of job if exists
1423
+ */
1306
1424
  set cronExpression(value) {
1425
+ if (!isValidCron(value)) {
1426
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${value}"`);
1427
+ }
1307
1428
  this._cronExpression = value;
1308
1429
  }
1430
+ /** @returns The underlying scheduled job instance, or `null` if disabled. */
1309
1431
  get job() {
1310
1432
  return this._job;
1311
1433
  }
1434
+ /** Allows subclasses to manage the scheduled job instance. */
1312
1435
  set job(value) {
1313
1436
  this._job = value;
1314
1437
  }
1438
+ reschedule(newCronExpression) {
1439
+ let result = false;
1440
+ if (!isValidCron(newCronExpression)) {
1441
+ throw new Error(`Job ${this._jobName} cannot be re-scheduled with invalid cron: "${newCronExpression}"`);
1442
+ }
1443
+ this._cronExpression = newCronExpression;
1444
+ if (this._job) {
1445
+ const rescheduleSuccess = this._job.reschedule({
1446
+ rule: this._cronExpression,
1447
+ tz: "Europe/Prague"
1448
+ });
1449
+ if (!rescheduleSuccess) {
1450
+ throw new Error(`Job ${this._jobName} could not be re-scheduled`);
1451
+ } else {
1452
+ this.onInit(true);
1453
+ }
1454
+ result = rescheduleSuccess;
1455
+ }
1456
+ return result;
1457
+ }
1315
1458
  };
1316
1459
  __name(_FreshJob, "FreshJob");
1317
1460
  var FreshJob = _FreshJob;
@@ -1335,6 +1478,12 @@ var _ApiError = class _ApiError extends Error {
1335
1478
  __name(_ApiError, "ApiError");
1336
1479
  var ApiError = _ApiError;
1337
1480
 
1481
+ // src/types/maybe-type.ts
1482
+ function isMaybe(v, inner) {
1483
+ return v === null || inner(v);
1484
+ }
1485
+ __name(isMaybe, "isMaybe");
1486
+
1338
1487
  // src/config/eslint-config.ts
1339
1488
  import tseslint from "typescript-eslint";
1340
1489
  import eslintPluginPrettier from "eslint-plugin-prettier";
@@ -1545,6 +1694,14 @@ export {
1545
1694
  TransactionType,
1546
1695
  createDeferred,
1547
1696
  eslint_config_default as freshEslintConfig,
1697
+ hasOwn,
1698
+ isEnumValue,
1699
+ isFlag01,
1700
+ isMaybe,
1701
+ isNumber,
1702
+ isNumberInRange,
1703
+ isObject,
1704
+ isString,
1548
1705
  isValidCron,
1549
1706
  logger
1550
1707
  };