@talkpilot/core-db 1.3.28 → 1.3.29
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/README.md +22 -12
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts +1 -15
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js +1 -24
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts +0 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts.map +1 -1
- package/dist/municipal/streets/streets.getters.d.ts +1 -2
- package/dist/municipal/streets/streets.getters.d.ts.map +1 -1
- package/dist/municipal/streets/streets.getters.js +1 -6
- package/dist/municipal/streets/streets.getters.js.map +1 -1
- package/dist/municipal/streets/streets.types.d.ts +0 -1
- package/dist/municipal/streets/streets.types.d.ts.map +1 -1
- package/dist/talkpilot/clients/clients.quota.constants.d.ts +2 -0
- package/dist/talkpilot/clients/clients.quota.constants.d.ts.map +1 -0
- package/dist/talkpilot/clients/clients.quota.constants.js +5 -0
- package/dist/talkpilot/clients/clients.quota.constants.js.map +1 -0
- package/dist/talkpilot/clients/clients.quota.getters.d.ts +7 -0
- package/dist/talkpilot/clients/clients.quota.getters.d.ts.map +1 -0
- package/dist/talkpilot/clients/clients.quota.getters.js +80 -0
- package/dist/talkpilot/clients/clients.quota.getters.js.map +1 -0
- package/dist/talkpilot/clients/clients.quota.types.d.ts +22 -0
- package/dist/talkpilot/clients/clients.quota.types.d.ts.map +1 -0
- package/dist/{bulkWrite/bulkWrite.types.js → talkpilot/clients/clients.quota.types.js} +1 -1
- package/dist/talkpilot/clients/clients.quota.types.js.map +1 -0
- package/dist/talkpilot/clients/clients.quota.utils.d.ts +8 -0
- package/dist/talkpilot/clients/clients.quota.utils.d.ts.map +1 -0
- package/dist/talkpilot/clients/clients.quota.utils.js +45 -0
- package/dist/talkpilot/clients/clients.quota.utils.js.map +1 -0
- package/dist/talkpilot/clients/clients.types.d.ts +3 -0
- package/dist/talkpilot/clients/clients.types.d.ts.map +1 -1
- package/dist/talkpilot/clients/index.d.ts +2 -0
- package/dist/talkpilot/clients/index.d.ts.map +1 -1
- package/dist/talkpilot/clients/index.js +2 -0
- package/dist/talkpilot/clients/index.js.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +4 -0
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +31 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +2 -0
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +0 -7
- package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +3 -38
- package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +0 -1
- package/src/municipal/streets/streets.getters.ts +1 -16
- package/src/municipal/streets/streets.types.ts +0 -1
- package/src/talkpilot/clients/__tests__/clients.quota.spec.ts +123 -0
- package/src/talkpilot/clients/clients.quota.getters.ts +119 -0
- package/src/talkpilot/clients/clients.quota.types.ts +25 -0
- package/src/talkpilot/clients/clients.types.ts +4 -0
- package/src/talkpilot/clients/index.ts +2 -0
- package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +45 -0
- package/src/talkpilot/clientsConfig/clientsConfig.types.ts +3 -0
- package/dist/bulkWrite/bulkWrite.ops.d.ts +0 -17
- package/dist/bulkWrite/bulkWrite.ops.d.ts.map +0 -1
- package/dist/bulkWrite/bulkWrite.ops.js +0 -35
- package/dist/bulkWrite/bulkWrite.ops.js.map +0 -1
- package/dist/bulkWrite/bulkWrite.types.d.ts +0 -4
- package/dist/bulkWrite/bulkWrite.types.d.ts.map +0 -1
- package/dist/bulkWrite/bulkWrite.types.js.map +0 -1
- package/dist/bulkWrite/index.d.ts +0 -3
- package/dist/bulkWrite/index.d.ts.map +0 -1
- package/dist/bulkWrite/index.js +0 -19
- package/dist/bulkWrite/index.js.map +0 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts +0 -10
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts.map +0 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js +0 -15
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js.map +0 -1
- package/dist/municipal/streets/streets.setters.d.ts +0 -10
- package/dist/municipal/streets/streets.setters.d.ts.map +0 -1
- package/dist/municipal/streets/streets.setters.js +0 -15
- package/dist/municipal/streets/streets.setters.js.map +0 -1
- package/dist/reconcile/index.d.ts +0 -5
- package/dist/reconcile/index.d.ts.map +0 -1
- package/dist/reconcile/index.js +0 -21
- package/dist/reconcile/index.js.map +0 -1
- package/dist/reconcile/reconcile.diff.d.ts +0 -4
- package/dist/reconcile/reconcile.diff.d.ts.map +0 -1
- package/dist/reconcile/reconcile.diff.js +0 -36
- package/dist/reconcile/reconcile.diff.js.map +0 -1
- package/dist/reconcile/reconcile.execute.d.ts +0 -7
- package/dist/reconcile/reconcile.execute.d.ts.map +0 -1
- package/dist/reconcile/reconcile.execute.js +0 -20
- package/dist/reconcile/reconcile.execute.js.map +0 -1
- package/dist/reconcile/reconcile.ops.d.ts +0 -12
- package/dist/reconcile/reconcile.ops.d.ts.map +0 -1
- package/dist/reconcile/reconcile.ops.js +0 -24
- package/dist/reconcile/reconcile.ops.js.map +0 -1
- package/dist/reconcile/reconcile.types.d.ts +0 -32
- package/dist/reconcile/reconcile.types.d.ts.map +0 -1
- package/dist/reconcile/reconcile.types.js +0 -3
- package/dist/reconcile/reconcile.types.js.map +0 -1
- package/src/bulkWrite/__tests__/bulkWrite.spec.ts +0 -65
- package/src/bulkWrite/bulkWrite.ops.ts +0 -45
- package/src/bulkWrite/bulkWrite.types.ts +0 -4
- package/src/bulkWrite/index.ts +0 -2
- package/src/municipal/departmentsSubjects/__tests__/departmentsSubjects.spec.ts +0 -272
- package/src/municipal/streets/__tests__/streets.spec.ts +0 -253
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clients.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"clients.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D,YAAY,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,eAAe,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE3I,MAAM,MAAM,MAAM,GAAG;IACnB,GAAG,CAAC,EAAE,QAAQ,CAAC;IACf,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,mBAAmB,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,GAAG,EAAE,QAAQ,CAAA;CAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
@@ -16,4 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./clients.getters"), exports);
|
|
18
18
|
__exportStar(require("./clients.types"), exports);
|
|
19
|
+
__exportStar(require("./clients.quota.getters"), exports);
|
|
20
|
+
__exportStar(require("./clients.quota.types"), exports);
|
|
19
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,kDAAgC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,kDAAgC;AAChC,0DAAwC;AACxC,wDAAsC"}
|
|
@@ -8,4 +8,8 @@ export declare function updateProductConfig<K extends KnownProductKey>(clientId:
|
|
|
8
8
|
export declare const getActiveScanId: (clientId: string) => Promise<string | undefined>;
|
|
9
9
|
export declare const updateActiveScanId: (clientId: string, scanId: string) => Promise<void>;
|
|
10
10
|
export declare const clearActiveScanId: (clientId: string) => Promise<void>;
|
|
11
|
+
export declare function listClientIdsWithConfiguredQuota(): Promise<string[]>;
|
|
12
|
+
export declare function incrementClientQuotaUsage(clientId: string, amount?: number): Promise<void>;
|
|
13
|
+
export declare function resetClientQuotaUsage(clientId: string): Promise<void>;
|
|
14
|
+
export { getClientQuotaState, updateQuotaAlertsConfig, markQuotaAlertSent, markQuotaExpiredSent, resetClientQuotaSettings, } from "../clients/clients.quota.getters";
|
|
11
15
|
//# sourceMappingURL=clientsConfig.getters.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientsConfig.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clientsConfig/clientsConfig.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAGf,QAAQ,EAER,eAAe,EAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"clientsConfig.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clientsConfig/clientsConfig.getters.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAGf,QAAQ,EAER,eAAe,EAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,eAAO,MAAM,0BAA0B,QAAO,UAAU,CAAC,eAAe,CAEvE,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,8DAE/C,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAU,OAAO,MAAM,8DAIzD,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAChC,cAAc,eAAe,KAC5B,OAAO,CAAC,IAAI,CAEd,CAAC;AAEF,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,eAAe,EACjE,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,CAAC,EACb,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,GAC5B,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,KACf,OAAO,CAAC,MAAM,GAAG,SAAS,CAG5B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAC7B,UAAU,MAAM,EAChB,QAAQ,MAAM,KACb,OAAO,CAAC,IAAI,CAEd,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAU,UAAU,MAAM,KAAG,OAAO,CAAC,IAAI,CAQtE,CAAC;AAEF,wBAAsB,gCAAgC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC,CAO1E;AAED,wBAAsB,yBAAyB,CAC7C,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,MAAU,GACjB,OAAO,CAAC,IAAI,CAAC,CASf;AAED,wBAAsB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAW3E;AAED,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,kCAAkC,CAAC"}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.clearActiveScanId = exports.updateActiveScanId = exports.getActiveScanId = exports.createClientConfigDoc = exports.getClientConfigByPhone = exports.getClientConfig = exports.getClientsConfigCollection = void 0;
|
|
3
|
+
exports.resetClientQuotaSettings = exports.markQuotaExpiredSent = exports.markQuotaAlertSent = exports.updateQuotaAlertsConfig = exports.getClientQuotaState = exports.clearActiveScanId = exports.updateActiveScanId = exports.getActiveScanId = exports.createClientConfigDoc = exports.getClientConfigByPhone = exports.getClientConfig = exports.getClientsConfigCollection = void 0;
|
|
4
4
|
exports.updateProductConfig = updateProductConfig;
|
|
5
|
+
exports.listClientIdsWithConfiguredQuota = listClientIdsWithConfiguredQuota;
|
|
6
|
+
exports.incrementClientQuotaUsage = incrementClientQuotaUsage;
|
|
7
|
+
exports.resetClientQuotaUsage = resetClientQuotaUsage;
|
|
5
8
|
const index_1 = require("../index");
|
|
9
|
+
const clients_quota_getters_1 = require("../clients/clients.quota.getters");
|
|
6
10
|
const getClientsConfigCollection = () => {
|
|
7
11
|
return (0, index_1.getDb)().collection("clientsConfig");
|
|
8
12
|
};
|
|
@@ -51,4 +55,30 @@ const clearActiveScanId = async (clientId) => {
|
|
|
51
55
|
}
|
|
52
56
|
};
|
|
53
57
|
exports.clearActiveScanId = clearActiveScanId;
|
|
58
|
+
async function listClientIdsWithConfiguredQuota() {
|
|
59
|
+
const docs = await (0, exports.getClientsConfigCollection)()
|
|
60
|
+
.find({ "quota.totalQuota": { $exists: true, $ne: null } })
|
|
61
|
+
.project({ clientId: 1, _id: 0 })
|
|
62
|
+
.toArray();
|
|
63
|
+
return docs.map(({ clientId }) => clientId);
|
|
64
|
+
}
|
|
65
|
+
async function incrementClientQuotaUsage(clientId, amount = 1) {
|
|
66
|
+
const result = await (0, exports.getClientsConfigCollection)().updateOne({ clientId, "quota.totalQuota": { $exists: true } }, { $inc: { "quota.usedQuota": amount } });
|
|
67
|
+
if (result.matchedCount === 0) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function resetClientQuotaUsage(clientId) {
|
|
72
|
+
const result = await (0, exports.getClientsConfigCollection)().updateOne({ clientId }, { $set: { "quota.usedQuota": 0 } });
|
|
73
|
+
if (result.matchedCount === 0) {
|
|
74
|
+
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
75
|
+
}
|
|
76
|
+
await (0, clients_quota_getters_1.resetClientQuotaSettings)(clientId);
|
|
77
|
+
}
|
|
78
|
+
var clients_quota_getters_2 = require("../clients/clients.quota.getters");
|
|
79
|
+
Object.defineProperty(exports, "getClientQuotaState", { enumerable: true, get: function () { return clients_quota_getters_2.getClientQuotaState; } });
|
|
80
|
+
Object.defineProperty(exports, "updateQuotaAlertsConfig", { enumerable: true, get: function () { return clients_quota_getters_2.updateQuotaAlertsConfig; } });
|
|
81
|
+
Object.defineProperty(exports, "markQuotaAlertSent", { enumerable: true, get: function () { return clients_quota_getters_2.markQuotaAlertSent; } });
|
|
82
|
+
Object.defineProperty(exports, "markQuotaExpiredSent", { enumerable: true, get: function () { return clients_quota_getters_2.markQuotaExpiredSent; } });
|
|
83
|
+
Object.defineProperty(exports, "resetClientQuotaSettings", { enumerable: true, get: function () { return clients_quota_getters_2.resetClientQuotaSettings; } });
|
|
54
84
|
//# sourceMappingURL=clientsConfig.getters.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientsConfig.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clientsConfig/clientsConfig.getters.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"clientsConfig.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clientsConfig/clientsConfig.getters.ts"],"names":[],"mappings":";;;AA+BA,kDAwBC;AA0BD,4EAOC;AAED,8DAYC;AAED,sDAWC;AAnHD,oCAOkB;AAElB,4EAA4E;AAErE,MAAM,0BAA0B,GAAG,GAAgC,EAAE;IAC1E,OAAO,IAAA,aAAK,GAAE,CAAC,UAAU,CAAkB,eAAe,CAAC,CAAC;AAC9D,CAAC,CAAC;AAFW,QAAA,0BAA0B,8BAErC;AAEK,MAAM,eAAe,GAAG,CAAC,QAAgB,EAAE,EAAE;IAClD,OAAO,IAAA,kCAA0B,GAAE,CAAC,OAAO,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;AAC5D,CAAC,CAAC;AAFW,QAAA,eAAe,mBAE1B;AAEK,MAAM,sBAAsB,GAAG,KAAK,EAAE,KAAa,EAAE,EAAE;IAC5D,MAAM,MAAM,GAAG,MAAM,IAAA,+BAAuB,EAAC,KAAK,CAAC,CAAC;IACpD,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,OAAO,IAAA,uBAAe,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC1C,CAAC,CAAC;AAJW,QAAA,sBAAsB,0BAIjC;AAEK,MAAM,qBAAqB,GAAG,KAAK,EACxC,YAA6B,EACd,EAAE;IACjB,MAAM,IAAA,kCAA0B,GAAE,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;AAC7D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M,IAAA,kCAA0B,GAAE,CAAC,SAAS,CACzD,EAAE,QAAQ,EAAE,EACZ,EAAE,IAAI,EAAE,EAAE,iBAAiB,EAAE,CAAC,EAAE,EAAE,CACnC,CAAC;IAEF,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,wCAAwC,QAAQ,EAAE,CAAC,CAAC;IACtE,CAAC;IAED,MAAM,IAAA,gDAAwB,EAAC,QAAQ,CAAC,CAAC;AAC3C,CAAC;AAED,0EAM0C;AALxC,4HAAA,mBAAmB,OAAA;AACnB,gIAAA,uBAAuB,OAAA;AACvB,2HAAA,kBAAkB,OAAA;AAClB,6HAAA,oBAAoB,OAAA;AACpB,iIAAA,wBAAwB,OAAA"}
|
|
@@ -73,6 +73,7 @@ export type CommunicationsConfig = {
|
|
|
73
73
|
sms?: SmsConfig;
|
|
74
74
|
whatsapp?: WhatsappConfig;
|
|
75
75
|
};
|
|
76
|
+
import type { ClientQuotaUsage } from "../clients/clients.quota.types";
|
|
76
77
|
export type ClientConfig<P extends Products = Products> = {
|
|
77
78
|
clientId: string;
|
|
78
79
|
phoneNumber?: string;
|
|
@@ -81,6 +82,7 @@ export type ClientConfig<P extends Products = Products> = {
|
|
|
81
82
|
timezone?: string;
|
|
82
83
|
communications?: CommunicationsConfig;
|
|
83
84
|
callLengthThresholds?: CallLengthThresholds;
|
|
85
|
+
quota?: ClientQuotaUsage;
|
|
84
86
|
};
|
|
85
87
|
export type ClientConfigDoc<P extends Products = Products> = WithId<ClientConfig<P>>;
|
|
86
88
|
//# sourceMappingURL=clientsConfig.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clientsConfig.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clientsConfig/clientsConfig.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAE,MAA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,GACR,SAAS,GACT,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElB,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG,EAAE,CAAC;AAE3B,MAAM,MAAM,cAAc,GAAG,EAAE,CAAC;AAEhC,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,GAAG,CAAC,EAAE,SAAS,CAAC;IAChB,QAAQ,CAAC,EAAE,cAAc,CAAC;CAC3B,CAAC;AAEF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI;IACxD,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,QAAQ,EAAE,CAAC,CAAC;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,oBAAoB,CAAC;IACtC,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;IAC5C,KAAK,CAAC,EAAE,gBAAgB,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,QAAQ,GAAG,QAAQ,IAAI,MAAM,CACjE,YAAY,CAAC,CAAC,CAAC,CAChB,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// TalkPilot DB exports
|
|
2
2
|
export * from "./talkpilot";
|
|
3
3
|
export * from "./connection";
|
|
4
|
-
export * from "./bulkWrite";
|
|
5
4
|
|
|
6
5
|
export {
|
|
7
6
|
municipalDataMongodbClient,
|
|
@@ -26,9 +25,3 @@ export {
|
|
|
26
25
|
export * from "./websitalk/scans/scans.getters";
|
|
27
26
|
export type * from "./websitalk/scans/scans.types";
|
|
28
27
|
export * from "./websitalk/websiteUrls";
|
|
29
|
-
|
|
30
|
-
export { ObjectId } from "mongodb";
|
|
31
|
-
export type { Filter } from "mongodb";
|
|
32
|
-
export type { AnyBulkWriteOperation as BulkWriteOp } from "mongodb";
|
|
33
|
-
export { MongoBulkWriteError as BulkWriteError } from "mongodb";
|
|
34
|
-
export type { OptionalUnlessRequiredId } from "mongodb";
|
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import {CityName, DepartmentSubject, getDb} from "../index";
|
|
2
|
-
import {
|
|
3
|
-
import {VectorSearchResult} from "./departmentsSubjects.types";
|
|
1
|
+
import { CityName, DepartmentSubject, getDb } from "../index";
|
|
2
|
+
import { Collection, Filter } from "mongodb";
|
|
3
|
+
import { VectorSearchResult } from "./departmentsSubjects.types";
|
|
4
4
|
|
|
5
5
|
export const getDepartmentsSubjectsCollection =
|
|
6
6
|
(): Collection<DepartmentSubject> => {
|
|
7
7
|
return getDb().collection<DepartmentSubject>("departmentsSubjects");
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export const bulkWriteDepartmentsSubjects = async (
|
|
11
|
-
operations: AnyBulkWriteOperation<DepartmentSubject>[],
|
|
12
|
-
options?: BulkWriteOptions,
|
|
13
|
-
): Promise<BulkWriteResult> => {
|
|
14
|
-
return getDepartmentsSubjectsCollection().bulkWrite(operations, options);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
10
|
/**
|
|
18
11
|
* Get all departments subjects for a given city.
|
|
19
12
|
* No DB sort; tree build does not depend on order. Callers that need alphabetical order
|
|
@@ -287,31 +280,3 @@ export const deleteDepartmentSubjectGuidelines = async (
|
|
|
287
280
|
export const findSubjectByQuery = async (query: Partial<DepartmentSubject>) => {
|
|
288
281
|
return await getDepartmentsSubjectsCollection().findOne(query);
|
|
289
282
|
};
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* Get department subjects matching a filter
|
|
293
|
-
* @param filter - Mongo filter
|
|
294
|
-
* @returns Array of matching documents
|
|
295
|
-
*/
|
|
296
|
-
export const findDepartmentSubjectsByFilter = async (
|
|
297
|
-
filter: Filter<DepartmentSubject>,
|
|
298
|
-
): Promise<DepartmentSubject[]> => {
|
|
299
|
-
return getDepartmentsSubjectsCollection().find(filter).toArray();
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Update the isActive flag for a subject
|
|
304
|
-
* @param id - _id of the document to update
|
|
305
|
-
* @param isActive - Active status to set
|
|
306
|
-
* @returns Updated DepartmentSubject document, null if not found
|
|
307
|
-
*/
|
|
308
|
-
export const updateDepartmentSubjectActive = async (
|
|
309
|
-
id: ObjectId,
|
|
310
|
-
isActive: boolean,
|
|
311
|
-
): Promise<DepartmentSubject | null> => {
|
|
312
|
-
return await getDepartmentsSubjectsCollection().findOneAndUpdate(
|
|
313
|
-
{_id: id},
|
|
314
|
-
{$set: {isActive, updatedAt: new Date()}},
|
|
315
|
-
{returnDocument: "after"},
|
|
316
|
-
);
|
|
317
|
-
};
|
|
@@ -1,25 +1,11 @@
|
|
|
1
1
|
import { CityName, getDb, ObjectId, Street } from "../index";
|
|
2
2
|
import type { StreetHint } from "./streets.types";
|
|
3
|
-
import {
|
|
4
|
-
AnyBulkWriteOperation,
|
|
5
|
-
BulkWriteOptions,
|
|
6
|
-
BulkWriteResult,
|
|
7
|
-
Collection,
|
|
8
|
-
Filter,
|
|
9
|
-
ObjectId as MongoObjectId,
|
|
10
|
-
} from "mongodb";
|
|
3
|
+
import { Collection, Filter, ObjectId as MongoObjectId } from "mongodb";
|
|
11
4
|
|
|
12
5
|
export const getStreetsCollection = (): Collection<Street> => {
|
|
13
6
|
return getDb().collection<Street>("streets");
|
|
14
7
|
};
|
|
15
8
|
|
|
16
|
-
export const bulkWriteStreets = async (
|
|
17
|
-
operations: AnyBulkWriteOperation<Street>[],
|
|
18
|
-
options?: BulkWriteOptions,
|
|
19
|
-
): Promise<BulkWriteResult> => {
|
|
20
|
-
return getStreetsCollection().bulkWrite(operations, options);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
9
|
export const findStreets = async (
|
|
24
10
|
filter: Filter<Street> = {},
|
|
25
11
|
): Promise<Street[]> => {
|
|
@@ -68,7 +54,6 @@ export const createStreet = async (
|
|
|
68
54
|
): Promise<MongoObjectId> => {
|
|
69
55
|
const street: Omit<Street, "_id"> = {
|
|
70
56
|
...streetData,
|
|
71
|
-
isActive: streetData.isActive ?? true,
|
|
72
57
|
createdAt: new Date(),
|
|
73
58
|
updatedAt: new Date(),
|
|
74
59
|
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getClientsConfigCollection,
|
|
3
|
+
incrementClientQuotaUsage,
|
|
4
|
+
listClientIdsWithConfiguredQuota,
|
|
5
|
+
resetClientQuotaUsage,
|
|
6
|
+
} from "../../clientsConfig/clientsConfig.getters";
|
|
7
|
+
import { getClientsCollection } from "../clients.getters";
|
|
8
|
+
import {
|
|
9
|
+
markQuotaAlertSent,
|
|
10
|
+
markQuotaExpiredSent,
|
|
11
|
+
updateQuotaAlertsConfig,
|
|
12
|
+
} from "../clients.quota.getters";
|
|
13
|
+
|
|
14
|
+
describe("quota getters", () => {
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await getClientsConfigCollection().deleteMany({});
|
|
17
|
+
await getClientsCollection().deleteMany({});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const seed = async (clientId: string, configQuota: object, clientQuota?: object) => {
|
|
21
|
+
await getClientsConfigCollection().insertOne({
|
|
22
|
+
clientId,
|
|
23
|
+
products: {},
|
|
24
|
+
quota: configQuota,
|
|
25
|
+
} as any);
|
|
26
|
+
if (clientQuota !== undefined) {
|
|
27
|
+
await getClientsCollection().insertOne({
|
|
28
|
+
clientId,
|
|
29
|
+
createdAt: new Date(),
|
|
30
|
+
updatedAt: new Date(),
|
|
31
|
+
credits: 0,
|
|
32
|
+
quota: clientQuota,
|
|
33
|
+
} as any);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
it("updateQuotaAlertsConfig stores alerts on clients", async () => {
|
|
38
|
+
const clientId = "quota-alerts";
|
|
39
|
+
await seed(clientId, { totalQuota: 100, usedQuota: 5 });
|
|
40
|
+
|
|
41
|
+
await updateQuotaAlertsConfig(clientId, [
|
|
42
|
+
{ thresholdPct: 50, recipientEmails: ["a@example.com"] },
|
|
43
|
+
{ thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] },
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
const client = await getClientsCollection().findOne({ clientId });
|
|
47
|
+
expect(client?.quota?.alerts).toEqual([
|
|
48
|
+
{ thresholdPct: 50, recipientEmails: ["a@example.com"] },
|
|
49
|
+
{ thresholdPct: 80, recipientEmails: ["b@example.com", "c@example.com"] },
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const config = await getClientsConfigCollection().findOne({ clientId });
|
|
53
|
+
expect(config?.quota).toEqual({ totalQuota: 100, usedQuota: 5 });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("markQuotaAlertSent returns true only on first transition", async () => {
|
|
57
|
+
const clientId = "quota-alert-sent";
|
|
58
|
+
await seed(
|
|
59
|
+
clientId,
|
|
60
|
+
{ totalQuota: 100, usedQuota: 0 },
|
|
61
|
+
{ alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"] }] },
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(await markQuotaAlertSent(clientId, 0)).toBe(true);
|
|
65
|
+
expect(await markQuotaAlertSent(clientId, 0)).toBe(false);
|
|
66
|
+
|
|
67
|
+
const client = await getClientsCollection().findOne({ clientId });
|
|
68
|
+
expect(client?.quota?.alerts?.[0]?.thresholdSent).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("markQuotaExpiredSent returns true only on first transition", async () => {
|
|
72
|
+
const clientId = "quota-notify";
|
|
73
|
+
await seed(clientId, { totalQuota: 10, usedQuota: 10 });
|
|
74
|
+
|
|
75
|
+
expect(await markQuotaExpiredSent(clientId)).toBe(true);
|
|
76
|
+
expect(await markQuotaExpiredSent(clientId)).toBe(false);
|
|
77
|
+
|
|
78
|
+
const client = await getClientsCollection().findOne({ clientId });
|
|
79
|
+
expect(client?.quota?.expiredSent).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("listClientIdsWithConfiguredQuota returns clientIds with totalQuota set", async () => {
|
|
83
|
+
await seed("quota-a", { totalQuota: 100, usedQuota: 0 });
|
|
84
|
+
await seed("quota-b", { totalQuota: 50, usedQuota: 10 });
|
|
85
|
+
await getClientsConfigCollection().insertOne({
|
|
86
|
+
clientId: "no-quota",
|
|
87
|
+
products: {},
|
|
88
|
+
} as any);
|
|
89
|
+
|
|
90
|
+
const ids = await listClientIdsWithConfiguredQuota();
|
|
91
|
+
expect(ids.sort()).toEqual(["quota-a", "quota-b"]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("incrementClientQuotaUsage increments usedQuota on clientsConfig", async () => {
|
|
95
|
+
const clientId = "quota-inc";
|
|
96
|
+
await seed(clientId, { totalQuota: 100, usedQuota: 3 });
|
|
97
|
+
|
|
98
|
+
await incrementClientQuotaUsage(clientId, 2);
|
|
99
|
+
const config = await getClientsConfigCollection().findOne({ clientId });
|
|
100
|
+
expect(config?.quota?.usedQuota).toBe(5);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("resetClientQuotaUsage zeros usedQuota and clears client quota flags", async () => {
|
|
104
|
+
const clientId = "quota-reset";
|
|
105
|
+
await seed(
|
|
106
|
+
clientId,
|
|
107
|
+
{ totalQuota: 100, usedQuota: 50 },
|
|
108
|
+
{
|
|
109
|
+
expiredSent: true,
|
|
110
|
+
alerts: [{ thresholdPct: 50, recipientEmails: ["a@b.com"], thresholdSent: true }],
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
await resetClientQuotaUsage(clientId);
|
|
115
|
+
|
|
116
|
+
const config = await getClientsConfigCollection().findOne({ clientId });
|
|
117
|
+
expect(config?.quota?.usedQuota).toBe(0);
|
|
118
|
+
|
|
119
|
+
const client = await getClientsCollection().findOne({ clientId });
|
|
120
|
+
expect(client?.quota?.expiredSent).toBeUndefined();
|
|
121
|
+
expect(client?.quota?.alerts?.[0]?.thresholdSent).toBeUndefined();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { ObjectId } from "mongodb";
|
|
2
|
+
import { ClientQuotaSettings } from "./clients.quota.types";
|
|
3
|
+
|
|
4
|
+
export type { QuotaThresholdAlert, ClientQuotaSettings, QuotaAlertInput, ClientQuotaUsage, ClientQuotaState } from "./clients.quota.types";
|
|
2
5
|
|
|
3
6
|
export type Client = {
|
|
4
7
|
_id?: ObjectId;
|
|
@@ -9,6 +12,7 @@ export type Client = {
|
|
|
9
12
|
apiKey?: string | null;
|
|
10
13
|
credits: number;
|
|
11
14
|
managedBy?: string;
|
|
15
|
+
quota?: ClientQuotaSettings;
|
|
12
16
|
};
|
|
13
17
|
|
|
14
18
|
export type ClientDoc = Client & { _id: ObjectId };
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
KnownProductKey,
|
|
8
8
|
} from "../index";
|
|
9
9
|
import { Collection } from "mongodb";
|
|
10
|
+
import { resetClientQuotaSettings } from "../clients/clients.quota.getters";
|
|
10
11
|
|
|
11
12
|
export const getClientsConfigCollection = (): Collection<ClientConfigDoc> => {
|
|
12
13
|
return getDb().collection<ClientConfigDoc>("clientsConfig");
|
|
@@ -77,3 +78,47 @@ export const clearActiveScanId = async (clientId: string): Promise<void> => {
|
|
|
77
78
|
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
78
79
|
}
|
|
79
80
|
};
|
|
81
|
+
|
|
82
|
+
export async function listClientIdsWithConfiguredQuota(): Promise<string[]> {
|
|
83
|
+
const docs = await getClientsConfigCollection()
|
|
84
|
+
.find({ "quota.totalQuota": { $exists: true, $ne: null } })
|
|
85
|
+
.project({ clientId: 1, _id: 0 })
|
|
86
|
+
.toArray();
|
|
87
|
+
|
|
88
|
+
return docs.map(({ clientId }) => clientId);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function incrementClientQuotaUsage(
|
|
92
|
+
clientId: string,
|
|
93
|
+
amount: number = 1,
|
|
94
|
+
): Promise<void> {
|
|
95
|
+
const result = await getClientsConfigCollection().updateOne(
|
|
96
|
+
{ clientId, "quota.totalQuota": { $exists: true } },
|
|
97
|
+
{ $inc: { "quota.usedQuota": amount } },
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (result.matchedCount === 0) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function resetClientQuotaUsage(clientId: string): Promise<void> {
|
|
106
|
+
const result = await getClientsConfigCollection().updateOne(
|
|
107
|
+
{ clientId },
|
|
108
|
+
{ $set: { "quota.usedQuota": 0 } },
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (result.matchedCount === 0) {
|
|
112
|
+
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
await resetClientQuotaSettings(clientId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export {
|
|
119
|
+
getClientQuotaState,
|
|
120
|
+
updateQuotaAlertsConfig,
|
|
121
|
+
markQuotaAlertSent,
|
|
122
|
+
markQuotaExpiredSent,
|
|
123
|
+
resetClientQuotaSettings,
|
|
124
|
+
} from "../clients/clients.quota.getters";
|
|
@@ -111,6 +111,8 @@ export type CommunicationsConfig = {
|
|
|
111
111
|
whatsapp?: WhatsappConfig;
|
|
112
112
|
};
|
|
113
113
|
|
|
114
|
+
import type { ClientQuotaUsage } from "../clients/clients.quota.types";
|
|
115
|
+
|
|
114
116
|
export type ClientConfig<P extends Products = Products> = {
|
|
115
117
|
clientId: string;
|
|
116
118
|
phoneNumber?: string;
|
|
@@ -119,6 +121,7 @@ export type ClientConfig<P extends Products = Products> = {
|
|
|
119
121
|
timezone?: string;
|
|
120
122
|
communications?: CommunicationsConfig;
|
|
121
123
|
callLengthThresholds?: CallLengthThresholds;
|
|
124
|
+
quota?: ClientQuotaUsage;
|
|
122
125
|
};
|
|
123
126
|
|
|
124
127
|
export type ClientConfigDoc<P extends Products = Products> = WithId<
|