@things-factory/calendar 10.1.283 → 10.1.290

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.
@@ -6,8 +6,13 @@ tslib_1.__exportStar(require("./middlewares"), exports);
6
6
  tslib_1.__exportStar(require("./service"), exports);
7
7
  require("./routes");
8
8
  const auth_base_1 = require("@things-factory/auth-base");
9
+ const notification_1 = require("@things-factory/notification");
10
+ const shell_1 = require("@things-factory/shell");
9
11
  const role_tasks_1 = require("./role-tasks");
12
+ const working_days_1 = require("./service/calendar/working-days");
10
13
  for (const task of role_tasks_1.ROLE_TASKS)
11
14
  (0, auth_base_1.registerRoleTask)(task);
12
15
  require("./work-doors");
16
+ /* A domain's notices leave the app inside its working hours (ADR-0164 amendment 2) - its working days and hours are ours. */
17
+ (0, notification_1.registerNoticeWorkingTime)((domain, now) => (0, working_days_1.workingTimeOf)((0, shell_1.getDataSource)().manager, domain, now));
13
18
  //# sourceMappingURL=index.js.map
@@ -16,6 +16,37 @@ export declare const PUBLIC_HOLIDAYS_NAME = "\uAD6D\uAC00 \uACF5\uD734\uC77C";
16
16
  export declare function daysOffBetween(tx: EntityManager, domain: Domain, from: string, to: string): Promise<Map<string, string>>;
17
17
  /** The working days from `from` to `to` (both kept), in order. */
18
18
  export declare function workingDaysBetween(tx: EntityManager, domain: Domain, from: string, to: string): Promise<string[]>;
