@talkpilot/core-db 1.3.6 → 1.3.8
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/.cursor/rules/development.mdc +65 -65
- package/DEVELOPMENT.md +141 -141
- package/README.md +243 -190
- package/README_OLD.md +160 -160
- package/dist/municipal/tickets/tickets.statistics.getters.d.ts +4 -0
- package/dist/municipal/tickets/tickets.statistics.getters.d.ts.map +1 -1
- package/dist/municipal/tickets/tickets.statistics.getters.js +30 -3
- package/dist/municipal/tickets/tickets.statistics.getters.js.map +1 -1
- package/dist/talkpilot/calls/calls.dashboard.d.ts +3 -0
- package/dist/talkpilot/calls/calls.dashboard.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.dashboard.js +191 -0
- package/dist/talkpilot/calls/calls.dashboard.js.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.getters.js +12 -15
- package/dist/talkpilot/calls/calls.statistics.getters.js.map +1 -1
- package/dist/talkpilot/calls/calls.statistics.types.d.ts +4 -1
- package/dist/talkpilot/calls/calls.statistics.types.d.ts.map +1 -1
- package/dist/talkpilot/flows/flows.schema.d.ts +3 -0
- package/dist/talkpilot/flows/flows.schema.d.ts.map +1 -1
- package/dist/talkpilot/flows/flows.schema.js +1 -0
- package/dist/talkpilot/flows/flows.schema.js.map +1 -1
- package/dist/talkpilot/flows/flows.types.d.ts +1 -0
- package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
- package/jest.config.js +20 -20
- package/package.json +46 -46
- package/src/connection.ts +54 -54
- package/src/index.ts +26 -26
- package/src/municipal/index.ts +22 -22
- package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +92 -92
- package/src/municipal/muniIssues/__tests__/muniIssues.setup.spec.ts +92 -92
- package/src/municipal/muniIssues/index.ts +17 -17
- package/src/municipal/muniIssues/muniIssues.constants.ts +18 -18
- package/src/municipal/muniIssues/muniIssues.getters.ts +24 -24
- package/src/municipal/muniIssues/muniIssues.schema.ts +66 -66
- package/src/municipal/muniIssues/muniIssues.setters.ts +24 -24
- package/src/municipal/muniIssues/muniIssues.setup.ts +65 -65
- package/src/municipal/muniIssues/muniIssues.types.ts +37 -37
- package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +30 -30
- package/src/municipal/tickets/__tests__/tickets.statistics.spec.ts +68 -51
- package/src/municipal/tickets/index.ts +3 -3
- package/src/municipal/tickets/tickets.constants.ts +8 -8
- package/src/municipal/tickets/tickets.getters.ts +121 -121
- package/src/municipal/tickets/tickets.statistics.aggregation.ts +96 -96
- package/src/municipal/tickets/tickets.statistics.getters.ts +109 -71
- package/src/municipal/tickets/tickets.types.ts +53 -53
- package/src/talkpilot/calls/__tests__/calls.dashboard.spec.ts +149 -149
- package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +351 -344
- package/src/talkpilot/calls/calls.constants.ts +20 -20
- package/src/talkpilot/calls/calls.statistics.getters.ts +587 -587
- package/src/talkpilot/calls/calls.statistics.types.ts +47 -44
- package/src/talkpilot/calls/calls.types.ts +119 -119
- package/src/talkpilot/calls/dashboard/calls.dashboard.ts +292 -292
- package/src/talkpilot/calls/dashboard/calls.dashboard.types.ts +57 -57
- package/src/talkpilot/calls/index.ts +6 -6
- package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +190 -190
- package/src/talkpilot/contextNotes/__tests__/contextNotes.getters.spec.ts +176 -176
- package/src/talkpilot/contextNotes/contextNotes.getters.ts +86 -86
- package/src/talkpilot/contextNotes/contextNotes.types.ts +20 -20
- package/src/talkpilot/contextNotes/index.ts +2 -2
- package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +96 -71
- package/src/talkpilot/flows/flows.schema.ts +154 -153
- package/src/talkpilot/flows/flows.types.ts +187 -186
- package/src/talkpilot/index.ts +31 -31
- package/src/talkpilot/products/__tests__/products.getters.spec.ts +45 -45
- package/src/talkpilot/products/index.ts +2 -2
- package/src/talkpilot/products/products.getters.ts +12 -12
- package/src/talkpilot/products/products.types.ts +9 -9
- package/src/test-utils/db-utils.ts +26 -26
- package/src/test-utils/factories/index.ts +15 -15
- package/src/test-utils/factories/municipal/muniIssues.ts +32 -32
- package/src/test-utils/factories/talkpilot/contextNotes.ts +33 -33
- package/src/test-utils/factories/websitalk/scans.ts +23 -23
- package/src/utils/date.utils.ts +116 -116
- package/src/websitalk/index.ts +15 -15
- package/src/websitalk/mongodb-client.ts +61 -61
- package/src/websitalk/scans/__tests__/scans.spec.ts +218 -218
- package/src/websitalk/scans/index.ts +2 -2
- package/src/websitalk/scans/scans.getters.ts +113 -113
- package/src/websitalk/scans/scans.types.ts +53 -53
- package/tsconfig.json +23 -23
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { Factory } from "fishery";
|
|
2
|
-
import { faker } from "@faker-js/faker";
|
|
3
|
-
import type { CreateMuniIssueInput } from "../../../municipal";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Test factory for `createMuniIssue` input.
|
|
7
|
-
* `jira.key` uses `sequence` so each build gets a unique key (the real key
|
|
8
|
-
* comes from Jira in production).
|
|
9
|
-
*/
|
|
10
|
-
export const muniIssueInputFactory = Factory.define<CreateMuniIssueInput>(
|
|
11
|
-
({ sequence }) => ({
|
|
12
|
-
callSid: `CA${faker.string.alphanumeric(10)}`,
|
|
13
|
-
clientId: faker.string.uuid(),
|
|
14
|
-
cityName: faker.location.city(),
|
|
15
|
-
issueContent: {
|
|
16
|
-
issuerName: faker.person.fullName(),
|
|
17
|
-
issuerRole: faker.person.jobTitle(),
|
|
18
|
-
issueDescription: faker.lorem.sentence(),
|
|
19
|
-
desiredBehavior: faker.lorem.sentence(),
|
|
20
|
-
},
|
|
21
|
-
jira: {
|
|
22
|
-
key: `CP-${sequence}`,
|
|
23
|
-
syncStatus: "created",
|
|
24
|
-
},
|
|
25
|
-
}),
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
export function createMuniIssueInput(
|
|
29
|
-
overrides?: Partial<CreateMuniIssueInput>,
|
|
30
|
-
): CreateMuniIssueInput {
|
|
31
|
-
return muniIssueInputFactory.build(overrides);
|
|
32
|
-
}
|
|
1
|
+
import { Factory } from "fishery";
|
|
2
|
+
import { faker } from "@faker-js/faker";
|
|
3
|
+
import type { CreateMuniIssueInput } from "../../../municipal";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Test factory for `createMuniIssue` input.
|
|
7
|
+
* `jira.key` uses `sequence` so each build gets a unique key (the real key
|
|
8
|
+
* comes from Jira in production).
|
|
9
|
+
*/
|
|
10
|
+
export const muniIssueInputFactory = Factory.define<CreateMuniIssueInput>(
|
|
11
|
+
({ sequence }) => ({
|
|
12
|
+
callSid: `CA${faker.string.alphanumeric(10)}`,
|
|
13
|
+
clientId: faker.string.uuid(),
|
|
14
|
+
cityName: faker.location.city(),
|
|
15
|
+
issueContent: {
|
|
16
|
+
issuerName: faker.person.fullName(),
|
|
17
|
+
issuerRole: faker.person.jobTitle(),
|
|
18
|
+
issueDescription: faker.lorem.sentence(),
|
|
19
|
+
desiredBehavior: faker.lorem.sentence(),
|
|
20
|
+
},
|
|
21
|
+
jira: {
|
|
22
|
+
key: `CP-${sequence}`,
|
|
23
|
+
syncStatus: "created",
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export function createMuniIssueInput(
|
|
29
|
+
overrides?: Partial<CreateMuniIssueInput>,
|
|
30
|
+
): CreateMuniIssueInput {
|
|
31
|
+
return muniIssueInputFactory.build(overrides);
|
|
32
|
+
}
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { Factory } from "fishery";
|
|
2
|
-
import { ObjectId } from "mongodb";
|
|
3
|
-
import { faker } from "@faker-js/faker";
|
|
4
|
-
import type { ContextNote, ContextNoteEntry } from "../../../talkpilot";
|
|
5
|
-
|
|
6
|
-
export const contextNoteEntryFactory = Factory.define<ContextNoteEntry>(() => ({
|
|
7
|
-
_id: new ObjectId(),
|
|
8
|
-
title: faker.lorem.words(3),
|
|
9
|
-
content: faker.lorem.sentences(2),
|
|
10
|
-
activeFrom: new Date(),
|
|
11
|
-
expiresAt: null,
|
|
12
|
-
createdAt: new Date(),
|
|
13
|
-
updatedAt: new Date(),
|
|
14
|
-
}));
|
|
15
|
-
|
|
16
|
-
export const contextNoteFactory = Factory.define<ContextNote & { _id: ObjectId }>(() => ({
|
|
17
|
-
_id: new ObjectId(),
|
|
18
|
-
clientId: faker.string.uuid(),
|
|
19
|
-
product: "Municipal",
|
|
20
|
-
notes: [],
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
export function createContextNoteEntry(
|
|
24
|
-
overrides?: Partial<ContextNoteEntry>,
|
|
25
|
-
): ContextNoteEntry {
|
|
26
|
-
return { ...contextNoteEntryFactory.build(), ...overrides } as ContextNoteEntry;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function createContextNote(
|
|
30
|
-
overrides?: Partial<ContextNote>,
|
|
31
|
-
): ContextNote & { _id: ObjectId } {
|
|
32
|
-
return { ...contextNoteFactory.build(), ...overrides } as ContextNote & { _id: ObjectId };
|
|
33
|
-
}
|
|
1
|
+
import { Factory } from "fishery";
|
|
2
|
+
import { ObjectId } from "mongodb";
|
|
3
|
+
import { faker } from "@faker-js/faker";
|
|
4
|
+
import type { ContextNote, ContextNoteEntry } from "../../../talkpilot";
|
|
5
|
+
|
|
6
|
+
export const contextNoteEntryFactory = Factory.define<ContextNoteEntry>(() => ({
|
|
7
|
+
_id: new ObjectId(),
|
|
8
|
+
title: faker.lorem.words(3),
|
|
9
|
+
content: faker.lorem.sentences(2),
|
|
10
|
+
activeFrom: new Date(),
|
|
11
|
+
expiresAt: null,
|
|
12
|
+
createdAt: new Date(),
|
|
13
|
+
updatedAt: new Date(),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
export const contextNoteFactory = Factory.define<ContextNote & { _id: ObjectId }>(() => ({
|
|
17
|
+
_id: new ObjectId(),
|
|
18
|
+
clientId: faker.string.uuid(),
|
|
19
|
+
product: "Municipal",
|
|
20
|
+
notes: [],
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
export function createContextNoteEntry(
|
|
24
|
+
overrides?: Partial<ContextNoteEntry>,
|
|
25
|
+
): ContextNoteEntry {
|
|
26
|
+
return { ...contextNoteEntryFactory.build(), ...overrides } as ContextNoteEntry;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createContextNote(
|
|
30
|
+
overrides?: Partial<ContextNote>,
|
|
31
|
+
): ContextNote & { _id: ObjectId } {
|
|
32
|
+
return { ...contextNoteFactory.build(), ...overrides } as ContextNote & { _id: ObjectId };
|
|
33
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { Factory } from "fishery";
|
|
2
|
-
import { faker } from "@faker-js/faker";
|
|
3
|
-
import type { Scan } from "../../../websitalk";
|
|
4
|
-
|
|
5
|
-
export const scanFactory = Factory.define<Scan>(() => ({
|
|
6
|
-
clientId: faker.string.uuid(),
|
|
7
|
-
baseUrl: faker.internet.url(),
|
|
8
|
-
status: "CHECKING",
|
|
9
|
-
activeTasksCount: 0,
|
|
10
|
-
pagesScanned: 0,
|
|
11
|
-
pagesSkipped: 0,
|
|
12
|
-
skipped: [],
|
|
13
|
-
siteChrome: {
|
|
14
|
-
url: faker.internet.url(),
|
|
15
|
-
sourceType: "site_chrome",
|
|
16
|
-
},
|
|
17
|
-
createdAt: new Date(),
|
|
18
|
-
updatedAt: new Date(),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
export function createScanFixture(overrides?: Partial<Scan>): Scan {
|
|
22
|
-
return scanFactory.build(overrides);
|
|
23
|
-
}
|
|
1
|
+
import { Factory } from "fishery";
|
|
2
|
+
import { faker } from "@faker-js/faker";
|
|
3
|
+
import type { Scan } from "../../../websitalk";
|
|
4
|
+
|
|
5
|
+
export const scanFactory = Factory.define<Scan>(() => ({
|
|
6
|
+
clientId: faker.string.uuid(),
|
|
7
|
+
baseUrl: faker.internet.url(),
|
|
8
|
+
status: "CHECKING",
|
|
9
|
+
activeTasksCount: 0,
|
|
10
|
+
pagesScanned: 0,
|
|
11
|
+
pagesSkipped: 0,
|
|
12
|
+
skipped: [],
|
|
13
|
+
siteChrome: {
|
|
14
|
+
url: faker.internet.url(),
|
|
15
|
+
sourceType: "site_chrome",
|
|
16
|
+
},
|
|
17
|
+
createdAt: new Date(),
|
|
18
|
+
updatedAt: new Date(),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
export function createScanFixture(overrides?: Partial<Scan>): Scan {
|
|
22
|
+
return scanFactory.build(overrides);
|
|
23
|
+
}
|
package/src/utils/date.utils.ts
CHANGED
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared timezone-aware date helpers for statistics aggregations.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/** Milliseconds in a single day. */
|
|
6
|
-
export const DAY_MS = 24 * 60 * 60 * 1000;
|
|
7
|
-
|
|
8
|
-
/** Maps a short weekday name (as produced by Intl) to its index (Sun = 0). */
|
|
9
|
-
export const WEEKDAY: Record<string, number> = {
|
|
10
|
-
Sun: 0,
|
|
11
|
-
Mon: 1,
|
|
12
|
-
Tue: 2,
|
|
13
|
-
Wed: 3,
|
|
14
|
-
Thu: 4,
|
|
15
|
-
Fri: 5,
|
|
16
|
-
Sat: 6,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/** Parses a `YYYY-MM-DD` string into `[year, month, day]` numbers. */
|
|
20
|
-
export const parseYmd = (dateStr: string): [number, number, number] => {
|
|
21
|
-
const [y, m, d] = dateStr.split("-").map(Number);
|
|
22
|
-
return [y, m, d];
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/** Builds a zero-padded `YYYY-MM-DD` string from numeric parts. */
|
|
26
|
-
export const ymdToStr = (y: number, m: number, d: number): string =>
|
|
27
|
-
`${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
28
|
-
|
|
29
|
-
/** Parses a `YYYY-MM-DD` string as midnight UTC. */
|
|
30
|
-
export const toUtcDate = (dateStr: string): Date =>
|
|
31
|
-
new Date(`${dateStr}T00:00:00.000Z`);
|
|
32
|
-
|
|
33
|
-
/** Formats a date as a `YYYY-MM-DD` string in UTC. */
|
|
34
|
-
export const formatDate = (date: Date): string => date.toISOString().slice(0, 10);
|
|
35
|
-
|
|
36
|
-
/** Adds `days` to a `YYYY-MM-DD` string, returning the resulting `YYYY-MM-DD`. */
|
|
37
|
-
export const addDaysToDateStr = (dateStr: string, days: number): string =>
|
|
38
|
-
formatDate(new Date(toUtcDate(dateStr).getTime() + days * DAY_MS));
|
|
39
|
-
|
|
40
|
-
/** Next civil calendar day (proleptic Gregorian). Avoids DST 24h steps that can stall iteration. */
|
|
41
|
-
export const incrementYmd = (
|
|
42
|
-
y: number,
|
|
43
|
-
m: number,
|
|
44
|
-
d: number,
|
|
45
|
-
): [number, number, number] => {
|
|
46
|
-
const next = new Date(Date.UTC(y, m - 1, d + 1));
|
|
47
|
-
return [next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate()];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Formats a date as the calendar day in the given timezone.
|
|
52
|
-
* `en-CA` yields ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable
|
|
53
|
-
* (string compare == chronological compare) and matches MongoDB's `%Y-%m-%d`.
|
|
54
|
-
*/
|
|
55
|
-
export const formatYmdInTz = (date: Date, timezone: string): string =>
|
|
56
|
-
new Intl.DateTimeFormat("en-CA", { timeZone: timezone }).format(date);
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns the calendar day in the given timezone as numeric parts.
|
|
60
|
-
* Reads parts by `type` (not string order), so `en-US` is fine here.
|
|
61
|
-
*/
|
|
62
|
-
export const getYmdInTz = (date: Date, timezone: string) => {
|
|
63
|
-
const parts = Object.fromEntries(
|
|
64
|
-
new Intl.DateTimeFormat("en-US", {
|
|
65
|
-
timeZone: timezone,
|
|
66
|
-
year: "numeric",
|
|
67
|
-
month: "numeric",
|
|
68
|
-
day: "numeric",
|
|
69
|
-
})
|
|
70
|
-
.formatToParts(date)
|
|
71
|
-
.map((p) => [p.type, Number(p.value)]),
|
|
72
|
-
);
|
|
73
|
-
return { y: parts.year, m: parts.month, d: parts.day };
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/** Weekday index (Sun = 0) of a date in the given timezone. */
|
|
77
|
-
export const getWeekdayInTz = (date: Date, timezone: string): number =>
|
|
78
|
-
WEEKDAY[
|
|
79
|
-
new Intl.DateTimeFormat("en-US", { timeZone: timezone, weekday: "short" }).format(
|
|
80
|
-
date,
|
|
81
|
-
)
|
|
82
|
-
] ?? 0;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Returns the given `YYYY-MM-DD` day at noon UTC — a stable anchor inside the
|
|
86
|
-
* calendar day, far from midnight so timezone/DST shifts can't cross a date boundary.
|
|
87
|
-
*/
|
|
88
|
-
export const dateAtNoonUtc = (dateStr: string): Date => {
|
|
89
|
-
const [y, m, d] = parseYmd(dateStr);
|
|
90
|
-
return new Date(Date.UTC(y, m - 1, d, 12, 0, 0));
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* UTC instant at the start of the given `YYYY-MM-DD` calendar day in `timezone`.
|
|
95
|
-
* Binary-searches the boundary so it stays correct across DST transitions.
|
|
96
|
-
*/
|
|
97
|
-
export const startOfCalendarDayInTz = (dateStr: string, timezone: string): Date => {
|
|
98
|
-
const anchor = dateAtNoonUtc(dateStr).getTime();
|
|
99
|
-
let low = anchor - 2 * DAY_MS;
|
|
100
|
-
let high = anchor + DAY_MS;
|
|
101
|
-
|
|
102
|
-
while (high - low > 1 /* ms */) {
|
|
103
|
-
const mid = Math.floor((low + high) / 2);
|
|
104
|
-
if (formatYmdInTz(new Date(mid), timezone) >= dateStr) high = mid;
|
|
105
|
-
else low = mid;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return new Date(high);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/** UTC instant at the last millisecond of the given calendar day in `timezone`. */
|
|
112
|
-
export const endOfCalendarDayInTz = (dateStr: string, timezone: string): Date => {
|
|
113
|
-
const [y, m, d] = parseYmd(dateStr);
|
|
114
|
-
const nextDay = new Date(Date.UTC(y, m - 1, d + 1)).toISOString().slice(0, 10);
|
|
115
|
-
return new Date(startOfCalendarDayInTz(nextDay, timezone).getTime() - 1);
|
|
116
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Shared timezone-aware date helpers for statistics aggregations.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Milliseconds in a single day. */
|
|
6
|
+
export const DAY_MS = 24 * 60 * 60 * 1000;
|
|
7
|
+
|
|
8
|
+
/** Maps a short weekday name (as produced by Intl) to its index (Sun = 0). */
|
|
9
|
+
export const WEEKDAY: Record<string, number> = {
|
|
10
|
+
Sun: 0,
|
|
11
|
+
Mon: 1,
|
|
12
|
+
Tue: 2,
|
|
13
|
+
Wed: 3,
|
|
14
|
+
Thu: 4,
|
|
15
|
+
Fri: 5,
|
|
16
|
+
Sat: 6,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Parses a `YYYY-MM-DD` string into `[year, month, day]` numbers. */
|
|
20
|
+
export const parseYmd = (dateStr: string): [number, number, number] => {
|
|
21
|
+
const [y, m, d] = dateStr.split("-").map(Number);
|
|
22
|
+
return [y, m, d];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Builds a zero-padded `YYYY-MM-DD` string from numeric parts. */
|
|
26
|
+
export const ymdToStr = (y: number, m: number, d: number): string =>
|
|
27
|
+
`${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
28
|
+
|
|
29
|
+
/** Parses a `YYYY-MM-DD` string as midnight UTC. */
|
|
30
|
+
export const toUtcDate = (dateStr: string): Date =>
|
|
31
|
+
new Date(`${dateStr}T00:00:00.000Z`);
|
|
32
|
+
|
|
33
|
+
/** Formats a date as a `YYYY-MM-DD` string in UTC. */
|
|
34
|
+
export const formatDate = (date: Date): string => date.toISOString().slice(0, 10);
|
|
35
|
+
|
|
36
|
+
/** Adds `days` to a `YYYY-MM-DD` string, returning the resulting `YYYY-MM-DD`. */
|
|
37
|
+
export const addDaysToDateStr = (dateStr: string, days: number): string =>
|
|
38
|
+
formatDate(new Date(toUtcDate(dateStr).getTime() + days * DAY_MS));
|
|
39
|
+
|
|
40
|
+
/** Next civil calendar day (proleptic Gregorian). Avoids DST 24h steps that can stall iteration. */
|
|
41
|
+
export const incrementYmd = (
|
|
42
|
+
y: number,
|
|
43
|
+
m: number,
|
|
44
|
+
d: number,
|
|
45
|
+
): [number, number, number] => {
|
|
46
|
+
const next = new Date(Date.UTC(y, m - 1, d + 1));
|
|
47
|
+
return [next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate()];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Formats a date as the calendar day in the given timezone.
|
|
52
|
+
* `en-CA` yields ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable
|
|
53
|
+
* (string compare == chronological compare) and matches MongoDB's `%Y-%m-%d`.
|
|
54
|
+
*/
|
|
55
|
+
export const formatYmdInTz = (date: Date, timezone: string): string =>
|
|
56
|
+
new Intl.DateTimeFormat("en-CA", { timeZone: timezone }).format(date);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns the calendar day in the given timezone as numeric parts.
|
|
60
|
+
* Reads parts by `type` (not string order), so `en-US` is fine here.
|
|
61
|
+
*/
|
|
62
|
+
export const getYmdInTz = (date: Date, timezone: string) => {
|
|
63
|
+
const parts = Object.fromEntries(
|
|
64
|
+
new Intl.DateTimeFormat("en-US", {
|
|
65
|
+
timeZone: timezone,
|
|
66
|
+
year: "numeric",
|
|
67
|
+
month: "numeric",
|
|
68
|
+
day: "numeric",
|
|
69
|
+
})
|
|
70
|
+
.formatToParts(date)
|
|
71
|
+
.map((p) => [p.type, Number(p.value)]),
|
|
72
|
+
);
|
|
73
|
+
return { y: parts.year, m: parts.month, d: parts.day };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Weekday index (Sun = 0) of a date in the given timezone. */
|
|
77
|
+
export const getWeekdayInTz = (date: Date, timezone: string): number =>
|
|
78
|
+
WEEKDAY[
|
|
79
|
+
new Intl.DateTimeFormat("en-US", { timeZone: timezone, weekday: "short" }).format(
|
|
80
|
+
date,
|
|
81
|
+
)
|
|
82
|
+
] ?? 0;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Returns the given `YYYY-MM-DD` day at noon UTC — a stable anchor inside the
|
|
86
|
+
* calendar day, far from midnight so timezone/DST shifts can't cross a date boundary.
|
|
87
|
+
*/
|
|
88
|
+
export const dateAtNoonUtc = (dateStr: string): Date => {
|
|
89
|
+
const [y, m, d] = parseYmd(dateStr);
|
|
90
|
+
return new Date(Date.UTC(y, m - 1, d, 12, 0, 0));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* UTC instant at the start of the given `YYYY-MM-DD` calendar day in `timezone`.
|
|
95
|
+
* Binary-searches the boundary so it stays correct across DST transitions.
|
|
96
|
+
*/
|
|
97
|
+
export const startOfCalendarDayInTz = (dateStr: string, timezone: string): Date => {
|
|
98
|
+
const anchor = dateAtNoonUtc(dateStr).getTime();
|
|
99
|
+
let low = anchor - 2 * DAY_MS;
|
|
100
|
+
let high = anchor + DAY_MS;
|
|
101
|
+
|
|
102
|
+
while (high - low > 1 /* ms */) {
|
|
103
|
+
const mid = Math.floor((low + high) / 2);
|
|
104
|
+
if (formatYmdInTz(new Date(mid), timezone) >= dateStr) high = mid;
|
|
105
|
+
else low = mid;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return new Date(high);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/** UTC instant at the last millisecond of the given calendar day in `timezone`. */
|
|
112
|
+
export const endOfCalendarDayInTz = (dateStr: string, timezone: string): Date => {
|
|
113
|
+
const [y, m, d] = parseYmd(dateStr);
|
|
114
|
+
const nextDay = new Date(Date.UTC(y, m - 1, d + 1)).toISOString().slice(0, 10);
|
|
115
|
+
return new Date(startOfCalendarDayInTz(nextDay, timezone).getTime() - 1);
|
|
116
|
+
};
|
package/src/websitalk/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { Db, ObjectId as MongoObjectId } from "mongodb";
|
|
2
|
-
|
|
3
|
-
export * from "./scans";
|
|
4
|
-
export { websitalkMongodbClient } from "./mongodb-client";
|
|
5
|
-
|
|
6
|
-
let db: Db;
|
|
7
|
-
export const setDb = (d: Db) => {
|
|
8
|
-
db = d;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export const getDb = (): Db => {
|
|
12
|
-
if (!db) throw new Error("Website Talk DB not initialised");
|
|
13
|
-
return db;
|
|
14
|
-
};
|
|
15
|
-
export const ObjectId = MongoObjectId;
|
|
1
|
+
import { Db, ObjectId as MongoObjectId } from "mongodb";
|
|
2
|
+
|
|
3
|
+
export * from "./scans";
|
|
4
|
+
export { websitalkMongodbClient } from "./mongodb-client";
|
|
5
|
+
|
|
6
|
+
let db: Db;
|
|
7
|
+
export const setDb = (d: Db) => {
|
|
8
|
+
db = d;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const getDb = (): Db => {
|
|
12
|
+
if (!db) throw new Error("Website Talk DB not initialised");
|
|
13
|
+
return db;
|
|
14
|
+
};
|
|
15
|
+
export const ObjectId = MongoObjectId;
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { MongoClient, Db } from "mongodb";
|
|
2
|
-
import { setDb } from "./index";
|
|
3
|
-
import { validateConfig, validateMongoUri } from "../utils/validation";
|
|
4
|
-
|
|
5
|
-
class WebsitalkMongoDBClient {
|
|
6
|
-
private client: MongoClient | null = null;
|
|
7
|
-
private db: Db | null = null;
|
|
8
|
-
private readonly defaultDbName = "website-talk";
|
|
9
|
-
|
|
10
|
-
async connect(uri?: string, dbName?: string): Promise<void> {
|
|
11
|
-
if (this.client) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const mongodbUri = uri || process.env.MONGO_URI || process.env.MONGODB_URI;
|
|
16
|
-
validateConfig("MONGO_URI", mongodbUri);
|
|
17
|
-
validateMongoUri(mongodbUri!);
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
this.client = new MongoClient(mongodbUri!);
|
|
21
|
-
await this.client.connect();
|
|
22
|
-
const targetDbName =
|
|
23
|
-
dbName || process.env.WEBSITALK_DB_NAME || this.defaultDbName;
|
|
24
|
-
this.db = this.client.db(targetDbName);
|
|
25
|
-
setDb(this.db);
|
|
26
|
-
console.info(`[core-db] Website Talk MongoDB connected: ${targetDbName}`);
|
|
27
|
-
} catch (error) {
|
|
28
|
-
console.error("[core-db] Website Talk connection failed", error);
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async disconnect(): Promise<void> {
|
|
34
|
-
if (this.client) {
|
|
35
|
-
try {
|
|
36
|
-
await this.client.close();
|
|
37
|
-
this.client = null;
|
|
38
|
-
this.db = null;
|
|
39
|
-
console.info("Website Talk MongoDB disconnected successfully");
|
|
40
|
-
} catch (error) {
|
|
41
|
-
console.error("[core-db] Website Talk disconnection failed", error);
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
getDb(): Db {
|
|
48
|
-
if (!this.db) {
|
|
49
|
-
throw new Error(
|
|
50
|
-
"Website Talk database not initialized. Call connect() first.",
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
return this.db;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
isConnected(): boolean {
|
|
57
|
-
return this.client !== null && this.client !== undefined;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const websitalkMongodbClient = new WebsitalkMongoDBClient();
|
|
1
|
+
import { MongoClient, Db } from "mongodb";
|
|
2
|
+
import { setDb } from "./index";
|
|
3
|
+
import { validateConfig, validateMongoUri } from "../utils/validation";
|
|
4
|
+
|
|
5
|
+
class WebsitalkMongoDBClient {
|
|
6
|
+
private client: MongoClient | null = null;
|
|
7
|
+
private db: Db | null = null;
|
|
8
|
+
private readonly defaultDbName = "website-talk";
|
|
9
|
+
|
|
10
|
+
async connect(uri?: string, dbName?: string): Promise<void> {
|
|
11
|
+
if (this.client) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const mongodbUri = uri || process.env.MONGO_URI || process.env.MONGODB_URI;
|
|
16
|
+
validateConfig("MONGO_URI", mongodbUri);
|
|
17
|
+
validateMongoUri(mongodbUri!);
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
this.client = new MongoClient(mongodbUri!);
|
|
21
|
+
await this.client.connect();
|
|
22
|
+
const targetDbName =
|
|
23
|
+
dbName || process.env.WEBSITALK_DB_NAME || this.defaultDbName;
|
|
24
|
+
this.db = this.client.db(targetDbName);
|
|
25
|
+
setDb(this.db);
|
|
26
|
+
console.info(`[core-db] Website Talk MongoDB connected: ${targetDbName}`);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error("[core-db] Website Talk connection failed", error);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async disconnect(): Promise<void> {
|
|
34
|
+
if (this.client) {
|
|
35
|
+
try {
|
|
36
|
+
await this.client.close();
|
|
37
|
+
this.client = null;
|
|
38
|
+
this.db = null;
|
|
39
|
+
console.info("Website Talk MongoDB disconnected successfully");
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("[core-db] Website Talk disconnection failed", error);
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getDb(): Db {
|
|
48
|
+
if (!this.db) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
"Website Talk database not initialized. Call connect() first.",
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return this.db;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
isConnected(): boolean {
|
|
57
|
+
return this.client !== null && this.client !== undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const websitalkMongodbClient = new WebsitalkMongoDBClient();
|