@talkpilot/core-db 1.1.19 → 1.2.0

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 (130) hide show
  1. package/.cursor/rules/development.mdc +65 -65
  2. package/DEVELOPMENT.md +98 -98
  3. package/README.md +160 -160
  4. package/dist/talkpilot/calls/calls.getters.d.ts +2 -1
  5. package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -1
  6. package/dist/talkpilot/calls/calls.getters.js +176 -0
  7. package/dist/talkpilot/calls/calls.getters.js.map +1 -1
  8. package/dist/talkpilot/calls/calls.types.d.ts +48 -0
  9. package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
  10. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +1 -0
  11. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  12. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +13 -0
  13. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  14. package/jest.config.js +19 -19
  15. package/package.json +45 -45
  16. package/src/__tests__/setup.ts +20 -20
  17. package/src/connection.ts +42 -42
  18. package/src/index.ts +16 -16
  19. package/src/municipal/__tests__/validation.spec.ts +62 -62
  20. package/src/municipal/cities/cities.getters.ts +50 -50
  21. package/src/municipal/cities/cities.types.ts +11 -11
  22. package/src/municipal/cities/index.ts +2 -2
  23. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +282 -282
  24. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +72 -72
  25. package/src/municipal/departmentsSubjects/index.ts +9 -9
  26. package/src/municipal/index.ts +21 -21
  27. package/src/municipal/mongodb-client.ts +61 -61
  28. package/src/municipal/streets/index.ts +2 -2
  29. package/src/municipal/streets/streets.getters.ts +125 -125
  30. package/src/municipal/streets/streets.types.ts +18 -18
  31. package/src/municipal/systemInstructions/__tests__/getters.spec.ts +113 -113
  32. package/src/municipal/systemInstructions/__tests__/setters.spec.ts +274 -274
  33. package/src/municipal/systemInstructions/index.ts +7 -7
  34. package/src/municipal/systemInstructions/instructions.getters.ts +57 -57
  35. package/src/municipal/systemInstructions/instructions.setters.ts +119 -119
  36. package/src/municipal/systemInstructions/instructions.types.ts +30 -30
  37. package/src/municipal/tickets/__tests__/tickets.getters.spec.ts +66 -66
  38. package/src/municipal/tickets/index.ts +2 -2
  39. package/src/municipal/tickets/tickets.getters.ts +261 -261
  40. package/src/municipal/tickets/tickets.types.ts +43 -43
  41. package/src/municipal/utils/types.ts +11 -11
  42. package/src/talkpilot/__tests__/db.spec.ts +38 -38
  43. package/src/talkpilot/__tests__/mongodb-client.spec.ts +18 -18
  44. package/src/talkpilot/__tests__/validation.spec.ts +68 -68
  45. package/src/talkpilot/agents/__tests__/agents.getters.spec.ts +29 -29
  46. package/src/talkpilot/agents/agents.getters.ts +34 -34
  47. package/src/talkpilot/agents/agents.types.ts +14 -14
  48. package/src/talkpilot/agents/index.ts +2 -2
  49. package/src/talkpilot/backgroundToolResults/__tests__/backgroundToolResults.getters.spec.ts +147 -147
  50. package/src/talkpilot/backgroundToolResults/backgroundToolResults.getters.ts +65 -65
  51. package/src/talkpilot/backgroundToolResults/backgroundToolResults.types.ts +23 -23
  52. package/src/talkpilot/backgroundToolResults/index.ts +2 -2
  53. package/src/talkpilot/calls/__tests__/callStats.utils.spec.ts +128 -128
  54. package/src/talkpilot/calls/__tests__/calls.spec.ts +252 -252
  55. package/src/talkpilot/calls/calls.getters.ts +446 -248
  56. package/src/talkpilot/calls/calls.types.ts +171 -115
  57. package/src/talkpilot/calls/index.ts +2 -2
  58. package/src/talkpilot/clientAudioBuffers/__tests__/clientAudioBuffer.getters.spec.ts +160 -160
  59. package/src/talkpilot/clientAudioBuffers/clientAudioBuffer.getters.ts +117 -117
  60. package/src/talkpilot/clientAudioBuffers/clientsAudioBuffers.types.ts +25 -25
  61. package/src/talkpilot/clientAudioBuffers/index.ts +2 -2
  62. package/src/talkpilot/clients/clients.getters.ts +16 -16
  63. package/src/talkpilot/clients/clients.types.ts +14 -14
  64. package/src/talkpilot/clients/index.ts +2 -2
  65. package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +106 -106
  66. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +44 -22
  67. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +94 -94
  68. package/src/talkpilot/clientsConfig/index.ts +2 -2
  69. package/src/talkpilot/flows/__tests__/flows.schema.spec.ts +67 -67
  70. package/src/talkpilot/flows/flows.getter.ts +14 -14
  71. package/src/talkpilot/flows/flows.schema.ts +153 -153
  72. package/src/talkpilot/flows/flows.types.ts +184 -184
  73. package/src/talkpilot/flows/index.ts +2 -2
  74. package/src/talkpilot/groups/__tests__/groups.spec.ts +90 -90
  75. package/src/talkpilot/groups/__tests__/phone.utils.spec.ts +32 -32
  76. package/src/talkpilot/groups/groups.getters.ts +30 -30
  77. package/src/talkpilot/groups/groups.types.ts +29 -29
  78. package/src/talkpilot/groups/index.ts +3 -3
  79. package/src/talkpilot/groups/phone.utils.ts +46 -46
  80. package/src/talkpilot/index.ts +29 -29
  81. package/src/talkpilot/leads/index.ts +2 -2
  82. package/src/talkpilot/leads/leads.getter.ts +6 -6
  83. package/src/talkpilot/leads/leads.schema.ts +33 -33
  84. package/src/talkpilot/leads/leads.types.ts +20 -20
  85. package/src/talkpilot/mongodb-client.ts +78 -78
  86. package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +247 -247
  87. package/src/talkpilot/phone_numbers/index.ts +2 -2
  88. package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +154 -154
  89. package/src/talkpilot/phone_numbers/phone_numbers.schema.ts +17 -17
  90. package/src/talkpilot/phone_numbers/phone_numbers.types.ts +30 -30
  91. package/src/talkpilot/plans/__tests__/plans.spec.ts +70 -70
  92. package/src/talkpilot/plans/index.ts +2 -2
  93. package/src/talkpilot/plans/plans.getters.ts +132 -132
  94. package/src/talkpilot/plans/plans.types.ts +89 -89
  95. package/src/talkpilot/results/index.ts +7 -7
  96. package/src/talkpilot/results/results.getter.ts +35 -35
  97. package/src/talkpilot/results/results.schema.ts +25 -25
  98. package/src/talkpilot/results/results.types.ts +34 -34
  99. package/src/talkpilot/retry_analyze/__tests__/retryAnalyze.getters.spec.ts +156 -156
  100. package/src/talkpilot/retry_analyze/index.ts +2 -2
  101. package/src/talkpilot/retry_analyze/retryAnalyze.getters.ts +75 -75
  102. package/src/talkpilot/retry_analyze/retryAnalyze.types.ts +13 -13
  103. package/src/talkpilot/sessions/__tests__/sessions.spec.ts +147 -147
  104. package/src/talkpilot/sessions/index.ts +2 -2
  105. package/src/talkpilot/sessions/sessions.getter.ts +92 -92
  106. package/src/talkpilot/sessions/sessions.schema.ts +34 -34
  107. package/src/talkpilot/sessions/sessions.types.ts +30 -30
  108. package/src/talkpilot/subscriptions/__tests__/subscriptions.getters.utils.spec.ts +45 -45
  109. package/src/talkpilot/subscriptions/index.ts +3 -3
  110. package/src/talkpilot/subscriptions/subscriptions.getters.ts +146 -146
  111. package/src/talkpilot/subscriptions/subscriptions.getters.utils.ts +33 -33
  112. package/src/talkpilot/subscriptions/subscriptions.types.ts +66 -66
  113. package/src/talkpilot/utils/__tests__/query.utils.spec.ts +49 -49
  114. package/src/talkpilot/utils/query.utils.ts +21 -21
  115. package/src/test-utils/db-utils.ts +24 -24
  116. package/src/test-utils/factories/index.ts +12 -12
  117. package/src/test-utils/factories/municipal/cities.ts +16 -16
  118. package/src/test-utils/factories/municipal/departmentsSubjects.ts +37 -37
  119. package/src/test-utils/factories/municipal/streets.ts +22 -22
  120. package/src/test-utils/factories/municipal/tickets.ts +39 -39
  121. package/src/test-utils/factories/talkpilot/agents.ts +19 -19
  122. package/src/test-utils/factories/talkpilot/calls.ts +37 -37
  123. package/src/test-utils/factories/talkpilot/clientAudioBuffers.ts +20 -20
  124. package/src/test-utils/factories/talkpilot/clientsConfig.ts +18 -18
  125. package/src/test-utils/factories/talkpilot/flows.ts +33 -33
  126. package/src/test-utils/factories/talkpilot/groups.ts +33 -33
  127. package/src/test-utils/factories/talkpilot/phone_numbers.ts +22 -22
  128. package/src/test-utils/factories/talkpilot/sessions.ts +35 -35
  129. package/src/utils/validation.ts +23 -23
  130. package/tsconfig.json +23 -23