19
+ /**
20
+ * **The working hours of a domain** (ADR-0164 amendment 2) - when its working day begins and ends, in its own zone. Two
21
+ * domain attributes, `HH:mm`; unset or unreadable, 09:00 to 18:00.
22
+ */
23
+ export declare const WORK_START_ATTRIBUTE = "Calendar::workStart";
24
+ export declare const WORK_END_ATTRIBUTE = "Calendar::workEnd";
25
+ export declare const WORK_HOURS_DEFAULT: {
26
+ readonly start: "09:00";
27
+ readonly end: "18:00";
28
+ };
29
+ /** The hours the attributes say - each its default when it is not `HH:mm`, both when the day would end before it begins. */
30
+ export declare function workHoursOf(attributes: Record<string, string | undefined> | null | undefined): {
31
+ start: string;
32
+ end: string;
33
+ };
34
+ /**
35
+ * Whether `now` is inside working hours, and when the next working day's hours begin - today's, when today is a
36
+ * working day not yet begun. `workingDays` are the working days from `now`'s day on, in order.
37
+ */
38
+ export declare function workingTimeAt(now: Date, timezone: string, hours: {
39
+ start: string;
40
+ end: string;
41
+ }, workingDays: readonly string[]): {
42
+ working: boolean;
43
+ nextStart: Date;
44
+ };
45
+ /** Whether a domain is in its working hours now, and when they next begin - its working days and its hours. */
46
+ export declare function workingTimeOf(tx: EntityManager, domain: Domain, now?: Date): Promise<{
47
+ working: boolean;
48
+ nextStart: Date;
49
+ }>;
19
50
  /**
20
51
  * A year's public holidays as the import gives them: the domain's public holiday calendar (made when there is none)
21
52
  * holds exactly these for the year - a day the import no longer gives is taken away. The number kept.
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DAYS_OFF_MAX_SPAN = exports.PUBLIC_HOLIDAYS_NAME = exports.WORKING_DAYS_MAX_SPAN = exports.WORK_WEEKDAYS = void 0;
3
+ exports.DAYS_OFF_MAX_SPAN = exports.WORK_HOURS_DEFAULT = exports.WORK_END_ATTRIBUTE = exports.WORK_START_ATTRIBUTE = exports.PUBLIC_HOLIDAYS_NAME = exports.WORKING_DAYS_MAX_SPAN = exports.WORK_WEEKDAYS = void 0;
4
4
  exports.daysOffBetween = daysOffBetween;
5
5
  exports.workingDaysBetween = workingDaysBetween;
6
+ exports.workHoursOf = workHoursOf;
7
+ exports.workingTimeAt = workingTimeAt;
8
+ exports.workingTimeOf = workingTimeOf;
6
9
  exports.savePublicHolidays = savePublicHolidays;
7
10
  exports.daysOffOfCalendar = daysOffOfCalendar;
8
11
  exports.saveDaysOff = saveDaysOff;
@@ -75,6 +78,42 @@ async function workingDaysBetween(tx, domain, from, to) {
75
78
  days.push(day);
76
79
  return days;
77
80
  }
81
+ /**
82
+ * **The working hours of a domain** (ADR-0164 amendment 2) - when its working day begins and ends, in its own zone. Two
83
+ * domain attributes, `HH:mm`; unset or unreadable, 09:00 to 18:00.
84
+ */
85
+ exports.WORK_START_ATTRIBUTE = 'Calendar::workStart';
86
+ exports.WORK_END_ATTRIBUTE = 'Calendar::workEnd';
87
+ exports.WORK_HOURS_DEFAULT = { start: '09:00', end: '18:00' };
88
+ const HHMM = /^([01]\d|2[0-3]):([0-5]\d)$/;
89
+ /** The hours the attributes say - each its default when it is not `HH:mm`, both when the day would end before it begins. */
90
+ function workHoursOf(attributes) {
91
+ const read = (name, fallback) => {
92
+ const value = String(attributes?.[name] ?? '').trim();
93
+ return HHMM.test(value) ? value : fallback;
94
+ };
95
+ const hours = { start: read(exports.WORK_START_ATTRIBUTE, exports.WORK_HOURS_DEFAULT.start), end: read(exports.WORK_END_ATTRIBUTE, exports.WORK_HOURS_DEFAULT.end) };
96
+ return hours.start < hours.end ? hours : { ...exports.WORK_HOURS_DEFAULT };
97
+ }
98
+ /**
99
+ * Whether `now` is inside working hours, and when the next working day's hours begin - today's, when today is a
100
+ * working day not yet begun. `workingDays` are the working days from `now`'s day on, in order.
101
+ */
102
+ function workingTimeAt(now, timezone, hours, workingDays) {
103
+ const today = (0, shell_1.calendarDay)(timezone, now);
104
+ const at = (day, hhmm) => (0, recurrence_1.instantOf)(day, Number(hhmm.slice(0, 2)), Number(hhmm.slice(3, 5)), 0, timezone);
105
+ const todayWorks = workingDays[0] === today;
106
+ const working = todayWorks && now >= at(today, hours.start) && now < at(today, hours.end);
107
+ const next = todayWorks && now < at(today, hours.start) ? today : workingDays.find(day => day > today);
108
+ return { working, nextStart: at(next ?? dayAfter(today), hours.start) };
109
+ }
110
+ /** Whether a domain is in its working hours now, and when they next begin - its working days and its hours. */
111
+ async function workingTimeOf(tx, domain, now = new Date()) {
112
+ const timezone = domain.timezone;
113
+ const today = (0, shell_1.calendarDay)(timezone, now);
114
+ const days = await workingDaysBetween(tx, domain, today, new Date(Date.parse(`${today}T00:00:00Z`) + 31 * 86400000).toISOString().slice(0, 10));
115
+ return workingTimeAt(now, timezone, workHoursOf(await (0, shell_1.loadDomainAttributes)(domain, 'Calendar::')), days);
116
+ }
78
117
  /**
79
118
  * A year's public holidays as the import gives them: the domain's public holiday calendar (made when there is none)
80
119
  * holds exactly these for the year - a day the import no longer gives is taken away. The number kept.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/calendar",
3
- "version": "10.1.283",
3
+ "version": "10.1.290",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "dist-client/index.js",
6
6
  "files": [
@@ -50,10 +50,11 @@
50
50
  "@operato/graphql": "^10.29.69",
51
51
  "@operato/popup": "^10.0.0",
52
52
  "@operato/shell": "^10.29.69",
53
- "@things-factory/ai-assistant": "^10.1.283",
54
- "@things-factory/auth-base": "^10.1.283",
55
- "@things-factory/shell": "^10.1.283",
53
+ "@things-factory/ai-assistant": "^10.1.290",
54
+ "@things-factory/auth-base": "^10.1.290",
55
+ "@things-factory/notification": "^10.1.290",
56
+ "@things-factory/shell": "^10.1.290",
56
57
  "cron-parser": "^4.3.0"
57
58
  },
58
- "gitHead": "912e4835a175273005bba4a98da160906caa2b40"
59
+ "gitHead": "b3d769457fc4976b73726bb1556b5036351f1b10"
59
60
  }