@talkpilot/core-db 1.3.35 → 1.3.36
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 +54 -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 +24 -0
- package/dist/talkpilot/clients/clients.quota.types.d.ts.map +1 -0
- package/dist/talkpilot/clients/clients.quota.types.js +3 -0
- package/dist/talkpilot/clients/clients.quota.types.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 +5 -0
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +37 -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/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 +27 -0
- package/src/talkpilot/clients/clients.types.ts +4 -0
- package/src/talkpilot/clients/index.ts +2 -0
- package/src/talkpilot/clientsConfig/__tests__/clientsConfig.spec.ts +26 -0
- package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +54 -0
- package/src/talkpilot/clientsConfig/clientsConfig.types.ts +3 -0
package/README.md
CHANGED
|
@@ -160,6 +160,9 @@ Refer to `DEVELOPMENT.md` for the full walkthrough, token instructions, and fact
|
|
|
160
160
|
| 1.3.24 | **Breaking:** removed `flowId` from `WebsiteTalkProduct` — flow source of truth is `phone_numbers.flow_id`. Added `getPrimaryPhoneFlowId(clientId)`. |
|
|
161
161
|
| 1.3.25 | Added `disconnectDb()` to close all MongoDB clients opened by `ensureDbConnected()` — for one-shot scripts that must exit cleanly. |
|
|
162
162
|
| 1.3.28 | Soft-delete (`isActive`) on Streets/DepartmentSubjects, new bulk-write getters, MongoDB type re-exports, and the generic `bulkWrite` op-builder module. |
|
|
163
|
+
| 1.3.29 | Added client call-quota module (CP-1500): `ClientConfig.quota` for usage limits (`totalQuota`, `usedQuota`, `expiresAt`), alert settings on `clients`, and getters for state, increment/reset, threshold alerts, and expiry notification tracking. |
|
|
164
|
+
| 1.3.32 | `clientDisplayName` lives on `clientsConfig.quota` (not top-level). `getClientDisplayName` returns `"-"` when unset; never exposes `clientId`. |
|
|
165
|
+
|
|
163
166
|
### 1.1.9
|
|
164
167
|
|
|
165
168
|
1. `npm run build`.
|
|
@@ -544,6 +547,57 @@ Adds an `isActive` soft-delete convention, bulk-write getters for Streets/Depart
|
|
|
544
547
|
|
|
545
548
|
No — purely additive.
|
|
546
549
|
|
|
550
|
+
### 1.3.29 — Client call quota (CP-1500)
|
|
551
|
+
|
|
552
|
+
Per-client call quota with usage counters, optional expiration, and configurable threshold/expiry email alerts. Usage lives on clientsConfig; alert config and send-tracking live on clients. TalkPilot Server consumes these getters to block calls when quota is exceeded or expired and to send notification emails.
|
|
553
|
+
|
|
554
|
+
**Data model**
|
|
555
|
+
|
|
556
|
+
| Location | Field | Purpose |
|
|
557
|
+
| --- | --- | --- |
|
|
558
|
+
| `clientsConfig.quota` | `totalQuota`, `usedQuota`, `expiresAt?` | Call allowance and consumption. `getClientQuotaState` returns `null` when `totalQuota` is unset (quota disabled for that client). |
|
|
559
|
+
| `clients.quota` | `alerts?`, `expiredSent?` | Up to 3 threshold alerts (each with up to 3 recipient emails) and a flag set after the expiry email is sent. |
|
|
560
|
+
|
|
561
|
+
**Added**
|
|
562
|
+
|
|
563
|
+
| Export | Purpose |
|
|
564
|
+
| --- | --- |
|
|
565
|
+
| `ClientQuotaUsage` | Usage shape: `totalQuota`, `usedQuota`, optional `expiresAt`. |
|
|
566
|
+
| `ClientQuotaSettings` | Alert settings on the `clients` doc: `alerts`, `expiredSent`. |
|
|
567
|
+
| `ClientQuotaState` | Merged view returned by `getClientQuotaState` (usage + settings). |
|
|
568
|
+
| `QuotaThresholdAlert` / `QuotaAlertInput` | Threshold alert with `thresholdPct`, `recipientEmails`, and optional `thresholdSent` send-tracking. |
|
|
569
|
+
| `getClientQuotaState(clientId)` | Read combined quota state; `null` when no quota is configured. |
|
|
570
|
+
| `incrementClientQuotaUsage(clientId, amount?)` | Atomically increment `clientsConfig.quota.usedQuota` (default `amount = 1`). No-op when `quota.totalQuota` is absent. |
|
|
571
|
+
| `resetClientQuotaUsage(clientId)` | Reset `usedQuota` to `0` and clear alert/expiry send flags via `resetClientQuotaSettings`. |
|
|
572
|
+
| `updateQuotaAlertsConfig(clientId, alerts)` | Replace threshold alert config; preserves `thresholdSent` for unchanged thresholds. |
|
|
573
|
+
| `markQuotaAlertSent(clientId, alertIndex)` | Idempotently mark a threshold alert as sent; returns whether the update applied. |
|
|
574
|
+
| `markQuotaExpiredSent(clientId)` | Idempotently mark the expiry notification as sent; returns whether the update applied. |
|
|
575
|
+
| `resetClientQuotaSettings(clientId)` | Clear `expiredSent` and strip `thresholdSent` from alerts (keeps alert definitions). |
|
|
576
|
+
|
|
577
|
+
**Do I need to update my app?**
|
|
578
|
+
|
|
579
|
+
No — purely additive. TalkPilot Server should upgrade to 1.3.29 (or later) to enforce quota on inbound/outbound calls and to drive threshold/expiry emails via the new getters.
|
|
580
|
+
|
|
581
|
+
### 1.3.32 — Quota `clientDisplayName`
|
|
582
|
+
|
|
583
|
+
Optional human-readable account label stored on `clientsConfig.quota` for quota notification emails. TalkPilot Server uses `getClientDisplayName` instead of exposing raw `clientId` in alert/expiry email copy.
|
|
584
|
+
|
|
585
|
+
**Changed**
|
|
586
|
+
|
|
587
|
+
| Area | What |
|
|
588
|
+
| --- | --- |
|
|
589
|
+
| `ClientQuotaUsage.clientDisplayName?` | Optional display name on `clientsConfig.quota` — e.g. `"Acme Corp"`. Included in `ClientQuotaState` via `getClientQuotaState`. |
|
|
590
|
+
|
|
591
|
+
**Added**
|
|
592
|
+
|
|
593
|
+
| Export | Purpose |
|
|
594
|
+
| --- | --- |
|
|
595
|
+
| `getClientDisplayName(clientId)` | Returns trimmed `quota.clientDisplayName`, or `"-"` when unset, missing, or blank. Never falls back to `clientId`. |
|
|
596
|
+
|
|
597
|
+
**Do I need to update my app?**
|
|
598
|
+
|
|
599
|
+
No — purely additive. Set `clientsConfig.quota.clientDisplayName` when provisioning quota for a client. **TalkPilot Server** should upgrade to **1.3.32** (or later) to use the label in quota emails.
|
|
600
|
+
|
|
547
601
|
## 🛠 CI/CD & deployment
|
|
548
602
|
|
|
549
603
|
- This package is consumed by Cloud Build-based services (TalkPilot Server, MIS, CIS) as a dependency when Docker images are built. Keep `dist/` in sync with your builds because the compiled artifact is what downstream services install.
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clients.quota.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.getters.ts"],"names":[],"mappings":";;AAOA,kDAoBC;AAYD,0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,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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
/** Human-readable account label for quota alert emails. */
|
|
21
|
+
clientDisplayName?: string;
|
|
22
|
+
};
|
|
23
|
+
export type ClientQuotaState = ClientQuotaUsage & ClientQuotaSettings;
|
|
24
|
+
//# sourceMappingURL=clients.quota.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;IACjB,2DAA2D;IAC3D,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clients.quota.types.js","sourceRoot":"","sources":["../../../src/talkpilot/clients/clients.quota.types.ts"],"names":[],"mappings":""}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { ObjectId } from "mongodb";
|
|
2
|
+
import { ClientQuotaSettings } from "./clients.quota.types";
|
|
3
|
+
export type { QuotaThresholdAlert, ClientQuotaSettings, QuotaAlertInput, ClientQuotaUsage, ClientQuotaState } from "./clients.quota.types";
|
|
2
4
|
export type Client = {
|
|
3
5
|
_id?: ObjectId;
|
|
4
6
|
createdAt: Date;
|
|
@@ -8,6 +10,7 @@ export type Client = {
|
|
|
8
10
|
apiKey?: string | null;
|
|
9
11
|
credits: number;
|
|
10
12
|
managedBy?: string;
|
|
13
|
+
quota?: ClientQuotaSettings;
|
|
11
14
|
};
|
|
12
15
|
export type ClientDoc = Client & {
|
|
13
16
|
_id: ObjectId;
|
|
@@ -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"}
|
|
@@ -2,10 +2,15 @@ import { ClientConfigDoc, Products, KnownProductKey } from "../index";
|
|
|
2
2
|
import { Collection } from "mongodb";
|
|
3
3
|
export declare const getClientsConfigCollection: () => Collection<ClientConfigDoc>;
|
|
4
4
|
export declare const getClientConfig: (clientId: string) => Promise<import("mongodb").WithId<ClientConfigDoc> | null>;
|
|
5
|
+
export declare const getClientDisplayName: (clientId: string) => Promise<string>;
|
|
5
6
|
export declare const getClientConfigByPhone: (phone: string) => Promise<import("mongodb").WithId<ClientConfigDoc> | null>;
|
|
6
7
|
export declare const createClientConfigDoc: (clientConfig: ClientConfigDoc) => Promise<void>;
|
|
7
8
|
export declare function updateProductConfig<K extends KnownProductKey>(clientId: string, productKey: K, updates: Partial<Products[K]>): Promise<void>;
|
|
8
9
|
export declare const getActiveScanId: (clientId: string) => Promise<string | undefined>;
|
|
9
10
|
export declare const updateActiveScanId: (clientId: string, scanId: string) => Promise<void>;
|
|
10
11
|
export declare const clearActiveScanId: (clientId: string) => Promise<void>;
|
|
12
|
+
export declare function listClientIdsWithConfiguredQuota(): Promise<string[]>;
|
|
13
|
+
export declare function incrementClientQuotaUsage(clientId: string, amount?: number): Promise<void>;
|
|
14
|
+
export declare function resetClientQuotaUsage(clientId: string): Promise<void>;
|
|
15
|
+
export { getClientQuotaState, updateQuotaAlertsConfig, markQuotaAlertSent, markQuotaExpiredSent, resetClientQuotaSettings, } from "../clients/clients.quota.getters";
|
|
11
16
|
//# 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,oBAAoB,GAAU,UAAU,MAAM,KAAG,OAAO,CAAC,MAAM,CAO3E,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.getClientDisplayName = 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
|
};
|
|
@@ -11,6 +15,12 @@ const getClientConfig = (clientId) => {
|
|
|
11
15
|
return (0, exports.getClientsConfigCollection)().findOne({ clientId });
|
|
12
16
|
};
|
|
13
17
|
exports.getClientConfig = getClientConfig;
|
|
18
|
+
const getClientDisplayName = async (clientId) => {
|
|
19
|
+
const doc = await (0, exports.getClientsConfigCollection)().findOne({ clientId }, { projection: { "quota.clientDisplayName": 1, _id: 0 } });
|
|
20
|
+
const name = doc?.quota?.clientDisplayName?.trim();
|
|
21
|
+
return name || "-";
|
|
22
|
+
};
|
|
23
|
+
exports.getClientDisplayName = getClientDisplayName;
|
|
14
24
|
const getClientConfigByPhone = async (phone) => {
|
|
15
25
|
const client = await (0, index_1.findClientByPhoneNumber)(phone);
|
|
16
26
|
if (!client)
|
|
@@ -51,4 +61,30 @@ const clearActiveScanId = async (clientId) => {
|
|
|
51
61
|
}
|
|
52
62
|
};
|
|
53
63
|
exports.clearActiveScanId = clearActiveScanId;
|
|
64
|
+
async function listClientIdsWithConfiguredQuota() {
|
|
65
|
+
const docs = await (0, exports.getClientsConfigCollection)()
|
|
66
|
+
.find({ "quota.totalQuota": { $exists: true, $ne: null } })
|
|
67
|
+
.project({ clientId: 1, _id: 0 })
|
|
68
|
+
.toArray();
|
|
69
|
+
return docs.map(({ clientId }) => clientId);
|
|
70
|
+
}
|
|
71
|
+
async function incrementClientQuotaUsage(clientId, amount = 1) {
|
|
72
|
+
const result = await (0, exports.getClientsConfigCollection)().updateOne({ clientId, "quota.totalQuota": { $exists: true } }, { $inc: { "quota.usedQuota": amount } });
|
|
73
|
+
if (result.matchedCount === 0) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function resetClientQuotaUsage(clientId) {
|
|
78
|
+
const result = await (0, exports.getClientsConfigCollection)().updateOne({ clientId }, { $set: { "quota.usedQuota": 0 } });
|
|
79
|
+
if (result.matchedCount === 0) {
|
|
80
|
+
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
81
|
+
}
|
|
82
|
+
await (0, clients_quota_getters_1.resetClientQuotaSettings)(clientId);
|
|
83
|
+
}
|
|
84
|
+
var clients_quota_getters_2 = require("../clients/clients.quota.getters");
|
|
85
|
+
Object.defineProperty(exports, "getClientQuotaState", { enumerable: true, get: function () { return clients_quota_getters_2.getClientQuotaState; } });
|
|
86
|
+
Object.defineProperty(exports, "updateQuotaAlertsConfig", { enumerable: true, get: function () { return clients_quota_getters_2.updateQuotaAlertsConfig; } });
|
|
87
|
+
Object.defineProperty(exports, "markQuotaAlertSent", { enumerable: true, get: function () { return clients_quota_getters_2.markQuotaAlertSent; } });
|
|
88
|
+
Object.defineProperty(exports, "markQuotaExpiredSent", { enumerable: true, get: function () { return clients_quota_getters_2.markQuotaExpiredSent; } });
|
|
89
|
+
Object.defineProperty(exports, "resetClientQuotaSettings", { enumerable: true, get: function () { return clients_quota_getters_2.resetClientQuotaSettings; } });
|
|
54
90
|
//# 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/cl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AAC,QAAgB;IAC1D,MAAM,MAAM,GAAG,MAAM,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"}
|
|
@@ -75,6 +75,7 @@ export type CommunicationsConfig = {
|
|
|
75
75
|
sms?: SmsConfig;
|
|
76
76
|
whatsapp?: WhatsappConfig;
|
|
77
77
|
};
|
|
78
|
+
import type { ClientQuotaUsage } from "../clients/clients.quota.types";
|
|
78
79
|
export type ClientConfig<P extends Products = Products> = {
|
|
79
80
|
clientId: string;
|
|
80
81
|
phoneNumber?: string;
|
|
@@ -83,6 +84,7 @@ export type ClientConfig<P extends Products = Products> = {
|
|
|
83
84
|
timezone?: string;
|
|
84
85
|
communications?: CommunicationsConfig;
|
|
85
86
|
callLengthThresholds?: CallLengthThresholds;
|
|
87
|
+
quota?: ClientQuotaUsage;
|
|
86
88
|
};
|
|
87
89
|
export type ClientConfigDoc<P extends Products = Products> = WithId<ClientConfig<P>>;
|
|
88
90
|
//# 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,C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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,27 @@
|
|
|
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
|
+
/** Human-readable account label for quota alert emails. */
|
|
24
|
+
clientDisplayName?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
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 };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createClientConfigDoc,
|
|
3
3
|
getClientConfig,
|
|
4
|
+
getClientDisplayName,
|
|
4
5
|
} from "../clientsConfig.getters";
|
|
5
6
|
import { createClientConfig } from "../../../test-utils/factories";
|
|
6
7
|
|
|
@@ -59,6 +60,31 @@ describe("db.clientsConfig", () => {
|
|
|
59
60
|
expect(result).toBeNull();
|
|
60
61
|
});
|
|
61
62
|
|
|
63
|
+
it("should return quota.clientDisplayName when set on client config", async () => {
|
|
64
|
+
const clientId = "display-name-client";
|
|
65
|
+
await createClientConfigDoc(
|
|
66
|
+
createClientConfig({
|
|
67
|
+
clientId,
|
|
68
|
+
quota: { totalQuota: 100, usedQuota: 0, clientDisplayName: "Acme Corp" },
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect(await getClientDisplayName(clientId)).toBe("Acme Corp");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should return '-' when quota.clientDisplayName is missing", async () => {
|
|
76
|
+
const clientId = "no-display-name-client";
|
|
77
|
+
await createClientConfigDoc(
|
|
78
|
+
createClientConfig({ clientId, quota: { totalQuota: 100, usedQuota: 0 } }),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(await getClientDisplayName(clientId)).toBe("-");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should return '-' when client config is missing", async () => {
|
|
85
|
+
expect(await getClientDisplayName("missing-client")).toBe("-");
|
|
86
|
+
});
|
|
87
|
+
|
|
62
88
|
it("given moked_106 with both toolsPrompts when saved and retrieved then both prompts are persisted", async () => {
|
|
63
89
|
// Given
|
|
64
90
|
const clientId = "tools-prompts-both";
|
|
@@ -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");
|
|
@@ -16,6 +17,15 @@ export const getClientConfig = (clientId: string) => {
|
|
|
16
17
|
return getClientsConfigCollection().findOne({ clientId });
|
|
17
18
|
};
|
|
18
19
|
|
|
20
|
+
export const getClientDisplayName = async (clientId: string): Promise<string> => {
|
|
21
|
+
const doc = await getClientsConfigCollection().findOne(
|
|
22
|
+
{ clientId },
|
|
23
|
+
{ projection: { "quota.clientDisplayName": 1, _id: 0 } },
|
|
24
|
+
);
|
|
25
|
+
const name = doc?.quota?.clientDisplayName?.trim();
|
|
26
|
+
return name || "-";
|
|
27
|
+
};
|
|
28
|
+
|
|
19
29
|
export const getClientConfigByPhone = async (phone: string) => {
|
|
20
30
|
const client = await findClientByPhoneNumber(phone);
|
|
21
31
|
if (!client) return null;
|
|
@@ -77,3 +87,47 @@ export const clearActiveScanId = async (clientId: string): Promise<void> => {
|
|
|
77
87
|
throw new Error(`No client config found for clientId: ${clientId}`);
|
|
78
88
|
}
|
|
79
89
|
};
|
|
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";
|
|
@@ -113,6 +113,8 @@ export type CommunicationsConfig = {
|
|
|
113
113
|
whatsapp?: WhatsappConfig;
|
|
114
114
|
};
|
|
115
115
|
|
|
116
|
+
import type { ClientQuotaUsage } from "../clients/clients.quota.types";
|
|
117
|
+
|
|
116
118
|
export type ClientConfig<P extends Products = Products> = {
|
|
117
119
|
clientId: string;
|
|
118
120
|
phoneNumber?: string;
|
|
@@ -121,6 +123,7 @@ export type ClientConfig<P extends Products = Products> = {
|
|
|
121
123
|
timezone?: string;
|
|
122
124
|
communications?: CommunicationsConfig;
|
|
123
125
|
callLengthThresholds?: CallLengthThresholds;
|
|
126
|
+
quota?: ClientQuotaUsage;
|
|
124
127
|
};
|
|
125
128
|
|
|
126
129
|
export type ClientConfigDoc<P extends Products = Products> = WithId<
|