@talkpilot/core-db 1.3.4 → 1.3.5

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.
Files changed (110) hide show
  1. package/.claude/settings.local.json +5 -1
  2. package/DEVELOPMENT.md +28 -0
  3. package/README.md +21 -0
  4. package/dist/connection.d.ts.map +1 -1
  5. package/dist/connection.js +10 -0
  6. package/dist/connection.js.map +1 -1
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +8 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/municipal/index.d.ts +1 -0
  12. package/dist/municipal/index.d.ts.map +1 -1
  13. package/dist/municipal/index.js +1 -0
  14. package/dist/municipal/index.js.map +1 -1
  15. package/dist/municipal/muniIssues/index.d.ts +7 -0
  16. package/dist/municipal/muniIssues/index.d.ts.map +1 -0
  17. package/dist/municipal/muniIssues/index.js +29 -0
  18. package/dist/municipal/muniIssues/index.js.map +1 -0
  19. package/dist/municipal/muniIssues/muniIssues.constants.d.ts +14 -0
  20. package/dist/municipal/muniIssues/muniIssues.constants.d.ts.map +1 -0
  21. package/dist/municipal/muniIssues/muniIssues.constants.js +17 -0
  22. package/dist/municipal/muniIssues/muniIssues.constants.js.map +1 -0
  23. package/dist/municipal/muniIssues/muniIssues.getters.d.ts +7 -0
  24. package/dist/municipal/muniIssues/muniIssues.getters.d.ts.map +1 -0
  25. package/dist/municipal/muniIssues/muniIssues.getters.js +24 -0
  26. package/dist/municipal/muniIssues/muniIssues.getters.js.map +1 -0
  27. package/dist/municipal/muniIssues/muniIssues.schema.d.ts +4 -0
  28. package/dist/municipal/muniIssues/muniIssues.schema.d.ts.map +1 -0
  29. package/dist/municipal/muniIssues/muniIssues.schema.js +63 -0
  30. package/dist/municipal/muniIssues/muniIssues.schema.js.map +1 -0
  31. package/dist/municipal/muniIssues/muniIssues.setters.d.ts +4 -0
  32. package/dist/municipal/muniIssues/muniIssues.setters.d.ts.map +1 -0
  33. package/dist/municipal/muniIssues/muniIssues.setters.js +22 -0
  34. package/dist/municipal/muniIssues/muniIssues.setters.js.map +1 -0
  35. package/dist/municipal/muniIssues/muniIssues.setup.d.ts +3 -0
  36. package/dist/municipal/muniIssues/muniIssues.setup.d.ts.map +1 -0
  37. package/dist/municipal/muniIssues/muniIssues.setup.js +61 -0
  38. package/dist/municipal/muniIssues/muniIssues.setup.js.map +1 -0
  39. package/dist/municipal/muniIssues/muniIssues.types.d.ts +30 -0
  40. package/dist/municipal/muniIssues/muniIssues.types.d.ts.map +1 -0
  41. package/dist/municipal/muniIssues/muniIssues.types.js +3 -0
  42. package/dist/municipal/muniIssues/muniIssues.types.js.map +1 -0
  43. package/dist/talkpilot/calls/dashboard/calls.dashboard.d.ts +1 -33
  44. package/dist/talkpilot/calls/dashboard/calls.dashboard.d.ts.map +1 -1
  45. package/dist/talkpilot/calls/dashboard/calls.dashboard.js +146 -131
  46. package/dist/talkpilot/calls/dashboard/calls.dashboard.js.map +1 -1
  47. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.d.ts +6 -27
  48. package/dist/talkpilot/calls/dashboard/calls.dashboard.types.d.ts.map +1 -1
  49. package/dist/test-utils/db-utils.d.ts.map +1 -1
  50. package/dist/test-utils/db-utils.js +2 -0
  51. package/dist/test-utils/db-utils.js.map +1 -1
  52. package/dist/test-utils/factories/index.d.ts +2 -0
  53. package/dist/test-utils/factories/index.d.ts.map +1 -1
  54. package/dist/test-utils/factories/index.js +2 -0
  55. package/dist/test-utils/factories/index.js.map +1 -1
  56. package/dist/test-utils/factories/municipal/muniIssues.d.ts +10 -0
  57. package/dist/test-utils/factories/municipal/muniIssues.d.ts.map +1 -0
  58. package/dist/test-utils/factories/municipal/muniIssues.js +30 -0
  59. package/dist/test-utils/factories/municipal/muniIssues.js.map +1 -0
  60. package/dist/test-utils/factories/websitalk/scans.d.ts +5 -0
  61. package/dist/test-utils/factories/websitalk/scans.d.ts.map +1 -0
  62. package/dist/test-utils/factories/websitalk/scans.js +25 -0
  63. package/dist/test-utils/factories/websitalk/scans.js.map +1 -0
  64. package/dist/websitalk/index.d.ts +7 -0
  65. package/dist/websitalk/index.d.ts.map +1 -0
  66. package/dist/websitalk/index.js +34 -0
  67. package/dist/websitalk/index.js.map +1 -0
  68. package/dist/websitalk/mongodb-client.d.ts +13 -0
  69. package/dist/websitalk/mongodb-client.d.ts.map +1 -0
  70. package/dist/websitalk/mongodb-client.js +56 -0
  71. package/dist/websitalk/mongodb-client.js.map +1 -0
  72. package/dist/websitalk/scans/index.d.ts +3 -0
  73. package/dist/websitalk/scans/index.d.ts.map +1 -0
  74. package/dist/websitalk/scans/index.js +19 -0
  75. package/dist/websitalk/scans/index.js.map +1 -0
  76. package/dist/websitalk/scans/scans.getters.d.ts +12 -0
  77. package/dist/websitalk/scans/scans.getters.d.ts.map +1 -0
  78. package/dist/websitalk/scans/scans.getters.js +74 -0
  79. package/dist/websitalk/scans/scans.getters.js.map +1 -0
  80. package/dist/websitalk/scans/scans.types.d.ts +45 -0
  81. package/dist/websitalk/scans/scans.types.d.ts.map +1 -0
  82. package/dist/websitalk/scans/scans.types.js +3 -0
  83. package/dist/websitalk/scans/scans.types.js.map +1 -0
  84. package/package.json +1 -1
  85. package/src/connection.ts +12 -0
  86. package/src/index.ts +10 -0
  87. package/src/municipal/index.ts +1 -0
  88. package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +92 -0
  89. package/src/municipal/muniIssues/__tests__/muniIssues.setup.spec.ts +92 -0
  90. package/src/municipal/muniIssues/index.ts +17 -0
  91. package/src/municipal/muniIssues/muniIssues.constants.ts +18 -0
  92. package/src/municipal/muniIssues/muniIssues.getters.ts +24 -0
  93. package/src/municipal/muniIssues/muniIssues.schema.ts +66 -0
  94. package/src/municipal/muniIssues/muniIssues.setters.ts +24 -0
  95. package/src/municipal/muniIssues/muniIssues.setup.ts +65 -0
  96. package/src/municipal/muniIssues/muniIssues.types.ts +37 -0
  97. package/src/talkpilot/calls/__tests__/calls.dashboard.spec.ts +111 -8
  98. package/src/talkpilot/calls/dashboard/calls.dashboard.ts +197 -148
  99. package/src/talkpilot/calls/dashboard/calls.dashboard.types.ts +12 -25
  100. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +0 -7
  101. package/src/test-utils/db-utils.ts +3 -1
  102. package/src/test-utils/factories/index.ts +2 -0
  103. package/src/test-utils/factories/municipal/muniIssues.ts +32 -0
  104. package/src/test-utils/factories/websitalk/scans.ts +23 -0
  105. package/src/websitalk/index.ts +15 -0
  106. package/src/websitalk/mongodb-client.ts +61 -0
  107. package/src/websitalk/scans/__tests__/scans.spec.ts +218 -0
  108. package/src/websitalk/scans/index.ts +2 -0
  109. package/src/websitalk/scans/scans.getters.ts +113 -0
  110. package/src/websitalk/scans/scans.types.ts +53 -0
