@weft-framework/server 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
package/dist/calendar.js
ADDED
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import { defineJob } from "./jobs.js";
|
|
2
|
+
const MINUTE = 60_000;
|
|
3
|
+
const DAY = 86_400_000;
|
|
4
|
+
const formats = new Map();
|
|
5
|
+
function format(tz) {
|
|
6
|
+
let f = formats.get(tz);
|
|
7
|
+
if (!f) {
|
|
8
|
+
f = new Intl.DateTimeFormat("en-US", {
|
|
9
|
+
timeZone: tz,
|
|
10
|
+
hourCycle: "h23",
|
|
11
|
+
year: "numeric",
|
|
12
|
+
month: "numeric",
|
|
13
|
+
day: "numeric",
|
|
14
|
+
hour: "numeric",
|
|
15
|
+
minute: "numeric",
|
|
16
|
+
second: "numeric",
|
|
17
|
+
});
|
|
18
|
+
formats.set(tz, f);
|
|
19
|
+
}
|
|
20
|
+
return f;
|
|
21
|
+
}
|
|
22
|
+
export function isTimeZone(tz) {
|
|
23
|
+
if (typeof tz !== "string" || tz === "" || tz.length > 64)
|
|
24
|
+
return false;
|
|
25
|
+
try {
|
|
26
|
+
format(tz);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const pad = (n, w = 2) => String(n).padStart(w, "0");
|
|
34
|
+
function dayNumber(y, m, d) {
|
|
35
|
+
return Math.floor(Date.UTC(y, m - 1, d) / DAY);
|
|
36
|
+
}
|
|
37
|
+
function fromDayNumber(n) {
|
|
38
|
+
const d = new Date(n * DAY);
|
|
39
|
+
return { year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate() };
|
|
40
|
+
}
|
|
41
|
+
const weekdayOf = (n) => (((n + 3) % 7) + 7) % 7;
|
|
42
|
+
const dateOf = (n) => {
|
|
43
|
+
const d = fromDayNumber(n);
|
|
44
|
+
return `${pad(d.year, 4)}-${pad(d.month)}-${pad(d.day)}`;
|
|
45
|
+
};
|
|
46
|
+
export function wallTime(ms, tz) {
|
|
47
|
+
const parts = {};
|
|
48
|
+
for (const p of format(tz).formatToParts(new Date(ms)))
|
|
49
|
+
if (p.type !== "literal")
|
|
50
|
+
parts[p.type] = Number(p.value);
|
|
51
|
+
const { year, month, day, hour, minute } = parts;
|
|
52
|
+
return {
|
|
53
|
+
year: year,
|
|
54
|
+
month: month,
|
|
55
|
+
day: day,
|
|
56
|
+
hour: hour,
|
|
57
|
+
minute: minute,
|
|
58
|
+
weekday: weekdayOf(dayNumber(year, month, day)),
|
|
59
|
+
date: `${pad(year, 4)}-${pad(month)}-${pad(day)}`,
|
|
60
|
+
time: `${pad(hour)}:${pad(minute)}`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function offsetAt(ms, tz) {
|
|
64
|
+
const t = Math.floor(ms / 1000) * 1000;
|
|
65
|
+
const parts = {};
|
|
66
|
+
for (const p of format(tz).formatToParts(new Date(t)))
|
|
67
|
+
if (p.type !== "literal")
|
|
68
|
+
parts[p.type] = Number(p.value);
|
|
69
|
+
return Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second) - t;
|
|
70
|
+
}
|
|
71
|
+
export function zonedTime(tz, date, time = "00:00") {
|
|
72
|
+
const d = DATE.exec(date);
|
|
73
|
+
const t = TIME.exec(time);
|
|
74
|
+
if (!d || !validDate(date) || !t)
|
|
75
|
+
throw new RangeError(`not a date and time: ${date} ${time}`);
|
|
76
|
+
const guess = Date.UTC(+d[1], +d[2] - 1, +d[3], +t[1], +t[2]);
|
|
77
|
+
const before = offsetAt(guess - DAY / 2, tz);
|
|
78
|
+
const after = offsetAt(guess + DAY / 2, tz);
|
|
79
|
+
const a = guess - before;
|
|
80
|
+
const b = guess - after;
|
|
81
|
+
const matches = (ms) => ms + offsetAt(ms, tz) === guess;
|
|
82
|
+
return matches(a) || !matches(b) ? a : b;
|
|
83
|
+
}
|
|
84
|
+
const DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
85
|
+
const TIME = /^([01]\d|2[0-4]):([0-5]\d)$/;
|
|
86
|
+
function validDate(s) {
|
|
87
|
+
const m = DATE.exec(s);
|
|
88
|
+
if (!m)
|
|
89
|
+
return false;
|
|
90
|
+
const v = fromDayNumber(dayNumber(+m[1], +m[2], +m[3]));
|
|
91
|
+
return v.year === +m[1] && v.month === +m[2] && v.day === +m[3];
|
|
92
|
+
}
|
|
93
|
+
function dayOfDate(s) {
|
|
94
|
+
const m = DATE.exec(s);
|
|
95
|
+
return dayNumber(+m[1], +m[2], +m[3]);
|
|
96
|
+
}
|
|
97
|
+
const minutesOf = (time) => {
|
|
98
|
+
const t = TIME.exec(time);
|
|
99
|
+
return +t[1] * 60 + +t[2];
|
|
100
|
+
};
|
|
101
|
+
const WEEKDAYS = ["MO", "TU", "WE", "TH", "FR", "SA", "SU"];
|
|
102
|
+
const BYDAY = /^([+-]?[1-5])?(MO|TU|WE|TH|FR|SA|SU)$/;
|
|
103
|
+
export function parseRecurrence(text) {
|
|
104
|
+
const r = { freq: "daily" };
|
|
105
|
+
let freq = false;
|
|
106
|
+
for (const part of text.replace(/^RRULE:/i, "").split(";")) {
|
|
107
|
+
if (part === "")
|
|
108
|
+
continue;
|
|
109
|
+
const [k, v = ""] = part.split("=", 2);
|
|
110
|
+
const list = () => v.split(",").filter(Boolean);
|
|
111
|
+
const nums = () => list().map((x) => (/^[+-]?\d{1,2}$/.test(x) ? Number(x) : NaN));
|
|
112
|
+
switch (k.toUpperCase()) {
|
|
113
|
+
case "FREQ":
|
|
114
|
+
if (!["DAILY", "WEEKLY", "MONTHLY", "YEARLY"].includes(v.toUpperCase()))
|
|
115
|
+
throw new RangeError(`unsupported FREQ ${v}`);
|
|
116
|
+
r.freq = v.toLowerCase();
|
|
117
|
+
freq = true;
|
|
118
|
+
break;
|
|
119
|
+
case "INTERVAL":
|
|
120
|
+
r.interval = Number(v);
|
|
121
|
+
break;
|
|
122
|
+
case "COUNT":
|
|
123
|
+
r.count = Number(v);
|
|
124
|
+
break;
|
|
125
|
+
case "UNTIL": {
|
|
126
|
+
const m = /^(\d{4})(\d{2})(\d{2})(T\d{6}Z?)?$/.exec(v);
|
|
127
|
+
if (!m)
|
|
128
|
+
throw new RangeError(`bad UNTIL ${v}`);
|
|
129
|
+
r.until = `${m[1]}-${m[2]}-${m[3]}`;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case "BYDAY":
|
|
133
|
+
r.byDay = list().map((x) => x.toUpperCase());
|
|
134
|
+
break;
|
|
135
|
+
case "BYMONTHDAY":
|
|
136
|
+
r.byMonthDay = nums();
|
|
137
|
+
break;
|
|
138
|
+
case "BYMONTH":
|
|
139
|
+
r.byMonth = nums();
|
|
140
|
+
break;
|
|
141
|
+
case "WKST":
|
|
142
|
+
if (v.toUpperCase() !== "MO")
|
|
143
|
+
throw new RangeError("only WKST=MO");
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
throw new RangeError(`unsupported rule part ${k}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (!freq)
|
|
150
|
+
throw new RangeError("a rule needs FREQ");
|
|
151
|
+
checkRecurrence(r);
|
|
152
|
+
return r;
|
|
153
|
+
}
|
|
154
|
+
function checkRecurrence(r) {
|
|
155
|
+
if (!["daily", "weekly", "monthly", "yearly"].includes(r.freq))
|
|
156
|
+
throw new RangeError("freq is daily, weekly, monthly or yearly");
|
|
157
|
+
if (r.interval !== undefined && (!Number.isInteger(r.interval) || r.interval < 1 || r.interval > 1000))
|
|
158
|
+
throw new RangeError("interval is 1 to 1000");
|
|
159
|
+
if (r.count !== undefined && (!Number.isInteger(r.count) || r.count < 1 || r.count > 10_000))
|
|
160
|
+
throw new RangeError("count is 1 to 10000");
|
|
161
|
+
if (r.until !== undefined && !validDate(r.until))
|
|
162
|
+
throw new RangeError("until is a YYYY-MM-DD date");
|
|
163
|
+
for (const d of r.byDay ?? []) {
|
|
164
|
+
const m = BYDAY.exec(d);
|
|
165
|
+
if (!m)
|
|
166
|
+
throw new RangeError(`bad weekday ${d}`);
|
|
167
|
+
if (m[1] && (r.freq === "daily" || r.freq === "weekly"))
|
|
168
|
+
throw new RangeError(`${d}: ordinals only for monthly and yearly`);
|
|
169
|
+
}
|
|
170
|
+
for (const d of r.byMonthDay ?? [])
|
|
171
|
+
if (!Number.isInteger(d) || d === 0 || d < -31 || d > 31)
|
|
172
|
+
throw new RangeError(`bad month day ${d}`);
|
|
173
|
+
for (const m of r.byMonth ?? [])
|
|
174
|
+
if (!Number.isInteger(m) || m < 1 || m > 12)
|
|
175
|
+
throw new RangeError(`bad month ${m}`);
|
|
176
|
+
for (const d of r.except ?? [])
|
|
177
|
+
if (!validDate(d))
|
|
178
|
+
throw new RangeError(`bad exception date ${d}`);
|
|
179
|
+
}
|
|
180
|
+
function monthDays(y, m, r, firstDay) {
|
|
181
|
+
const start = dayNumber(y, m, 1);
|
|
182
|
+
const length = dayNumber(m === 12 ? y + 1 : y, m === 12 ? 1 : m + 1, 1) - start;
|
|
183
|
+
const out = new Set();
|
|
184
|
+
for (const md of r.byMonthDay ?? []) {
|
|
185
|
+
const d = md > 0 ? md : length + 1 + md;
|
|
186
|
+
if (d >= 1 && d <= length)
|
|
187
|
+
out.add(start + d - 1);
|
|
188
|
+
}
|
|
189
|
+
for (const spec of r.byDay ?? []) {
|
|
190
|
+
const [, ord, wd] = BYDAY.exec(spec);
|
|
191
|
+
const want = WEEKDAYS.indexOf(wd);
|
|
192
|
+
const all = [];
|
|
193
|
+
for (let i = 0; i < length; i++)
|
|
194
|
+
if (weekdayOf(start + i) === want)
|
|
195
|
+
all.push(start + i);
|
|
196
|
+
if (!ord)
|
|
197
|
+
all.forEach((x) => out.add(x));
|
|
198
|
+
else {
|
|
199
|
+
const n = Number(ord);
|
|
200
|
+
const pick = n > 0 ? all[n - 1] : all[all.length + n];
|
|
201
|
+
if (pick !== undefined)
|
|
202
|
+
out.add(pick);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (!r.byMonthDay?.length && !r.byDay?.length) {
|
|
206
|
+
const d = fromDayNumber(firstDay).day;
|
|
207
|
+
if (d <= length)
|
|
208
|
+
out.add(start + d - 1);
|
|
209
|
+
}
|
|
210
|
+
return [...out].sort((a, b) => a - b);
|
|
211
|
+
}
|
|
212
|
+
export function occurrences(e, from, to, max = 1000) {
|
|
213
|
+
if (!isTimeZone(e.timeZone))
|
|
214
|
+
throw new RangeError(`unknown time zone ${String(e.timeZone)}`);
|
|
215
|
+
if (!validDate(e.date) || !TIME.exec(e.time) || e.time === "24:00")
|
|
216
|
+
throw new RangeError("date is YYYY-MM-DD and time HH:MM");
|
|
217
|
+
if (!Number.isInteger(e.duration) || e.duration < 0 || e.duration > 366 * 24 * 60)
|
|
218
|
+
throw new RangeError("duration is whole minutes");
|
|
219
|
+
const out = [];
|
|
220
|
+
const at = (day) => {
|
|
221
|
+
const date = dateOf(day);
|
|
222
|
+
const start = zonedTime(e.timeZone, date, e.time);
|
|
223
|
+
return { start, end: start + e.duration * MINUTE, date };
|
|
224
|
+
};
|
|
225
|
+
const first = dayOfDate(e.date);
|
|
226
|
+
const r = e.repeat;
|
|
227
|
+
if (!r) {
|
|
228
|
+
const o = at(first);
|
|
229
|
+
return o.start >= from && o.start < to && max > 0 ? [o] : [];
|
|
230
|
+
}
|
|
231
|
+
checkRecurrence(r);
|
|
232
|
+
const interval = r.interval ?? 1;
|
|
233
|
+
const until = r.until ? dayOfDate(r.until) : Infinity;
|
|
234
|
+
const except = new Set(r.except ?? []);
|
|
235
|
+
const fromDay = Math.floor(from / DAY) - 2;
|
|
236
|
+
const toDay = Math.floor(to / DAY) + 2;
|
|
237
|
+
let seen = 0;
|
|
238
|
+
const take = (day) => {
|
|
239
|
+
if (day < first)
|
|
240
|
+
return true;
|
|
241
|
+
if (day > until || day > toDay)
|
|
242
|
+
return false;
|
|
243
|
+
seen++;
|
|
244
|
+
if (r.count !== undefined && seen > r.count)
|
|
245
|
+
return false;
|
|
246
|
+
if (day >= fromDay && !except.has(dateOf(day))) {
|
|
247
|
+
const o = at(day);
|
|
248
|
+
if (o.start >= from && o.start < to)
|
|
249
|
+
out.push(o);
|
|
250
|
+
}
|
|
251
|
+
return out.length < max;
|
|
252
|
+
};
|
|
253
|
+
const f = fromDayNumber(first);
|
|
254
|
+
for (let period = 0; period < 200_000; period++) {
|
|
255
|
+
let days;
|
|
256
|
+
if (r.freq === "daily") {
|
|
257
|
+
const day = first + period * interval;
|
|
258
|
+
if (r.count === undefined && day < fromDay - interval) {
|
|
259
|
+
period = Math.max(period, Math.floor((fromDay - first) / interval) - 1);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const wd = WEEKDAYS[weekdayOf(day)];
|
|
263
|
+
const { month, day: md } = fromDayNumber(day);
|
|
264
|
+
const ok = (!r.byDay?.length || r.byDay.includes(wd)) &&
|
|
265
|
+
(!r.byMonth?.length || r.byMonth.includes(month)) &&
|
|
266
|
+
(!r.byMonthDay?.length || r.byMonthDay.some((x) => x === md));
|
|
267
|
+
days = ok ? [day] : [];
|
|
268
|
+
if (day > until || day > toDay)
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
else if (r.freq === "weekly") {
|
|
272
|
+
const monday = first - weekdayOf(first) + period * 7 * interval;
|
|
273
|
+
if (monday > until || monday > toDay)
|
|
274
|
+
break;
|
|
275
|
+
if (r.count === undefined && monday + 7 < fromDay)
|
|
276
|
+
continue;
|
|
277
|
+
const picks = r.byDay?.length ? r.byDay.map((d) => WEEKDAYS.indexOf(d)) : [weekdayOf(first)];
|
|
278
|
+
days = [...new Set(picks)].sort((a, b) => a - b).map((i) => monday + i);
|
|
279
|
+
}
|
|
280
|
+
else if (r.freq === "monthly") {
|
|
281
|
+
const index = f.year * 12 + (f.month - 1) + period * interval;
|
|
282
|
+
const y = Math.floor(index / 12);
|
|
283
|
+
const m = (index % 12) + 1;
|
|
284
|
+
if (dayNumber(y, m, 1) > until || dayNumber(y, m, 1) > toDay)
|
|
285
|
+
break;
|
|
286
|
+
if (r.count === undefined && dayNumber(y, m, 1) + 31 < fromDay)
|
|
287
|
+
continue;
|
|
288
|
+
days = r.byMonth?.length && !r.byMonth.includes(m) ? [] : monthDays(y, m, r, first);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const y = f.year + period * interval;
|
|
292
|
+
if (dayNumber(y, 1, 1) > until || dayNumber(y, 1, 1) > toDay)
|
|
293
|
+
break;
|
|
294
|
+
if (r.count === undefined && dayNumber(y, 12, 31) < fromDay)
|
|
295
|
+
continue;
|
|
296
|
+
days = [];
|
|
297
|
+
for (const m of r.byMonth?.length ? [...r.byMonth].sort((a, b) => a - b) : [f.month]) {
|
|
298
|
+
const picks = monthDays(y, m, r.byDay?.length || r.byMonthDay?.length ? r : { ...r, byMonthDay: [f.day] }, first);
|
|
299
|
+
days.push(...picks);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const day of days)
|
|
303
|
+
if (!take(day))
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
}
|
|
308
|
+
const DAY_KEYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
309
|
+
function checkRanges(r, where) {
|
|
310
|
+
if (!Array.isArray(r))
|
|
311
|
+
throw new RangeError(`${where}: hours are [from, to] pairs`);
|
|
312
|
+
for (const p of r) {
|
|
313
|
+
if (!Array.isArray(p) || p.length !== 2 || !TIME.test(p[0]) || !TIME.test(p[1]) || minutesOf(p[0]) >= minutesOf(p[1])) {
|
|
314
|
+
throw new RangeError(`${where}: ${JSON.stringify(p)} is not a [from, to] pair of HH:MM`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return r;
|
|
318
|
+
}
|
|
319
|
+
export function checkAvailability(a) {
|
|
320
|
+
if (!isTimeZone(a.timeZone))
|
|
321
|
+
throw new RangeError(`unknown time zone ${String(a.timeZone)}`);
|
|
322
|
+
for (const k of Object.keys(a.hours ?? {})) {
|
|
323
|
+
if (!DAY_KEYS.includes(k))
|
|
324
|
+
throw new RangeError(`hours: ${k} is not a weekday (mon..sun)`);
|
|
325
|
+
checkRanges(a.hours[k], `hours.${k}`);
|
|
326
|
+
}
|
|
327
|
+
for (const [d, r] of Object.entries(a.dates ?? {})) {
|
|
328
|
+
if (!validDate(d))
|
|
329
|
+
throw new RangeError(`dates: ${d} is not YYYY-MM-DD`);
|
|
330
|
+
checkRanges(r, `dates.${d}`);
|
|
331
|
+
}
|
|
332
|
+
const whole = (v, dflt, lo, hi, what) => {
|
|
333
|
+
const x = v ?? dflt;
|
|
334
|
+
if (!Number.isInteger(x) || x < lo || x > hi)
|
|
335
|
+
throw new RangeError(`${what} is ${lo} to ${hi}`);
|
|
336
|
+
return x;
|
|
337
|
+
};
|
|
338
|
+
const duration = whole(a.duration, 0, 1, 24 * 60, "duration");
|
|
339
|
+
return {
|
|
340
|
+
timeZone: a.timeZone,
|
|
341
|
+
hours: a.hours ?? {},
|
|
342
|
+
dates: a.dates,
|
|
343
|
+
duration,
|
|
344
|
+
step: whole(a.step, duration, 1, 24 * 60, "step"),
|
|
345
|
+
bufferBefore: whole(a.bufferBefore, 0, 0, 24 * 60, "bufferBefore"),
|
|
346
|
+
bufferAfter: whole(a.bufferAfter, 0, 0, 24 * 60, "bufferAfter"),
|
|
347
|
+
minNotice: whole(a.minNotice, 0, 0, 366 * 24 * 60, "minNotice"),
|
|
348
|
+
maxDaysAhead: whole(a.maxDaysAhead, 90, 1, 3660, "maxDaysAhead"),
|
|
349
|
+
capacity: whole(a.capacity, 1, 1, 10_000, "capacity"),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
export function availableSlots(a, o) {
|
|
353
|
+
const c = checkAvailability(a);
|
|
354
|
+
const max = o.max ?? 2000;
|
|
355
|
+
const earliest = o.now + c.minNotice * MINUTE;
|
|
356
|
+
const latest = o.now + c.maxDaysAhead * DAY;
|
|
357
|
+
const from = Math.max(o.from, earliest);
|
|
358
|
+
const to = Math.min(o.to, latest);
|
|
359
|
+
const out = [];
|
|
360
|
+
if (from >= to)
|
|
361
|
+
return out;
|
|
362
|
+
const busy = [...(o.busy ?? [])].sort((x, y) => x.start - y.start);
|
|
363
|
+
const firstDay = dayOfDate(wallTime(from, c.timeZone).date);
|
|
364
|
+
const lastDay = dayOfDate(wallTime(to, c.timeZone).date);
|
|
365
|
+
for (let day = firstDay; day <= lastDay && out.length < max; day++) {
|
|
366
|
+
const date = dateOf(day);
|
|
367
|
+
const ranges = c.dates && Object.hasOwn(c.dates, date) ? c.dates[date] : (c.hours[DAY_KEYS[weekdayOf(day)]] ?? []);
|
|
368
|
+
for (const [a1, b1] of ranges) {
|
|
369
|
+
for (let m = minutesOf(a1); m + c.duration <= minutesOf(b1); m += c.step) {
|
|
370
|
+
const wall = (mins) => mins >= 24 * 60 ? zonedTime(c.timeZone, dateOf(day + 1), `${pad(Math.floor((mins - 1440) / 60))}:${pad(mins % 60)}`) : zonedTime(c.timeZone, date, `${pad(Math.floor(mins / 60))}:${pad(mins % 60)}`);
|
|
371
|
+
const start = wall(m);
|
|
372
|
+
const end = start + c.duration * MINUTE;
|
|
373
|
+
if (start < from || start >= to)
|
|
374
|
+
continue;
|
|
375
|
+
if (out.length && out.at(-1).start >= start)
|
|
376
|
+
continue;
|
|
377
|
+
const blockStart = start - c.bufferBefore * MINUTE;
|
|
378
|
+
const blockEnd = end + c.bufferAfter * MINUTE;
|
|
379
|
+
let overlapping = 0;
|
|
380
|
+
for (const b of busy) {
|
|
381
|
+
if (b.start >= blockEnd)
|
|
382
|
+
break;
|
|
383
|
+
if (b.end > blockStart)
|
|
384
|
+
overlapping++;
|
|
385
|
+
}
|
|
386
|
+
if (overlapping >= c.capacity)
|
|
387
|
+
continue;
|
|
388
|
+
out.push({ start, end });
|
|
389
|
+
if (out.length >= max)
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return out;
|
|
395
|
+
}
|
|
396
|
+
const userId = (ctx) => {
|
|
397
|
+
const u = ctx?.user;
|
|
398
|
+
return u && (typeof u.id === "string" || typeof u.id === "number") ? String(u.id) : null;
|
|
399
|
+
};
|
|
400
|
+
export function defineBookings(o) {
|
|
401
|
+
if (!/^[A-Za-z][A-Za-z0-9]{0,40}$/.test(o.name))
|
|
402
|
+
throw new Error("defineBookings: name is letters and digits");
|
|
403
|
+
for (const m of o.reminders?.before ?? []) {
|
|
404
|
+
if (!Number.isInteger(m) || m < 1 || m > 60 * 24 * 60)
|
|
405
|
+
throw new Error("defineBookings: reminders are 1 minute to 60 days before");
|
|
406
|
+
}
|
|
407
|
+
const now = o.now ?? Date.now;
|
|
408
|
+
const changeUntil = o.changeUntil ?? 0;
|
|
409
|
+
let jobs = null;
|
|
410
|
+
const store = o.store;
|
|
411
|
+
const job = defineJob(`${o.name}Reminder`, [
|
|
412
|
+
{ name: "id", type: "string" },
|
|
413
|
+
{ name: "minutes", type: "number" },
|
|
414
|
+
], async (jctx, id, minutes) => {
|
|
415
|
+
const b = await store.get(id);
|
|
416
|
+
if (!b || b.status !== "active" || b.start - minutes * MINUTE > now() + MINUTE)
|
|
417
|
+
return;
|
|
418
|
+
if (userId({ user: jctx.user }) !== b.owner)
|
|
419
|
+
return;
|
|
420
|
+
await o.reminders?.send(jctx, b, minutes);
|
|
421
|
+
}, { attempts: 3 });
|
|
422
|
+
const remind = async (ctx, b) => {
|
|
423
|
+
if (!o.reminders || !jobs)
|
|
424
|
+
return [];
|
|
425
|
+
const ids = [];
|
|
426
|
+
for (const m of o.reminders.before) {
|
|
427
|
+
const at = b.start - m * MINUTE;
|
|
428
|
+
if (at <= now())
|
|
429
|
+
continue;
|
|
430
|
+
const id = await jobs.enqueue(ctx, job, { id: b.id, minutes: m }, { runAt: at, as: { user: b.owner } });
|
|
431
|
+
if (id)
|
|
432
|
+
ids.push(id);
|
|
433
|
+
}
|
|
434
|
+
return ids;
|
|
435
|
+
};
|
|
436
|
+
const unremind = async (b) => {
|
|
437
|
+
for (const id of b.reminders)
|
|
438
|
+
await jobs?.cancel(id).catch(() => false);
|
|
439
|
+
};
|
|
440
|
+
const scheduleOf = async (ctx, resource) => {
|
|
441
|
+
if (typeof resource !== "string" || resource === "" || resource.length > 200)
|
|
442
|
+
return null;
|
|
443
|
+
const a = await o.resource(resource, ctx);
|
|
444
|
+
return a ? checkAvailability(a) : null;
|
|
445
|
+
};
|
|
446
|
+
const isSlot = (a, start) => availableSlots(a, { from: start, to: start + 1, now: now() }).some((s) => s.start === start);
|
|
447
|
+
const mayManage = async (ctx, resource) => (o.manage ? await o.manage(ctx, resource) : false);
|
|
448
|
+
const visible = async (ctx, id) => {
|
|
449
|
+
if (typeof id !== "string" || !/^[A-Za-z0-9_-]{1,80}$/.test(id))
|
|
450
|
+
return null;
|
|
451
|
+
const b = await store.get(id);
|
|
452
|
+
if (!b)
|
|
453
|
+
return null;
|
|
454
|
+
const me = userId(ctx);
|
|
455
|
+
if (me !== null && b.owner === me)
|
|
456
|
+
return { b, staff: false };
|
|
457
|
+
return (await mayManage(ctx, b.resource)) ? { b, staff: true } : null;
|
|
458
|
+
};
|
|
459
|
+
const self = {
|
|
460
|
+
name: o.name,
|
|
461
|
+
job,
|
|
462
|
+
use(j) {
|
|
463
|
+
jobs = j;
|
|
464
|
+
return this;
|
|
465
|
+
},
|
|
466
|
+
async slots(ctx, resource, range) {
|
|
467
|
+
const a = await scheduleOf(ctx, resource);
|
|
468
|
+
if (!a || !Number.isFinite(range.from) || !Number.isFinite(range.to))
|
|
469
|
+
return [];
|
|
470
|
+
const to = Math.min(range.to, range.from + 62 * DAY);
|
|
471
|
+
const busy = await store.overlapping(resource, range.from - DAY, to + DAY);
|
|
472
|
+
return availableSlots(a, { from: range.from, to, now: now(), busy: busy.map((b) => ({ start: b.blockStart, end: b.blockEnd })) });
|
|
473
|
+
},
|
|
474
|
+
async book(ctx, resource, start, opts = {}) {
|
|
475
|
+
const me = userId(ctx);
|
|
476
|
+
if (me === null)
|
|
477
|
+
return { ok: false, reason: "forbidden" };
|
|
478
|
+
const a = await scheduleOf(ctx, resource);
|
|
479
|
+
if (!a)
|
|
480
|
+
return { ok: false, reason: "not-found" };
|
|
481
|
+
if (o.allow && !(await o.allow(ctx, resource)))
|
|
482
|
+
return { ok: false, reason: "forbidden" };
|
|
483
|
+
const note = opts.note ?? "";
|
|
484
|
+
if (typeof note !== "string" || note.length > 2000)
|
|
485
|
+
return { ok: false, reason: "invalid" };
|
|
486
|
+
if (!Number.isInteger(start) || !isSlot(a, start))
|
|
487
|
+
return { ok: false, reason: "unavailable" };
|
|
488
|
+
const c = checkAvailability(a);
|
|
489
|
+
const end = start + c.duration * MINUTE;
|
|
490
|
+
const b = {
|
|
491
|
+
id: `${o.name.toLowerCase()}_${crypto.randomUUID().replace(/-/g, "")}`,
|
|
492
|
+
resource,
|
|
493
|
+
owner: me,
|
|
494
|
+
start,
|
|
495
|
+
end,
|
|
496
|
+
blockStart: start - c.bufferBefore * MINUTE,
|
|
497
|
+
blockEnd: end + c.bufferAfter * MINUTE,
|
|
498
|
+
status: "active",
|
|
499
|
+
note,
|
|
500
|
+
created: now(),
|
|
501
|
+
reminders: [],
|
|
502
|
+
};
|
|
503
|
+
if (!(await store.insert(b, c.capacity)))
|
|
504
|
+
return { ok: false, reason: "unavailable" };
|
|
505
|
+
const reminders = await remind(ctx, b);
|
|
506
|
+
if (reminders.length) {
|
|
507
|
+
b.reminders = reminders;
|
|
508
|
+
await store.update(b.id, { reminders });
|
|
509
|
+
}
|
|
510
|
+
return { ok: true, booking: b };
|
|
511
|
+
},
|
|
512
|
+
async cancel(ctx, id) {
|
|
513
|
+
const v = await visible(ctx, id);
|
|
514
|
+
if (!v)
|
|
515
|
+
return { ok: false, reason: "not-found" };
|
|
516
|
+
const { b, staff } = v;
|
|
517
|
+
if (b.status !== "active")
|
|
518
|
+
return { ok: true, booking: b };
|
|
519
|
+
if (!staff && b.start - changeUntil * MINUTE <= now())
|
|
520
|
+
return { ok: false, reason: "too-late" };
|
|
521
|
+
await store.update(b.id, { status: "cancelled", reminders: [] });
|
|
522
|
+
await unremind(b);
|
|
523
|
+
return { ok: true, booking: { ...b, status: "cancelled", reminders: [] } };
|
|
524
|
+
},
|
|
525
|
+
async reschedule(ctx, id, start) {
|
|
526
|
+
const v = await visible(ctx, id);
|
|
527
|
+
if (!v)
|
|
528
|
+
return { ok: false, reason: "not-found" };
|
|
529
|
+
const { b, staff } = v;
|
|
530
|
+
if (b.status !== "active")
|
|
531
|
+
return { ok: false, reason: "not-found" };
|
|
532
|
+
if (!staff && b.start - changeUntil * MINUTE <= now())
|
|
533
|
+
return { ok: false, reason: "too-late" };
|
|
534
|
+
const a = await scheduleOf(ctx, b.resource);
|
|
535
|
+
if (!a)
|
|
536
|
+
return { ok: false, reason: "not-found" };
|
|
537
|
+
if (!Number.isInteger(start) || !isSlot(a, start))
|
|
538
|
+
return { ok: false, reason: "unavailable" };
|
|
539
|
+
const c = checkAvailability(a);
|
|
540
|
+
const end = start + c.duration * MINUTE;
|
|
541
|
+
const to = { start, end, blockStart: start - c.bufferBefore * MINUTE, blockEnd: end + c.bufferAfter * MINUTE };
|
|
542
|
+
if (!(await store.move(b.id, to, c.capacity)))
|
|
543
|
+
return { ok: false, reason: "unavailable" };
|
|
544
|
+
await unremind(b);
|
|
545
|
+
const moved = { ...b, ...to, reminders: [] };
|
|
546
|
+
moved.reminders = await remind(ctx, moved);
|
|
547
|
+
await store.update(b.id, { reminders: moved.reminders });
|
|
548
|
+
return { ok: true, booking: moved };
|
|
549
|
+
},
|
|
550
|
+
async mine(ctx, range) {
|
|
551
|
+
const me = userId(ctx);
|
|
552
|
+
return me === null ? [] : await store.byOwner(me, range.from, range.to);
|
|
553
|
+
},
|
|
554
|
+
async forResource(ctx, resource, range) {
|
|
555
|
+
if (!(await mayManage(ctx, resource)))
|
|
556
|
+
return null;
|
|
557
|
+
return await store.overlapping(resource, range.from, range.to);
|
|
558
|
+
},
|
|
559
|
+
async get(ctx, id) {
|
|
560
|
+
return (await visible(ctx, id))?.b ?? null;
|
|
561
|
+
},
|
|
562
|
+
};
|
|
563
|
+
return self;
|
|
564
|
+
}
|
|
565
|
+
export class MemoryBookingStore {
|
|
566
|
+
rows = new Map();
|
|
567
|
+
count(resource, blockStart, blockEnd, except) {
|
|
568
|
+
let n = 0;
|
|
569
|
+
for (const b of this.rows.values()) {
|
|
570
|
+
if (b.resource === resource && b.status === "active" && b.id !== except && b.blockStart < blockEnd && b.blockEnd > blockStart)
|
|
571
|
+
n++;
|
|
572
|
+
}
|
|
573
|
+
return n;
|
|
574
|
+
}
|
|
575
|
+
insert(b, capacity) {
|
|
576
|
+
if (this.count(b.resource, b.blockStart, b.blockEnd) >= capacity)
|
|
577
|
+
return false;
|
|
578
|
+
this.rows.set(b.id, structuredClone(b));
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
get(id) {
|
|
582
|
+
const b = this.rows.get(id);
|
|
583
|
+
return b ? structuredClone(b) : null;
|
|
584
|
+
}
|
|
585
|
+
move(id, to, capacity) {
|
|
586
|
+
const b = this.rows.get(id);
|
|
587
|
+
if (!b || b.status !== "active" || this.count(b.resource, to.blockStart, to.blockEnd, id) >= capacity)
|
|
588
|
+
return false;
|
|
589
|
+
Object.assign(b, to);
|
|
590
|
+
return true;
|
|
591
|
+
}
|
|
592
|
+
update(id, patch) {
|
|
593
|
+
const b = this.rows.get(id);
|
|
594
|
+
if (b)
|
|
595
|
+
Object.assign(b, structuredClone(patch));
|
|
596
|
+
}
|
|
597
|
+
overlapping(resource, from, to) {
|
|
598
|
+
return [...this.rows.values()]
|
|
599
|
+
.filter((b) => b.resource === resource && b.status === "active" && b.blockStart < to && b.blockEnd > from)
|
|
600
|
+
.sort((x, y) => x.start - y.start)
|
|
601
|
+
.map((b) => structuredClone(b));
|
|
602
|
+
}
|
|
603
|
+
byOwner(owner, from, to) {
|
|
604
|
+
return [...this.rows.values()]
|
|
605
|
+
.filter((b) => b.owner === owner && b.end > from && b.start < to)
|
|
606
|
+
.sort((x, y) => x.start - y.start)
|
|
607
|
+
.map((b) => structuredClone(b));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
export class SqlBookingStore {
|
|
611
|
+
db;
|
|
612
|
+
t;
|
|
613
|
+
constructor(db, table = "weft_bookings") {
|
|
614
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table))
|
|
615
|
+
throw new Error(`SqlBookingStore: bad table name ${table}`);
|
|
616
|
+
this.db = db;
|
|
617
|
+
this.t = table;
|
|
618
|
+
db.exec(`CREATE TABLE IF NOT EXISTS ${table} (id TEXT PRIMARY KEY, resource TEXT NOT NULL, owner TEXT NOT NULL, ` +
|
|
619
|
+
`start INTEGER NOT NULL, "end" INTEGER NOT NULL, block_start INTEGER NOT NULL, block_end INTEGER NOT NULL, ` +
|
|
620
|
+
`status TEXT NOT NULL, note TEXT NOT NULL, created INTEGER NOT NULL, reminders TEXT NOT NULL)`);
|
|
621
|
+
db.exec(`CREATE INDEX IF NOT EXISTS ${table}_resource ON ${table} (resource, status, block_start)`);
|
|
622
|
+
db.exec(`CREATE INDEX IF NOT EXISTS ${table}_owner ON ${table} (owner, start)`);
|
|
623
|
+
}
|
|
624
|
+
row(r) {
|
|
625
|
+
const o = r;
|
|
626
|
+
return {
|
|
627
|
+
id: String(o.id),
|
|
628
|
+
resource: String(o.resource),
|
|
629
|
+
owner: String(o.owner),
|
|
630
|
+
start: Number(o.start),
|
|
631
|
+
end: Number(o.end),
|
|
632
|
+
blockStart: Number(o.block_start),
|
|
633
|
+
blockEnd: Number(o.block_end),
|
|
634
|
+
status: o.status === "active" ? "active" : "cancelled",
|
|
635
|
+
note: String(o.note),
|
|
636
|
+
created: Number(o.created),
|
|
637
|
+
reminders: (() => {
|
|
638
|
+
try {
|
|
639
|
+
const v = JSON.parse(String(o.reminders));
|
|
640
|
+
return Array.isArray(v) ? v.map(String) : [];
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
return [];
|
|
644
|
+
}
|
|
645
|
+
})(),
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
busyCount(except) {
|
|
649
|
+
return (`(SELECT COUNT(*) FROM ${this.t} WHERE resource = ? AND status = 'active' AND id <> '${except}' ` +
|
|
650
|
+
`AND block_start < ? AND block_end > ?)`);
|
|
651
|
+
}
|
|
652
|
+
insert(b, capacity) {
|
|
653
|
+
const rows = this.db
|
|
654
|
+
.prepare(`INSERT INTO ${this.t} (id, resource, owner, start, "end", block_start, block_end, status, note, created, reminders) ` +
|
|
655
|
+
`SELECT ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ? WHERE ${this.busyCount("")} < ? RETURNING id`)
|
|
656
|
+
.all(b.id, b.resource, b.owner, b.start, b.end, b.blockStart, b.blockEnd, b.note, b.created, JSON.stringify(b.reminders), b.resource, b.blockEnd, b.blockStart, capacity);
|
|
657
|
+
return rows.length > 0;
|
|
658
|
+
}
|
|
659
|
+
get(id) {
|
|
660
|
+
const r = this.db.prepare(`SELECT * FROM ${this.t} WHERE id = ?`).all(id)[0];
|
|
661
|
+
return r ? this.row(r) : null;
|
|
662
|
+
}
|
|
663
|
+
move(id, to, capacity) {
|
|
664
|
+
const rows = this.db
|
|
665
|
+
.prepare(`UPDATE ${this.t} SET start = ?, "end" = ?, block_start = ?, block_end = ? WHERE id = ? AND status = 'active' ` +
|
|
666
|
+
`AND (SELECT COUNT(*) FROM ${this.t} AS o WHERE o.resource = ${this.t}.resource AND o.status = 'active' AND o.id <> ? ` +
|
|
667
|
+
`AND o.block_start < ? AND o.block_end > ?) < ? RETURNING id`)
|
|
668
|
+
.all(to.start, to.end, to.blockStart, to.blockEnd, id, id, to.blockEnd, to.blockStart, capacity);
|
|
669
|
+
return rows.length > 0;
|
|
670
|
+
}
|
|
671
|
+
update(id, patch) {
|
|
672
|
+
if (patch.status !== undefined)
|
|
673
|
+
this.db.prepare(`UPDATE ${this.t} SET status = ? WHERE id = ?`).run(patch.status, id);
|
|
674
|
+
if (patch.reminders !== undefined)
|
|
675
|
+
this.db.prepare(`UPDATE ${this.t} SET reminders = ? WHERE id = ?`).run(JSON.stringify(patch.reminders), id);
|
|
676
|
+
}
|
|
677
|
+
overlapping(resource, from, to) {
|
|
678
|
+
return this.db
|
|
679
|
+
.prepare(`SELECT * FROM ${this.t} WHERE resource = ? AND status = 'active' AND block_start < ? AND block_end > ? ORDER BY start`)
|
|
680
|
+
.all(resource, to, from)
|
|
681
|
+
.map((r) => this.row(r));
|
|
682
|
+
}
|
|
683
|
+
byOwner(owner, from, to) {
|
|
684
|
+
return this.db
|
|
685
|
+
.prepare(`SELECT * FROM ${this.t} WHERE owner = ? AND "end" > ? AND start < ? ORDER BY start`)
|
|
686
|
+
.all(owner, from, to)
|
|
687
|
+
.map((r) => this.row(r));
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
export { DAY_KEYS as WEEKDAY_KEYS };
|