@talkpilot/core-db 1.3.12 → 1.3.13
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 +307 -297
- package/dist/municipal/tickets/tickets.statistics.getters.d.ts.map +1 -1
- package/dist/municipal/tickets/tickets.statistics.getters.js +6 -2
- package/dist/municipal/tickets/tickets.statistics.getters.js.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 +2 -0
- package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
- package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts +4 -0
- package/dist/talkpilot/subscriptions/subscriptions.utils.d.ts.map +1 -0
- package/dist/talkpilot/subscriptions/subscriptions.utils.js +20 -0
- package/dist/talkpilot/subscriptions/subscriptions.utils.js.map +1 -0
- package/dist/test-utils/factories/talkpilot/flows.d.ts.map +1 -1
- package/dist/test-utils/factories/talkpilot/flows.js +1 -0
- package/dist/test-utils/factories/talkpilot/flows.js.map +1 -1
- package/jest.config.js +20 -20
- package/package.json +46 -46
- package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +94 -94
- package/src/municipal/tickets/__tests__/tickets.statistics.spec.ts +99 -99
- package/src/municipal/tickets/index.ts +9 -9
- package/src/municipal/tickets/tickets.statistics.aggregation.ts +110 -110
- package/src/municipal/tickets/tickets.statistics.getters.ts +145 -132
- package/src/municipal/tickets/tickets.types.ts +54 -54
- package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +483 -481
- package/src/talkpilot/calls/calls.statistics.getters.ts +668 -668
- package/src/talkpilot/calls/calls.statistics.types.ts +48 -48
- package/src/talkpilot/clientsConfig/clientsConfig.types.ts +127 -127
- package/src/talkpilot/contextNotes/__tests__/contextNotes.getters.spec.ts +217 -217
- package/src/talkpilot/contextNotes/contextNotes.getters.ts +85 -85
- package/src/talkpilot/flows/flows.schema.ts +1 -0
- package/src/talkpilot/flows/flows.types.ts +2 -0
- package/src/talkpilot/products/__tests__/products.getters.spec.ts +44 -44
- package/src/test-utils/factories/talkpilot/contextNotes.ts +40 -40
- package/src/test-utils/factories/talkpilot/flows.ts +1 -0
- package/src/utils/date.utils.ts +126 -126
- package/tsconfig.json +23 -23
- package/README_OLD.md +0 -160
- package/dist/talkpilot/calls/calls.dashboard.d.ts +0 -3
- package/dist/talkpilot/calls/calls.dashboard.d.ts.map +0 -1
- package/dist/talkpilot/calls/calls.dashboard.js +0 -191
- package/dist/talkpilot/calls/calls.dashboard.js.map +0 -1
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { getProductsCollection, getAllProducts } from "../products.getters";
|
|
2
|
-
import { ObjectId } from "../../index";
|
|
3
|
-
|
|
4
|
-
describe("products getters", () => {
|
|
5
|
-
it('should return the "products" collection', () => {
|
|
6
|
-
const collection = getProductsCollection();
|
|
7
|
-
expect(collection.collectionName).toBe("products");
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it("should return all product documents", async () => {
|
|
11
|
-
await getProductsCollection().insertMany([
|
|
12
|
-
{ _id: new ObjectId(), name: "Municipal" },
|
|
13
|
-
{ _id: new ObjectId(), name: "Clinics" },
|
|
14
|
-
]);
|
|
15
|
-
|
|
16
|
-
const results = await getAllProducts();
|
|
17
|
-
expect(results.length).toBeGreaterThanOrEqual(2);
|
|
18
|
-
expect(results.map((p) => p.name)).toEqual(
|
|
19
|
-
expect.arrayContaining(["Municipal", "Clinics"]),
|
|
20
|
-
);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("should store and retrieve displayName locale map", async () => {
|
|
24
|
-
const id = new ObjectId();
|
|
25
|
-
await getProductsCollection().insertOne({
|
|
26
|
-
_id: id,
|
|
27
|
-
name: "Municipal",
|
|
28
|
-
displayName: { he: "עירייה", en: "Municipal" },
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const results = await getAllProducts();
|
|
32
|
-
const saved = results.find((p) => p._id.equals(id));
|
|
33
|
-
expect(saved?.displayName).toEqual({ he: "עירייה", en: "Municipal" });
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("should return products without displayName without error", async () => {
|
|
37
|
-
const id = new ObjectId();
|
|
38
|
-
await getProductsCollection().insertOne({ _id: id, name: "Rcure" });
|
|
39
|
-
|
|
40
|
-
const results = await getAllProducts();
|
|
41
|
-
const saved = results.find((p) => p._id.equals(id));
|
|
42
|
-
expect(saved?.displayName).toBeUndefined();
|
|
43
|
-
});
|
|
44
|
-
});
|
|
1
|
+
import { getProductsCollection, getAllProducts } from "../products.getters";
|
|
2
|
+
import { ObjectId } from "../../index";
|
|
3
|
+
|
|
4
|
+
describe("products getters", () => {
|
|
5
|
+
it('should return the "products" collection', () => {
|
|
6
|
+
const collection = getProductsCollection();
|
|
7
|
+
expect(collection.collectionName).toBe("products");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return all product documents", async () => {
|
|
11
|
+
await getProductsCollection().insertMany([
|
|
12
|
+
{ _id: new ObjectId(), name: "Municipal" },
|
|
13
|
+
{ _id: new ObjectId(), name: "Clinics" },
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
const results = await getAllProducts();
|
|
17
|
+
expect(results.length).toBeGreaterThanOrEqual(2);
|
|
18
|
+
expect(results.map((p) => p.name)).toEqual(
|
|
19
|
+
expect.arrayContaining(["Municipal", "Clinics"]),
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should store and retrieve displayName locale map", async () => {
|
|
24
|
+
const id = new ObjectId();
|
|
25
|
+
await getProductsCollection().insertOne({
|
|
26
|
+
_id: id,
|
|
27
|
+
name: "Municipal",
|
|
28
|
+
displayName: { he: "עירייה", en: "Municipal" },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const results = await getAllProducts();
|
|
32
|
+
const saved = results.find((p) => p._id.equals(id));
|
|
33
|
+
expect(saved?.displayName).toEqual({ he: "עירייה", en: "Municipal" });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should return products without displayName without error", async () => {
|
|
37
|
+
const id = new ObjectId();
|
|
38
|
+
await getProductsCollection().insertOne({ _id: id, name: "Rcure" });
|
|
39
|
+
|
|
40
|
+
const results = await getAllProducts();
|
|
41
|
+
const saved = results.find((p) => p._id.equals(id));
|
|
42
|
+
expect(saved?.displayName).toBeUndefined();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,40 +1,40 @@
|
|
|
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<
|
|
17
|
-
ContextNote & { _id: ObjectId }
|
|
18
|
-
>(() => ({
|
|
19
|
-
_id: new ObjectId(),
|
|
20
|
-
clientId: faker.string.uuid(),
|
|
21
|
-
product: "Municipal",
|
|
22
|
-
notes: [],
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
export function createContextNoteEntry(
|
|
26
|
-
overrides?: Partial<ContextNoteEntry>,
|
|
27
|
-
): ContextNoteEntry {
|
|
28
|
-
return {
|
|
29
|
-
...contextNoteEntryFactory.build(),
|
|
30
|
-
...overrides,
|
|
31
|
-
} as ContextNoteEntry;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function createContextNote(
|
|
35
|
-
overrides?: Partial<ContextNote>,
|
|
36
|
-
): ContextNote & { _id: ObjectId } {
|
|
37
|
-
return { ...contextNoteFactory.build(), ...overrides } as ContextNote & {
|
|
38
|
-
_id: ObjectId;
|
|
39
|
-
};
|
|
40
|
-
}
|
|
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<
|
|
17
|
+
ContextNote & { _id: ObjectId }
|
|
18
|
+
>(() => ({
|
|
19
|
+
_id: new ObjectId(),
|
|
20
|
+
clientId: faker.string.uuid(),
|
|
21
|
+
product: "Municipal",
|
|
22
|
+
notes: [],
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
export function createContextNoteEntry(
|
|
26
|
+
overrides?: Partial<ContextNoteEntry>,
|
|
27
|
+
): ContextNoteEntry {
|
|
28
|
+
return {
|
|
29
|
+
...contextNoteEntryFactory.build(),
|
|
30
|
+
...overrides,
|
|
31
|
+
} as ContextNoteEntry;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createContextNote(
|
|
35
|
+
overrides?: Partial<ContextNote>,
|
|
36
|
+
): ContextNote & { _id: ObjectId } {
|
|
37
|
+
return { ...contextNoteFactory.build(), ...overrides } as ContextNote & {
|
|
38
|
+
_id: ObjectId;
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/utils/date.utils.ts
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
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 =>
|
|
35
|
-
date.toISOString().slice(0, 10);
|
|
36
|
-
|
|
37
|
-
/** Adds `days` to a `YYYY-MM-DD` string, returning the resulting `YYYY-MM-DD`. */
|
|
38
|
-
export const addDaysToDateStr = (dateStr: string, days: number): string =>
|
|
39
|
-
formatDate(new Date(toUtcDate(dateStr).getTime() + days * DAY_MS));
|
|
40
|
-
|
|
41
|
-
/** Next civil calendar day (proleptic Gregorian). Avoids DST 24h steps that can stall iteration. */
|
|
42
|
-
export const incrementYmd = (
|
|
43
|
-
y: number,
|
|
44
|
-
m: number,
|
|
45
|
-
d: number,
|
|
46
|
-
): [number, number, number] => {
|
|
47
|
-
const next = new Date(Date.UTC(y, m - 1, d + 1));
|
|
48
|
-
return [next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate()];
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Formats a date as the calendar day in the given timezone.
|
|
53
|
-
* `en-CA` yields ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable
|
|
54
|
-
* (string compare == chronological compare) and matches MongoDB's `%Y-%m-%d`.
|
|
55
|
-
*/
|
|
56
|
-
export const formatYmdInTz = (date: Date, timezone: string): string =>
|
|
57
|
-
new Intl.DateTimeFormat("en-CA", { timeZone: timezone }).format(date);
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Returns the calendar day in the given timezone as numeric parts.
|
|
61
|
-
* Reads parts by `type` (not string order), so `en-US` is fine here.
|
|
62
|
-
*/
|
|
63
|
-
export const getYmdInTz = (date: Date, timezone: string) => {
|
|
64
|
-
const parts = Object.fromEntries(
|
|
65
|
-
new Intl.DateTimeFormat("en-US", {
|
|
66
|
-
timeZone: timezone,
|
|
67
|
-
year: "numeric",
|
|
68
|
-
month: "numeric",
|
|
69
|
-
day: "numeric",
|
|
70
|
-
})
|
|
71
|
-
.formatToParts(date)
|
|
72
|
-
.map((p) => [p.type, Number(p.value)]),
|
|
73
|
-
);
|
|
74
|
-
return { y: parts.year, m: parts.month, d: parts.day };
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/** Weekday index (Sun = 0) of a date in the given timezone. */
|
|
78
|
-
export const getWeekdayInTz = (date: Date, timezone: string): number =>
|
|
79
|
-
WEEKDAY[
|
|
80
|
-
new Intl.DateTimeFormat("en-US", {
|
|
81
|
-
timeZone: timezone,
|
|
82
|
-
weekday: "short",
|
|
83
|
-
}).format(date)
|
|
84
|
-
] ?? 0;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns the given `YYYY-MM-DD` day at noon UTC — a stable anchor inside the
|
|
88
|
-
* calendar day, far from midnight so timezone/DST shifts can't cross a date boundary.
|
|
89
|
-
*/
|
|
90
|
-
export const dateAtNoonUtc = (dateStr: string): Date => {
|
|
91
|
-
const [y, m, d] = parseYmd(dateStr);
|
|
92
|
-
return new Date(Date.UTC(y, m - 1, d, 12, 0, 0));
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* UTC instant at the start of the given `YYYY-MM-DD` calendar day in `timezone`.
|
|
97
|
-
* Binary-searches the boundary so it stays correct across DST transitions.
|
|
98
|
-
*/
|
|
99
|
-
export const startOfCalendarDayInTz = (
|
|
100
|
-
dateStr: string,
|
|
101
|
-
timezone: string,
|
|
102
|
-
): Date => {
|
|
103
|
-
const anchor = dateAtNoonUtc(dateStr).getTime();
|
|
104
|
-
let low = anchor - 2 * DAY_MS;
|
|
105
|
-
let high = anchor + DAY_MS;
|
|
106
|
-
|
|
107
|
-
while (high - low > 1 /* ms */) {
|
|
108
|
-
const mid = Math.floor((low + high) / 2);
|
|
109
|
-
if (formatYmdInTz(new Date(mid), timezone) >= dateStr) high = mid;
|
|
110
|
-
else low = mid;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return new Date(high);
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
/** UTC instant at the last millisecond of the given calendar day in `timezone`. */
|
|
117
|
-
export const endOfCalendarDayInTz = (
|
|
118
|
-
dateStr: string,
|
|
119
|
-
timezone: string,
|
|
120
|
-
): Date => {
|
|
121
|
-
const [y, m, d] = parseYmd(dateStr);
|
|
122
|
-
const nextDay = new Date(Date.UTC(y, m - 1, d + 1))
|
|
123
|
-
.toISOString()
|
|
124
|
-
.slice(0, 10);
|
|
125
|
-
return new Date(startOfCalendarDayInTz(nextDay, timezone).getTime() - 1);
|
|
126
|
-
};
|
|
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 =>
|
|
35
|
+
date.toISOString().slice(0, 10);
|
|
36
|
+
|
|
37
|
+
/** Adds `days` to a `YYYY-MM-DD` string, returning the resulting `YYYY-MM-DD`. */
|
|
38
|
+
export const addDaysToDateStr = (dateStr: string, days: number): string =>
|
|
39
|
+
formatDate(new Date(toUtcDate(dateStr).getTime() + days * DAY_MS));
|
|
40
|
+
|
|
41
|
+
/** Next civil calendar day (proleptic Gregorian). Avoids DST 24h steps that can stall iteration. */
|
|
42
|
+
export const incrementYmd = (
|
|
43
|
+
y: number,
|
|
44
|
+
m: number,
|
|
45
|
+
d: number,
|
|
46
|
+
): [number, number, number] => {
|
|
47
|
+
const next = new Date(Date.UTC(y, m - 1, d + 1));
|
|
48
|
+
return [next.getUTCFullYear(), next.getUTCMonth() + 1, next.getUTCDate()];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Formats a date as the calendar day in the given timezone.
|
|
53
|
+
* `en-CA` yields ISO-8601 `YYYY-MM-DD`, which is lexicographically sortable
|
|
54
|
+
* (string compare == chronological compare) and matches MongoDB's `%Y-%m-%d`.
|
|
55
|
+
*/
|
|
56
|
+
export const formatYmdInTz = (date: Date, timezone: string): string =>
|
|
57
|
+
new Intl.DateTimeFormat("en-CA", { timeZone: timezone }).format(date);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns the calendar day in the given timezone as numeric parts.
|
|
61
|
+
* Reads parts by `type` (not string order), so `en-US` is fine here.
|
|
62
|
+
*/
|
|
63
|
+
export const getYmdInTz = (date: Date, timezone: string) => {
|
|
64
|
+
const parts = Object.fromEntries(
|
|
65
|
+
new Intl.DateTimeFormat("en-US", {
|
|
66
|
+
timeZone: timezone,
|
|
67
|
+
year: "numeric",
|
|
68
|
+
month: "numeric",
|
|
69
|
+
day: "numeric",
|
|
70
|
+
})
|
|
71
|
+
.formatToParts(date)
|
|
72
|
+
.map((p) => [p.type, Number(p.value)]),
|
|
73
|
+
);
|
|
74
|
+
return { y: parts.year, m: parts.month, d: parts.day };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Weekday index (Sun = 0) of a date in the given timezone. */
|
|
78
|
+
export const getWeekdayInTz = (date: Date, timezone: string): number =>
|
|
79
|
+
WEEKDAY[
|
|
80
|
+
new Intl.DateTimeFormat("en-US", {
|
|
81
|
+
timeZone: timezone,
|
|
82
|
+
weekday: "short",
|
|
83
|
+
}).format(date)
|
|
84
|
+
] ?? 0;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Returns the given `YYYY-MM-DD` day at noon UTC — a stable anchor inside the
|
|
88
|
+
* calendar day, far from midnight so timezone/DST shifts can't cross a date boundary.
|
|
89
|
+
*/
|
|
90
|
+
export const dateAtNoonUtc = (dateStr: string): Date => {
|
|
91
|
+
const [y, m, d] = parseYmd(dateStr);
|
|
92
|
+
return new Date(Date.UTC(y, m - 1, d, 12, 0, 0));
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* UTC instant at the start of the given `YYYY-MM-DD` calendar day in `timezone`.
|
|
97
|
+
* Binary-searches the boundary so it stays correct across DST transitions.
|
|
98
|
+
*/
|
|
99
|
+
export const startOfCalendarDayInTz = (
|
|
100
|
+
dateStr: string,
|
|
101
|
+
timezone: string,
|
|
102
|
+
): Date => {
|
|
103
|
+
const anchor = dateAtNoonUtc(dateStr).getTime();
|
|
104
|
+
let low = anchor - 2 * DAY_MS;
|
|
105
|
+
let high = anchor + DAY_MS;
|
|
106
|
+
|
|
107
|
+
while (high - low > 1 /* ms */) {
|
|
108
|
+
const mid = Math.floor((low + high) / 2);
|
|
109
|
+
if (formatYmdInTz(new Date(mid), timezone) >= dateStr) high = mid;
|
|
110
|
+
else low = mid;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return new Date(high);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** UTC instant at the last millisecond of the given calendar day in `timezone`. */
|
|
117
|
+
export const endOfCalendarDayInTz = (
|
|
118
|
+
dateStr: string,
|
|
119
|
+
timezone: string,
|
|
120
|
+
): Date => {
|
|
121
|
+
const [y, m, d] = parseYmd(dateStr);
|
|
122
|
+
const nextDay = new Date(Date.UTC(y, m - 1, d + 1))
|
|
123
|
+
.toISOString()
|
|
124
|
+
.slice(0, 10);
|
|
125
|
+
return new Date(startOfCalendarDayInTz(nextDay, timezone).getTime() - 1);
|
|
126
|
+
};
|
package/tsconfig.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"lib": ["ES2022"],
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationMap": true,
|
|
9
|
-
"sourceMap": true,
|
|
10
|
-
"outDir": "dist",
|
|
11
|
-
"rootDir": "src",
|
|
12
|
-
"strict": true,
|
|
13
|
-
"esModuleInterop": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"skipLibCheck": true,
|
|
16
|
-
"resolveJsonModule": true,
|
|
17
|
-
"experimentalDecorators": true,
|
|
18
|
-
"emitDecoratorMetadata": true,
|
|
19
|
-
"baseUrl": "."
|
|
20
|
-
},
|
|
21
|
-
"include": ["src/**/*"],
|
|
22
|
-
"exclude": ["node_modules", "dist", "test", "**/*.test.ts", "**/__tests__/*", "**/*.spec.ts"]
|
|
23
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"outDir": "dist",
|
|
11
|
+
"rootDir": "src",
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"experimentalDecorators": true,
|
|
18
|
+
"emitDecoratorMetadata": true,
|
|
19
|
+
"baseUrl": "."
|
|
20
|
+
},
|
|
21
|
+
"include": ["src/**/*"],
|
|
22
|
+
"exclude": ["node_modules", "dist", "test", "**/*.test.ts", "**/__tests__/*", "**/*.spec.ts"]
|
|
23
|
+
}
|
package/README_OLD.md
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
# @talkpilot/core-db
|
|
2
|
-
|
|
3
|
-
[NPM Version](https://www.npmjs.com/package/@talkpilot/core-db)
|
|
4
|
-
|
|
5
|
-
A TypeScript-based core database management package designed to provide centralized database connections, ORM integrations, and client utilities for other projects. This package manages connections to both **TalkPilot** and **Municipal** MongoDB databases.
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- 🚀 **Multi-Domain Database Management**: Centralized handlers for both TalkPilot and Municipal Data domains.
|
|
10
|
-
- 📦 **Reusable Getters**: Standardized functions for fetching data from various collections (calls, agents, leads, cities, etc.).
|
|
11
|
-
- 🏗️ **Type-safe**: Built with TypeScript for full type safety across all your database interactions.
|
|
12
|
-
- 🛠️ **Environment Aware**: Configurable via environment variables or explicit parameters.
|
|
13
|
-
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
This package is intended to be used as a dependency in other projects. If you're working locally, you can install it using a file path:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
# In your other project:
|
|
20
|
-
npm install /path/to/core-db
|
|
21
|
-
# or if published to a registry:
|
|
22
|
-
npm install @talkpilot/core-db
|
|
23
|
-
# or via GitHub
|
|
24
|
-
npm install github:talkpilot/core-db
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Quick Start
|
|
28
|
-
|
|
29
|
-
### 1. Initialize Database Connections
|
|
30
|
-
|
|
31
|
-
You must initialize the database clients before using any of the getters.
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import { mongodbClient, municipalDataMongodbClient } from '@talkpilot/core-db';
|
|
35
|
-
|
|
36
|
-
async function bootstrap() {
|
|
37
|
-
// Initialize TalkPilot DB
|
|
38
|
-
await mongodbClient.connect(process.env.TALKPILOT_MONGO_URI);
|
|
39
|
-
|
|
40
|
-
// Initialize Municipal Data DB (optional)
|
|
41
|
-
await municipalDataMongodbClient.connect(process.env.MUNICIPAL_MONGO_URI);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
bootstrap().catch(console.error);
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### 2. Using Getters
|
|
48
|
-
|
|
49
|
-
Once initialized, you can import and use any of the exported database getters.
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
import { findAgents, findCities } from '@talkpilot/core-db';
|
|
53
|
-
|
|
54
|
-
async function getSummary() {
|
|
55
|
-
// Get all agents from TalkPilot DB
|
|
56
|
-
const agents = await findAgents();
|
|
57
|
-
console.log('Total Agents:', agents.length);
|
|
58
|
-
|
|
59
|
-
// Get all cities from Municipal Data DB
|
|
60
|
-
const cities = await findCities();
|
|
61
|
-
console.log('Total Cities:', cities.length);
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Available Domains
|
|
66
|
-
|
|
67
|
-
The package exports two main sets of tools:
|
|
68
|
-
|
|
69
|
-
### TalkPilot Domain
|
|
70
|
-
|
|
71
|
-
Includes access to:
|
|
72
|
-
|
|
73
|
-
- `agents`, `calls`, `clients`, `flows`, `leads`, `phone_numbers`, `plans`, `results`, `sessions`, `subscriptions`, etc.
|
|
74
|
-
- Exported as root level functions or via `mongodbClient`.
|
|
75
|
-
|
|
76
|
-
### Municipal Domain
|
|
77
|
-
|
|
78
|
-
Includes access to:
|
|
79
|
-
|
|
80
|
-
- `cities`, `streets`, `departmentsSubjects`, `tickets`.
|
|
81
|
-
- Functions are prefixed where necessary or accessible via `getMunicipalDataDb`.
|
|
82
|
-
|
|
83
|
-
## Environment Variables
|
|
84
|
-
|
|
85
|
-
The clients will automatically attempt to use the following environment variables if no URI is provided to the `connect()` method:
|
|
86
|
-
|
|
87
|
-
- `MONGO_URI` or `MONGODB_URI`: Primary MongoDB connection string.
|
|
88
|
-
- `MONGODB_DB_NAME`: Database name (defaults to 'municipal-data' for the municipal client if not specified).
|
|
89
|
-
|
|
90
|
-
## Development
|
|
91
|
-
|
|
92
|
-
### Setup
|
|
93
|
-
|
|
94
|
-
1. Clone the repository and install dependencies:
|
|
95
|
-
```bash
|
|
96
|
-
git clone https://github.com/talkpilot/core-db.git
|
|
97
|
-
cd core-db
|
|
98
|
-
npm install
|
|
99
|
-
```
|
|
100
|
-
2. Build the project:
|
|
101
|
-
```bash
|
|
102
|
-
npm run build
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
This will generate the `dist/` directory with the compiled JavaScript and type definitions.
|
|
106
|
-
|
|
107
|
-
### Publishing New Versions
|
|
108
|
-
|
|
109
|
-
To publish a new version of the package:
|
|
110
|
-
|
|
111
|
-
1. **Test your changes**:
|
|
112
|
-
```bash
|
|
113
|
-
npm test
|
|
114
|
-
```
|
|
115
|
-
2. **Update the version**:
|
|
116
|
-
```bash
|
|
117
|
-
# Bumps patch version (0.1.0 -> 0.1.1)
|
|
118
|
-
npm version patch
|
|
119
|
-
# Or for minor changes (0.1.0 -> 0.2.0)
|
|
120
|
-
# npm version minor
|
|
121
|
-
# Or for major changes (0.1.0 -> 1.0.0)
|
|
122
|
-
# npm version major
|
|
123
|
-
```
|
|
124
|
-
3. **Publish to npm**:
|
|
125
|
-
Ensure you have the shared team token in your `~/.npmrc` (see [Development Guide](./DEVELOPMENT.md#team-access--authentication)).
|
|
126
|
-
4. **Update in other repos**:
|
|
127
|
-
```bash
|
|
128
|
-
npm update @talkpilot/core-db
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
## License
|
|
132
|
-
|
|
133
|
-
MIT
|
|
134
|
-
|
|
135
|
-
---
|
|
136
|
-
|
|
137
|
-
**Interested in contributing?** Check out our [Development Guide](./DEVELOPMENT.md) for standards and setup instructions.
|
|
138
|
-
|
|
139
|
-
## Release Notes
|
|
140
|
-
|
|
141
|
-
### 1.1.9
|
|
142
|
-
|
|
143
|
-
- `getPhoneNumbersForFlows(clientId)` — lists each phone number linked to a flow for that client (newest first), with `flowId`, `phoneNumber`, and `isPrimary`.
|
|
144
|
-
### 1.1.5
|
|
145
|
-
|
|
146
|
-
- `createPurchasedPhoneNumber` for API-bought numbers (Twilio/Telnyx metadata); optional `flowId` until linked to a flow. Types/schema updated accordingly.
|
|
147
|
-
|
|
148
|
-
### 1.0.16
|
|
149
|
-
|
|
150
|
-
- Fixed critical issues present in version 1.0.15.
|
|
151
|
-
- **Warning**: Version 1.0.15 is bugged and should not be used. Please use at least version 1.0.16.
|
|
152
|
-
|
|
153
|
-
### 1.0.20
|
|
154
|
-
|
|
155
|
-
- Added to Moked106Config this param: withNeedAttentionCalls
|
|
156
|
-
- withNeedAttentionCalls?: boolean
|
|
157
|
-
|
|
158
|
-
### 1.0.27
|
|
159
|
-
- Added an optional `language` field to `ClientConfig` (e.g. Hebrew/English/Russian/French/Spanish) and updated tests.
|
|
160
|
-
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"calls.dashboard.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/calls/calls.dashboard.ts"],"names":[],"mappings":"AAEA,OAAO,EAA8B,oBAAoB,EAAE,oBAAoB,EAAiD,MAAM,eAAe,CAAC;AAmGtJ,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,oBAAoB,CAAC,CAuG/B"}
|