@@ -1,117 +1,117 @@
1
- import { getDb } from "../index";
2
- import {
3
- ClientAudioBufferDoc,
4
- ClientAudioBufferType,
5
- CreateAudioBufferInput,
6
- } from "./clientsAudioBuffers.types";
7
- import { Collection, InsertOneResult, DeleteResult } from "mongodb";
8
-
9
- const getClientAudioBuffersCollection =
10
- (): Collection<ClientAudioBufferType> => {
11
- return getDb().collection<ClientAudioBufferType>("clientAudioBuffers");
12
- };
13
-
14
- /**
15
- * Deletes all audio buffers associated with a specific callSid.
16
- * Useful for clearing context between different tickets in the same call.
17
- */
18
- const deleteAudioBuffersByCallSid = async (
19
- callSid: string,
20
- ): Promise<DeleteResult> => {
21
- return getClientAudioBuffersCollection().deleteMany({ callSid });
22
- };
23
-
24
- const insertClientAudioBuffer = async (
25
- audioBufferInput: CreateAudioBufferInput,
26
- ): Promise<InsertOneResult<ClientAudioBufferType>> => {
27
- const collection = getClientAudioBuffersCollection();
28
- const realtimeDate = new Date();
29
- const ttlMs = audioBufferInput.ttlMs ?? 3600_000;
30
- const expiresAt = new Date(realtimeDate.getTime() + ttlMs);
31
-
32
- const inputFieldsForDocument = {
33
- callSid: audioBufferInput.callSid,
34
- clientId: audioBufferInput.clientId,
35
- bucketStartMs: audioBufferInput.bucketStartMs,
36
- bucketDurationMs: audioBufferInput.bucketDurationMs,
37
- data: audioBufferInput.data,
38
- };
39
- const clientAudioBufferMongoDocument: ClientAudioBufferType = {
40
- ...inputFieldsForDocument,
41
- codec: "mulaw",
42
- sampleRateHz: 8000,
43
- channels: 1,
44
- createdAt: realtimeDate,
45
- expiresAt: expiresAt,
46
- };
47
- return collection.insertOne(clientAudioBufferMongoDocument);
48
- };
49
-
50
- const findClientAudioBuffersByCallSidAndRange = async (
51
- callSid: string,
52
- startMs?: number,
53
- endMs?: number,
54
- ): Promise<ClientAudioBufferDoc[]> => {
55
- const collection = getClientAudioBuffersCollection();
56
- if (startMs === undefined) {
57
- return collection.find({ callSid }).toArray();
58
- }
59
- const endMsToUse = endMs ?? Date.now();
60
- return collection
61
- .find({
62
- callSid,
63
- bucketStartMs: {
64
- $gte: startMs,
65
- $lte: endMsToUse,
66
- },
67
- })
68
- .toArray();
69
- };
70
-
71
- const getAudioChunksByCallSid = async (
72
- callSid: string,
73
- limit?: number,
74
- ): Promise<Buffer[]> => {
75
- const query = getClientAudioBuffersCollection().find({ callSid });
76
- if (limit) {
77
- query.sort({ bucketStartMs: -1 }).limit(limit);
78
- } else {
79
- query.sort({ bucketStartMs: 1 });
80
- }
81
-
82
- const docs = await query.toArray();
83
-
84
- if (!docs.length) {
85
- return [];
86
- }
87
- const orderedDocs = limit ? docs.reverse() : docs;
88
-
89
- return orderedDocs.map((chunk) => {
90
- const data: any = chunk.data;
91
- if (Buffer.isBuffer(data)) return data;
92
- if (data?.buffer) return Buffer.from(data.buffer);
93
- return Buffer.from(data);
94
- });
95
- };
96
-
97
- const getMergedAudioByCallSid = async (
98
- callSid: string,
99
- limit: number,
100
- ): Promise<Buffer> => {
101
- const buffers = await getAudioChunksByCallSid(callSid, limit);
102
-
103
- if (!buffers.length) {
104
- return Buffer.alloc(0);
105
- }
106
-
107
- return Buffer.concat(buffers);
108
- };
109
-
110
- export {
111
- insertClientAudioBuffer,
112
- getClientAudioBuffersCollection,
113
- findClientAudioBuffersByCallSidAndRange,
114
- getAudioChunksByCallSid,
115
- getMergedAudioByCallSid,
116
- deleteAudioBuffersByCallSid,
117
- };
1
+ import { getDb } from "../index";
2
+ import {
3
+ ClientAudioBufferDoc,
4
+ ClientAudioBufferType,
5
+ CreateAudioBufferInput,
6
+ } from "./clientsAudioBuffers.types";
7
+ import { Collection, InsertOneResult, DeleteResult } from "mongodb";
8
+
9
+ const getClientAudioBuffersCollection =
10
+ (): Collection<ClientAudioBufferType> => {
11
+ return getDb().collection<ClientAudioBufferType>("clientAudioBuffers");
12
+ };
13
+
14
+ /**
15
+ * Deletes all audio buffers associated with a specific callSid.
16
+ * Useful for clearing context between different tickets in the same call.
17
+ */
18
+ const deleteAudioBuffersByCallSid = async (
19
+ callSid: string,
20
+ ): Promise<DeleteResult> => {
21
+ return getClientAudioBuffersCollection().deleteMany({ callSid });
22
+ };
23
+
24
+ const insertClientAudioBuffer = async (
25
+ audioBufferInput: CreateAudioBufferInput,
26
+ ): Promise<InsertOneResult<ClientAudioBufferType>> => {
27
+ const collection = getClientAudioBuffersCollection();
28
+ const realtimeDate = new Date();
29
+ const ttlMs = audioBufferInput.ttlMs ?? 3600_000;
30
+ const expiresAt = new Date(realtimeDate.getTime() + ttlMs);
31
+
32
+ const inputFieldsForDocument = {
33
+ callSid: audioBufferInput.callSid,
34
+ clientId: audioBufferInput.clientId,
35
+ bucketStartMs: audioBufferInput.bucketStartMs,
36
+ bucketDurationMs: audioBufferInput.bucketDurationMs,
37
+ data: audioBufferInput.data,
38
+ };
39
+ const clientAudioBufferMongoDocument: ClientAudioBufferType = {
40
+ ...inputFieldsForDocument,
41
+ codec: "mulaw",
42
+ sampleRateHz: 8000,
43
+ channels: 1,
44
+ createdAt: realtimeDate,
45
+ expiresAt: expiresAt,
46
+ };
47
+ return collection.insertOne(clientAudioBufferMongoDocument);
48
+ };
49
+
50
+ const findClientAudioBuffersByCallSidAndRange = async (
51
+ callSid: string,
52
+ startMs?: number,
53
+ endMs?: number,
54
+ ): Promise<ClientAudioBufferDoc[]> => {
55
+ const collection = getClientAudioBuffersCollection();
56
+ if (startMs === undefined) {
57
+ return collection.find({ callSid }).toArray();
58
+ }
59
+ const endMsToUse = endMs ?? Date.now();
60
+ return collection
61
+ .find({
62
+ callSid,
63
+ bucketStartMs: {
64
+ $gte: startMs,
65
+ $lte: endMsToUse,
66
+ },
67
+ })
68
+ .toArray();
69
+ };
70
+
71
+ const getAudioChunksByCallSid = async (
72
+ callSid: string,
73
+ limit?: number,
74
+ ): Promise<Buffer[]> => {
75
+ const query = getClientAudioBuffersCollection().find({ callSid });
76
+ if (limit) {
77
+ query.sort({ bucketStartMs: -1 }).limit(limit);
78
+ } else {
79
+ query.sort({ bucketStartMs: 1 });
80
+ }
81
+
82
+ const docs = await query.toArray();
83
+
84
+ if (!docs.length) {
85
+ return [];
86
+ }
87
+ const orderedDocs = limit ? docs.reverse() : docs;
88
+
89
+ return orderedDocs.map((chunk) => {
90
+ const data: any = chunk.data;
91
+ if (Buffer.isBuffer(data)) return data;
92
+ if (data?.buffer) return Buffer.from(data.buffer);
93
+ return Buffer.from(data);
94
+ });
95
+ };
96
+
97
+ const getMergedAudioByCallSid = async (
98
+ callSid: string,
99
+ limit: number,
100
+ ): Promise<Buffer> => {
101
+ const buffers = await getAudioChunksByCallSid(callSid, limit);
102
+
103
+ if (!buffers.length) {
104
+ return Buffer.alloc(0);
105
+ }
106
+
107
+ return Buffer.concat(buffers);
108
+ };
109
+
110
+ export {
111
+ insertClientAudioBuffer,
112
+ getClientAudioBuffersCollection,
113
+ findClientAudioBuffersByCallSidAndRange,
114
+ getAudioChunksByCallSid,
115
+ getMergedAudioByCallSid,
116
+ deleteAudioBuffersByCallSid,
117
+ };
@@ -1,25 +1,25 @@
1
- import { WithId } from "mongodb";
2
-
3
- export type ClientAudioBufferType = {
4
- callSid: string;
5
- clientId: string;
6
- bucketStartMs: number;
7
- bucketDurationMs: number;
8
- codec: "mulaw";
9
- sampleRateHz: 8000;
10
- channels: 1;
11
- data: Buffer;
12
- expiresAt: Date;
13
- createdAt: Date;
14
- };
15
-
16
- export type CreateAudioBufferInput = {
17
- callSid: string;
18
- clientId: string;
19
- bucketStartMs: number;
20
- bucketDurationMs: number;
21
- data: Buffer;
22
- ttlMs?: number;
23
- };
24
-
25
- export type ClientAudioBufferDoc = WithId<ClientAudioBufferType>;
1
+ import { WithId } from "mongodb";
2
+
3
+ export type ClientAudioBufferType = {
4
+ callSid: string;
5
+ clientId: string;
6
+ bucketStartMs: number;
7
+ bucketDurationMs: number;
8
+ codec: "mulaw";
9
+ sampleRateHz: 8000;
10
+ channels: 1;
11
+ data: Buffer;
12
+ expiresAt: Date;
13
+ createdAt: Date;
14
+ };
15
+
16
+ export type CreateAudioBufferInput = {
17
+ callSid: string;
18
+ clientId: string;
19
+ bucketStartMs: number;
20
+ bucketDurationMs: number;
21
+ data: Buffer;
22
+ ttlMs?: number;
23
+ };
24
+
25
+ export type ClientAudioBufferDoc = WithId<ClientAudioBufferType>;
@@ -1,2 +1,2 @@
1
- export * from "./clientAudioBuffer.getters";
2
- export * from "./clientsAudioBuffers.types";
1
+ export * from "./clientAudioBuffer.getters";
2
+ export * from "./clientsAudioBuffers.types";
@@ -1,16 +1,16 @@
1
- import { getDb, Client } from "../index";
2
- import { Collection } from "mongodb";
3
-
4
- export const getClientsCollection = (): Collection<Client> => {
5
- return getDb().collection<Client>("clients");
6
- };
7
-
8
- export const getClientByAPIKey = async (
9
- apiKey: string,
10
- ): Promise<Client | null> => {
11
- const client = await getClientsCollection().findOne({
12
- apiKey,
13
- isActive: true,
14
- });
15
- return client ? client : null;
16
- };
1
+ import { getDb, Client } from "../index";
2
+ import { Collection } from "mongodb";
3
+
4
+ export const getClientsCollection = (): Collection<Client> => {
5
+ return getDb().collection<Client>("clients");
6
+ };
7
+
8
+ export const getClientByAPIKey = async (
9
+ apiKey: string,
10
+ ): Promise<Client | null> => {
11
+ const client = await getClientsCollection().findOne({
12
+ apiKey,
13
+ isActive: true,
14
+ });
15
+ return client ? client : null;
16
+ };
@@ -1,14 +1,14 @@
1
- import { ObjectId } from "mongodb";
2
-
3
- export type Client = {
4
- _id?: ObjectId;
5
- createdAt: Date;
6
- updatedAt: Date;
7
- clientId: string;
8
- isActive?: boolean | null;
9
- apiKey?: string | null;
10
- credits: number;
11
- managedBy?: string;
12
- };
13
-
14
- export type ClientDoc = Client & { _id: ObjectId };
1
+ import { ObjectId } from "mongodb";
2
+
3
+ export type Client = {
4
+ _id?: ObjectId;
5
+ createdAt: Date;
6
+ updatedAt: Date;
7
+ clientId: string;
8
+ isActive?: boolean | null;
9
+ apiKey?: string | null;
10
+ credits: number;
11
+ managedBy?: string;
12
+ };
13
+
14
+ export type ClientDoc = Client & { _id: ObjectId };
@@ -1,2 +1,2 @@
1
- export * from "./clients.getters";
2
- export * from "./clients.types";
1
+ export * from "./clients.getters";
2
+ export * from "./clients.types";
@@ -1,106 +1,106 @@
1
- import {
2
- createClientConfigDoc,
3
- getClientConfig,
4
- } from "../clientsConfig.getters";
5
- import { createClientConfig } from "../../../test-utils/factories";
6
-
7
- describe("db.clientsConfig", () => {
8
- describe("getClientConfig", () => {
9
- it("should return client config by clientId", async () => {
10
- const clientId = "test-client-id";
11
- const clientConfig = createClientConfig({
12
- clientId,
13
- language: "Hebrew",
14
- products: { feature1: true, feature2: false } as any,
15
- });
16
-
17
- await createClientConfigDoc(clientConfig);
18
-
19
- const result = await getClientConfig(clientId);
20
-
21
- expect(result).toMatchObject({
22
- clientId,
23
- language: "Hebrew",
24
- products: { feature1: true, feature2: false },
25
- });
26
- });
27
-
28
- it("should support websiteTalk product config", async () => {
29
- const clientId = "websiteTalk-test";
30
- const clientConfig = createClientConfig({
31
- clientId,
32
- products: {
33
- websiteTalk: {
34
- logoUrl: "https://cdn.example.com/branding/acme/logo.png",
35
- companyName: "Acme Ltd",
36
- timezone: "Asia/Jerusalem",
37
- language: "he",
38
- defaultBaseUrl: "https://acme.example",
39
- scheduler: {
40
- type: "cron",
41
- schedule: "0 9 * * 1-5"
42
- },
43
- },
44
- },
45
- });
46
-
47
- await createClientConfigDoc(clientConfig);
48
-
49
- const result = await getClientConfig(clientId);
50
-
51
- expect(result).toMatchObject({
52
- clientId,
53
- products: {
54
- websiteTalk: {
55
- logoUrl: "https://cdn.example.com/branding/acme/logo.png",
56
- companyName: "Acme Ltd",
57
- timezone: "Asia/Jerusalem",
58
- language: "he",
59
- defaultBaseUrl: "https://acme.example",
60
- scheduler: {
61
- type: "cron",
62
- },
63
- },
64
- },
65
- });
66
- });
67
-
68
- it("should return null for non-existent clientId", async () => {
69
- const result = await getClientConfig("non-existent-client");
70
- expect(result).toBeNull();
71
- });
72
-
73
- it("should support communications config", async () => {
74
- const clientId = "communications-test";
75
- const clientConfig = createClientConfig({
76
- clientId,
77
- communications: {
78
- sendGrid: {
79
- emailTo: ["test@example.com"],
80
- templateId: "temp-123",
81
- senderId: "sender-123",
82
- },
83
- sms: {},
84
- whatsapp: {},
85
- },
86
- });
87
-
88
- await createClientConfigDoc(clientConfig);
89
-
90
- const result = await getClientConfig(clientId);
91
-
92
- expect(result).toMatchObject({
93
- clientId,
94
- communications: {
95
- sendGrid: {
96
- emailTo: ["test@example.com"],
97
- templateId: "temp-123",
98
- senderId: "sender-123",
99
- },
100
- sms: {},
101
- whatsapp: {},
102
- },
103
- });
104
- });
105
- });
106
- });
1
+ import {
2
+ createClientConfigDoc,
3
+ getClientConfig,
4
+ } from "../clientsConfig.getters";
5
+ import { createClientConfig } from "../../../test-utils/factories";
6
+
7
+ describe("db.clientsConfig", () => {
8
+ describe("getClientConfig", () => {
9
+ it("should return client config by clientId", async () => {
10
+ const clientId = "test-client-id";
11
+ const clientConfig = createClientConfig({
12
+ clientId,
13
+ language: "Hebrew",
14
+ products: { feature1: true, feature2: false } as any,
15
+ });
16
+
17
+ await createClientConfigDoc(clientConfig);
18
+
19
+ const result = await getClientConfig(clientId);
20
+
21
+ expect(result).toMatchObject({
22
+ clientId,
23
+ language: "Hebrew",
24
+ products: { feature1: true, feature2: false },
25
+ });
26
+ });
27
+
28
+ it("should support websiteTalk product config", async () => {
29
+ const clientId = "websiteTalk-test";
30
+ const clientConfig = createClientConfig({
31
+ clientId,
32
+ products: {
33
+ websiteTalk: {
34
+ logoUrl: "https://cdn.example.com/branding/acme/logo.png",
35
+ companyName: "Acme Ltd",
36
+ timezone: "Asia/Jerusalem",
37
+ language: "he",
38
+ defaultBaseUrl: "https://acme.example",
39
+ scheduler: {
40
+ type: "cron",
41
+ schedule: "0 9 * * 1-5"
42
+ },
43
+ },
44
+ },
45
+ });
46
+
47
+ await createClientConfigDoc(clientConfig);
48
+
49
+ const result = await getClientConfig(clientId);
50
+
51
+ expect(result).toMatchObject({
52
+ clientId,
53
+ products: {
54
+ websiteTalk: {
55
+ logoUrl: "https://cdn.example.com/branding/acme/logo.png",
56
+ companyName: "Acme Ltd",
57
+ timezone: "Asia/Jerusalem",
58
+ language: "he",
59
+ defaultBaseUrl: "https://acme.example",
60
+ scheduler: {
61
+ type: "cron",
62
+ },
63
+ },
64
+ },
65
+ });
66
+ });
67
+
68
+ it("should return null for non-existent clientId", async () => {
69
+ const result = await getClientConfig("non-existent-client");
70
+ expect(result).toBeNull();
71
+ });
72
+
73
+ it("should support communications config", async () => {
74
+ const clientId = "communications-test";
75
+ const clientConfig = createClientConfig({
76
+ clientId,
77
+ communications: {
78
+ sendGrid: {
79
+ emailTo: ["test@example.com"],
80
+ templateId: "temp-123",
81
+ senderId: "sender-123",
82
+ },
83
+ sms: {},
84
+ whatsapp: {},
85
+ },
86
+ });
87
+
88
+ await createClientConfigDoc(clientConfig);
89
+
90
+ const result = await getClientConfig(clientId);
91
+
92
+ expect(result).toMatchObject({
93
+ clientId,
94
+ communications: {
95
+ sendGrid: {
96
+ emailTo: ["test@example.com"],
97
+ templateId: "temp-123",
98
+ senderId: "sender-123",
99
+ },
100
+ sms: {},
101
+ whatsapp: {},
102
+ },
103
+ });
104
+ });
105
+ });
106
+ });
@@ -1,22 +1,44 @@
1
- import { ClientConfigDoc, getDb, findClientByPhoneNumber } from "../index";
2
- import { Collection } from "mongodb";
3
-
4
- export const getClientsConfigCollection = (): Collection<ClientConfigDoc> => {
5
- return getDb().collection<ClientConfigDoc>("clientsConfig");
6
- };
7
-
8
- export const getClientConfig = (clientId: string) => {
9
- return getClientsConfigCollection().findOne({ clientId });
10
- };
11
-
12
- export const getClientConfigByPhone = async (phone: string) => {
13
- const client = await findClientByPhoneNumber(phone);
14
- if (!client) return null;
15
- return getClientConfig(client.clientId);
16
- };
17
-
18
- export const createClientConfigDoc = async (
19
- clientConfig: ClientConfigDoc,
20
- ): Promise<void> => {
21
- await getClientsConfigCollection().insertOne(clientConfig);
22
- };
1
+ import { ClientConfigDoc, getDb, findClientByPhoneNumber } from "../index";
2
+ import { Collection } from "mongodb";
3
+
4
+ export const getClientsConfigCollection = (): Collection<ClientConfigDoc> => {
5
+ return getDb().collection<ClientConfigDoc>("clientsConfig");
6
+ };
7
+
8
+ export const getClientConfig = (clientId: string) => {
9
+ return getClientsConfigCollection().findOne({ clientId });
10
+ };
11
+
12
+ export const getClientConfigByPhone = async (phone: string) => {
13
+ const client = await findClientByPhoneNumber(phone);
14
+ if (!client) return null;
15
+ return getClientConfig(client.clientId);
16
+ };
17
+
18
+ export const createClientConfigDoc = async (
19
+ clientConfig: ClientConfigDoc,
20
+ ): Promise<void> => {
21
+ await getClientsConfigCollection().insertOne(clientConfig);
22
+ };
23
+
24
+ export async function updateWebsitalkDefaultBaseUrl(
25
+ clientId: string,
26
+ defaultBaseUrl: string,
27
+ ): Promise<void> {
28
+ try {
29
+ const result = await getClientsConfigCollection().updateOne(
30
+ { clientId },
31
+ { $set: { "products.websiteTalk.defaultBaseUrl": defaultBaseUrl } },
32
+ );
33
+
34
+ if (result.matchedCount === 0) {
35
+ throw new Error(`No client config found for clientId: ${clientId}`);
36
+ }
37
+ } catch (error) {
38
+ console.error(
39
+ `[core-db] Error updating defaultBaseUrl for client: ${clientId}`,
40
+ error
41
+ );
42
+ throw error;
43
+ }
44
+ }