kcommons 18.26.4 → 18.26.6

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.
@@ -19,6 +19,7 @@ export interface IReportsPluginConfigCompany {
19
19
  to_emails: string[];
20
20
  cc_emails: string[];
21
21
  bcc_emails: string[];
22
+ version: string;
22
23
  last_run_at: string | null;
23
24
  next_run_at: string | null;
24
25
  created_at: string;
@@ -5,4 +5,4 @@ export type IScheduleShape = Pick<IReportsPluginConfigCompany, "frequency_type"
5
5
  * `send_time` ("HH:mm"), `day_of_week` (0-6) and `date_of_month` (1-31) are in IST.
6
6
  * MONTHLY clamps `date_of_month` to the last day of shorter months.
7
7
  */
8
- export declare const calculateNextRunAt: (scheduleConfig: IScheduleShape, after: Date) => Date | null;
8
+ export declare const calculateNextRunAt: (scheduleConfig: IScheduleShape, after: Date) => Date;
@@ -45,7 +45,7 @@ const calculateNextRunAt = (scheduleConfig, after) => {
45
45
  : getMonthlyRunAt(istYear, istMonth + 1);
46
46
  }
47
47
  default:
48
- return null;
48
+ throw new Error(`Invalid value for frequency_type: ${scheduleConfig.frequency_type}`);
49
49
  }
50
50
  };
51
51
  exports.calculateNextRunAt = calculateNextRunAt;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * A point in time accepted by {@link calculateTimeDifferenceInSeconds}.
