@talkpilot/core-db 1.3.31 → 1.3.33
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 +12 -23
- package/dist/bulkWrite/bulkWrite.ops.d.ts +17 -0
- package/dist/bulkWrite/bulkWrite.ops.d.ts.map +1 -0
- package/dist/bulkWrite/bulkWrite.ops.js +35 -0
- package/dist/bulkWrite/bulkWrite.ops.js.map +1 -0
- package/dist/bulkWrite/bulkWrite.types.d.ts +4 -0
- package/dist/bulkWrite/bulkWrite.types.d.ts.map +1 -0
- package/dist/{talkpilot/clients/clients.quota.types.js → bulkWrite/bulkWrite.types.js} +1 -1
- package/dist/bulkWrite/bulkWrite.types.js.map +1 -0
- package/dist/bulkWrite/index.d.ts +3 -0
- package/dist/bulkWrite/index.d.ts.map +1 -0
- package/dist/bulkWrite/index.js +19 -0
- package/dist/bulkWrite/index.js.map +1 -0
- package/dist/configuration/index.d.ts +7 -0
- package/dist/configuration/index.d.ts.map +1 -0
- package/dist/configuration/index.js +34 -0
- package/dist/configuration/index.js.map +1 -0
- package/dist/configuration/mongodb-client.d.ts +13 -0
- package/dist/configuration/mongodb-client.d.ts.map +1 -0
- package/dist/configuration/mongodb-client.js +56 -0
- package/dist/configuration/mongodb-client.js.map +1 -0
- package/dist/configuration/prompts/index.d.ts +3 -0
- package/dist/configuration/prompts/index.d.ts.map +1 -0
- package/dist/configuration/prompts/index.js +18 -0
- package/dist/configuration/prompts/index.js.map +1 -0
- package/dist/configuration/prompts/prompts.getters.d.ts +21 -0
- package/dist/configuration/prompts/prompts.getters.d.ts.map +1 -0
- package/dist/configuration/prompts/prompts.getters.js +35 -0
- package/dist/configuration/prompts/prompts.getters.js.map +1 -0
- package/dist/configuration/prompts/prompts.types.d.ts +9 -0
- package/dist/configuration/prompts/prompts.types.d.ts.map +1 -0
- package/dist/configuration/prompts/prompts.types.js +3 -0
- package/dist/configuration/prompts/prompts.types.js.map +1 -0
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +11 -0
- package/dist/connection.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts +15 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.d.ts.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js +24 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.getters.js.map +1 -1
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts +10 -0
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.d.ts.map +1 -0
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js +15 -0
- package/dist/municipal/departmentsSubjects/departmentsSubjects.setters.js.map +1 -0
- package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts +1 -0
- package/dist/municipal/departmentsSubjects/departmentsSubjects.types.d.ts.map +1 -1
- package/dist/municipal/streets/streets.getters.d.ts +2 -1
- package/dist/municipal/streets/streets.getters.d.ts.map +1 -1
- package/dist/municipal/streets/streets.getters.js +6 -1
- package/dist/municipal/streets/streets.getters.js.map +1 -1
- package/dist/municipal/streets/streets.setters.d.ts +10 -0
- package/dist/municipal/streets/streets.setters.d.ts.map +1 -0
- package/dist/municipal/streets/streets.setters.js +15 -0
- package/dist/municipal/streets/streets.setters.js.map +1 -0
- package/dist/municipal/streets/streets.types.d.ts +1 -0
- package/dist/municipal/streets/streets.types.d.ts.map +1 -1
- package/dist/reconcile/index.d.ts +5 -0
- package/dist/reconcile/index.d.ts.map +1 -0
- package/dist/reconcile/index.js +21 -0
- package/dist/reconcile/index.js.map +1 -0
- package/dist/reconcile/reconcile.diff.d.ts +4 -0
- package/dist/reconcile/reconcile.diff.d.ts.map +1 -0
- package/dist/reconcile/reconcile.diff.js +36 -0
- package/dist/reconcile/reconcile.diff.js.map +1 -0
- package/dist/reconcile/reconcile.execute.d.ts +7 -0
- package/dist/reconcile/reconcile.execute.d.ts.map +1 -0
- package/dist/reconcile/reconcile.execute.js +20 -0
- package/dist/reconcile/reconcile.execute.js.map +1 -0
- package/dist/reconcile/reconcile.ops.d.ts +12 -0
- package/dist/reconcile/reconcile.ops.d.ts.map +1 -0
- package/dist/reconcile/reconcile.ops.js +24 -0
- package/dist/reconcile/reconcile.ops.js.map +1 -0
- package/dist/reconcile/reconcile.types.d.ts +32 -0
- package/dist/reconcile/reconcile.types.d.ts.map +1 -0
- package/dist/reconcile/reconcile.types.js +3 -0
- package/dist/reconcile/reconcile.types.js.map +1 -0
- package/dist/talkpilot/clients/clients.types.d.ts +0 -3
- package/dist/talkpilot/clients/clients.types.d.ts.map +1 -1
- package/dist/talkpilot/clients/index.d.ts +0 -2
- package/dist/talkpilot/clients/index.d.ts.map +1 -1
- package/dist/talkpilot/clients/index.js +0 -2
- package/dist/talkpilot/clients/index.js.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +0 -5
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +1 -37
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts +3 -4
- package/dist/talkpilot/clientsConfig/clientsConfig.types.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.types.js.map +1 -1
- package/dist/test-utils/db-utils.d.ts.map +1 -1
- package/dist/test-utils/db-utils.js +2 -0
- package/dist/test-utils/db-utils.js.map +1 -1
- package/package.json +1 -1
- package/src/bulkWrite/__tests__/bulkWrite.spec.ts +65 -0
- package/src/bulkWrite/bulkWrite.ops.ts +45 -0
- package/src/bulkWrite/bulkWrite.types.ts +4 -0
- package/src/bulkWrite/index.ts +2 -0
- package/src/configuration/index.ts +15 -0
- package/src/configuration/mongodb-client.ts +61 -0
- package/src/configuration/prompts/index.ts +2 -0
- package/src/configuration/prompts/prompts.getters.ts +37 -0
- package/src/configuration/prompts/prompts.types.ts +10 -0
- package/src/connection.ts +13 -0
- package/src/index.ts +14 -0
- package/src/municipal/departmentsSubjects/__tests__/departmentsSubjects.spec.ts +272 -0
- package/src/municipal/departmentsSubjects/departmentsSubjects.getters.ts +38 -3
- package/src/municipal/departmentsSubjects/departmentsSubjects.types.ts +1 -0
- package/src/municipal/streets/__tests__/streets.spec.ts +253 -0
- package/src/municipal/streets/streets.getters.ts +16 -1
- package/src/municipal/streets/streets.types.ts +1 -0
- package/src/talkpilot/clients/clients.types.ts +0 -4
- package/src/talkpilot/clients/index.ts +0 -2
- package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +0 -21
- package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +0 -54
- package/src/talkpilot/clientsConfig/clientsConfig.types.ts +3 -5
- package/src/test-utils/db-utils.ts +8 -1
- package/dist/talkpilot/clients/clients.quota.constants.d.ts +0 -2
- package/dist/talkpilot/clients/clients.quota.constants.d.ts.map +0 -1
- package/dist/talkpilot/clients/clients.quota.constants.js +0 -5
- package/dist/talkpilot/clients/clients.quota.constants.js.map +0 -1
- package/dist/talkpilot/clients/clients.quota.getters.d.ts +0 -7
- package/dist/talkpilot/clients/clients.quota.getters.d.ts.map +0 -1
- package/dist/talkpilot/clients/clients.quota.getters.js +0 -80
- package/dist/talkpilot/clients/clients.quota.getters.js.map +0 -1
- package/dist/talkpilot/clients/clients.quota.types.d.ts +0 -22
- package/dist/talkpilot/clients/clients.quota.types.d.ts.map +0 -1
- package/dist/talkpilot/clients/clients.quota.types.js.map +0 -1
- package/dist/talkpilot/clients/clients.quota.utils.d.ts +0 -8
- package/dist/talkpilot/clients/clients.quota.utils.d.ts.map +0 -1
- package/dist/talkpilot/clients/clients.quota.utils.js +0 -45
- package/dist/talkpilot/clients/clients.quota.utils.js.map +0 -1
- package/src/talkpilot/clients/__tests__/clients.quota.spec.ts +0 -123
- package/src/talkpilot/clients/clients.quota.getters.ts +0 -119
- package/src/talkpilot/clients/clients.quota.types.ts +0 -25
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
KnownProductKey,
|
|
8
8
|
} from "../index";
|
|
9
9
|
import { Collection } from "mongodb";
|
|
10
|
-
import { resetClientQuotaSettings } from "../clients/clients.quota.getters";
|
|
11
10
|
|
|
12
11
|
export const getClientsConfigCollection = (): Collection<ClientConfigDoc> => {
|
|
13
12
|
return getDb().collection<ClientConfigDoc>("clientsConfig");
|
|
@@ -17,15 +16,6 @@ export const getClientConfig = (clientId: string) => {
|
|
|
17
16
|
return getClientsConfigCollection().findOne({ clientId });
|
|
18
17
|
};
|
|
19
18
|
|
|
20
|
-
export const getClientDisplayName = async (clientId: string): Promise<string> => {
|
|
21
|
-
const doc = await getClientsConfigCollection().findOne(
|
|
22
|
-
{ clientId },
|
|
23
|
-
{ projection: { clientDisplayName: 1, _id: 0 } },
|
|
24
|
-
);
|
|
25
|
-
const name = doc?.clientDisplayName?.trim();
|
|
26
|
-
return name || clientId;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
19
|
export const getClientConfigByPhone = async (phone: string) => {
|
|
30
20
|
const client = await findClientByPhoneNumber(phone);
|
|
31
21
|
if (!client) return null;
|
|
@@ -87,47 +77,3 @@ export const clearActiveScanId = async (clientId: string): Promise<void> => {
|
|
|
87
77
|
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
88
78
|
}
|
|
89
79
|
};
|
|
90
|
-
|
|
91
|
-
export async function listClientIdsWithConfiguredQuota(): Promise<string[]> {
|
|
92
|
-
const docs = await getClientsConfigCollection()
|
|
93
|
-
.find({ "quota.totalQuota": { $exists: true, $ne: null } })
|
|
94
|
-
.project({ clientId: 1, _id: 0 })
|
|
95
|
-
.toArray();
|
|
96
|
-
|
|
97
|
-
return docs.map(({ clientId }) => clientId);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export async function incrementClientQuotaUsage(
|
|
101
|
-
clientId: string,
|
|
102
|
-
amount: number = 1,
|
|
103
|
-
): Promise<void> {
|
|
104
|
-
const result = await getClientsConfigCollection().updateOne(
|
|
105
|
-
{ clientId, "quota.totalQuota": { $exists: true } },
|
|
106
|
-
{ $inc: { "quota.usedQuota": amount } },
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
if (result.matchedCount === 0) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export async function resetClientQuotaUsage(clientId: string): Promise<void> {
|
|
115
|
-
const result = await getClientsConfigCollection().updateOne(
|
|
116
|
-
{ clientId },
|
|
117
|
-
{ $set: { "quota.usedQuota": 0 } },
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
if (result.matchedCount === 0) {
|
|
121
|
-
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
await resetClientQuotaSettings(clientId);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export {
|
|
128
|
-
getClientQuotaState,
|
|
129
|
-
updateQuotaAlertsConfig,
|
|
130
|
-
markQuotaAlertSent,
|
|
131
|
-
markQuotaExpiredSent,
|
|
132
|
-
resetClientQuotaSettings,
|
|
133
|
-
} from "../clients/clients.quota.getters";
|
|
@@ -12,10 +12,12 @@ export type InitiateCallForShortSlaConfig = {
|
|
|
12
12
|
flow_id?: string | string[];
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
/** Per-tool LLM system prompt overrides. Falls back to
|
|
15
|
+
/** Per-tool LLM system prompt overrides. Falls back to configuration-db defaults when absent. */
|
|
16
16
|
export type Moked106ToolPrompts = {
|
|
17
17
|
findSubjectsSystemPrompt?: string;
|
|
18
18
|
findStreetsSystemPrompt?: string;
|
|
19
|
+
transcriptionStreetSystemPrompt?: string;
|
|
20
|
+
transcriptionSubjectSystemPrompt?: string;
|
|
19
21
|
};
|
|
20
22
|
|
|
21
23
|
/** Feature flags and behavior config for the moked_106 municipal call-center product. */
|
|
@@ -111,18 +113,14 @@ export type CommunicationsConfig = {
|
|
|
111
113
|
whatsapp?: WhatsappConfig;
|
|
112
114
|
};
|
|
113
115
|
|
|
114
|
-
import type { ClientQuotaUsage } from "../clients/clients.quota.types";
|
|
115
|
-
|
|
116
116
|
export type ClientConfig<P extends Products = Products> = {
|
|
117
117
|
clientId: string;
|
|
118
|
-
clientDisplayName?: string;
|
|
119
118
|
phoneNumber?: string;
|
|
120
119
|
language?: ClientLanguageName;
|
|
121
120
|
products: P;
|
|
122
121
|
timezone?: string;
|
|
123
122
|
communications?: CommunicationsConfig;
|
|
124
123
|
callLengthThresholds?: CallLengthThresholds;
|
|
125
|
-
quota?: ClientQuotaUsage;
|
|
126
124
|
};
|
|
127
125
|
|
|
128
126
|
export type ClientConfigDoc<P extends Products = Products> = WithId<
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { MongoMemoryServer } from "mongodb-memory-server";
|
|
2
2
|
import { MongoClient, Db } from "mongodb";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
setDb,
|
|
5
|
+
setMunicipalDataDb,
|
|
6
|
+
setWebsitalkDb,
|
|
7
|
+
setConfigurationDb,
|
|
8
|
+
} from "../index";
|
|
4
9
|
|
|
5
10
|
let mongoServer: MongoMemoryServer;
|
|
6
11
|
let client: MongoClient;
|
|
@@ -14,9 +19,11 @@ export async function initTestDb(): Promise<Db> {
|
|
|
14
19
|
const talkpilotDb = client.db();
|
|
15
20
|
const municipalDb = client.db("municipal-data");
|
|
16
21
|
const websitalkDb = client.db("website-talk");
|
|
22
|
+
const configurationDb = client.db("configuration");
|
|
17
23
|
setDb(talkpilotDb);
|
|
18
24
|
setMunicipalDataDb(municipalDb);
|
|
19
25
|
setWebsitalkDb(websitalkDb);
|
|
26
|
+
setConfigurationDb(configurationDb);
|
|
20
27
|
return talkpilotDb;
|
|
21
28
|
}
|
|
22
29
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clients.quota.constants.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,MAAM,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clients.quota.constants.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG,GAAG,CAAC"}
|
|
@@ -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,KA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
|
|
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,8 +0,0 @@
|
|
|
1
|
-
import type { ClientQuotaNotifications, ClientQuotaState, ClientQuotaStatus, QuotaThresholdAlert } from "./clients.quota.types";
|
|
2
|
-
export declare const computeClientQuotaStatus: (quota: ClientQuotaState | null) => ClientQuotaStatus;
|
|
3
|
-
export declare const getQuotaAlerts: (quota?: ClientQuotaState) => QuotaThresholdAlert[];
|
|
4
|
-
export declare const findFullQuotaAlertIndex: (alerts: QuotaThresholdAlert[]) => number;
|
|
5
|
-
export declare const getFullQuotaAlert: (quota?: ClientQuotaState) => QuotaThresholdAlert | undefined;
|
|
6
|
-
export declare const isFullQuotaAlert: (thresholdPct: number) => boolean;
|
|
7
|
-
export declare const toClientQuotaNotifications: (quota: ClientQuotaState | null) => ClientQuotaNotifications;
|
|
8
|
-
//# sourceMappingURL=clients.quota.utils.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clients.quota.utils.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,wBAAwB,EACxB,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,uBAAuB,CAAC;AAQ/B,eAAO,MAAM,wBAAwB,GACnC,OAAO,gBAAgB,GAAG,IAAI,KAC7B,iBAiBF,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,QAAQ,gBAAgB,KAAG,mBAAmB,EAClC,CAAC;AAE5C,eAAO,MAAM,uBAAuB,GAAI,QAAQ,mBAAmB,EAAE,KAAG,MACP,CAAC;AAElE,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,gBAAgB,KACvB,mBAAmB,GAAG,SAIxB,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,cAAc,MAAM,KAAG,OAClB,CAAC;AAEvC,eAAO,MAAM,0BAA0B,GACrC,OAAO,gBAAgB,GAAG,IAAI,KAC7B,wBASD,CAAC"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toClientQuotaNotifications = exports.isFullQuotaAlert = exports.getFullQuotaAlert = exports.findFullQuotaAlertIndex = exports.getQuotaAlerts = exports.computeClientQuotaStatus = void 0;
|
|
4
|
-
const clients_quota_constants_1 = require("./clients.quota.constants");
|
|
5
|
-
const computeUsagePct = (usedQuota, totalQuota) => totalQuota > 0 ? Math.round((usedQuota / totalQuota) * 100) : 0;
|
|
6
|
-
const isPastExpiration = (expiresAt) => expiresAt ? Date.now() > new Date(expiresAt).getTime() : false;
|
|
7
|
-
const computeClientQuotaStatus = (quota) => {
|
|
8
|
-
if (!quota) {
|
|
9
|
-
return { hasQuota: false };
|
|
10
|
-
}
|
|
11
|
-
const totalQuota = quota.totalQuota;
|
|
12
|
-
const usedQuota = quota.usedQuota ?? 0;
|
|
13
|
-
return {
|
|
14
|
-
hasQuota: true,
|
|
15
|
-
totalQuota,
|
|
16
|
-
usedQuota,
|
|
17
|
-
pct: computeUsagePct(usedQuota, totalQuota),
|
|
18
|
-
expiresAt: quota.expiresAt ? new Date(quota.expiresAt).toISOString() : null,
|
|
19
|
-
isExceeded: usedQuota >= totalQuota,
|
|
20
|
-
isExpired: isPastExpiration(quota.expiresAt),
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
exports.computeClientQuotaStatus = computeClientQuotaStatus;
|
|
24
|
-
const getQuotaAlerts = (quota) => quota?.alerts?.length ? quota.alerts : [];
|
|
25
|
-
exports.getQuotaAlerts = getQuotaAlerts;
|
|
26
|
-
const findFullQuotaAlertIndex = (alerts) => alerts.findIndex((a) => a.thresholdPct >= clients_quota_constants_1.FULL_QUOTA_THRESHOLD);
|
|
27
|
-
exports.findFullQuotaAlertIndex = findFullQuotaAlertIndex;
|
|
28
|
-
const getFullQuotaAlert = (quota) => {
|
|
29
|
-
const alerts = (0, exports.getQuotaAlerts)(quota);
|
|
30
|
-
const index = (0, exports.findFullQuotaAlertIndex)(alerts);
|
|
31
|
-
return index >= 0 ? alerts[index] : undefined;
|
|
32
|
-
};
|
|
33
|
-
exports.getFullQuotaAlert = getFullQuotaAlert;
|
|
34
|
-
const isFullQuotaAlert = (thresholdPct) => thresholdPct >= clients_quota_constants_1.FULL_QUOTA_THRESHOLD;
|
|
35
|
-
exports.isFullQuotaAlert = isFullQuotaAlert;
|
|
36
|
-
const toClientQuotaNotifications = (quota) => ({
|
|
37
|
-
alerts: (0, exports.getQuotaAlerts)(quota ?? undefined).map(({ thresholdPct, recipientEmails, thresholdSent }) => ({
|
|
38
|
-
thresholdPct,
|
|
39
|
-
recipientEmails: recipientEmails ?? [],
|
|
40
|
-
...(thresholdSent ? { thresholdSent: true } : {}),
|
|
41
|
-
})),
|
|
42
|
-
...(quota?.expiredSent ? { expiredSent: true } : {}),
|
|
43
|
-
});
|
|
44
|
-
exports.toClientQuotaNotifications = toClientQuotaNotifications;
|
|
45
|
-
//# sourceMappingURL=clients.quota.utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clients.quota.utils.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.utils.ts"],"names":[],"mappings":";;;AAAA,uEAAiE;AAQjE,MAAM,eAAe,GAAG,CAAC,SAAiB,EAAE,UAAkB,EAAU,EAAE,CACxE,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAElE,MAAM,gBAAgB,GAAG,CAAC,SA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,EAAE;IACnC,MAAM,MAAM,GAAG,IAAA,sBAAc,EAAC,KAAK,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,IAAA,+BAAuB,EAAC,MAAM,CAAC,CAAC;IAC9C,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAChD,CAAC,CAAC;AANW,QAAA,iBAAiB,qBAM5B;AAEK,MAAM,gBAAgB,GAAG,CAAC,YAAoB,EAAW,EAAE,CAChE,YAAY,IAAI,8CAAoB,CAAC;AAD1B,QAAA,gBAAgB,oBACU;AAEhC,MAAM,0BAA0B,GAAG,CACxC,KAA8B,EACJ,EAAE,CAAC,CAAC;IAC9B,MAAM,EAAE,IAAA,sBAAc,EAAC,KAAK,IAAI,SAAS,CAAC,CAAC,GAAG,CAC5C,CAAC,EAAE,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,EAAE,EAAE,CAAC,CAAC;QACrD,YAAY;QACZ,eAAe,EAAE,eAAe,IAAI,EAAE;QACtC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC3D,CAAC,CACH;IACD,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;CAC9D,CAAC,CAAC;AAXU,QAAA,0BAA0B,8BAWpC"}
|
|
@@ -1,123 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -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;
|