@@ -0,0 +1,24 @@
1
+ import { Collection, ObjectId } from "mongodb";
2
+ import { getDb } from "../index";
3
+ import { MuniIssue } from "./muniIssues.types";
4
+ import { MUNI_ISSUES_COLLECTION } from "./muniIssues.constants";
5
+
6
+ export const getMuniIssuesCollection = (): Collection<MuniIssue> => {
7
+ return getDb().collection<MuniIssue>(MUNI_ISSUES_COLLECTION);
8
+ };
9
+
10
+ export const generateMuniIssueId = (): ObjectId => new ObjectId();
11
+
12
+ export const getMuniIssueById = async (
13
+ id: string,
14
+ ): Promise<MuniIssue | null> => {
15
+ // A malformed id is treated as not-found rather than throwing on `new ObjectId`.
16
+ if (!ObjectId.isValid(id)) return null;
17
+ return getMuniIssuesCollection().findOne({ _id: new ObjectId(id) });
18
+ };
19
+
20
+ export const getMuniIssueByJiraKey = async (
21
+ jiraKey: string,
22
+ ): Promise<MuniIssue | null> => {
23
+ return getMuniIssuesCollection().findOne({ "jira.key": jiraKey });
24
+ };
@@ -0,0 +1,66 @@
1
+ import type { Document, IndexDescription } from "mongodb";
2
+ import {
3
+ MUNI_ISSUE_SYNC_STATUSES,
4
+ MUNI_ISSUE_TEXT_MAX_LENGTH,
5
+ MUNI_ISSUE_NAME_MAX_LENGTH,
6
+ } from "./muniIssues.constants";
7
+
8
+ export const muniIssuesJsonSchema: Document = {
9
+ bsonType: "object",
10
+ required: [
11
+ "callSid",
12
+ "clientId",
13
+ "cityName",
14
+ "issueContent",
15
+ "jira",
16
+ "createdAt",
17
+ ],
18
+ properties: {
19
+ callSid: { bsonType: "string" },
20
+ clientId: { bsonType: "string" },
21
+ cityName: { bsonType: "string" },
22
+ issueContent: {
23
+ bsonType: "object",
24
+ required: [
25
+ "issuerName",
26
+ "issuerRole",
27
+ "issueDescription",
28
+ "desiredBehavior",
29
+ ],
30
+ properties: {
31
+ issuerName: {
32
+ bsonType: "string",
33
+ maxLength: MUNI_ISSUE_NAME_MAX_LENGTH,
34
+ },
35
+ issuerRole: {
36
+ bsonType: "string",
37
+ maxLength: MUNI_ISSUE_NAME_MAX_LENGTH,
38
+ },
39
+ issueDescription: {
40
+ bsonType: "string",
41
+ maxLength: MUNI_ISSUE_TEXT_MAX_LENGTH,
42
+ },
43
+ desiredBehavior: {
44
+ bsonType: "string",
45
+ maxLength: MUNI_ISSUE_TEXT_MAX_LENGTH,
46
+ },
47
+ },
48
+ },
49
+ jira: {
50
+ bsonType: "object",
51
+ required: ["key", "syncStatus"],
52
+ properties: {
53
+ key: { bsonType: "string" },
54
+ syncStatus: { enum: [...MUNI_ISSUE_SYNC_STATUSES] },
55
+ },
56
+ },
57
+ createdAt: { bsonType: "date" },
58
+ },
59
+ };
60
+
61
+ export const muniIssuesIndexes: IndexDescription[] = [
62
+ { key: { callSid: 1 }, name: "callSid_1" },
63
+ { key: { createdAt: -1 }, name: "createdAt_-1" },
64
+ // Unique: one stored document per Jira ticket, guarding against duplicate writes.
65
+ { key: { "jira.key": 1 }, name: "jira_key_unique", unique: true },
66
+ ];
@@ -0,0 +1,24 @@
1
+ import { ObjectId } from "mongodb";
2
+ import { CreateMuniIssueInput, MuniIssue } from "./muniIssues.types";
3
+ import { getMuniIssuesCollection } from "./muniIssues.getters";
4
+ import { ensureMuniIssuesCollectionOnce } from "./muniIssues.setup";
5
+
6
+ // Required `id`: MIS pre-generates it (generateMuniIssueId) and embeds it in the Jira ticket — the two-way link.
7
+ export const createMuniIssue = async (
8
+ issueData: CreateMuniIssueInput,
9
+ id: ObjectId,
10
+ ): Promise<ObjectId> => {
11
+ await ensureMuniIssuesCollectionOnce();
12
+
13
+ const issue: MuniIssue = {
14
+ _id: id,
15
+ ...issueData,
16
+ jira: {
17
+ ...issueData.jira,
18
+ syncStatus: issueData.jira.syncStatus ?? "created",
19
+ },
20
+ createdAt: new Date(),
21
+ };
22
+ const { insertedId } = await getMuniIssuesCollection().insertOne(issue);
23
+ return insertedId;
24
+ };
@@ -0,0 +1,65 @@
1
+ import { Db, Document, MongoServerError } from "mongodb";
2
+ import { getDb } from "../index";
3
+ import {
4
+ MUNI_ISSUES_COLLECTION,
5
+ NAMESPACE_EXISTS,
6
+ VALIDATION_LEVEL,
7
+ VALIDATION_ACTION,
8
+ } from "./muniIssues.constants";
9
+ import { muniIssuesIndexes, muniIssuesJsonSchema } from "./muniIssues.schema";
10
+
11
+ const applyValidator = (db: Db, validator: Document): Promise<unknown> =>
12
+ db.command({
13
+ collMod: MUNI_ISSUES_COLLECTION,
14
+ validator,
15
+ validationLevel: VALIDATION_LEVEL,
16
+ validationAction: VALIDATION_ACTION,
17
+ });
18
+
19
+ // Provisions validator and indexes: createCollection first, collMod if already exists.
20
+ export const ensureMuniIssuesCollection = async (): Promise<void> => {
21
+ const db = getDb();
22
+ const validator = { $jsonSchema: muniIssuesJsonSchema };
23
+
24
+ const existing = await db
25
+ .listCollections({ name: MUNI_ISSUES_COLLECTION })
26
+ .toArray();
27
+
28
+ if (existing.length === 0) {
29
+ try {
30
+ await db.createCollection(MUNI_ISSUES_COLLECTION, {
31
+ validator,
32
+ validationLevel: VALIDATION_LEVEL,
33
+ validationAction: VALIDATION_ACTION,
34
+ });
35
+ } catch (err) {
36
+ // NamespaceExists race: collection exists — apply validator via collMod.
37
+ if (err instanceof MongoServerError && err.code === NAMESPACE_EXISTS) {
38
+ await applyValidator(db, validator);
39
+ } else {
40
+ throw err;
41
+ }
42
+ }
43
+ } else {
44
+ await applyValidator(db, validator);
45
+ }
46
+
47
+ await db.collection(MUNI_ISSUES_COLLECTION).createIndexes(muniIssuesIndexes);
48
+ };
49
+
50
+ // Per-Db cache so each test's in-memory Db re-provisions.
51
+ const provisioningByDb = new WeakMap<Db, Promise<void>>();
52
+
53
+ // Once per Db; evicts failed run so the next call retries.
54
+ export const ensureMuniIssuesCollectionOnce = (): Promise<void> => {
55
+ const db = getDb();
56
+ let provisioning = provisioningByDb.get(db);
57
+ if (!provisioning) {
58
+ provisioning = ensureMuniIssuesCollection().catch((err) => {
59
+ provisioningByDb.delete(db);
60
+ throw err;
61
+ });
62
+ provisioningByDb.set(db, provisioning);
63
+ }
64
+ return provisioning;
65
+ };
@@ -0,0 +1,37 @@
1
+ import { ObjectId } from "mongodb";
2
+
3
+ import { CityName } from "../utils/types";
4
+ import { MUNI_ISSUE_SYNC_STATUSES } from "./muniIssues.constants";
5
+
6
+ export type MuniIssueSyncStatus = (typeof MUNI_ISSUE_SYNC_STATUSES)[number];
7
+
8
+ export type MuniIssueContent = {
9
+ issuerName: string;
10
+ issuerRole: string;
11
+ issueDescription: string;
12
+ desiredBehavior: string;
13
+ };
14
+
15
+ export type MuniIssueJira = {
16
+ key: string;
17
+ syncStatus: MuniIssueSyncStatus;
18
+ };
19
+
20
+ export type MuniIssue = {
21
+ _id: ObjectId;
22
+ callSid: string;
23
+ clientId: string;
24
+ cityName: CityName;
25
+ issueContent: MuniIssueContent;
26
+ jira: MuniIssueJira;
27
+ createdAt: Date;
28
+ };
29
+
30
+ // _id/createdAt are set on insert; jira.syncStatus defaults to "created"
31
+ // because the issue is only stored after its Jira ticket exists (CP-1115).
32
+ export type CreateMuniIssueInput = Omit<
33
+ MuniIssue,
34
+ "_id" | "createdAt" | "jira"
35
+ > & {
36
+ jira: { key: string; syncStatus?: MuniIssueSyncStatus };
37
+ };
@@ -25,8 +25,8 @@ describe("getDashboardStats", () => {
25
25
 
26
26
  const params = {
27
27
  clientId,
28
- startDate: "2026-05-01",
29
- endDate: "2026-05-31",
28
+ startDate: "2026-05-01T00:00:00.000Z",
29
+ endDate: "2026-05-31T23:59:59.999Z",
30
30
  };
31
31
 
32
32
  const result = await getDashboardStats(params);
@@ -35,12 +35,115 @@ describe("getDashboardStats", () => {
35
35
  expect(result.kpis.totalCalls).toBe(0);
36
36
  expect(result.kpis.completedCount).toBe(0);
37
37
  expect(result.kpis.avgDurationSeconds).toBe(0);
38
- expect(result.charts.volumeData).toEqual([]);
39
- expect(result.charts.heatmap).toEqual({});
40
- expect(result.charts.callLengthBuckets).toEqual({
41
- short: 0,
42
- medium: 0,
43
- long: 0,
38
+ expect(result.kpis.busyCount).toBe(0);
39
+ expect(result.kpis.timeSavedMinutes).toBe(0);
40
+ // ~30 day range -> daily buckets, gap-filled with zeros across the whole range
41
+ expect(result.charts.volumeGranularity).toBe("day");
42
+ expect(result.charts.volumeData.length).toBeGreaterThan(0);
43
+ expect(result.charts.volumeData.every((d) => d.completed === 0)).toBe(true);
44
+ expect(result.charts.volumeData[0].date).toBe("2026-05-01");
45
+ expect(
46
+ result.charts.volumeData[result.charts.volumeData.length - 1].date,
47
+ ).toBe("2026-05-31");
48
+ });
49
+
50
+ it("should pick a bucket size that keeps the volume chart readable across range lengths", async () => {
51
+ const clientId = "client-dash-456";
52
+
53
+ await getClientsConfigCollection().insertOne({
54
+ clientId,
55
+ timezone: "UTC",
56
+ products: {},
57
+ } as any);
58
+
59
+ const oneDay = await getDashboardStats({
60
+ clientId,
61
+ startDate: "2026-05-01T00:00:00.000Z",
62
+ endDate: "2026-05-01T23:59:59.999Z",
63
+ });
64
+ expect(oneDay.charts.volumeGranularity).toBe("hour");
65
+
66
+ const threeWeeks = await getDashboardStats({
67
+ clientId,
68
+ startDate: "2026-05-01T00:00:00.000Z",
69
+ endDate: "2026-05-21T23:59:59.999Z",
70
+ });
71
+ expect(threeWeeks.charts.volumeGranularity).toBe("day");
72
+
73
+ const fourMonths = await getDashboardStats({
74
+ clientId,
75
+ startDate: "2026-01-01T00:00:00.000Z",
76
+ endDate: "2026-04-30T23:59:59.999Z",
77
+ });
78
+ expect(fourMonths.charts.volumeGranularity).toBe("week");
79
+
80
+ const eighteenMonths = await getDashboardStats({
81
+ clientId,
82
+ startDate: "2025-01-01T00:00:00.000Z",
83
+ endDate: "2026-06-30T23:59:59.999Z",
84
+ });
85
+ expect(eighteenMonths.charts.volumeGranularity).toBe("month");
86
+
87
+ const threeYears = await getDashboardStats({
88
+ clientId,
89
+ startDate: "2023-01-01T00:00:00.000Z",
90
+ endDate: "2026-01-01T23:59:59.999Z",
91
+ });
92
+ expect(threeYears.charts.volumeGranularity).toBe("quarter");
93
+
94
+ const fiveYears = await getDashboardStats({
95
+ clientId,
96
+ startDate: "2021-01-01T00:00:00.000Z",
97
+ endDate: "2026-01-01T23:59:59.999Z",
98
+ });
99
+ expect(fiveYears.charts.volumeGranularity).toBe("year");
100
+ });
101
+
102
+ it("should fill gaps with zero-count buckets so the chart has no missing points", async () => {
103
+ const clientId = "client-dash-789";
104
+
105
+ await getClientsConfigCollection().insertOne({
106
+ clientId,
107
+ timezone: "UTC",
108
+ products: {},
109
+ } as any);
110
+
111
+ // Sparse data: only two of the day's hours have calls.
112
+ await getCallsCollection().insertMany([
113
+ {
114
+ clientId,
115
+ status: "completed",
116
+ callLength: 60,
117
+ createdAt: new Date("2026-05-01T09:15:00Z"),
118
+ },
119
+ {
120
+ clientId,
121
+ status: "completed",
122
+ callLength: 90,
123
+ createdAt: new Date("2026-05-01T18:45:00Z"),
124
+ },
125
+ ] as any);
126
+
127
+ const result = await getDashboardStats({
128
+ clientId,
129
+ startDate: "2026-05-01T00:00:00.000Z",
130
+ endDate: "2026-05-01T23:59:59.999Z",
44
131
  });
132
+
133
+ expect(result.charts.volumeGranularity).toBe("hour");
134
+ // A full day of hourly buckets, with no gaps, sorted chronologically.
135
+ expect(result.charts.volumeData).toHaveLength(24);
136
+ expect(result.charts.volumeData[0].date).toBe("2026-05-01T00:00");
137
+ expect(result.charts.volumeData[23].date).toBe("2026-05-01T23:00");
138
+
139
+ const populated = result.charts.volumeData.filter((d) => d.completed > 0);
140
+ expect(populated).toHaveLength(2);
141
+ expect(populated.map((d) => d.date)).toEqual([
142
+ "2026-05-01T09:00",
143
+ "2026-05-01T18:00",
144
+ ]);
145
+
146
+ const empty = result.charts.volumeData.filter((d) => d.completed === 0);
147
+ expect(empty).toHaveLength(22);
45
148
  });
46
149
  });