3
+ *
4
+ * - `Date` — any valid `Date` object.
5
+ * - `string` — a 24-hour time of day in strict `HH:MM` format
6
+ * (e.g. `"09:05"`, `"23:59"`). Leading zeros are required and seconds
7
+ * are not supported.
8
+ */
9
+ type TimeInput = Date | string;
10
+ /**
11
+ * Calculates the absolute difference in seconds between two times. The
12
+ * result is never negative, so the order of `start` and `end` does not
13
+ * matter.
14
+ *
15
+ * The behaviour depends on the input types:
16
+ *
17
+ * - **Both `Date` objects** — the full date-time difference is used (via
18
+ * `date-fns` `differenceInSeconds`), so differences across days are
19
+ * included. Fractional seconds are truncated.
20
+ * - **Any `string` input (or a mix of `Date` and `string`)** — only the time
21
+ * of day is compared. The calendar date of any `Date` is ignored, and its
22
+ * local hours/minutes/seconds are used.
23
+ *
24
+ * The time-of-day result is **not** wrapped around midnight: `"22:00"` →
25
+ * `"02:00"` gives `72000` (the gap between the two times of day), not the
26
+ * overnight duration `14400`. Handle overnight ranges at the call site if
27
+ * needed.
28
+ *
29
+ * @param start - The start time: a `Date` or an `HH:MM` (24-hour) string.
30
+ * @param end - The end time: a `Date` or an `HH:MM` (24-hour) string.
31
+ * @returns The absolute number of seconds between `start` and `end`
32
+ * (never negative).
33
+ * @throws {Error} If a string input is not in `HH:MM` format, or a `Date`
34
+ * input is invalid (in the time-of-day comparison path).
35
+ *
36
+ * @example
37
+ * calculateTimeDifferenceInSeconds("09:00", "17:30"); // 30600
38
+ *
39
+ * @example
40
+ * // Order does not matter
41
+ * calculateTimeDifferenceInSeconds("18:00", "09:00"); // 32400
42
+ *
43
+ * @example
44
+ * // Both Dates: full date-time difference, across days
45
+ * calculateTimeDifferenceInSeconds(
46
+ * new Date("2024-01-01T23:00:00"),
47
+ * new Date("2024-01-02T01:00:00"),
48
+ * ); // 7200
49
+ *
50
+ * @example
51
+ * // Mixed: only the Date's time of day (10:15:30) is used
52
+ * calculateTimeDifferenceInSeconds(new Date("2024-01-01T10:15:30"), "11:00"); // 2670
53
+ */
54
+ export declare function calculateTimeDifferenceInSeconds(start: TimeInput, end: TimeInput): number;
55
+ export {};
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateTimeDifferenceInSeconds = calculateTimeDifferenceInSeconds;
4
+ const date_fns_1 = require("date-fns");
5
+ /**
6
+ * Converts a time input into the number of seconds elapsed since midnight.
7
+ *
8
+ * For a `Date`, only its local time of day (hours, minutes, seconds) is used;
9
+ * the calendar date and milliseconds are ignored. For a string, the `HH:MM`
10
+ * value is parsed and seconds are taken as `0`.
11
+ *
12
+ * @param input - A valid `Date` or an `HH:MM` time string.
13
+ * @returns Seconds since midnight, in the range `0`–`86399`.
14
+ * @throws {Error} If `input` is an invalid `Date` (e.g. `new Date("foo")`).
15
+ * @throws {Error} If `input` is a string not matching `HH:MM` (24-hour).
16
+ */
17
+ function toSeconds(input) {
18
+ if (input instanceof Date) {
19
+ if (!(0, date_fns_1.isValid)(input)) {
20
+ throw new Error("Invalid Date object");
21
+ }
22
+ return (input.getHours() * 3600 + input.getMinutes() * 60 + input.getSeconds());
23
+ }
24
+ const match = /^([01]\d|2[0-3]):([0-5]\d)$/.exec(input);
25
+ if (!match) {
26
+ throw new Error(`Invalid time format: ${input}. Expected HH:MM`);
27
+ }
28
+ const hours = Number(match[1]);
29
+ const minutes = Number(match[2]);
30
+ return hours * 3600 + minutes * 60;
31
+ }
32
+ /**
33
+ * Calculates the absolute difference in seconds between two times. The
34
+ * result is never negative, so the order of `start` and `end` does not
35
+ * matter.
36
+ *
37
+ * The behaviour depends on the input types:
38
+ *
39
+ * - **Both `Date` objects** — the full date-time difference is used (via
40
+ * `date-fns` `differenceInSeconds`), so differences across days are
41
+ * included. Fractional seconds are truncated.
42
+ * - **Any `string` input (or a mix of `Date` and `string`)** — only the time
43
+ * of day is compared. The calendar date of any `Date` is ignored, and its
44
+ * local hours/minutes/seconds are used.
45
+ *
46
+ * The time-of-day result is **not** wrapped around midnight: `"22:00"` →
47
+ * `"02:00"` gives `72000` (the gap between the two times of day), not the
48
+ * overnight duration `14400`. Handle overnight ranges at the call site if
49
+ * needed.
50
+ *
51
+ * @param start - The start time: a `Date` or an `HH:MM` (24-hour) string.
52
+ * @param end - The end time: a `Date` or an `HH:MM` (24-hour) string.
53
+ * @returns The absolute number of seconds between `start` and `end`
54
+ * (never negative).
55
+ * @throws {Error} If a string input is not in `HH:MM` format, or a `Date`
56
+ * input is invalid (in the time-of-day comparison path).
57
+ *
58
+ * @example
59
+ * calculateTimeDifferenceInSeconds("09:00", "17:30"); // 30600
60
+ *
61
+ * @example
62
+ * // Order does not matter
63
+ * calculateTimeDifferenceInSeconds("18:00", "09:00"); // 32400
64
+ *
65
+ * @example
66
+ * // Both Dates: full date-time difference, across days
67
+ * calculateTimeDifferenceInSeconds(
68
+ * new Date("2024-01-01T23:00:00"),
69
+ * new Date("2024-01-02T01:00:00"),
70
+ * ); // 7200
71
+ *
72
+ * @example
73
+ * // Mixed: only the Date's time of day (10:15:30) is used
74
+ * calculateTimeDifferenceInSeconds(new Date("2024-01-01T10:15:30"), "11:00"); // 2670
75
+ */
76
+ function calculateTimeDifferenceInSeconds(start, end) {
77
+ if (start instanceof Date && end instanceof Date) {
78
+ return Math.abs((0, date_fns_1.differenceInSeconds)(end, start));
79
+ }
80
+ return Math.abs(toSeconds(end) - toSeconds(start));
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kcommons",
3
- "version": "18.26.4",
3
+ "version": "18.26.6",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",