@talkpilot/core-db 1.0.4 → 1.0.11
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/dist/talkpilot/calls/calls.getters.d.ts +5 -2
- package/dist/talkpilot/calls/calls.getters.d.ts.map +1 -1
- package/dist/talkpilot/calls/calls.getters.js +25 -1
- package/dist/talkpilot/calls/calls.getters.js.map +1 -1
- package/dist/talkpilot/calls/calls.types.d.ts +31 -1
- package/dist/talkpilot/calls/calls.types.d.ts.map +1 -1
- package/dist/talkpilot/calls/calls.types.js +3 -0
- package/dist/talkpilot/calls/calls.types.js.map +1 -1
- package/dist/talkpilot/clientAudioBuffers/index.d.ts +3 -0
- package/dist/talkpilot/clientAudioBuffers/index.d.ts.map +1 -0
- package/dist/talkpilot/clientAudioBuffers/index.js +19 -0
- package/dist/talkpilot/clientAudioBuffers/index.js.map +1 -0
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.d.ts.map +1 -1
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js +5 -2
- package/dist/talkpilot/clientsConfig/clientsConfig.getters.js.map +1 -1
- package/dist/talkpilot/flows/flows.types.d.ts +4 -0
- package/dist/talkpilot/flows/flows.types.d.ts.map +1 -1
- package/dist/talkpilot/groups/index.d.ts +1 -0
- package/dist/talkpilot/groups/index.d.ts.map +1 -1
- package/dist/talkpilot/groups/index.js +1 -0
- package/dist/talkpilot/groups/index.js.map +1 -1
- package/dist/talkpilot/index.d.ts +1 -1
- package/dist/talkpilot/index.d.ts.map +1 -1
- package/dist/talkpilot/index.js +1 -1
- package/dist/talkpilot/index.js.map +1 -1
- package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts +6 -3
- package/dist/talkpilot/phone_numbers/phone_numbers.getter.d.ts.map +1 -1
- package/dist/talkpilot/phone_numbers/phone_numbers.getter.js +35 -19
- package/dist/talkpilot/phone_numbers/phone_numbers.getter.js.map +1 -1
- package/dist/talkpilot/plans/plans.getters.d.ts +4 -2
- package/dist/talkpilot/plans/plans.getters.d.ts.map +1 -1
- package/dist/talkpilot/plans/plans.getters.js +14 -11
- package/dist/talkpilot/plans/plans.getters.js.map +1 -1
- package/dist/talkpilot/plans/plans.types.d.ts +1 -0
- package/dist/talkpilot/plans/plans.types.d.ts.map +1 -1
- package/dist/talkpilot/results/results.types.d.ts +2 -0
- package/dist/talkpilot/results/results.types.d.ts.map +1 -1
- package/dist/talkpilot/sessions/index.d.ts +2 -2
- package/dist/talkpilot/sessions/index.d.ts.map +1 -1
- package/dist/talkpilot/sessions/index.js +16 -3
- package/dist/talkpilot/sessions/index.js.map +1 -1
- package/dist/talkpilot/sessions/sessions.getter.d.ts.map +1 -1
- package/dist/talkpilot/sessions/sessions.getter.js +57 -6
- package/dist/talkpilot/sessions/sessions.getter.js.map +1 -1
- package/dist/talkpilot/subscriptions/subscriptions.getters.d.ts +2 -1
- package/dist/talkpilot/subscriptions/subscriptions.getters.d.ts.map +1 -1
- package/dist/talkpilot/subscriptions/subscriptions.getters.js +9 -11
- package/dist/talkpilot/subscriptions/subscriptions.getters.js.map +1 -1
- package/dist/talkpilot/subscriptions/subscriptions.types.d.ts +1 -0
- package/dist/talkpilot/subscriptions/subscriptions.types.d.ts.map +1 -1
- package/dist/talkpilot/utils/query.utils.d.ts +8 -0
- package/dist/talkpilot/utils/query.utils.d.ts.map +1 -0
- package/dist/talkpilot/utils/query.utils.js +17 -0
- package/dist/talkpilot/utils/query.utils.js.map +1 -0
- package/package.json +2 -2
- package/src/talkpilot/calls/__tests__/calls.spec.ts +58 -0
- package/src/talkpilot/calls/calls.getters.ts +29 -2
- package/src/talkpilot/calls/calls.types.ts +38 -1
- package/src/talkpilot/clientAudioBuffers/index.ts +2 -0
- package/src/talkpilot/clientsConfig/clientsConfig.getters.ts +6 -6
- package/src/talkpilot/flows/flows.types.ts +6 -0
- package/src/talkpilot/groups/index.ts +1 -0
- package/src/talkpilot/index.ts +1 -1
- package/src/talkpilot/phone_numbers/__tests__/phone_numbers.spec.ts +57 -1
- package/src/talkpilot/phone_numbers/phone_numbers.getter.ts +49 -18
- package/src/talkpilot/plans/__tests__/plans.spec.ts +66 -0
- package/src/talkpilot/plans/plans.getters.ts +26 -22
- package/src/talkpilot/plans/plans.types.ts +1 -0
- package/src/talkpilot/results/results.types.ts +2 -0
- package/src/talkpilot/sessions/__tests__/sessions.spec.ts +73 -10
- package/src/talkpilot/sessions/index.ts +2 -2
- package/src/talkpilot/sessions/sessions.getter.ts +66 -7
- package/src/talkpilot/subscriptions/subscriptions.getters.ts +19 -23
- package/src/talkpilot/subscriptions/subscriptions.types.ts +1 -0
- package/src/talkpilot/utils/__tests__/query.utils.spec.ts +49 -0
- package/src/talkpilot/utils/query.utils.ts +21 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/sessions/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/sessions/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}
|
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "getSessionsCollection", { enumerable: true, get: function () { return sessions_getter_1.getSessionsCollection; } });
|
|
17
|
+
__exportStar(require("./sessions.getter"), exports);
|
|
18
|
+
__exportStar(require("./sessions.types"), exports);
|
|
6
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/talkpilot/sessions/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/talkpilot/sessions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,mDAAiC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessions.getter.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/sessions/sessions.getter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAEhD,eAAO,MAAM,qBAAqB,QAAO,UAAU,CAAC,OAAO,CAClB,CAAC;
|
|
1
|
+
{"version":3,"file":"sessions.getter.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/sessions/sessions.getter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAElD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAEhD,eAAO,MAAM,qBAAqB,QAAO,UAAU,CAAC,OAAO,CAClB,CAAC;AA8B1C,eAAO,MAAM,eAAe,GAC1B,WAAW,QAAQ,EACnB,UAAU,MAAM,KACf,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAEhC,CAAC;AAEF,eAAO,MAAM,yBAAyB,GAAU,MAAM,MAAM,EAAE,IAAI,MAAM,oCA2CvE,CAAC"}
|
|
@@ -5,21 +5,72 @@ const mongodb_1 = require("mongodb");
|
|
|
5
5
|
const index_1 = require("../index");
|
|
6
6
|
const getSessionsCollection = () => (0, index_1.getDb)().collection('sessions');
|
|
7
7
|
exports.getSessionsCollection = getSessionsCollection;
|
|
8
|
+
const buildPhoneCandidates = (phone) => {
|
|
9
|
+
const raw = (phone ?? '').trim();
|
|
10
|
+
if (!raw)
|
|
11
|
+
return [];
|
|
12
|
+
const noPlus = raw.startsWith('+') ? raw.slice(1) : raw;
|
|
13
|
+
const digitsOnly = raw.replace(/\D/g, '');
|
|
14
|
+
const digitsOnlyNoPlus = noPlus.replace(/\D/g, '');
|
|
15
|
+
return Array.from(new Set([raw, noPlus, digitsOnly, digitsOnlyNoPlus].filter(Boolean)));
|
|
16
|
+
};
|
|
17
|
+
const buildFlowIdCandidates = (flowId) => {
|
|
18
|
+
if (!flowId)
|
|
19
|
+
return [];
|
|
20
|
+
// Some sessions store flow_id as string, others as ObjectId.
|
|
21
|
+
if (flowId instanceof mongodb_1.ObjectId) {
|
|
22
|
+
return [flowId, flowId.toString()];
|
|
23
|
+
}
|
|
24
|
+
const asString = String(flowId);
|
|
25
|
+
if (!asString)
|
|
26
|
+
return [];
|
|
27
|
+
if (mongodb_1.ObjectId.isValid(asString)) {
|
|
28
|
+
return [new mongodb_1.ObjectId(asString), asString];
|
|
29
|
+
}
|
|
30
|
+
return [asString];
|
|
31
|
+
};
|
|
8
32
|
const findSessionById = (sessionId, clientId) => {
|
|
9
33
|
return (0, exports.getSessionsCollection)().findOne({ _id: sessionId, clientId: clientId });
|
|
10
34
|
};
|
|
11
35
|
exports.findSessionById = findSessionById;
|
|
12
36
|
const findSessionOfIncomingCall = async (from, to) => {
|
|
13
|
-
const
|
|
37
|
+
const toCandidates = buildPhoneCandidates(to);
|
|
38
|
+
const fromCandidates = buildPhoneCandidates(from);
|
|
39
|
+
const receiverPhoneData = await (0, index_1.getPhoneNumbersCollection)().findOne({
|
|
40
|
+
phone_number: { $in: toCandidates },
|
|
41
|
+
});
|
|
14
42
|
if (!receiverPhoneData) {
|
|
15
43
|
return null;
|
|
16
44
|
}
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
45
|
+
const flowIdCandidates = buildFlowIdCandidates(receiverPhoneData.flow_id);
|
|
46
|
+
const [session] = await (0, exports.getSessionsCollection)()
|
|
47
|
+
.aggregate([
|
|
48
|
+
{
|
|
49
|
+
$match: {
|
|
50
|
+
flow_id: { $in: flowIdCandidates },
|
|
51
|
+
phone_numbers: {
|
|
52
|
+
$elemMatch: { phoneNumber: { $in: fromCandidates } },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
21
55
|
},
|
|
22
|
-
|
|
56
|
+
{
|
|
57
|
+
$addFields: {
|
|
58
|
+
phone_numbers: {
|
|
59
|
+
$filter: {
|
|
60
|
+
input: '$phone_numbers',
|
|
61
|
+
as: 'pn',
|
|
62
|
+
cond: { $in: ['$$pn.phoneNumber', fromCandidates] },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{ $limit: 1 },
|
|
68
|
+
])
|
|
69
|
+
.toArray();
|
|
70
|
+
if (!session) {
|
|
71
|
+
console.info('No session found for incoming call');
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
23
74
|
return session;
|
|
24
75
|
};
|
|
25
76
|
exports.findSessionOfIncomingCall = findSessionOfIncomingCall;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessions.getter.js","sourceRoot":"","sources":["../../../src/talkpilot/sessions/sessions.getter.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAEnC,oCAA4D;AAGrD,MAAM,qBAAqB,GAAG,GAAwB,EAAE,CAC7D,IAAA,aAAK,GAAE,CAAC,UAAU,CAAU,UAAU,CAAC,CAAC;AAD7B,QAAA,qBAAqB,yBACQ;
|
|
1
|
+
{"version":3,"file":"sessions.getter.js","sourceRoot":"","sources":["../../../src/talkpilot/sessions/sessions.getter.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAEnC,oCAA4D;AAGrD,MAAM,qBAAqB,GAAG,GAAwB,EAAE,CAC7D,IAAA,aAAK,GAAE,CAAC,UAAU,CAAU,UAAU,CAAC,CAAC;AAD7B,QAAA,qBAAqB,yBACQ;AAE1C,MAAM,oBAAoB,GAAG,CAAC,KAAa,EAAY,EAAE;IACvD,MAAM,GAAG,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACjC,IAAI,CAAC,GAAG;QAAE,OAAO,EAAE,CAAC;IAEpB,MAAM,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IACxD,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1C,MAAM,gBAAgB,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAEnD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1F,CAAC,CAAC;AAEF,MAAM,qBAAqB,GAAG,CAAC,MAAe,EAA4B,EAAE;IAC1E,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAA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B;QAC1B;YACE,MAAM,EAAE;gBACN,OAAO,EAAE,EAAE,GAAG,EAAE,gBAAgB,EAAE;gBAClC,aAAa,EAAE;oBACb,UAAU,EAAE,EAAE,WAAW,EAAE,EAAE,GAAG,EAAE,cAAc,EAAE,EAAE;iBACrD;aACF;SACF;QACD;YACE,UAAU,EAAE;gBACV,aAAa,EAAE;oBACb,OAAO,EAAE;wBACP,KAAK,EAAE,gBAAgB;wBACvB,EAAE,EAAE,IAAI;wBACR,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,kBAAkB,EAAE,cAAc,CAAC,EAAE;qBACpD;iBACF;aACF;SACF;QACD,EAAE,MAAM,EAAE,CAAC,EAAE;KACd,CAAC;SACD,OAAO,EAAE,CAAC;IAEb,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AA3CW,QAAA,yBAAyB,6BA2CpC"}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Subscription, SubscriptionDoc, SubscriptionFilter, SubscriptionQueryOptions } from './subscriptions.types';
|
|
2
|
-
import { ObjectId } from 'mongodb';
|
|
2
|
+
import { Filter, ObjectId } from 'mongodb';
|
|
3
3
|
export declare const getSubscriptionsCollection: () => import("mongodb").Collection<Subscription>;
|
|
4
4
|
/**
|
|
5
5
|
* A simple filter for Subscription: each field may be a value or a MongoDB operator expression
|
|
6
6
|
*/
|
|
7
7
|
export declare const findSubscriptions: (filter?: SubscriptionFilter, options?: SubscriptionQueryOptions) => Promise<SubscriptionDoc[]>;
|
|
8
|
+
export declare const countSubscriptions: (query: Filter<Subscription>) => Promise<number>;
|
|
8
9
|
export declare const createSubscriptionDoc: (subscriptionData: Omit<Subscription, "createdAt" | "updatedAt">) => Promise<SubscriptionDoc>;
|
|
9
10
|
export declare const updateSubscriptionDoc: (subscriptionId: ObjectId, updates: Partial<Subscription>) => Promise<SubscriptionDoc | null>;
|
|
10
11
|
export declare const incrementSubscriptionUsage: (subscriptionId: ObjectId, amount?: number) => Promise<SubscriptionDoc | null>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptions.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions.getters.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"subscriptions.getters.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions.getters.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,YAAY,EACZ,eAAe,EACf,kBAAkB,EAClB,wBAAwB,EACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAC,MAAM,EAAE,QAAQ,EAAC,MAAM,SAAS,CAAC;AAIzC,eAAO,MAAM,0BAA0B,kDAEtC,CAAC;AAEF;;GAEG;AAEH,eAAO,MAAM,iBAAiB,GAC5B,SAAS,kBAAkB,EAC3B,UAAU,wBAAwB,KACjC,OAAO,CAAC,eAAe,EAAE,CA4B3B,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAU,OAAO,MAAM,CAAC,YAAY,CAAC,KAAG,OAAO,CAAC,MAAM,CAEpF,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAChC,kBAAkB,IAAI,CAAC,YAAY,EAAE,WAAW,GAAG,WAAW,CAAC,KAC9D,OAAO,CAAC,eAAe,CAWzB,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAChC,gBAAgB,QAAQ,EACxB,SAAS,OAAO,CAAC,YAAY,CAAC,KAC7B,OAAO,CAAC,eAAe,GAAG,IAAI,CAahC,CAAC;AAEF,eAAO,MAAM,0BAA0B,GACrC,gBAAgB,QAAQ,EACxB,SAAQ,MAAU,KACjB,OAAO,CAAC,eAAe,GAAG,IAAI,CAahC,CAAC;AAEF,eAAO,MAAM,kCAAkC,GAC7C,gBAAgB,QAAQ,EACxB,iBAAiB,IAAI,KACpB,OAAO,CAAC,IAAI,CAcd,CAAC;AAEF,eAAO,MAAM,wBAAwB,GACnC,gBAAgB,QAAQ,EACxB,UAAU,YAAY,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KACvC,OAAO,CAAC,eAAe,GAAG,IAAI,CAYhC,CAAC"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pushNewSubscriptionCycle = exports.deactivateCurrentSubscriptionCycle = exports.incrementSubscriptionUsage = exports.updateSubscriptionDoc = exports.createSubscriptionDoc = exports.findSubscriptions = exports.getSubscriptionsCollection = void 0;
|
|
3
|
+
exports.pushNewSubscriptionCycle = exports.deactivateCurrentSubscriptionCycle = exports.incrementSubscriptionUsage = exports.updateSubscriptionDoc = exports.createSubscriptionDoc = exports.countSubscriptions = exports.findSubscriptions = exports.getSubscriptionsCollection = void 0;
|
|
4
4
|
const index_1 = require("../index");
|
|
5
5
|
const subscriptions_getters_utils_1 = require("./subscriptions.getters.utils");
|
|
6
|
+
const query_utils_1 = require("../utils/query.utils");
|
|
6
7
|
const getSubscriptionsCollection = () => {
|
|
7
8
|
return (0, index_1.getDb)().collection('subscriptions');
|
|
8
9
|
};
|
|
@@ -34,16 +35,14 @@ const findSubscriptions = async (filter, options) => {
|
|
|
34
35
|
query.$and = filter.and.map(subscriptions_getters_utils_1.buildSimpleQuery);
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
queryBuilder = queryBuilder.sort(options.sort);
|
|
40
|
-
}
|
|
41
|
-
if (options?.limit) {
|
|
42
|
-
queryBuilder = queryBuilder.limit(options.limit);
|
|
43
|
-
}
|
|
44
|
-
return await queryBuilder.toArray();
|
|
38
|
+
const cursor = (0, exports.getSubscriptionsCollection)().find(query);
|
|
39
|
+
return await (0, query_utils_1.applyQueryOptions)(cursor, options).toArray();
|
|
45
40
|
};
|
|
46
41
|
exports.findSubscriptions = findSubscriptions;
|
|
42
|
+
const countSubscriptions = async (query) => {
|
|
43
|
+
return (0, exports.getSubscriptionsCollection)().countDocuments(query);
|
|
44
|
+
};
|
|
45
|
+
exports.countSubscriptions = countSubscriptions;
|
|
47
46
|
const createSubscriptionDoc = async (subscriptionData) => {
|
|
48
47
|
const now = new Date();
|
|
49
48
|
const result = await (0, exports.getSubscriptionsCollection)().insertOne({
|
|
@@ -92,14 +91,13 @@ const deactivateCurrentSubscriptionCycle = async (subscriptionId, nextRenewalDat
|
|
|
92
91
|
};
|
|
93
92
|
exports.deactivateCurrentSubscriptionCycle = deactivateCurrentSubscriptionCycle;
|
|
94
93
|
const pushNewSubscriptionCycle = async (subscriptionId, newCycle) => {
|
|
95
|
-
|
|
94
|
+
return await (0, exports.getSubscriptionsCollection)().findOneAndUpdate({ _id: subscriptionId }, {
|
|
96
95
|
$push: {
|
|
97
96
|
cycles: newCycle,
|
|
98
97
|
},
|
|
99
98
|
}, {
|
|
100
99
|
returnDocument: 'after',
|
|
101
100
|
});
|
|
102
|
-
return result;
|
|
103
101
|
};
|
|
104
102
|
exports.pushNewSubscriptionCycle = pushNewSubscriptionCycle;
|
|
105
103
|
//# sourceMappingURL=subscriptions.getters.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptions.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions.getters.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"subscriptions.getters.js","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions.getters.ts"],"names":[],"mappings":";;;AAAA,oCAA+B;AAS/B,+EAA+D;AAC/D,sDAAuD;AAEhD,MAAM,0BAA0B,GAAG,GAAG,EAAE;IAC7C,OAAO,IAAA,aAAK,GAAE,CAAC,UAAU,CAAe,eAAe,CAAC,CAAC;AAC3D,CAAC,CAAC;AAFW,QAAA,0BAA0B,8BAErC;AAEF;;GAEG;A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AAA,kCAA0B,GAAE,CAAC,gBAAgB,CACxD,EAAE,GAAG,EAAE,cAAc,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAChD;QACE,IAAI,EAAE,EAAE,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE;QACpC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,IAAI,EAAE,EAAE;KAChC,EACD;QACE,YAAY,EAAE,CAAC,EAAE,sBAAsB,EAAE,IAAI,EAAE,CAAC;QAChD,cAAc,EAAE,OAAO;KACxB,CACF,CAAC;AACJ,CAAC,CAAC;AAhBW,QAAA,0BAA0B,8BAgBrC;AAEK,MAAM,kCAAkC,GAAG,KAAK,EACrD,cAAwB,EACxB,eAAqB,EACN,EAAE;IACjB,MAAM,IAAA,kCAA0B,GAAE,CAAC,SAAS,CAC1C,EAAE,GAAG,EAAE,cAAc,EAAE,iBAAiB,EAAE,IAAI,EAAE,EAChD;QACE,IAAI,EAAE;YACJ,gCAAgC,EAAE,KAAK;YACvC,eAAe,EAAE,eAAe;YAChC,SAAS,EAAE,IAAI,IAAI,EAAE;SACtB;KACF,EACD;QACE,YAAY,EAAE,CAAC,EAAE,sBAAsB,EAAE,IAAI,EAAE,CAAC;KACjD,CACF,CAAC;AACJ,CAAC,CAAC;AAjBW,QAAA,kCAAkC,sCAiB7C;AAEK,MAAM,wBAAwB,GAAG,KAAK,EAC3C,cAAwB,EACxB,QAAwC,EACP,EAAE;IACnC,OAAO,MAAM,IAAA,kCAA0B,GAAE,CAAC,gBAAgB,CACtD,EAAC,GAAG,EAAE,cAAc,EAAC,EACrB;QACE,KAAK,EAAE;YACL,MAAM,EAAE,QAAQ;SACjB;KACF,EACD;QACE,cAAc,EAAE,OAAO;KACxB,CACJ,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,wBAAwB,4BAenC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptions.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions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AC1D,EAAE,CAAC,EAAE,wBAAwB,EAAE,CAAC;IAChC,GAAG,CAAC,EAAE,wBAAwB,EAAE,CAAC;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
|
1
|
+
{"version":3,"file":"subscriptions.types.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/subscriptions/subscriptions.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,MAAM,iBAAiB,GAAG;IAC9B,KAAK,EAAE;QACL,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IACF,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,EAAE,IAAI,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,OAAO,CAAC;IACtB,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,CAAC,EAAE,IAAI,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,eAAe,EAAE,IAAI,CAAC;IACtB,MAAM,EAAE,iBAAiB,EAAE,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AACnD,MAAM,MAAM,aAAa,GAAG,eAAe,EAAE,CAAC;AAG9C,eAAO,MAAM,YAAY,qGAQf,CAAC;AACX,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,GAAG,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;CACzB,GAAG;KACD,CAAC,IAAI,MAAM,YAAY,CAAC,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;CACpD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG;IAC1D,EAAE,CAAC,EAAE,wBAAwB,EAAE,CAAC;IAChC,GAAG,CAAC,EAAE,wBAAwB,EAAE,CAAC;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FindCursor } from 'mongodb';
|
|
2
|
+
export type QueryOptions = {
|
|
3
|
+
sort?: any;
|
|
4
|
+
skip?: number;
|
|
5
|
+
limit?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const applyQueryOptions: <T>(cursor: FindCursor<T>, options?: QueryOptions) => FindCursor<T>;
|
|
8
|
+
//# sourceMappingURL=query.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.utils.d.ts","sourceRoot":"","sources":["../../../src/talkpilot/utils/query.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,MAAM,MAAM,YAAY,GAAG;IACzB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,CAAC,EACjC,QAAQ,UAAU,CAAC,CAAC,CAAC,EACrB,UAAU,YAAY,KACrB,UAAU,CAAC,CAAC,CASd,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyQueryOptions = void 0;
|
|
4
|
+
const applyQueryOptions = (cursor, options) => {
|
|
5
|
+
if (!options)
|
|
6
|
+
return cursor;
|
|
7
|
+
let result = cursor;
|
|
8
|
+
if (options.sort)
|
|
9
|
+
result = result.sort(options.sort);
|
|
10
|
+
if (options.skip)
|
|
11
|
+
result = result.skip(options.skip);
|
|
12
|
+
if (options.limit)
|
|
13
|
+
result = result.limit(options.limit);
|
|
14
|
+
return result;
|
|
15
|
+
};
|
|
16
|
+
exports.applyQueryOptions = applyQueryOptions;
|
|
17
|
+
//# sourceMappingURL=query.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.utils.js","sourceRoot":"","sources":["../../../src/talkpilot/utils/query.utils.ts"],"names":[],"mappings":";;;AAQO,MAAM,iBAAiB,GAAG,CAC/B,MAAqB,EACrB,OAAsB,EACP,EAAE;IACjB,IAAI,CAAC,OAAO;QAAE,OAAO,MAAM,CAAC;IAE5B,IAAI,MAAM,GAAG,MAAM,CAAC;IACpB,IAAI,OAAO,CAAC,IAAI;QAAE,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,IAAI,OAAO,CAAC,IAAI;QAAE,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,IAAI,OAAO,CAAC,KAAK;QAAE,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAExD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAZW,QAAA,iBAAiB,qBAY5B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@talkpilot/core-db",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"description": "Core database package for centralized connections and ORM integration.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"express": "^4.18.0",
|
|
25
25
|
"google-libphonenumber": "^3.2.0",
|
|
26
|
-
"mongodb": "^6.
|
|
26
|
+
"mongodb": "^6.11.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@faker-js/faker": "^8.0.0",
|
|
@@ -4,6 +4,10 @@ import {
|
|
|
4
4
|
getCallsByClient,
|
|
5
5
|
getCallsByPhoneNumber,
|
|
6
6
|
getCallByCallSid,
|
|
7
|
+
findCallsByQuery,
|
|
8
|
+
countCalls,
|
|
9
|
+
getCallsByClientAndDateRange,
|
|
10
|
+
getCallsCollection,
|
|
7
11
|
} from '../calls.getters';
|
|
8
12
|
import { ObjectId } from 'mongodb';
|
|
9
13
|
import { createOutGoingCallDoc } from '../../../test-utils/factories';
|
|
@@ -53,4 +57,58 @@ describe('db.calls', () => {
|
|
|
53
57
|
|
|
54
58
|
expect(result.some(c => c.callSid === call.callSid)).toBe(true);
|
|
55
59
|
});
|
|
60
|
+
|
|
61
|
+
describe('findCallsByQuery()', () => {
|
|
62
|
+
it('should find calls by status with limit and sort', async () => {
|
|
63
|
+
const clientId = 'client123';
|
|
64
|
+
const call1 = createOutGoingCallDoc({ clientId, status: 'completed' });
|
|
65
|
+
const call2 = createOutGoingCallDoc({ clientId, status: 'completed' });
|
|
66
|
+
const call3 = createOutGoingCallDoc({ clientId, status: 'busy' });
|
|
67
|
+
|
|
68
|
+
// We need to control createdAt to test sort. We'll insert directly.
|
|
69
|
+
await getCallsCollection().insertOne({ ...call1, createdAt: new Date('2023-01-01'), updatedAt: new Date(), env: 'test' });
|
|
70
|
+
await getCallsCollection().insertOne({ ...call2, createdAt: new Date('2023-01-02'), updatedAt: new Date(), env: 'test' });
|
|
71
|
+
await getCallsCollection().insertOne({ ...call3, createdAt: new Date(), updatedAt: new Date(), env: 'test' });
|
|
72
|
+
|
|
73
|
+
const result = await findCallsByQuery(
|
|
74
|
+
{ clientId, status: 'completed' },
|
|
75
|
+
{ sort: { createdAt: -1 }, limit: 1 }
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
expect(result.length).toBe(1);
|
|
79
|
+
expect(result[0].callSid).toBe(call2.callSid);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('countCalls()', () => {
|
|
84
|
+
it('should count calls matching query', async () => {
|
|
85
|
+
const clientId = 'countClient';
|
|
86
|
+
await createCallDoc(createOutGoingCallDoc({ clientId, status: 'completed' }));
|
|
87
|
+
await createCallDoc(createOutGoingCallDoc({ clientId, status: 'completed' }));
|
|
88
|
+
await createCallDoc(createOutGoingCallDoc({ clientId, status: 'failed' }));
|
|
89
|
+
|
|
90
|
+
const count = await countCalls({ clientId, status: 'completed' });
|
|
91
|
+
expect(count).toBe(2);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('getCallsByClientAndDateRange()', () => {
|
|
96
|
+
it('should return calls within date range', async () => {
|
|
97
|
+
const clientId = 'dateRangeClient';
|
|
98
|
+
const startDate = new Date('2023-05-01');
|
|
99
|
+
const endDate = new Date('2023-05-31');
|
|
100
|
+
|
|
101
|
+
const callInside = createOutGoingCallDoc({ clientId });
|
|
102
|
+
const callBefore = createOutGoingCallDoc({ clientId });
|
|
103
|
+
const callAfter = createOutGoingCallDoc({ clientId });
|
|
104
|
+
|
|
105
|
+
await getCallsCollection().insertOne({ ...callInside, createdAt: new Date('2023-05-15'), updatedAt: new Date(), env: 'test' });
|
|
106
|
+
await getCallsCollection().insertOne({ ...callBefore, createdAt: new Date('2023-04-30'), updatedAt: new Date(), env: 'test' });
|
|
107
|
+
await getCallsCollection().insertOne({ ...callAfter, createdAt: new Date('2023-06-01'), updatedAt: new Date(), env: 'test' });
|
|
108
|
+
|
|
109
|
+
const result = await getCallsByClientAndDateRange(clientId, startDate, endDate);
|
|
110
|
+
expect(result.length).toBe(1);
|
|
111
|
+
expect(result[0].callSid).toBe(callInside.callSid);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
56
114
|
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { getDb, Call, CallDoc, CallUpdateParams } from '../index';
|
|
1
|
+
import { getDb, Call, CallDoc, CallUpdateParams, CallQueryOptions } from '../index';
|
|
2
2
|
import type { CallsByHour } from './calls.types';
|
|
3
|
-
import { ObjectId } from 'mongodb';
|
|
3
|
+
import { ObjectId, Filter } from 'mongodb';
|
|
4
4
|
import * as process from 'node:process';
|
|
5
|
+
import { applyQueryOptions } from '../utils/query.utils';
|
|
5
6
|
|
|
6
7
|
export const getCallsCollection = () => {
|
|
7
8
|
return getDb().collection<Call>('calls');
|
|
@@ -50,6 +51,32 @@ export const updateCallByCallSid = async (
|
|
|
50
51
|
return result;
|
|
51
52
|
};
|
|
52
53
|
|
|
54
|
+
// get calls by client and date range
|
|
55
|
+
//client here is the user id
|
|
56
|
+
export const getCallsByClientAndDateRange = (clientId: string, startDate: Date, endDate: Date) => {
|
|
57
|
+
return getCallsCollection()
|
|
58
|
+
.find({
|
|
59
|
+
clientId,
|
|
60
|
+
createdAt: {
|
|
61
|
+
$gte: startDate,
|
|
62
|
+
$lte: endDate,
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
.toArray();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const findCallsByQuery = async (
|
|
69
|
+
query: Filter<Call>,
|
|
70
|
+
options?: CallQueryOptions
|
|
71
|
+
): Promise<CallDoc[]> => {
|
|
72
|
+
const cursor = getCallsCollection().find(query);
|
|
73
|
+
return await applyQueryOptions(cursor, options).toArray();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const countCalls = async (query: Filter<Call>): Promise<number> => {
|
|
77
|
+
return getCallsCollection().countDocuments(query);
|
|
78
|
+
};
|
|
79
|
+
|
|
53
80
|
/**
|
|
54
81
|
* Aggregate calls stats for a date range (createdAt in [startStr, endStr] in timezone).
|
|
55
82
|
* completed = agentHungUp=true or status='completed'.
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import { ObjectId, WithId } from 'mongodb';
|
|
1
|
+
import { ObjectId, Sort, WithId } from 'mongodb';
|
|
2
2
|
import { TranscriptionSegment } from '../results/results.types';
|
|
3
3
|
|
|
4
|
+
export const CONFERENCE_ROLE_CUSTOMER = 'customer' as const;
|
|
5
|
+
export const CONFERENCE_ROLE_SUPERVISOR = 'supervisor' as const;
|
|
6
|
+
|
|
7
|
+
export type ConferenceRole =
|
|
8
|
+
| typeof CONFERENCE_ROLE_CUSTOMER
|
|
9
|
+
| typeof CONFERENCE_ROLE_SUPERVISOR
|
|
10
|
+
| null;
|
|
11
|
+
|
|
4
12
|
export type Call = {
|
|
5
13
|
callSid: string;
|
|
6
14
|
flowId: ObjectId;
|
|
@@ -21,7 +29,36 @@ export type Call = {
|
|
|
21
29
|
env: string;
|
|
22
30
|
updatedAt: Date;
|
|
23
31
|
createdAt: Date;
|
|
32
|
+
isAnsweredByAnsweringMachine?: boolean;
|
|
24
33
|
agentHungUp?: boolean;
|
|
34
|
+
endReason?: string;
|
|
35
|
+
conferenceName?: string | null;
|
|
36
|
+
conferenceSid?: string | null;
|
|
37
|
+
conferenceParticipantSid?: string | null;
|
|
38
|
+
isConferenceCall?: boolean;
|
|
39
|
+
conferenceRole?: ConferenceRole;
|
|
40
|
+
redirectedCall?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type CallQueryOptions = {
|
|
44
|
+
sort?: Sort;
|
|
45
|
+
skip?: number;
|
|
46
|
+
limit?: number;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type CallsFilterParams = {
|
|
50
|
+
clientId: string;
|
|
51
|
+
callSid?: string;
|
|
52
|
+
startDate?: Date;
|
|
53
|
+
endDate?: Date;
|
|
54
|
+
status?: string;
|
|
55
|
+
customerPhoneNumber?: string;
|
|
56
|
+
agentPhoneNumber?: string;
|
|
57
|
+
flowId?: string;
|
|
58
|
+
runId?: string;
|
|
59
|
+
sessionId?: string;
|
|
60
|
+
isIncoming?: boolean;
|
|
61
|
+
search?: string;
|
|
25
62
|
};
|
|
26
63
|
|
|
27
64
|
export type ImmutableCallFields =
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { ClientConfigDoc, getDb } from '../index';
|
|
1
|
+
import { ClientConfigDoc, getDb, findClientByPhoneNumber } from '../index';
|
|
2
2
|
import { Collection } from 'mongodb';
|
|
3
3
|
|
|
4
4
|
export const getClientsConfigCollection = (): Collection<ClientConfigDoc> => {
|
|
5
|
-
return getDb().collection('clientsConfig');
|
|
5
|
+
return getDb().collection<ClientConfigDoc>('clientsConfig');
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export const getClientConfig = (clientId: string) => {
|
|
9
9
|
return getClientsConfigCollection().findOne({ clientId });
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
export const getClientConfigByPhone = async (
|
|
13
|
-
|
|
14
|
-
)
|
|
15
|
-
return
|
|
12
|
+
export const getClientConfigByPhone = async (phone: string) => {
|
|
13
|
+
const client = await findClientByPhoneNumber(phone);
|
|
14
|
+
if (!client) return null;
|
|
15
|
+
return getClientConfig(client.clientId);
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
export const createClientConfigDoc = async (clientConfig: ClientConfigDoc): Promise<void> => {
|
|
@@ -70,6 +70,8 @@ export type FlowTool = {
|
|
|
70
70
|
headers?: Record<string, string>;
|
|
71
71
|
parameters: Record<string, unknown>;
|
|
72
72
|
bodyFormat?: 'json' | 'form-data';
|
|
73
|
+
preventAudioDuringTool?: boolean;
|
|
74
|
+
sendUserAuthToken?: boolean;
|
|
73
75
|
};
|
|
74
76
|
|
|
75
77
|
export type SendMethod = 'sms' | 'whatsapp';
|
|
@@ -144,6 +146,10 @@ export type Flow = {
|
|
|
144
146
|
metaKeys?: MetaKey[] | null;
|
|
145
147
|
userProperties?: UserProperty[] | null;
|
|
146
148
|
tools?: FlowTool[];
|
|
149
|
+
afterCallEvent?: string;
|
|
150
|
+
|
|
151
|
+
// AI provider configuration
|
|
152
|
+
aiProvider?: any;
|
|
147
153
|
|
|
148
154
|
// Timestamps
|
|
149
155
|
createdAt?: Date;
|
package/src/talkpilot/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export * from './plans';
|
|
|
11
11
|
export * from './subscriptions';
|
|
12
12
|
export * from './groups';
|
|
13
13
|
export * from './calls';
|
|
14
|
-
export * from './clientAudioBuffers
|
|
14
|
+
export * from './clientAudioBuffers';
|
|
15
15
|
export * from './clientsConfig';
|
|
16
16
|
export { mongodbClient } from './mongodb-client';
|
|
17
17
|
|
|
@@ -2,9 +2,12 @@ import {
|
|
|
2
2
|
getPhoneDataByPhoneNumber,
|
|
3
3
|
getPhoneNumbersCollection,
|
|
4
4
|
getClientPhoneNumber,
|
|
5
|
+
getClientPhoneData,
|
|
6
|
+
createPhoneNumberEntity,
|
|
5
7
|
} from '../phone_numbers.getter';
|
|
6
8
|
import { getFlowsCollection } from '../../flows/flows.getter';
|
|
7
9
|
import { createFlow, createPhoneNumber } from '../../../test-utils/factories';
|
|
10
|
+
import { ObjectId } from 'mongodb';
|
|
8
11
|
|
|
9
12
|
describe('db.phoneNumbers', () => {
|
|
10
13
|
describe('getPhoneDataByPhoneNumber', () => {
|
|
@@ -31,7 +34,7 @@ describe('db.phoneNumbers', () => {
|
|
|
31
34
|
describe('getClientPhoneNumber', () => {
|
|
32
35
|
it('return phone number by client id', async () => {
|
|
33
36
|
const flow = createFlow();
|
|
34
|
-
const phoneData = createPhoneNumber({ flow_id: flow._id });
|
|
37
|
+
const phoneData = createPhoneNumber({ flow_id: flow._id, is_primary: true });
|
|
35
38
|
|
|
36
39
|
await getFlowsCollection().insertOne(flow);
|
|
37
40
|
await getPhoneNumbersCollection().insertOne(phoneData);
|
|
@@ -40,4 +43,57 @@ describe('db.phoneNumbers', () => {
|
|
|
40
43
|
expect(result).toBe(phoneData.phone_number);
|
|
41
44
|
});
|
|
42
45
|
});
|
|
46
|
+
|
|
47
|
+
describe('getClientPhoneData', () => {
|
|
48
|
+
it('returns primary phone data by default', async () => {
|
|
49
|
+
const clientId = 'multiPhoneClient';
|
|
50
|
+
const phone1 = createPhoneNumber({ client_id: clientId, is_primary: false, phone_number: '1', createdAt: new Date('2023-01-01') });
|
|
51
|
+
const phone2 = createPhoneNumber({ client_id: clientId, is_primary: true, phone_number: '2', createdAt: new Date('2023-01-02') });
|
|
52
|
+
|
|
53
|
+
await getPhoneNumbersCollection().insertOne(phone1);
|
|
54
|
+
await getPhoneNumbersCollection().insertOne(phone2);
|
|
55
|
+
|
|
56
|
+
const result = await getClientPhoneData(clientId);
|
|
57
|
+
expect(result?.phone_number).toBe('2');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('returns most recent non-primary phone data if isPrimary is false', async () => {
|
|
61
|
+
const clientId = 'multiPhoneClient2';
|
|
62
|
+
const phone1 = createPhoneNumber({ client_id: clientId, is_primary: false, phone_number: '1', createdAt: new Date('2023-01-01') });
|
|
63
|
+
const phone2 = createPhoneNumber({ client_id: clientId, is_primary: false, phone_number: '2', createdAt: new Date('2023-01-02') });
|
|
64
|
+
const phone3 = createPhoneNumber({ client_id: clientId, is_primary: true, phone_number: '3', createdAt: new Date('2023-01-03') });
|
|
65
|
+
|
|
66
|
+
await getPhoneNumbersCollection().insertOne(phone1);
|
|
67
|
+
await getPhoneNumbersCollection().insertOne(phone2);
|
|
68
|
+
await getPhoneNumbersCollection().insertOne(phone3);
|
|
69
|
+
|
|
70
|
+
const result = await getClientPhoneData(clientId, false);
|
|
71
|
+
expect(result?.phone_number).toBe('2');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('createPhoneNumberEntity', () => {
|
|
76
|
+
it('creates first phone number as primary', async () => {
|
|
77
|
+
const clientId = 'newClient';
|
|
78
|
+
const flowId = new ObjectId().toHexString();
|
|
79
|
+
const phoneNumber = '+123456789';
|
|
80
|
+
|
|
81
|
+
const result = await createPhoneNumberEntity(phoneNumber, flowId, clientId);
|
|
82
|
+
expect(result.phone_number).toBe(phoneNumber);
|
|
83
|
+
expect(result.is_primary).toBe(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('creates subsequent phone number as non-primary', async () => {
|
|
87
|
+
const clientId = 'existingClient';
|
|
88
|
+
const flowId = new ObjectId().toHexString();
|
|
89
|
+
|
|
90
|
+
// Create first one
|
|
91
|
+
await createPhoneNumberEntity('+111', flowId, clientId);
|
|
92
|
+
|
|
93
|
+
// Create second one
|
|
94
|
+
const result = await createPhoneNumberEntity('+222', flowId, clientId);
|
|
95
|
+
expect(result.phone_number).toBe('+222');
|
|
96
|
+
expect(result.is_primary).toBe(false);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
43
99
|
});
|