@talkpilot/core-db 1.3.26 → 1.3.28

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 (109) hide show
  1. package/README.md +60 -0
  2. package/dist/bulkWrite/bulkWrite.ops.d.ts +17 -0
  3. package/dist/bulkWrite/bulkWrite.ops.d.ts.map +1 -0
  4. package/dist/bulkWrite/bulkWrite.ops.js +35 -0
  5. package/dist/bulkWrite/bulkWrite.ops.js.map +1 -0
  6. package/dist/bulkWrite/bulkWrite.types.d.ts +4 -0
  7. package/dist/bulkWrite/bulkWrite.types.d.ts.map +1 -0
  8. package/dist/{talkpilot/clients/clients.quota.types.js → bulkWrite/bulkWrite.types.js} +1 -1
  9. package/dist/bulkWrite/bulkWrite.types.js.map +1 -0
  10. package/dist/bulkWrite/index.d.ts +3 -0
  11. package/dist/bulkWrite/index.d.ts.map +1 -0
  12. package/dist/bulkWrite/index.js +19 -0
  13. package/dist/bulkWrite/index.js.map +1 -0
  14. package/dist/index.d.ts +6 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +6 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts +15 -1
  19. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts.map +1 -1
  20. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js +24 -1
  21. package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js.map +1 -1
  22. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts +10 -0
  23. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts.map +1 -0
  24. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js +15 -0
  25. package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js.map +1 -0
  26. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts +1 -0
  27. package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts.map +1 -1
  28. package/dist/municipal/streets/streets.getters.d.ts +2 -1
  29. package/dist/municipal/streets/streets.getters.d.ts.map +1 -1
  30. package/dist/municipal/streets/streets.getters.js +6 -1
  31. package/dist/municipal/streets/streets.getters.js.map +1 -1
  32. package/dist/municipal/streets/streets.setters.d.ts +10 -0
  33. package/dist/municipal/streets/streets.setters.d.ts.map +1 -0
  34. package/dist/municipal/streets/streets.setters.js +15 -0
  35. package/dist/municipal/streets/streets.setters.js.map +1 -0
  36. package/dist/municipal/streets/streets.types.d.ts +1 -0
  37. package/dist/municipal/streets/streets.types.d.ts.map +1 -1
  38. package/dist/reconcile/index.d.ts +5 -0
  39. package/dist/reconcile/index.d.ts.map +1 -0
  40. package/dist/reconcile/index.js +21 -0
  41. package/dist/reconcile/index.js.map +1 -0
  42. package/dist/reconcile/reconcile.diff.d.ts +4 -0
  43. package/dist/reconcile/reconcile.diff.d.ts.map +1 -0
  44. package/dist/reconcile/reconcile.diff.js +36 -0
  45. package/dist/reconcile/reconcile.diff.js.map +1 -0
  46. package/dist/reconcile/reconcile.execute.d.ts +7 -0
  47. package/dist/reconcile/reconcile.execute.d.ts.map +1 -0
  48. package/dist/reconcile/reconcile.execute.js +20 -0
  49. package/dist/reconcile/reconcile.execute.js.map +1 -0
  50. package/dist/reconcile/reconcile.ops.d.ts +12 -0
  51. package/dist/reconcile/reconcile.ops.d.ts.map +1 -0
  52. package/dist/reconcile/reconcile.ops.js +24 -0
  53. package/dist/reconcile/reconcile.ops.js.map +1 -0
  54. package/dist/reconcile/reconcile.types.d.ts +32 -0
  55. package/dist/reconcile/reconcile.types.d.ts.map +1 -0
  56. package/dist/reconcile/reconcile.types.js +3 -0
  57. package/dist/reconcile/reconcile.types.js.map +1 -0
  58. package/dist/talkpilot/calls/calls.constants.d.ts +14 -0
  59. package/dist/talkpilot/calls/calls.constants.d.ts.map +1 -1
  60. package/dist/talkpilot/calls/calls.constants.js +15 -1
  61. package/dist/talkpilot/calls/calls.constants.js.map +1 -1
  62. package/dist/talkpilot/calls/calls.getters.d.ts +3 -0
  63. package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -1
  64. package/dist/talkpilot/calls/calls.getters.js +12 -1
  65. package/dist/talkpilot/calls/calls.getters.js.map +1 -1
  66. package/dist/talkpilot/calls/calls.types.d.ts +11 -2
  67. package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
  68. package/dist/talkpilot/clients/clients.types.d.ts +0 -3
  69. package/dist/talkpilot/clients/clients.types.d.ts.map +1 -1
  70. package/dist/talkpilot/clients/index.d.ts +0 -2
  71. package/dist/talkpilot/clients/index.d.ts.map +1 -1
  72. package/dist/talkpilot/clients/index.js +0 -2
  73. package/dist/talkpilot/clients/index.js.map +1 -1
  74. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +0 -3
  75. package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
  76. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +1 -23
  77. package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
  78. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +0 -2
  79. package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
  80. package/package.json +1 -1
  81. package/src/bulkWrite/__tests__/bulkWrite.spec.ts +65 -0
  82. package/src/bulkWrite/bulkWrite.ops.ts +45 -0
  83. package/src/bulkWrite/bulkWrite.types.ts +4 -0
  84. package/src/bulkWrite/index.ts +2 -0
  85. package/src/index.ts +7 -0
  86. package/src/municipal/departmentsSubjects/__tests__/departmentsSubjects.spec.ts +272 -0
  87. package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +38 -3
  88. package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +1 -0
  89. package/src/municipal/streets/__tests__/streets.spec.ts +253 -0
  90. package/src/municipal/streets/streets.getters.ts +16 -1
  91. package/src/municipal/streets/streets.types.ts +1 -0
  92. package/src/talkpilot/calls/__tests__/calls.spec.ts +189 -1
  93. package/src/talkpilot/calls/calls.constants.ts +17 -0
  94. package/src/talkpilot/calls/calls.getters.ts +19 -0
  95. package/src/talkpilot/calls/calls.types.ts +13 -10
  96. package/src/talkpilot/clients/clients.types.ts +0 -4
  97. package/src/talkpilot/clients/index.ts +0 -2
  98. package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +0 -36
  99. package/src/talkpilot/clientsConfig/clientsConfig.types.ts +0 -3
  100. package/dist/talkpilot/clients/clients.quota.getters.d.ts +0 -7
  101. package/dist/talkpilot/clients/clients.quota.getters.d.ts.map +0 -1
  102. package/dist/talkpilot/clients/clients.quota.getters.js +0 -80
  103. package/dist/talkpilot/clients/clients.quota.getters.js.map +0 -1
  104. package/dist/talkpilot/clients/clients.quota.types.d.ts +0 -22
  105. package/dist/talkpilot/clients/clients.quota.types.d.ts.map +0 -1
  106. package/dist/talkpilot/clients/clients.quota.types.js.map +0 -1
  107. package/src/talkpilot/clients/__tests__/clients.quota.spec.ts +0 -110
  108. package/src/talkpilot/clients/clients.quota.getters.ts +0 -119
  109. package/src/talkpilot/clients/clients.quota.types.ts +0 -25
