@app-connect/core 1.7.35 → 1.7.37
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/connector/mock.js +9 -4
- package/connector/proxy/engine.js +3 -2
- package/connector/proxy/index.js +2 -2
- package/docs/architecture.md +1 -0
- package/docs/handlers.md +1 -1
- package/docs/models.md +2 -0
- package/docs/routes.md +1 -1
- package/handlers/disposition.js +3 -2
- package/handlers/log.js +16 -6
- package/handlers/plugin.js +13 -6
- package/index.js +19 -4
- package/lib/callLogLookup.js +82 -9
- package/lib/migrateCallLogsSchema.js +315 -7
- package/models/callLogModel.js +6 -0
- package/package.json +1 -1
- package/releaseNotes.json +20 -0
- package/test/connector/developerPortal.test.js +166 -0
- package/test/connector/mock.test.js +131 -0
- package/test/connector/proxy/engine.test.js +85 -0
- package/test/connector/proxy/index.test.js +246 -0
- package/test/connector/proxy/sample.json +1 -0
- package/test/handlers/admin.test.js +344 -0
- package/test/handlers/appointment.test.js +260 -0
- package/test/handlers/calldown.test.js +310 -0
- package/test/handlers/disposition.test.js +396 -0
- package/test/handlers/log.test.js +324 -1
- package/test/handlers/managedOAuth.test.js +262 -0
- package/test/handlers/plugin.test.js +305 -0
- package/test/handlers/user.test.js +381 -0
- package/test/index.test.js +102 -0
- package/test/lib/analytics.test.js +146 -0
- package/test/lib/authSession.test.js +173 -0
- package/test/lib/encode.test.js +59 -0
- package/test/lib/errorHandler.test.js +246 -0
- package/test/lib/generalErrorMessage.test.js +82 -0
- package/test/lib/s3ErrorLogReport.test.js +187 -0
- package/test/mcp/mcpHandlerMore.test.js +384 -0
- package/test/mcp/tools/appointmentTools.test.js +362 -0
- package/test/models/callDownListModel.test.js +125 -0
- package/test/models/dynamo/lockSchema.test.js +37 -0
- package/test/models/dynamo/noteCacheSchema.test.js +45 -0
- package/test/models/llmSessionModel.test.js +91 -0
- package/test/models/models.test.js +92 -0
- package/test/routes/calldownRoutes.test.js +224 -0
- package/test/routes/coreRouterBroadRoutes.test.js +855 -0
- package/test/routes/dispositionRoutes.test.js +192 -0
- package/test/routes/managedAuthRoutes.test.js +151 -0
- package/test/routes/pluginRoutes.test.js +262 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
const calldown = require('../../handlers/calldown');
|
|
2
|
+
const { generateJwt } = require('../../lib/jwt');
|
|
3
|
+
const { CallDownListModel } = require('../../models/callDownListModel');
|
|
4
|
+
const { UserModel } = require('../../models/userModel');
|
|
5
|
+
|
|
6
|
+
describe('Calldown Handler', () => {
|
|
7
|
+
const originalSecret = process.env.APP_SERVER_SECRET_KEY;
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
process.env.APP_SERVER_SECRET_KEY = 'test-app-server-secret-key-123456';
|
|
11
|
+
await UserModel.sync({ force: true });
|
|
12
|
+
await CallDownListModel.sync({ force: true });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(async () => {
|
|
16
|
+
await CallDownListModel.destroy({ where: {} });
|
|
17
|
+
await UserModel.destroy({ where: {} });
|
|
18
|
+
jest.restoreAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterAll(() => {
|
|
22
|
+
if (originalSecret === undefined) {
|
|
23
|
+
delete process.env.APP_SERVER_SECRET_KEY;
|
|
24
|
+
} else {
|
|
25
|
+
process.env.APP_SERVER_SECRET_KEY = originalSecret;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function tokenFor(userId) {
|
|
30
|
+
return generateJwt({
|
|
31
|
+
id: userId,
|
|
32
|
+
platform: 'testCRM',
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function createUser(id = 'user-1') {
|
|
37
|
+
return UserModel.create({
|
|
38
|
+
id,
|
|
39
|
+
platform: 'testCRM',
|
|
40
|
+
accessToken: 'token',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
test('schedule creates a scheduled record for the JWT user', async () => {
|
|
45
|
+
await createUser('user-1');
|
|
46
|
+
|
|
47
|
+
const result = await calldown.schedule({
|
|
48
|
+
jwtToken: tokenFor('user-1'),
|
|
49
|
+
body: {
|
|
50
|
+
contactId: 12345,
|
|
51
|
+
contactType: 'Lead',
|
|
52
|
+
contactName: 'Ignored By Current Schema',
|
|
53
|
+
phoneNumber: '+15551234567',
|
|
54
|
+
scheduledAt: '2026-07-02T13:00:00.000Z',
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(result.id).toMatch(/^[0-9a-f]{32}$/);
|
|
59
|
+
const record = await CallDownListModel.findByPk(result.id);
|
|
60
|
+
expect(record).toMatchObject({
|
|
61
|
+
id: result.id,
|
|
62
|
+
userId: 'user-1',
|
|
63
|
+
contactId: '12345',
|
|
64
|
+
contactType: 'Lead',
|
|
65
|
+
status: 'scheduled',
|
|
66
|
+
});
|
|
67
|
+
expect(record.scheduledAt.toISOString()).toBe('2026-07-02T13:00:00.000Z');
|
|
68
|
+
expect(record.lastCallAt).toBeNull();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('schedule uses current defaults for optional body fields', async () => {
|
|
72
|
+
await createUser('user-1');
|
|
73
|
+
|
|
74
|
+
const result = await calldown.schedule({
|
|
75
|
+
jwtToken: tokenFor('user-1'),
|
|
76
|
+
body: {},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const record = await CallDownListModel.findByPk(result.id);
|
|
80
|
+
expect(record.contactId).toBeNull();
|
|
81
|
+
expect(record.contactType).toBe('contact');
|
|
82
|
+
expect(record.status).toBe('scheduled');
|
|
83
|
+
expect(record.scheduledAt).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('schedule rejects invalid jwt and missing users', async () => {
|
|
87
|
+
await expect(calldown.schedule({
|
|
88
|
+
jwtToken: 'not-a-valid-token',
|
|
89
|
+
body: {},
|
|
90
|
+
})).rejects.toThrow('Unauthorized');
|
|
91
|
+
|
|
92
|
+
await expect(calldown.schedule({
|
|
93
|
+
jwtToken: tokenFor('missing-user'),
|
|
94
|
+
body: {},
|
|
95
|
+
})).rejects.toThrow('User not found');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('list returns all records for the JWT user ordered by scheduledAt', async () => {
|
|
99
|
+
await createUser('user-1');
|
|
100
|
+
await createUser('user-2');
|
|
101
|
+
await CallDownListModel.bulkCreate([
|
|
102
|
+
{
|
|
103
|
+
id: 'later',
|
|
104
|
+
userId: 'user-1',
|
|
105
|
+
contactId: 'contact-2',
|
|
106
|
+
status: 'called',
|
|
107
|
+
scheduledAt: new Date('2026-07-02T14:00:00.000Z'),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'earlier',
|
|
111
|
+
userId: 'user-1',
|
|
112
|
+
contactId: 'contact-1',
|
|
113
|
+
status: 'scheduled',
|
|
114
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'other-user',
|
|
118
|
+
userId: 'user-2',
|
|
119
|
+
contactId: 'contact-3',
|
|
120
|
+
status: 'scheduled',
|
|
121
|
+
scheduledAt: new Date('2026-07-02T12:00:00.000Z'),
|
|
122
|
+
},
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
const { items } = await calldown.list({
|
|
126
|
+
jwtToken: tokenFor('user-1'),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
expect(items.map((item) => item.id)).toEqual(['earlier', 'later']);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('list filters called and not-called statuses according to current implementation', async () => {
|
|
133
|
+
await createUser('user-1');
|
|
134
|
+
await CallDownListModel.bulkCreate([
|
|
135
|
+
{
|
|
136
|
+
id: 'called',
|
|
137
|
+
userId: 'user-1',
|
|
138
|
+
status: 'called',
|
|
139
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: 'scheduled',
|
|
143
|
+
userId: 'user-1',
|
|
144
|
+
status: 'scheduled',
|
|
145
|
+
scheduledAt: new Date('2026-07-02T14:00:00.000Z'),
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: 'removed-status',
|
|
149
|
+
userId: 'user-1',
|
|
150
|
+
status: 'removed',
|
|
151
|
+
scheduledAt: new Date('2026-07-02T15:00:00.000Z'),
|
|
152
|
+
},
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
const called = await calldown.list({
|
|
156
|
+
jwtToken: tokenFor('user-1'),
|
|
157
|
+
status: 'called',
|
|
158
|
+
});
|
|
159
|
+
const notCalled = await calldown.list({
|
|
160
|
+
jwtToken: tokenFor('user-1'),
|
|
161
|
+
status: 'not_called',
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(called.items.map((item) => item.id)).toEqual(['called']);
|
|
165
|
+
expect(notCalled.items.map((item) => item.id)).toEqual(['scheduled', 'removed-status']);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('remove physically deletes only records owned by the JWT user', async () => {
|
|
169
|
+
await createUser('user-1');
|
|
170
|
+
await createUser('user-2');
|
|
171
|
+
await CallDownListModel.bulkCreate([
|
|
172
|
+
{
|
|
173
|
+
id: 'owned-record',
|
|
174
|
+
userId: 'user-1',
|
|
175
|
+
status: 'scheduled',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'other-record',
|
|
179
|
+
userId: 'user-2',
|
|
180
|
+
status: 'scheduled',
|
|
181
|
+
},
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
await expect(calldown.remove({
|
|
185
|
+
jwtToken: tokenFor('user-1'),
|
|
186
|
+
id: 'other-record',
|
|
187
|
+
})).rejects.toThrow('Not found');
|
|
188
|
+
|
|
189
|
+
await expect(calldown.remove({
|
|
190
|
+
jwtToken: tokenFor('user-1'),
|
|
191
|
+
id: 'owned-record',
|
|
192
|
+
})).resolves.toEqual({ successful: true });
|
|
193
|
+
|
|
194
|
+
await expect(CallDownListModel.findByPk('owned-record')).resolves.toBeNull();
|
|
195
|
+
await expect(CallDownListModel.findByPk('other-record')).resolves.not.toBeNull();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('markCalled updates status and lastCallAt for owned records', async () => {
|
|
199
|
+
await createUser('user-1');
|
|
200
|
+
await CallDownListModel.create({
|
|
201
|
+
id: 'call-to-mark',
|
|
202
|
+
userId: 'user-1',
|
|
203
|
+
status: 'scheduled',
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const result = await calldown.markCalled({
|
|
207
|
+
jwtToken: tokenFor('user-1'),
|
|
208
|
+
id: 'call-to-mark',
|
|
209
|
+
lastCallAt: '2026-07-02T14:30:00.000Z',
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(result).toEqual({ successful: true });
|
|
213
|
+
const record = await CallDownListModel.findByPk('call-to-mark');
|
|
214
|
+
expect(record.status).toBe('called');
|
|
215
|
+
expect(record.lastCallAt.toISOString()).toBe('2026-07-02T14:30:00.000Z');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('markCalled preserves owner isolation and returns the current database error shape', async () => {
|
|
219
|
+
await createUser('user-1');
|
|
220
|
+
await createUser('user-2');
|
|
221
|
+
await CallDownListModel.create({
|
|
222
|
+
id: 'other-user-call',
|
|
223
|
+
userId: 'user-2',
|
|
224
|
+
status: 'scheduled',
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const result = await calldown.markCalled({
|
|
228
|
+
jwtToken: tokenFor('user-1'),
|
|
229
|
+
id: 'other-user-call',
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
expect(result).toEqual({
|
|
233
|
+
successful: false,
|
|
234
|
+
returnMessage: {
|
|
235
|
+
message: 'Database operation failed',
|
|
236
|
+
messageType: 'warning',
|
|
237
|
+
ttl: 5000,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
const record = await CallDownListModel.findByPk('other-user-call');
|
|
241
|
+
expect(record.status).toBe('scheduled');
|
|
242
|
+
expect(record.lastCallAt).toBeNull();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('update applies allowed fields and ignores disallowed fields', async () => {
|
|
246
|
+
await createUser('user-1');
|
|
247
|
+
await CallDownListModel.create({
|
|
248
|
+
id: 'call-to-update',
|
|
249
|
+
userId: 'user-1',
|
|
250
|
+
contactId: 'old-contact',
|
|
251
|
+
contactType: 'Lead',
|
|
252
|
+
status: 'scheduled',
|
|
253
|
+
scheduledAt: new Date('2026-07-02T13:00:00.000Z'),
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const result = await calldown.update({
|
|
257
|
+
jwtToken: tokenFor('user-1'),
|
|
258
|
+
id: 'call-to-update',
|
|
259
|
+
updateData: {
|
|
260
|
+
contactId: 'new-contact',
|
|
261
|
+
contactType: 'Contact',
|
|
262
|
+
status: 'called',
|
|
263
|
+
scheduledAt: '2026-07-02T15:00:00.000Z',
|
|
264
|
+
lastCallAt: '2026-07-02T15:10:00.000Z',
|
|
265
|
+
unexpectedField: 'ignored',
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
expect(result).toEqual({ successful: true });
|
|
270
|
+
const record = await CallDownListModel.findByPk('call-to-update');
|
|
271
|
+
expect(record).toMatchObject({
|
|
272
|
+
contactId: 'new-contact',
|
|
273
|
+
contactType: 'Contact',
|
|
274
|
+
status: 'called',
|
|
275
|
+
});
|
|
276
|
+
expect(record.scheduledAt.toISOString()).toBe('2026-07-02T15:00:00.000Z');
|
|
277
|
+
expect(record.lastCallAt.toISOString()).toBe('2026-07-02T15:10:00.000Z');
|
|
278
|
+
expect(record.unexpectedField).toBeUndefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('update rejects no-op and cross-owner updates', async () => {
|
|
282
|
+
await createUser('user-1');
|
|
283
|
+
await createUser('user-2');
|
|
284
|
+
await CallDownListModel.create({
|
|
285
|
+
id: 'other-user-update',
|
|
286
|
+
userId: 'user-2',
|
|
287
|
+
status: 'scheduled',
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
await expect(calldown.update({
|
|
291
|
+
jwtToken: tokenFor('user-1'),
|
|
292
|
+
id: 'other-user-update',
|
|
293
|
+
updateData: {
|
|
294
|
+
status: 'called',
|
|
295
|
+
},
|
|
296
|
+
})).rejects.toThrow('Not found');
|
|
297
|
+
|
|
298
|
+
await expect(calldown.update({
|
|
299
|
+
jwtToken: tokenFor('user-1'),
|
|
300
|
+
id: 'other-user-update',
|
|
301
|
+
updateData: {
|
|
302
|
+
unexpectedField: 'ignored',
|
|
303
|
+
},
|
|
304
|
+
})).rejects.toThrow('No valid fields to update');
|
|
305
|
+
|
|
306
|
+
const record = await CallDownListModel.findByPk('other-user-update');
|
|
307
|
+
expect(record.status).toBe('scheduled');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
jest.mock('../../lib/oauth');
|
|
2
|
+
jest.mock('../../models/dynamo/connectorSchema', () => ({
|
|
3
|
+
Connector: {
|
|
4
|
+
getProxyConfig: jest.fn(),
|
|
5
|
+
},
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
const disposition = require('../../handlers/disposition');
|
|
9
|
+
const connectorRegistry = require('../../connector/registry');
|
|
10
|
+
const oauth = require('../../lib/oauth');
|
|
11
|
+
const { Connector } = require('../../models/dynamo/connectorSchema');
|
|
12
|
+
const { CallLogModel } = require('../../models/callLogModel');
|
|
13
|
+
const { UserModel } = require('../../models/userModel');
|
|
14
|
+
|
|
15
|
+
describe('Disposition Handler', () => {
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
await UserModel.sync({ force: true });
|
|
18
|
+
await CallLogModel.sync({ force: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
await CallLogModel.destroy({ where: {} });
|
|
23
|
+
await UserModel.destroy({ where: {} });
|
|
24
|
+
jest.restoreAllMocks();
|
|
25
|
+
jest.clearAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
async function createUser(overrides = {}) {
|
|
29
|
+
return UserModel.create({
|
|
30
|
+
id: 'user-1',
|
|
31
|
+
platform: 'testCRM',
|
|
32
|
+
hostname: 'crm.example.com',
|
|
33
|
+
accessToken: 'access-token',
|
|
34
|
+
platformAdditionalInfo: {},
|
|
35
|
+
...overrides,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function createCallLog(overrides = {}) {
|
|
40
|
+
return CallLogModel.create({
|
|
41
|
+
id: 'call-log-1',
|
|
42
|
+
sessionId: 'session-1',
|
|
43
|
+
extensionNumber: '',
|
|
44
|
+
hashedExtensionId: '',
|
|
45
|
+
platform: 'testCRM',
|
|
46
|
+
thirdPartyLogId: 'third-party-log-1',
|
|
47
|
+
userId: 'user-1',
|
|
48
|
+
contactId: 'contact-1',
|
|
49
|
+
...overrides,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function mockApiKeyConnector(overrides = {}) {
|
|
54
|
+
return {
|
|
55
|
+
getAuthType: jest.fn().mockResolvedValue('apiKey'),
|
|
56
|
+
getBasicAuth: jest.fn().mockReturnValue('encoded-api-key'),
|
|
57
|
+
upsertCallDisposition: jest.fn().mockResolvedValue({
|
|
58
|
+
logId: 'disposition-log-1',
|
|
59
|
+
returnMessage: {
|
|
60
|
+
message: 'Disposition saved',
|
|
61
|
+
messageType: 'success',
|
|
62
|
+
ttl: 2000,
|
|
63
|
+
},
|
|
64
|
+
extraDataTracking: {
|
|
65
|
+
providerStatus: 'updated',
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
...overrides,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
test('returns a warning when no matching call log exists', async () => {
|
|
73
|
+
await createUser();
|
|
74
|
+
const getConnectorSpy = jest.spyOn(connectorRegistry, 'getConnector');
|
|
75
|
+
|
|
76
|
+
const result = await disposition.upsertCallDisposition({
|
|
77
|
+
platform: 'testCRM',
|
|
78
|
+
userId: 'user-1',
|
|
79
|
+
sessionId: 'missing-session',
|
|
80
|
+
dispositions: [{ id: 'disposition-1' }],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(result).toEqual({
|
|
84
|
+
successful: false,
|
|
85
|
+
returnMessage: {
|
|
86
|
+
message: 'Cannot find log',
|
|
87
|
+
messageType: 'warning',
|
|
88
|
+
ttl: 3000,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
expect(getConnectorSpy).not.toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('returns a warning when the matching call log exists but user is missing', async () => {
|
|
95
|
+
await createCallLog();
|
|
96
|
+
const getConnectorSpy = jest.spyOn(connectorRegistry, 'getConnector');
|
|
97
|
+
|
|
98
|
+
const result = await disposition.upsertCallDisposition({
|
|
99
|
+
platform: 'testCRM',
|
|
100
|
+
userId: 'missing-user',
|
|
101
|
+
sessionId: 'session-1',
|
|
102
|
+
dispositions: [{ id: 'disposition-1' }],
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
expect(result).toEqual({
|
|
106
|
+
successful: false,
|
|
107
|
+
returnMessage: {
|
|
108
|
+
message: 'Cannot find user',
|
|
109
|
+
messageType: 'warning',
|
|
110
|
+
ttl: 3000,
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
expect(getConnectorSpy).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('upserts dispositions with apiKey auth and existing call log', async () => {
|
|
117
|
+
await createUser();
|
|
118
|
+
const existingCallLog = await createCallLog({
|
|
119
|
+
extensionNumber: '101',
|
|
120
|
+
});
|
|
121
|
+
const connector = mockApiKeyConnector();
|
|
122
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
123
|
+
const dispositions = [
|
|
124
|
+
{ id: 'disp-1', value: 'Interested' },
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
const result = await disposition.upsertCallDisposition({
|
|
128
|
+
platform: 'testCRM',
|
|
129
|
+
userId: 'user-1',
|
|
130
|
+
sessionId: 'session-1',
|
|
131
|
+
extensionNumber: '101',
|
|
132
|
+
dispositions,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(connectorRegistry.getConnector).toHaveBeenCalledWith('testCRM');
|
|
136
|
+
expect(connector.getAuthType).toHaveBeenCalledWith({
|
|
137
|
+
proxyId: undefined,
|
|
138
|
+
proxyConfig: null,
|
|
139
|
+
});
|
|
140
|
+
expect(connector.getBasicAuth).toHaveBeenCalledWith({
|
|
141
|
+
apiKey: 'access-token',
|
|
142
|
+
});
|
|
143
|
+
expect(connector.upsertCallDisposition).toHaveBeenCalledWith({
|
|
144
|
+
user: expect.objectContaining({ id: 'user-1' }),
|
|
145
|
+
existingCallLog: expect.objectContaining({
|
|
146
|
+
id: existingCallLog.id,
|
|
147
|
+
sessionId: 'session-1',
|
|
148
|
+
extensionNumber: '101',
|
|
149
|
+
}),
|
|
150
|
+
authHeader: 'Basic encoded-api-key',
|
|
151
|
+
dispositions,
|
|
152
|
+
proxyConfig: null,
|
|
153
|
+
});
|
|
154
|
+
expect(result).toEqual({
|
|
155
|
+
successful: true,
|
|
156
|
+
logId: 'disposition-log-1',
|
|
157
|
+
returnMessage: {
|
|
158
|
+
message: 'Disposition saved',
|
|
159
|
+
messageType: 'success',
|
|
160
|
+
ttl: 2000,
|
|
161
|
+
},
|
|
162
|
+
extraDataTracking: {
|
|
163
|
+
providerStatus: 'updated',
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('returns unsuccessful when provider upsert does not return a log id', async () => {
|
|
169
|
+
await createUser();
|
|
170
|
+
await createCallLog();
|
|
171
|
+
const connector = mockApiKeyConnector({
|
|
172
|
+
upsertCallDisposition: jest.fn().mockResolvedValue({
|
|
173
|
+
returnMessage: {
|
|
174
|
+
message: 'Provider did not update disposition',
|
|
175
|
+
messageType: 'warning',
|
|
176
|
+
ttl: 3000,
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
181
|
+
|
|
182
|
+
const result = await disposition.upsertCallDisposition({
|
|
183
|
+
platform: 'testCRM',
|
|
184
|
+
userId: 'user-1',
|
|
185
|
+
sessionId: 'session-1',
|
|
186
|
+
dispositions: [{ id: 'disp-1' }],
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
expect(result.successful).toBe(false);
|
|
190
|
+
expect(result.logId).toBeUndefined();
|
|
191
|
+
expect(result.returnMessage.message).toBe('Provider did not update disposition');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('matches call log by hashed extension id when extension number differs', async () => {
|
|
195
|
+
await createUser();
|
|
196
|
+
await createCallLog({
|
|
197
|
+
id: 'hashed-call-log',
|
|
198
|
+
extensionNumber: '',
|
|
199
|
+
hashedExtensionId: 'hashed-extension-1',
|
|
200
|
+
});
|
|
201
|
+
const connector = mockApiKeyConnector();
|
|
202
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
203
|
+
|
|
204
|
+
await disposition.upsertCallDisposition({
|
|
205
|
+
platform: 'testCRM',
|
|
206
|
+
userId: 'user-1',
|
|
207
|
+
sessionId: 'session-1',
|
|
208
|
+
extensionNumber: '101',
|
|
209
|
+
hashedExtensionId: 'hashed-extension-1',
|
|
210
|
+
dispositions: [{ id: 'disp-1' }],
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
expect(connector.upsertCallDisposition).toHaveBeenCalledWith(expect.objectContaining({
|
|
214
|
+
existingCallLog: expect.objectContaining({
|
|
215
|
+
id: 'hashed-call-log',
|
|
216
|
+
hashedExtensionId: 'hashed-extension-1',
|
|
217
|
+
}),
|
|
218
|
+
}));
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('falls back to legacy extension-number match when hashed extension id is provided', async () => {
|
|
222
|
+
await createUser();
|
|
223
|
+
await createCallLog({
|
|
224
|
+
id: 'legacy-extension-call-log',
|
|
225
|
+
extensionNumber: '101',
|
|
226
|
+
hashedExtensionId: '',
|
|
227
|
+
});
|
|
228
|
+
const connector = mockApiKeyConnector();
|
|
229
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
230
|
+
|
|
231
|
+
await disposition.upsertCallDisposition({
|
|
232
|
+
platform: 'testCRM',
|
|
233
|
+
userId: 'user-1',
|
|
234
|
+
sessionId: 'session-1',
|
|
235
|
+
extensionNumber: '101',
|
|
236
|
+
hashedExtensionId: 'new-hashed-extension',
|
|
237
|
+
dispositions: [{ id: 'disp-1' }],
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
expect(connector.upsertCallDisposition).toHaveBeenCalledWith(expect.objectContaining({
|
|
241
|
+
existingCallLog: expect.objectContaining({
|
|
242
|
+
id: 'legacy-extension-call-log',
|
|
243
|
+
extensionNumber: '101',
|
|
244
|
+
hashedExtensionId: '',
|
|
245
|
+
}),
|
|
246
|
+
}));
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('uses OAuth app and refreshed access token for oauth connectors', async () => {
|
|
250
|
+
await createUser({
|
|
251
|
+
accessToken: 'old-access-token',
|
|
252
|
+
refreshToken: 'refresh-token',
|
|
253
|
+
platformAdditionalInfo: {
|
|
254
|
+
tokenUrl: 'https://crm.example.com/oauth/token',
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
await createCallLog();
|
|
258
|
+
const connector = mockApiKeyConnector({
|
|
259
|
+
getAuthType: jest.fn().mockResolvedValue('oauth'),
|
|
260
|
+
getOauthInfo: jest.fn().mockResolvedValue({
|
|
261
|
+
clientId: 'client-id',
|
|
262
|
+
clientSecret: 'client-secret',
|
|
263
|
+
}),
|
|
264
|
+
});
|
|
265
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
266
|
+
const oauthApp = { app: true };
|
|
267
|
+
oauth.getOAuthApp.mockReturnValue(oauthApp);
|
|
268
|
+
oauth.checkAndRefreshAccessToken.mockResolvedValue({
|
|
269
|
+
id: 'user-1',
|
|
270
|
+
accessToken: 'refreshed-access-token',
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
await disposition.upsertCallDisposition({
|
|
274
|
+
platform: 'testCRM',
|
|
275
|
+
userId: 'user-1',
|
|
276
|
+
sessionId: 'session-1',
|
|
277
|
+
dispositions: [{ id: 'disp-1' }],
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
expect(connector.getOauthInfo).toHaveBeenCalledWith({
|
|
281
|
+
tokenUrl: 'https://crm.example.com/oauth/token',
|
|
282
|
+
hostname: 'crm.example.com',
|
|
283
|
+
proxyId: undefined,
|
|
284
|
+
proxyConfig: null,
|
|
285
|
+
});
|
|
286
|
+
expect(oauth.getOAuthApp).toHaveBeenCalledWith({
|
|
287
|
+
clientId: 'client-id',
|
|
288
|
+
clientSecret: 'client-secret',
|
|
289
|
+
});
|
|
290
|
+
expect(oauth.checkAndRefreshAccessToken).toHaveBeenCalledWith(oauthApp, expect.objectContaining({
|
|
291
|
+
id: 'user-1',
|
|
292
|
+
accessToken: 'old-access-token',
|
|
293
|
+
}));
|
|
294
|
+
expect(connector.upsertCallDisposition).toHaveBeenCalledWith(expect.objectContaining({
|
|
295
|
+
authHeader: 'Bearer refreshed-access-token',
|
|
296
|
+
user: expect.objectContaining({
|
|
297
|
+
accessToken: 'refreshed-access-token',
|
|
298
|
+
}),
|
|
299
|
+
}));
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('returns revoke-session warning when OAuth refresh cannot return a user', async () => {
|
|
303
|
+
await createUser();
|
|
304
|
+
await createCallLog();
|
|
305
|
+
const connector = mockApiKeyConnector({
|
|
306
|
+
getAuthType: jest.fn().mockResolvedValue('oauth'),
|
|
307
|
+
getOauthInfo: jest.fn().mockResolvedValue({}),
|
|
308
|
+
});
|
|
309
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
310
|
+
oauth.getOAuthApp.mockReturnValue({});
|
|
311
|
+
oauth.checkAndRefreshAccessToken.mockResolvedValue(null);
|
|
312
|
+
|
|
313
|
+
const result = await disposition.upsertCallDisposition({
|
|
314
|
+
platform: 'testCRM',
|
|
315
|
+
userId: 'user-1',
|
|
316
|
+
sessionId: 'session-1',
|
|
317
|
+
dispositions: [{ id: 'disp-1' }],
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(result).toEqual({
|
|
321
|
+
successful: false,
|
|
322
|
+
returnMessage: {
|
|
323
|
+
message: 'User session expired. Please connect again.',
|
|
324
|
+
messageType: 'warning',
|
|
325
|
+
ttl: 5000,
|
|
326
|
+
},
|
|
327
|
+
isRevokeUserSession: true,
|
|
328
|
+
});
|
|
329
|
+
expect(connector.upsertCallDisposition).not.toHaveBeenCalled();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('loads proxy config and passes it to connector methods', async () => {
|
|
333
|
+
await createUser({
|
|
334
|
+
platformAdditionalInfo: {
|
|
335
|
+
proxyId: 'proxy-1',
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
await createCallLog();
|
|
339
|
+
const proxyConfig = {
|
|
340
|
+
operations: {
|
|
341
|
+
upsertCallDisposition: { method: 'POST' },
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
Connector.getProxyConfig.mockResolvedValue(proxyConfig);
|
|
345
|
+
const connector = mockApiKeyConnector();
|
|
346
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
347
|
+
|
|
348
|
+
await disposition.upsertCallDisposition({
|
|
349
|
+
platform: 'testCRM',
|
|
350
|
+
userId: 'user-1',
|
|
351
|
+
sessionId: 'session-1',
|
|
352
|
+
dispositions: [{ id: 'disp-1' }],
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
expect(Connector.getProxyConfig).toHaveBeenCalledWith('proxy-1');
|
|
356
|
+
expect(connector.getAuthType).toHaveBeenCalledWith({
|
|
357
|
+
proxyId: 'proxy-1',
|
|
358
|
+
proxyConfig,
|
|
359
|
+
});
|
|
360
|
+
expect(connector.upsertCallDisposition).toHaveBeenCalledWith(expect.objectContaining({
|
|
361
|
+
proxyConfig,
|
|
362
|
+
}));
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test('maps provider failures through the API error handler', async () => {
|
|
366
|
+
await createUser();
|
|
367
|
+
await createCallLog();
|
|
368
|
+
const providerError = new Error('provider unauthorized');
|
|
369
|
+
providerError.response = {
|
|
370
|
+
status: 401,
|
|
371
|
+
data: {
|
|
372
|
+
message: 'Unauthorized',
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
const connector = mockApiKeyConnector({
|
|
376
|
+
upsertCallDisposition: jest.fn().mockRejectedValue(providerError),
|
|
377
|
+
});
|
|
378
|
+
jest.spyOn(connectorRegistry, 'getConnector').mockReturnValue(connector);
|
|
379
|
+
|
|
380
|
+
const result = await disposition.upsertCallDisposition({
|
|
381
|
+
platform: 'testCRM',
|
|
382
|
+
userId: 'user-1',
|
|
383
|
+
sessionId: 'session-1',
|
|
384
|
+
extensionNumber: '101',
|
|
385
|
+
hashedExtensionId: 'hashed-extension-1',
|
|
386
|
+
dispositions: [{ id: 'disp-1' }],
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
expect(result.successful).toBe(false);
|
|
390
|
+
expect(result.returnMessage.message).toBe('Authorization error');
|
|
391
|
+
expect(result.extraDataTracking).toEqual({
|
|
392
|
+
statusCode: 401,
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
|