@talkpilot/core-db 1.3.3 → 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.
- package/.claude/settings.local.json +11 -0
- package/DEVELOPMENT.md +28 -0
- package/README.md +51 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/municipal/index.d.ts +1 -0
- package/dist/municipal/index.d.ts.map +1 -1
- package/dist/municipal/index.js +1 -0
- package/dist/municipal/index.js.map +1 -1
- package/dist/municipal/muniIssues/index.d.ts +7 -0
- package/dist/municipal/muniIssues/index.d.ts.map +1 -0
- package/dist/municipal/muniIssues/index.js +29 -0
- package/dist/municipal/muniIssues/index.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.constants.d.ts +14 -0
- package/dist/municipal/muniIssues/muniIssues.constants.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.constants.js +17 -0
- package/dist/municipal/muniIssues/muniIssues.constants.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.getters.d.ts +7 -0
- package/dist/municipal/muniIssues/muniIssues.getters.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.getters.js +24 -0
- package/dist/municipal/muniIssues/muniIssues.getters.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.schema.d.ts +4 -0
- package/dist/municipal/muniIssues/muniIssues.schema.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.schema.js +63 -0
- package/dist/municipal/muniIssues/muniIssues.schema.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.setters.d.ts +4 -0
- package/dist/municipal/muniIssues/muniIssues.setters.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.setters.js +22 -0
- package/dist/municipal/muniIssues/muniIssues.setters.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.setup.d.ts +3 -0
- package/dist/municipal/muniIssues/muniIssues.setup.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.setup.js +61 -0
- package/dist/municipal/muniIssues/muniIssues.setup.js.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.types.d.ts +30 -0
- package/dist/municipal/muniIssues/muniIssues.types.d.ts.map +1 -0
- package/dist/municipal/muniIssues/muniIssues.types.js +3 -0
- package/dist/municipal/muniIssues/muniIssues.types.js.map +1 -0
- package/dist/municipal/tickets/index.d.ts +2 -1
- package/dist/municipal/tickets/index.d.ts.map +1 -1
- package/dist/municipal/tickets/index.js +1 -0
- package/dist/municipal/tickets/index.js.map +1 -1
- package/dist/municipal/tickets/tickets.constants.d.ts +7 -0
- package/dist/municipal/tickets/tickets.constants.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.constants.js +10 -0
- package/dist/municipal/tickets/tickets.constants.js.map +1 -0
- package/dist/municipal/tickets/tickets.deprecated.getters.d.ts +12 -0
- package/dist/municipal/tickets/tickets.deprecated.getters.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.deprecated.getters.js +131 -0
- package/dist/municipal/tickets/tickets.deprecated.getters.js.map +1 -0
- package/dist/municipal/tickets/tickets.getters.d.ts +0 -11
- package/dist/municipal/tickets/tickets.getters.d.ts.map +1 -1
- package/dist/municipal/tickets/tickets.getters.js +0 -128
- package/dist/municipal/tickets/tickets.getters.js.map +1 -1
- package/dist/municipal/tickets/tickets.statistics.aggregation.d.ts +35 -0
- package/dist/municipal/tickets/tickets.statistics.aggregation.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.aggregation.js +86 -0
- package/dist/municipal/tickets/tickets.statistics.aggregation.js.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.dates.d.ts +7 -0
- package/dist/municipal/tickets/tickets.statistics.dates.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.dates.js +40 -0
- package/dist/municipal/tickets/tickets.statistics.dates.js.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.getters.d.ts +8 -0
- package/dist/municipal/tickets/tickets.statistics.getters.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.getters.js +44 -0
- package/dist/municipal/tickets/tickets.statistics.getters.js.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.pipeline.d.ts +53 -0
- package/dist/municipal/tickets/tickets.statistics.pipeline.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.pipeline.js +112 -0
- package/dist/municipal/tickets/tickets.statistics.pipeline.js.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.utils.d.ts +7 -0
- package/dist/municipal/tickets/tickets.statistics.utils.d.ts.map +1 -0
- package/dist/municipal/tickets/tickets.statistics.utils.js +40 -0
- package/dist/municipal/tickets/tickets.statistics.utils.js.map +1 -0
- package/dist/municipal/tickets/tickets.types.d.ts +14 -5
- package/dist/municipal/tickets/tickets.types.d.ts.map +1 -1
- package/dist/talkpilot/calls/calls.constants.d.ts +17 -0
- package/dist/talkpilot/calls/calls.constants.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.constants.js +20 -0
- package/dist/talkpilot/calls/calls.constants.js.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.getters.d.ts +28 -0
- package/dist/talkpilot/calls/calls.statistics.getters.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.getters.js +424 -0
- package/dist/talkpilot/calls/calls.statistics.getters.js.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.ticketScope.d.ts +12 -0
- package/dist/talkpilot/calls/calls.statistics.ticketScope.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.ticketScope.js +37 -0
- package/dist/talkpilot/calls/calls.statistics.ticketScope.js.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.tickets.d.ts +17 -0
- package/dist/talkpilot/calls/calls.statistics.tickets.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.tickets.js +33 -0
- package/dist/talkpilot/calls/calls.statistics.tickets.js.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.types.d.ts +39 -0
- package/dist/talkpilot/calls/calls.statistics.types.d.ts.map +1 -0
- package/dist/talkpilot/calls/calls.statistics.types.js +3 -0
- package/dist/talkpilot/calls/calls.statistics.types.js.map +1 -0
- package/dist/talkpilot/calls/calls.types.d.ts +1 -2
- package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
- package/dist/talkpilot/calls/calls.types.js +0 -3
- package/dist/talkpilot/calls/calls.types.js.map +1 -1
- package/dist/talkpilot/calls/index.d.ts +3 -0
- package/dist/talkpilot/calls/index.d.ts.map +1 -1
- package/dist/talkpilot/calls/index.js +3 -0
- package/dist/talkpilot/calls/index.js.map +1 -1
- package/dist/test-utils/factories/index.d.ts +1 -0
- package/dist/test-utils/factories/index.d.ts.map +1 -1
- package/dist/test-utils/factories/index.js +1 -0
- package/dist/test-utils/factories/index.js.map +1 -1
- package/dist/test-utils/factories/municipal/muniIssues.d.ts +10 -0
- package/dist/test-utils/factories/municipal/muniIssues.d.ts.map +1 -0
- package/dist/test-utils/factories/municipal/muniIssues.js +30 -0
- package/dist/test-utils/factories/municipal/muniIssues.js.map +1 -0
- package/dist/utils/date.utils.d.ts +49 -0
- package/dist/utils/date.utils.d.ts.map +1 -0
- package/dist/utils/date.utils.js +103 -0
- package/dist/utils/date.utils.js.map +1 -0
- package/dist/utils/statistics.aggregation.d.ts +20 -0
- package/dist/utils/statistics.aggregation.d.ts.map +1 -0
- package/dist/utils/statistics.aggregation.js +43 -0
- package/dist/utils/statistics.aggregation.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/municipal/index.ts +1 -0
- package/src/municipal/muniIssues/__tests__/muniIssues.setters.spec.ts +92 -0
- package/src/municipal/muniIssues/__tests__/muniIssues.setup.spec.ts +92 -0
- package/src/municipal/muniIssues/index.ts +17 -0
- package/src/municipal/muniIssues/muniIssues.constants.ts +18 -0
- package/src/municipal/muniIssues/muniIssues.getters.ts +24 -0
- package/src/municipal/muniIssues/muniIssues.schema.ts +66 -0
- package/src/municipal/muniIssues/muniIssues.setters.ts +24 -0
- package/src/municipal/muniIssues/muniIssues.setup.ts +65 -0
- package/src/municipal/muniIssues/muniIssues.types.ts +37 -0
- package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +1 -37
- package/src/municipal/tickets/__tests__/tickets.statistics.spec.ts +51 -0
- package/src/municipal/tickets/index.ts +2 -1
- package/src/municipal/tickets/tickets.constants.ts +8 -0
- package/src/municipal/tickets/tickets.getters.ts +0 -140
- package/src/municipal/tickets/tickets.statistics.aggregation.ts +96 -0
- package/src/municipal/tickets/tickets.statistics.getters.ts +71 -0
- package/src/municipal/tickets/tickets.types.ts +19 -9
- package/src/talkpilot/calls/__tests__/calls.statistics.spec.ts +344 -0
- package/src/talkpilot/calls/calls.constants.ts +20 -0
- package/src/talkpilot/calls/calls.statistics.getters.ts +587 -0
- package/src/talkpilot/calls/calls.statistics.types.ts +44 -0
- package/src/talkpilot/calls/calls.types.ts +4 -2
- package/src/talkpilot/calls/index.ts +3 -0
- package/src/test-utils/factories/index.ts +1 -0
- package/src/test-utils/factories/municipal/muniIssues.ts +32 -0
- package/src/utils/date.utils.ts +116 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Mongo collection name for reported municipal issues. */
|
|
2
|
+
export const MUNI_ISSUES_COLLECTION = "muniIssues";
|
|
3
|
+
|
|
4
|
+
/** Sync state of the issue's Jira ticket at write time. */
|
|
5
|
+
export const MUNI_ISSUE_SYNC_STATUSES = ["created", "failed"] as const;
|
|
6
|
+
|
|
7
|
+
/** Max length for the long free-text fields — well above the ~2000-char target. */
|
|
8
|
+
export const MUNI_ISSUE_TEXT_MAX_LENGTH = 5000;
|
|
9
|
+
|
|
10
|
+
/** Max length for the short identity fields (a person's name and role). */
|
|
11
|
+
export const MUNI_ISSUE_NAME_MAX_LENGTH = 200;
|
|
12
|
+
|
|
13
|
+
/** MongoDB error 48 (NamespaceExists): collection already exists, e.g. concurrent createCollection. */
|
|
14
|
+
export const NAMESPACE_EXISTS = 48;
|
|
15
|
+
|
|
16
|
+
/** Reject (not just flag) writes that fail the $jsonSchema validator. */
|
|
17
|
+
export const VALIDATION_LEVEL = "strict" as const;
|
|
18
|
+
export const VALIDATION_ACTION = "error" as const;
|
|
@@ -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
|
+
};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
findTicketsByCallSid,
|
|
3
|
-
getTicketsCountByCityAndDateRange,
|
|
4
|
-
} from "../tickets.getters";
|
|
1
|
+
import { findTicketsByCallSid } from "../tickets.getters";
|
|
5
2
|
import { setDb } from "../../index";
|
|
6
3
|
import { Db, Collection } from "mongodb";
|
|
7
4
|
import { createTicket } from "../../../test-utils/factories";
|
|
@@ -30,37 +27,4 @@ describe("tickets getters", () => {
|
|
|
30
27
|
expect(mockCollection.find).toHaveBeenCalledWith({ callSid: "123" });
|
|
31
28
|
expect(result[0].callSid).toBe("123");
|
|
32
29
|
});
|
|
33
|
-
|
|
34
|
-
it("getTicketsCountByCityAndDateRange should not count tickets without callSid", async () => {
|
|
35
|
-
const aggregateNext = jest.fn().mockResolvedValue({ n: 1 });
|
|
36
|
-
mockCollection = {
|
|
37
|
-
...mockCollection,
|
|
38
|
-
aggregate: jest.fn().mockReturnValue({
|
|
39
|
-
next: aggregateNext,
|
|
40
|
-
}),
|
|
41
|
-
};
|
|
42
|
-
mockDb = {
|
|
43
|
-
collection: jest.fn().mockReturnValue(mockCollection),
|
|
44
|
-
};
|
|
45
|
-
setDb(mockDb as Db);
|
|
46
|
-
|
|
47
|
-
const result = await getTicketsCountByCityAndDateRange(
|
|
48
|
-
"ashdod",
|
|
49
|
-
"2026-01-01",
|
|
50
|
-
"2026-01-31",
|
|
51
|
-
"Asia/Jerusalem",
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
expect(mockCollection.aggregate).toHaveBeenCalledWith(
|
|
55
|
-
expect.arrayContaining([
|
|
56
|
-
{
|
|
57
|
-
$match: {
|
|
58
|
-
cityName: "ashdod",
|
|
59
|
-
callSid: { $exists: true, $nin: [null, ""] },
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
]),
|
|
63
|
-
);
|
|
64
|
-
expect(result).toBe(1);
|
|
65
|
-
});
|
|
66
30
|
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
findCallSidsWithDraftTicketsByCity,
|
|
3
|
+
findCallSidsWithTicketsByCity,
|
|
4
|
+
getTicketsCollection,
|
|
5
|
+
ticketStatsDateScopeFromYmd,
|
|
6
|
+
} from "../../../index";
|
|
7
|
+
import { createTicket } from "../../../test-utils/factories";
|
|
8
|
+
|
|
9
|
+
const CITY = "tests" as const;
|
|
10
|
+
const TZ = "Asia/Jerusalem";
|
|
11
|
+
const mayScope = () => ticketStatsDateScopeFromYmd("2026-05-01", "2026-05-31", TZ);
|
|
12
|
+
|
|
13
|
+
describe("tickets statistics getters", () => {
|
|
14
|
+
it("should scope callSid lookups to the provided createdAt range", async () => {
|
|
15
|
+
await getTicketsCollection().insertMany([
|
|
16
|
+
createTicket({
|
|
17
|
+
cityName: CITY,
|
|
18
|
+
callSid: "CA-in-range",
|
|
19
|
+
createdAt: new Date("2026-05-15T10:00:00.000Z"),
|
|
20
|
+
updatedAt: new Date("2026-05-15T10:00:00.000Z"),
|
|
21
|
+
}),
|
|
22
|
+
createTicket({
|
|
23
|
+
cityName: CITY,
|
|
24
|
+
callSid: "CA-out-of-range",
|
|
25
|
+
createdAt: new Date("2024-01-01T10:00:00.000Z"),
|
|
26
|
+
updatedAt: new Date("2024-01-01T10:00:00.000Z"),
|
|
27
|
+
}),
|
|
28
|
+
]);
|
|
29
|
+
expect(await findCallSidsWithTicketsByCity(CITY, mayScope())).toEqual(["CA-in-range"]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should treat whitespace-only event_subject_id as draft", async () => {
|
|
33
|
+
await getTicketsCollection().insertMany([
|
|
34
|
+
createTicket({
|
|
35
|
+
cityName: CITY,
|
|
36
|
+
callSid: "CA-draft-whitespace",
|
|
37
|
+
createdAt: new Date("2026-05-15T10:00:00.000Z"),
|
|
38
|
+
updatedAt: new Date("2026-05-15T10:00:00.000Z"),
|
|
39
|
+
externalCallFields: { event_subject_id: " \t " },
|
|
40
|
+
}),
|
|
41
|
+
createTicket({
|
|
42
|
+
cityName: CITY,
|
|
43
|
+
callSid: "CA-not-draft",
|
|
44
|
+
createdAt: new Date("2026-05-16T10:00:00.000Z"),
|
|
45
|
+
updatedAt: new Date("2026-05-16T10:00:00.000Z"),
|
|
46
|
+
externalCallFields: { event_subject_id: "100" },
|
|
47
|
+
}),
|
|
48
|
+
]);
|
|
49
|
+
expect(await findCallSidsWithDraftTicketsByCity(CITY, mayScope())).toEqual(["CA-draft-whitespace"]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Label used when a ticket has no resolvable department/subject. */
|
|
2
|
+
export const UNCLASSIFIED = "ללא מחלקה";
|
|
3
|
+
|
|
4
|
+
/** Max execution time (ms) for ticket statistics aggregations. */
|
|
5
|
+
export const STATS_MAX_TIME_MS = 30_000;
|
|
6
|
+
|
|
7
|
+
/** Default look-back window (days) for ticket statistics date ranges. */
|
|
8
|
+
export const DEFAULT_LOOKBACK_DAYS = 30;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CityName, getDb, ObjectId, Ticket } from "../index";
|
|
2
|
-
import type { SubjectStatsItem } from "./tickets.types";
|
|
3
2
|
import { Collection, Filter, ObjectId as MongoObjectId } from "mongodb";
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -68,145 +67,6 @@ export const deleteTicket = async (ticketId: string): Promise<boolean> => {
|
|
|
68
67
|
return result.deletedCount > 0;
|
|
69
68
|
};
|
|
70
69
|
|
|
71
|
-
/**
|
|
72
|
-
* Count tickets by city and date range (createdAt converted to given timezone).
|
|
73
|
-
* Used as "open" tickets count when status is not available.
|
|
74
|
-
*/
|
|
75
|
-
export async function getTicketsCountByCityAndDateRange(
|
|
76
|
-
cityName: string,
|
|
77
|
-
startStr: string,
|
|
78
|
-
endStr: string,
|
|
79
|
-
timezone: string,
|
|
80
|
-
): Promise<number> {
|
|
81
|
-
const doc = await getTicketsCollection()
|
|
82
|
-
.aggregate<{ n: number }>([
|
|
83
|
-
{ $match: { cityName, callSid: { $exists: true, $nin: [null, ""] } } },
|
|
84
|
-
{
|
|
85
|
-
$addFields: {
|
|
86
|
-
dateLocal: {
|
|
87
|
-
$dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
},
|
|
91
|
-
{ $match: { dateLocal: { $gte: startStr, $lte: endStr } } },
|
|
92
|
-
{ $count: "n" },
|
|
93
|
-
])
|
|
94
|
-
.next();
|
|
95
|
-
return doc?.n ?? 0;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Count of tickets by department (subject) from departmentsSubjects. Different departments (subject_id) are grouped, and sub-subjects are unified under their department.
|
|
100
|
-
* Date filter: createdAt in [startStr, endStr] (in the given timezone). Fallback to "Unclassified" when no match is found in the lookup.
|
|
101
|
-
*/
|
|
102
|
-
export async function getTicketsSubjectStats(
|
|
103
|
-
cityName: string,
|
|
104
|
-
startStr: string,
|
|
105
|
-
endStr: string,
|
|
106
|
-
timezone: string,
|
|
107
|
-
): Promise<SubjectStatsItem[]> {
|
|
108
|
-
const coll = getTicketsCollection();
|
|
109
|
-
const rows = await coll
|
|
110
|
-
.aggregate<{ _id: string; subject: string; count: number }>([
|
|
111
|
-
{ $match: { cityName } },
|
|
112
|
-
{
|
|
113
|
-
$addFields: {
|
|
114
|
-
dateLocal: {
|
|
115
|
-
$dateToString: { format: "%Y-%m-%d", date: "$createdAt", timezone },
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
{ $match: { dateLocal: { $gte: startStr, $lte: endStr } } },
|
|
120
|
-
{
|
|
121
|
-
$addFields: {
|
|
122
|
-
effectiveSubjectId: {
|
|
123
|
-
$ifNull: [
|
|
124
|
-
"$externalCallFields.event_subject_id",
|
|
125
|
-
{
|
|
126
|
-
$ifNull: [
|
|
127
|
-
"$externalCallFields.event_sub_subject_id",
|
|
128
|
-
"$externalCallFields.event_sub_subject_id2",
|
|
129
|
-
],
|
|
130
|
-
},
|
|
131
|
-
],
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
$lookup: {
|
|
137
|
-
from: "departmentsSubjects",
|
|
138
|
-
let: { sid: "$effectiveSubjectId", c: cityName },
|
|
139
|
-
pipeline: [
|
|
140
|
-
{
|
|
141
|
-
$match: {
|
|
142
|
-
$expr: {
|
|
143
|
-
$and: [
|
|
144
|
-
{ $eq: ["$cityName", "$$c"] },
|
|
145
|
-
{
|
|
146
|
-
$or: [
|
|
147
|
-
{ $eq: ["$subject_id", { $ifNull: ["$$sid", ""] }] },
|
|
148
|
-
{
|
|
149
|
-
$eq: ["$sub_subject_id", { $ifNull: ["$$sid", ""] }],
|
|
150
|
-
},
|
|
151
|
-
{
|
|
152
|
-
$eq: ["$sub_subject_id2", { $ifNull: ["$$sid", ""] }],
|
|
153
|
-
},
|
|
154
|
-
],
|
|
155
|
-
},
|
|
156
|
-
],
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
{ $limit: 1 },
|
|
161
|
-
],
|
|
162
|
-
as: "subj",
|
|
163
|
-
},
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
$addFields: {
|
|
167
|
-
subject_id: {
|
|
168
|
-
$cond: {
|
|
169
|
-
if: { $gt: [{ $size: "$subj" }, 0] },
|
|
170
|
-
then: {
|
|
171
|
-
$ifNull: [{ $arrayElemAt: ["$subj.subject_id", 0] }, ""],
|
|
172
|
-
},
|
|
173
|
-
else: "",
|
|
174
|
-
},
|
|
175
|
-
},
|
|
176
|
-
subject: {
|
|
177
|
-
$cond: {
|
|
178
|
-
if: { $gt: [{ $size: "$subj" }, 0] },
|
|
179
|
-
then: {
|
|
180
|
-
$ifNull: [
|
|
181
|
-
{ $arrayElemAt: ["$subj.subjectName", 0] },
|
|
182
|
-
"Unclassified",
|
|
183
|
-
],
|
|
184
|
-
},
|
|
185
|
-
else: "Unclassified",
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
$group: {
|
|
192
|
-
_id: "$subject_id",
|
|
193
|
-
subject: { $first: "$subject" },
|
|
194
|
-
count: { $sum: 1 },
|
|
195
|
-
},
|
|
196
|
-
},
|
|
197
|
-
{ $sort: { count: -1 } },
|
|
198
|
-
])
|
|
199
|
-
.toArray();
|
|
200
|
-
|
|
201
|
-
const total = rows.reduce((s: number, r: any) => s + r.count, 0);
|
|
202
|
-
return rows.map((r: any) => ({
|
|
203
|
-
subject_name: r.subject,
|
|
204
|
-
subject_id: r._id,
|
|
205
|
-
count: r.count,
|
|
206
|
-
percentage: total > 0 ? Math.round((r.count / total) * 100) : 0,
|
|
207
|
-
}));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
70
|
export const findTicketByQuery = async (query: Partial<Ticket>) => {
|
|
211
71
|
return await getTicketsCollection().findOne(query);
|
|
212
72
|
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { CityName } from "../utils/types";
|
|
2
|
+
import { STATS_MAX_TIME_MS, UNCLASSIFIED } from "./tickets.constants";
|
|
3
|
+
import { getTicketsCollection } from "./tickets.getters";
|
|
4
|
+
import type { TicketStatsDateRange } from "./tickets.types";
|
|
5
|
+
|
|
6
|
+
const effectiveSubjectIdExpr = {
|
|
7
|
+
$ifNull: [
|
|
8
|
+
"$externalCallFields.event_subject_id",
|
|
9
|
+
{
|
|
10
|
+
$ifNull: [
|
|
11
|
+
"$externalCallFields.event_sub_subject_id",
|
|
12
|
+
"$externalCallFields.event_sub_subject_id2",
|
|
13
|
+
],
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const departmentsSubjectsLookup = (cityName: CityName) => ({
|
|
19
|
+
$lookup: {
|
|
20
|
+
from: "departmentsSubjects",
|
|
21
|
+
let: { sid: "$effectiveSubjectId", c: cityName },
|
|
22
|
+
pipeline: [
|
|
23
|
+
{
|
|
24
|
+
$match: {
|
|
25
|
+
$expr: {
|
|
26
|
+
$and: [
|
|
27
|
+
{ $eq: ["$cityName", "$$c"] },
|
|
28
|
+
{
|
|
29
|
+
$or: [
|
|
30
|
+
{ $eq: ["$subject_id", { $ifNull: ["$$sid", ""] }] },
|
|
31
|
+
{ $eq: ["$sub_subject_id", { $ifNull: ["$$sid", ""] }] },
|
|
32
|
+
{ $eq: ["$sub_subject_id2", { $ifNull: ["$$sid", ""] }] },
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{ $limit: 1 },
|
|
40
|
+
],
|
|
41
|
+
as: "subj",
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const subjectLabelExpr = {
|
|
46
|
+
$cond: {
|
|
47
|
+
if: { $gt: [{ $size: "$subj" }, 0] },
|
|
48
|
+
then: {
|
|
49
|
+
$ifNull: [{ $arrayElemAt: ["$subj.subjectName", 0] }, UNCLASSIFIED],
|
|
50
|
+
},
|
|
51
|
+
else: UNCLASSIFIED,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ticketDateMatch = (dateRange: TicketStatsDateRange) => ({
|
|
56
|
+
createdAt: { $gte: dateRange.from, $lte: dateRange.to },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const isDraftSubjectIdExpr = {
|
|
60
|
+
$or: [
|
|
61
|
+
{ $eq: [{ $type: "$externalCallFields.event_subject_id" }, "missing"] },
|
|
62
|
+
{ $eq: ["$externalCallFields.event_subject_id", null] },
|
|
63
|
+
{
|
|
64
|
+
$regexMatch: {
|
|
65
|
+
input: { $ifNull: ["$externalCallFields.event_subject_id", ""] },
|
|
66
|
+
regex: /^\s*$/,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const ticketsWithCallSidMatch = (
|
|
73
|
+
cityName: CityName,
|
|
74
|
+
dateRange: TicketStatsDateRange,
|
|
75
|
+
) => ({
|
|
76
|
+
cityName,
|
|
77
|
+
callSid: { $exists: true, $nin: [null, ""] },
|
|
78
|
+
...ticketDateMatch(dateRange),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export const subjectResolutionStages = (
|
|
82
|
+
cityName: CityName,
|
|
83
|
+
): Record<string, unknown>[] => [
|
|
84
|
+
{ $addFields: { effectiveSubjectId: effectiveSubjectIdExpr } },
|
|
85
|
+
departmentsSubjectsLookup(cityName),
|
|
86
|
+
{ $addFields: { subject: subjectLabelExpr } },
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
export const runTicketStatsAggregate = async <T>(
|
|
90
|
+
pipeline: Record<string, unknown>[],
|
|
91
|
+
): Promise<T[]> => {
|
|
92
|
+
const rows = await getTicketsCollection()
|
|
93
|
+
.aggregate(pipeline, { maxTimeMS: STATS_MAX_TIME_MS })
|
|
94
|
+
.toArray();
|
|
95
|
+
return rows as T[];
|
|
96
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { CityName } from "../utils/types";
|
|
2
|
+
import {
|
|
3
|
+
DAY_MS,
|
|
4
|
+
endOfCalendarDayInTz,
|
|
5
|
+
startOfCalendarDayInTz,
|
|
6
|
+
} from "../../utils/date.utils";
|
|
7
|
+
import { DEFAULT_LOOKBACK_DAYS } from "./tickets.constants";
|
|
8
|
+
import {
|
|
9
|
+
isDraftSubjectIdExpr,
|
|
10
|
+
runTicketStatsAggregate,
|
|
11
|
+
subjectResolutionStages,
|
|
12
|
+
ticketsWithCallSidMatch,
|
|
13
|
+
} from "./tickets.statistics.aggregation";
|
|
14
|
+
import type {
|
|
15
|
+
TicketStatsDateRange,
|
|
16
|
+
TicketStatsDateScope,
|
|
17
|
+
TicketSubjectRow,
|
|
18
|
+
} from "./tickets.types";
|
|
19
|
+
|
|
20
|
+
export const resolveTicketStatsDateRange = (
|
|
21
|
+
dateScope?: TicketStatsDateScope,
|
|
22
|
+
): TicketStatsDateRange => {
|
|
23
|
+
const to = dateScope?.to ?? new Date();
|
|
24
|
+
const from =
|
|
25
|
+
dateScope?.from ?? new Date(to.getTime() - DEFAULT_LOOKBACK_DAYS * DAY_MS);
|
|
26
|
+
return { from, to };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const ticketStatsDateScopeFromYmd = (
|
|
30
|
+
startStr: string,
|
|
31
|
+
endStr: string,
|
|
32
|
+
timezone: string,
|
|
33
|
+
): TicketStatsDateScope => ({
|
|
34
|
+
from: startOfCalendarDayInTz(startStr, timezone),
|
|
35
|
+
to: endOfCalendarDayInTz(endStr, timezone),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const findCallSidsWithTicketsByCity = async (
|
|
39
|
+
cityName: CityName,
|
|
40
|
+
dateScope?: TicketStatsDateScope,
|
|
41
|
+
): Promise<string[]> => {
|
|
42
|
+
const rows = await runTicketStatsAggregate<{ _id: unknown }>([
|
|
43
|
+
{ $match: ticketsWithCallSidMatch(cityName, resolveTicketStatsDateRange(dateScope)) },
|
|
44
|
+
{ $group: { _id: "$callSid" } },
|
|
45
|
+
]);
|
|
46
|
+
return rows.map((r) => String(r._id)).filter(Boolean);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const findCallSidsWithDraftTicketsByCity = async (
|
|
50
|
+
cityName: CityName,
|
|
51
|
+
dateScope?: TicketStatsDateScope,
|
|
52
|
+
): Promise<string[]> => {
|
|
53
|
+
const rows = await runTicketStatsAggregate<{ _id: unknown }>([
|
|
54
|
+
{ $match: ticketsWithCallSidMatch(cityName, resolveTicketStatsDateRange(dateScope)) },
|
|
55
|
+
{ $match: { $expr: isDraftSubjectIdExpr } },
|
|
56
|
+
{ $group: { _id: "$callSid" } },
|
|
57
|
+
]);
|
|
58
|
+
return rows.map((r) => String(r._id)).filter(Boolean);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const findTicketSubjectRows = async (
|
|
62
|
+
cityName: CityName,
|
|
63
|
+
dateScope?: TicketStatsDateScope,
|
|
64
|
+
): Promise<TicketSubjectRow[]> => {
|
|
65
|
+
const rows = await runTicketStatsAggregate<{ callSid: unknown; subject: unknown }>([
|
|
66
|
+
{ $match: ticketsWithCallSidMatch(cityName, resolveTicketStatsDateRange(dateScope)) },
|
|
67
|
+
...subjectResolutionStages(cityName),
|
|
68
|
+
{ $project: { _id: 0, callSid: 1, subject: 1 } },
|
|
69
|
+
]);
|
|
70
|
+
return rows.map((r) => ({ callSid: String(r.callSid), subject: String(r.subject) }));
|
|
71
|
+
};
|