@@ -1,7 +0,0 @@
1
- import type { ClientQuotaState, QuotaAlertInput } from "./clients.quota.types";
2
- export declare function getClientQuotaState(clientId: string): Promise<ClientQuotaState | null>;
3
- export declare function updateQuotaAlertsConfig(clientId: string, alerts: QuotaAlertInput[]): Promise<void>;
4
- export declare function markQuotaAlertSent(clientId: string, alertIndex: number): Promise<boolean>;
5
- export declare function markQuotaExpiredSent(clientId: string): Promise<boolean>;
6
- export declare function resetClientQuotaSettings(clientId: string): Promise<void>;
7
- //# sourceMappingURL=clients.quota.getters.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"clients.quota.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.getters.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EAChB,MAAM,uBAAuB,CAAC;AAE/B,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAkBlC;AAYD,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,eAAe,EAAE,GACxB,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,OAAO,CAAC,CAYlB;AAED,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAY7E;AAED,wBAAsB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAiB9E"}
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getClientQuotaState = getClientQuotaState;
4
- exports.updateQuotaAlertsConfig = updateQuotaAlertsConfig;
5
- exports.markQuotaAlertSent = markQuotaAlertSent;
6
- exports.markQuotaExpiredSent = markQuotaExpiredSent;
7
- exports.resetClientQuotaSettings = resetClientQuotaSettings;
8
- const clientsConfig_getters_1 = require("../clientsConfig/clientsConfig.getters");
9
- const clients_getters_1 = require("./clients.getters");
10
- async function getClientQuotaState(clientId) {
11
- const [config, client] = await Promise.all([
12
- (0, clientsConfig_getters_1.getClientConfig)(clientId),
13
- (0, clients_getters_1.getClientsCollection)().findOne({ clientId }),
14
- ]);
15
- const usage = config?.quota;
16
- if (usage?.totalQuota === undefined || usage?.totalQuota === null)
17
- return null;
18
- const settings = client?.quota;
19
- return {
20
- totalQuota: usage.totalQuota,
21
- usedQuota: usage.usedQuota ?? 0,
22
- expiresAt: usage.expiresAt,
23
- alerts: settings?.alerts,
24
- expiredSent: settings?.expiredSent,
25
- };
26
- }
27
- const ensureClientDoc = async (clientId) => {
28
- const now = new Date();
29
- await (0, clients_getters_1.getClientsCollection)().updateOne({ clientId }, { $setOnInsert: { clientId, createdAt: now, updatedAt: now, credits: 0 } }, { upsert: true });
30
- await (0, clients_getters_1.getClientsCollection)().updateOne({ clientId }, { $set: { updatedAt: now } });
31
- };
32
- async function updateQuotaAlertsConfig(clientId, alerts) {
33
- const config = await (0, clientsConfig_getters_1.getClientConfig)(clientId);
34
- if (!config) {
35
- throw new Error(`No client config found for clientId: ${clientId}`);
36
- }
37
- await ensureClientDoc(clientId);
38
- const state = await getClientQuotaState(clientId);
39
- const existing = state?.alerts ?? [];
40
- const sentByThreshold = new Map(existing.filter((a) => a.thresholdSent).map((a) => [a.thresholdPct, true]));
41
- const merged = alerts.map((alert) => ({
42
- thresholdPct: alert.thresholdPct,
43
- recipientEmails: alert.recipientEmails,
44
- ...(sentByThreshold.get(alert.thresholdPct) ? { thresholdSent: true } : {}),
45
- }));
46
- const result = await (0, clients_getters_1.getClientsCollection)().updateOne({ clientId }, { $set: { "quota.alerts": merged, updatedAt: new Date() } });
47
- if (result.matchedCount === 0) {
48
- throw new Error(`No client found for clientId: ${clientId}`);
49
- }
50
- }
51
- async function markQuotaAlertSent(clientId, alertIndex) {
52
- await ensureClientDoc(clientId);
53
- const result = await (0, clients_getters_1.getClientsCollection)().findOneAndUpdate({
54
- clientId,
55
- [`quota.alerts.${alertIndex}.thresholdSent`]: { $ne: true },
56
- }, { $set: { [`quota.alerts.${alertIndex}.thresholdSent`]: true, updatedAt: new Date() } }, { returnDocument: "after" });
57
- return result !== null;
58
- }
59
- async function markQuotaExpiredSent(clientId) {
60
- await ensureClientDoc(clientId);
61
- const result = await (0, clients_getters_1.getClientsCollection)().findOneAndUpdate({
62
- clientId,
63
- "quota.expiredSent": { $ne: true },
64
- }, { $set: { "quota.expiredSent": true, updatedAt: new Date() } }, { returnDocument: "after" });
65
- return result !== null;
66
- }
67
- async function resetClientQuotaSettings(clientId) {
68
- const client = await (0, clients_getters_1.getClientsCollection)().findOne({ clientId });
69
- const alerts = client?.quota?.alerts?.map(({ thresholdSent: _s, ...alert }) => alert) ?? [];
70
- if (client) {
71
- await (0, clients_getters_1.getClientsCollection)().updateOne({ clientId }, {
72
- $set: {
73
- ...(alerts.length ? { "quota.alerts": alerts } : {}),
74
- updatedAt: new Date(),
75
- },
76
- $unset: { "quota.expiredSent": "" },
77
- });
78
- }
79
- }
80
- //# sourceMappingURL=clients.quota.getters.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"clients.quota.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.getters.ts"],"names":[],"mappings":";;AAOA,kDAoBC;AAYD,0DA6BC;AAED,gDAeC;AAED,oDAYC;AAED,4DAiBC;AAtHD,kFAAyE;AACzE,uDAAyD;AAMlD,KAAK,UAAU,mBAAmB,CACvC,QAAgB;IAEhB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACzC,IAAA,uCAAe,EAAC,QAAQ,CAAC;QACzB,IAAA,sCAAoB,GAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC;KAC7C,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,MAAM,EAAE,KAAK,CAAC;IAC5B,IAAI,KAAK,EAAE,UAAU,KAAK,SAAS,IAAI,KAAK,EAAE,UAAU,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAE/E,MAAM,QAAQ,GAAG,MAAM,EAAE,KAAK,CAAC;IAE/B,OAAO;QACL,UAAU,EAAE,KAAK,CAAC,UAAU;QAC5B,SAAS,EAAE,KAAK,CAAC,SAAS,IAAI,CAAC;QAC/B,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,MAAM,EAAE,QAAQ,EAAE,MAAM;QACxB,WAAW,EAAE,QAAQ,EAAE,WAAW;KACnC,CAAC;AACJ,CAAC;AAED,MAAM,eAAe,GAAG,KAAK,EAAE,QAAgB,EAAiB,EAAE;IAChE,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;IACvB,MAAM,IAAA,sCAAoB,GAAE,CAAC,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AE,EAC9D,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;IAEF,OAAO,MAAM,KAAK,IAAI,CAAC;AACzB,CAAC;AAEM,KAAK,UAAU,wBAAwB,CAAC,QAAgB;IAC7D,MAAM,MAAM,GAAG,MAAM,IAAA,sCAAoB,GAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;IAClE,MAAM,MAAM,GACV,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,aAAa,EAAE,EAAE,EAAE,GAAG,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAE/E,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,IAAA,sCAAoB,GAAE,CAAC,SAAS,CACpC,EAAE,QAAQ,EAAE,EACZ;YACE,IAAI,EAAE;gBACJ,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBACpD,SAAS,EAAE,IAAI,IAAI,EAAE;aACtB;YACD,MAAM,EAAE,EAAE,mBAAmB,EAAE,EAAE,EAAE;SACpC,CACF,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -1,22 +0,0 @@
1
- export type QuotaThresholdAlert = {
2
- thresholdPct: number;
3
- recipientEmails: string[];
4
- thresholdSent?: boolean;
5
- };
6
- export type ClientQuotaSettings = {
7
- /** Up to 3 threshold alerts, each with up to 3 recipient emails. */
8
- alerts?: QuotaThresholdAlert[];
9
- /** Set after expired-quota email is sent successfully. */
10
- expiredSent?: boolean;
11
- };
12
- export type QuotaAlertInput = {
13
- thresholdPct: number;
14
- recipientEmails: string[];
15
- };
16
- export type ClientQuotaUsage = {
17
- totalQuota: number;
18
- usedQuota: number;
19
- expiresAt?: Date;
20
- };
21
- export type ClientQuotaState = ClientQuotaUsage & ClientQuotaSettings;
22
- //# sourceMappingURL=clients.quota.types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"clients.quota.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,oEAAoE;IACpE,MAAM,CAAC,EAAE,mBAAmB,EAAE,CAAC;IAC/B,0DAA0D;IAC1D,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,mBAAmB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"clients.quota.types.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.types.ts"],"names":[],"mappings":""}
@@ -1,110 +0,0 @@
1
- import {
2
- getClientsConfigCollection,
3
- incrementClientQuotaUsage,
4
- resetClientQuotaUsage,
5
- } from "../../clientsConfig/clientsConfig.getters";
6
- import { getClientsCollection } from "../clients.getters";
7
- import {
8
- markQuotaAlertSent,
9
- markQuotaExpiredSent,
10
- updateQuotaAlertsConfig,
11
- } from "../clients.quota.getters";
12
-
13
- describe("quota getters", () => {
14
- beforeEach(async () => {
15
- await getClientsConfigCollection().deleteMany({});
16
- await getClientsCollection().deleteMany({});
17
- });
18
-
19
- const seed = async (clientId: string, configQuota: object, clientQuota?: object) => {
20
- await getClientsConfigCollection().insertOne({
21
- clientId,
22
- products: {},
23
- quota: configQuota,
24
- } as any);
25
- if (clientQuota !== undefined) {
26
- await getClientsCollection().insertOne({
27
- clientId,
28
- createdAt: new Date(),
29
- updatedAt: new Date(),
30
- credits: 0,
31
- quota: clientQuota,
32
- } as any);
33
- }
34
- };
35
-
36
- it("updateQuotaAlertsConfig stores alerts on clients", async () => {
37
- const clientId = "quota-alerts";
38
- await seed(clientId, { totalQuota: 100, usedQuota: 5 });
39
-
40
- await updateQuotaAlertsConfig(clientId, [
41
- { thresholdPct: 50, recipientEmails: ["a@example.com"] },
42
- { thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] },
43
- ]);
44
-
45
- const client = await getClientsCollection().findOne({ clientId });
46
- expect(client?.quota?.alerts).toEqual([
47
- { thresholdPct: 50, recipientEmails: ["a@example.com"] },
48
- { thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] },
49
- ]);
50
-
51
- const config = await getClientsConfigCollection().findOne({ clientId });
52
- expect(config?.quota).toEqual({ totalQuota: 100, usedQuota: 5 });
53
- });
54
-
55
- it("markQuotaAlertSent returns true only on first transition", async () => {
56
- const clientId = "quota-alert-sent";
57
- await seed(
58
- clientId,
59
- { totalQuota: 100, usedQuota: 0 },
60
- { alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"] }] },
61
- );
62
-
63
- expect(await markQuotaAlertSent(clientId, 0)).toBe(true);
64
- expect(await markQuotaAlertSent(clientId, 0)).toBe(false);
65
-
66
- const client = await getClientsCollection().findOne({ clientId });
67
- expect(client?.quota?.alerts?.[0]?.thresholdSent).toBe(true);
68
- });
69
-
70
- it("markQuotaExpiredSent returns true only on first transition", async () => {
71
- const clientId = "quota-notify";
72
- await seed(clientId, { totalQuota: 10, usedQuota: 10 });
73
-
74
- expect(await markQuotaExpiredSent(clientId)).toBe(true);
75
- expect(await markQuotaExpiredSent(clientId)).toBe(false);
76
-
77
- const client = await getClientsCollection().findOne({ clientId });
78
- expect(client?.quota?.expiredSent).toBe(true);
79
- });
80
-
81
- it("incrementClientQuotaUsage increments usedQuota on clientsConfig", async () => {
82
- const clientId = "quota-inc";
83
- await seed(clientId, { totalQuota: 100, usedQuota: 3 });
84
-
85
- await incrementClientQuotaUsage(clientId, 2);
86
- const config = await getClientsConfigCollection().findOne({ clientId });
87
- expect(config?.quota?.usedQuota).toBe(5);
88
- });
89
-
90
- it("resetClientQuotaUsage zeros usedQuota and clears client quota flags", async () => {
91
- const clientId = "quota-reset";
92
- await seed(
93
- clientId,
94
- { totalQuota: 100, usedQuota: 50 },
95
- {
96
- expiredSent: true,
97
- alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"], thresholdSent: true }],
98
- },
99
- );
100
-
101
- await resetClientQuotaUsage(clientId);
102
-
103
- const config = await getClientsConfigCollection().findOne({ clientId });
104
- expect(config?.quota?.usedQuota).toBe(0);
105
-
106
- const client = await getClientsCollection().findOne({ clientId });
107
- expect(client?.quota?.expiredSent).toBeUndefined();
108
- expect(client?.quota?.alerts?.[0]?.thresholdSent).toBeUndefined();
109
- });
110
- });
@@ -1,119 +0,0 @@
1
- import { getClientConfig } from "../clientsConfig/clientsConfig.getters";
2
- import { getClientsCollection } from "./clients.getters";
3
- import type {
4
- ClientQuotaState,
5
- QuotaAlertInput,
6
- } from "./clients.quota.types";
7
-
8
- export async function getClientQuotaState(
9
- clientId: string,
10
- ): Promise<ClientQuotaState | null> {
11
- const [config, client] = await Promise.all([
12
- getClientConfig(clientId),
13
- getClientsCollection().findOne({ clientId }),
14
- ]);
15
-
16
- const usage = config?.quota;
17
- if (usage?.totalQuota === undefined || usage?.totalQuota === null) return null;
18
-
19
- const settings = client?.quota;
20
-
21
- return {
22
- totalQuota: usage.totalQuota,
23
- usedQuota: usage.usedQuota ?? 0,
24
- expiresAt: usage.expiresAt,
25
- alerts: settings?.alerts,
26
- expiredSent: settings?.expiredSent,
27
- };
28
- }
29
-
30
- const ensureClientDoc = async (clientId: string): Promise<void> => {
31
- const now = new Date();
32
- await getClientsCollection().updateOne(
33
- { clientId },
34
- { $setOnInsert: { clientId, createdAt: now, updatedAt: now, credits: 0 } },
35
- { upsert: true },
36
- );
37
- await getClientsCollection().updateOne({ clientId }, { $set: { updatedAt: now } });
38
- };
39
-
40
- export async function updateQuotaAlertsConfig(
41
- clientId: string,
42
- alerts: QuotaAlertInput[],
43
- ): Promise<void> {
44
- const config = await getClientConfig(clientId);
45
- if (!config) {
46
- throw new Error(`No client config found for clientId: ${clientId}`);
47
- }
48
-
49
- await ensureClientDoc(clientId);
50
- const state = await getClientQuotaState(clientId);
51
- const existing = state?.alerts ?? [];
52
- const sentByThreshold = new Map(
53
- existing.filter((a) => a.thresholdSent).map((a) => [a.thresholdPct, true]),
54
- );
55
- const merged = alerts.map((alert) => ({
56
- thresholdPct: alert.thresholdPct,
57
- recipientEmails: alert.recipientEmails,
58
- ...(sentByThreshold.get(alert.thresholdPct) ? { thresholdSent: true as const } : {}),
59
- }));
60
-
61
- const result = await getClientsCollection().updateOne(
62
- { clientId },
63
- { $set: { "quota.alerts": merged, updatedAt: new Date() } },
64
- );
65
-
66
- if (result.matchedCount === 0) {
67
- throw new Error(`No client found for clientId: ${clientId}`);
68
- }
69
- }
70
-
71
- export async function markQuotaAlertSent(
72
- clientId: string,
73
- alertIndex: number,
74
- ): Promise<boolean> {
75
- await ensureClientDoc(clientId);
76
- const result = await getClientsCollection().findOneAndUpdate(
77
- {
78
- clientId,
79
- [`quota.alerts.${alertIndex}.thresholdSent`]: { $ne: true },
80
- },
81
- { $set: { [`quota.alerts.${alertIndex}.thresholdSent`]: true, updatedAt: new Date() } },
82
- { returnDocument: "after" },
83
- );
84
-
85
- return result !== null;
86
- }
87
-
88
- export async function markQuotaExpiredSent(clientId: string): Promise<boolean> {
89
- await ensureClientDoc(clientId);
90
- const result = await getClientsCollection().findOneAndUpdate(
91
- {
92
- clientId,
93
- "quota.expiredSent": { $ne: true },
94
- },
95
- { $set: { "quota.expiredSent": true, updatedAt: new Date() } },
96
- { returnDocument: "after" },
97
- );
98
-
99
- return result !== null;
100
- }
101
-
102
- export async function resetClientQuotaSettings(clientId: string): Promise<void> {
103
- const client = await getClientsCollection().findOne({ clientId });
104
- const alerts =
105
- client?.quota?.alerts?.map(({ thresholdSent: _s, ...alert }) => alert) ?? [];
106
-
107
- if (client) {
108
- await getClientsCollection().updateOne(
109
- { clientId },
110
- {
111
- $set: {
112
- ...(alerts.length ? { "quota.alerts": alerts } : {}),
113
- updatedAt: new Date(),
114
- },
115
- $unset: { "quota.expiredSent": "" },
116
- },
117
- );
118
- }
119
- }
@@ -1,25 +0,0 @@
1
- export type QuotaThresholdAlert = {
2
- thresholdPct: number;
3
- recipientEmails: string[];
4
- thresholdSent?: boolean;
5
- };
6
-
7
- export type ClientQuotaSettings = {
8
- /** Up to 3 threshold alerts, each with up to 3 recipient emails. */
9
- alerts?: QuotaThresholdAlert[];
10
- /** Set after expired-quota email is sent successfully. */
11
- expiredSent?: boolean;
12
- };
13
-
14
- export type QuotaAlertInput = {
15
- thresholdPct: number;
16
- recipientEmails: string[];
17
- };
18
-
19
- export type ClientQuotaUsage = {
20
- totalQuota: number;
21
- usedQuota: number;
22
- expiresAt?: Date;
23
- };
24
-
25
- export type ClientQuotaState = ClientQuotaUsage & ClientQuotaSettings;