@app-connect/core 1.7.0 → 1.7.3

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.
@@ -0,0 +1,279 @@
1
+ jest.mock('axios', () => jest.fn());
2
+ jest.mock('../../../models/dynamo/connectorSchema', () => ({
3
+ Connector: { getProxyConfig: jest.fn() }
4
+ }));
5
+ jest.mock('awesome-phonenumber', () => ({
6
+ parsePhoneNumber: jest.fn().mockReturnValue({})
7
+ }));
8
+ jest.mock('../../../models/userModel', () => ({
9
+ UserModel: { findByPk: jest.fn() }
10
+ }));
11
+
12
+ const axios = require('axios');
13
+ const { Connector } = require('../../../models/dynamo/connectorSchema');
14
+ const { UserModel } = require('../../../models/userModel');
15
+ const proxy = require('../../../connector/proxy/index');
16
+ const sampleConfig = require('./sample.json');
17
+
18
+ describe('proxy connector (high-level)', () => {
19
+ beforeEach(() => {
20
+ axios.mockReset();
21
+ });
22
+
23
+ test('createCallLog returns mapped logId using provided proxyConfig', async () => {
24
+ // Response matching mapping: response.activity.id (engine wraps as { response: response.data })
25
+ axios.mockResolvedValue({ data: { activity: { id: 'A-100' } } });
26
+
27
+ const user = { accessToken: 't-123', platformAdditionalInfo: { proxyId: 'p1' } };
28
+ const contactInfo = { id: 'c-1', name: 'Alice' };
29
+ const callLog = { direction: 'Outbound', startTime: Date.now(), duration: 60 };
30
+
31
+ const res = await proxy.createCallLog({
32
+ user,
33
+ contactInfo,
34
+ authHeader: 'Basic abc',
35
+ callLog,
36
+ note: 'hello',
37
+ additionalSubmission: {},
38
+ aiNote: '',
39
+ transcript: '',
40
+ hashedAccountId: 'h1',
41
+ isFromSSCL: false,
42
+ composedLogDetails: 'details',
43
+ proxyConfig: sampleConfig
44
+ });
45
+
46
+ expect(res.logId).toBe('A-100');
47
+ expect(res.returnMessage.messageType).toBe('success');
48
+ expect(axios).toHaveBeenCalledTimes(1);
49
+ const args = axios.mock.calls[0][0];
50
+ expect(args.method).toBe('POST');
51
+ expect(args.url).toMatch(/\/activities$/);
52
+ expect(args.headers['Content-Type']).toBe('application/json');
53
+ expect(args.data.subject).toMatch(/Call/);
54
+ expect(args.data.linked_contacts[0].contact_id).toBe('c-1');
55
+ });
56
+
57
+ test('getCallLog maps subject, note and fullBody', async () => {
58
+ axios.mockResolvedValue({ data: { activity: { subject: 'S', note: 'N', description: 'D' } } });
59
+
60
+ const out = await proxy.getCallLog({
61
+ user: { accessToken: 't-123', platformAdditionalInfo: { proxyId: 'p1' } },
62
+ callLogId: '123',
63
+ contactId: 'c-1',
64
+ authHeader: 'x',
65
+ proxyConfig: sampleConfig
66
+ });
67
+
68
+ expect(out.callLogInfo.subject).toBe('S');
69
+ expect(out.callLogInfo.note).toBe('N');
70
+ expect(out.callLogInfo.fullBody).toBe('D');
71
+ expect(out.callLogInfo.fullLogResponse).toBeDefined();
72
+ });
73
+
74
+ test('updateCallLog performs PUT and returns success message', async () => {
75
+ axios.mockResolvedValue({ data: { ok: true } });
76
+
77
+ const start = new Date('2020-01-01T00:00:00Z');
78
+ const res = await proxy.updateCallLog({
79
+ user: { accessToken: 't-123', platformAdditionalInfo: { proxyId: 'p1' } },
80
+ existingCallLog: { thirdPartyLogId: '77' },
81
+ authHeader: 'x',
82
+ recordingLink: '',
83
+ recordingDownloadLink: '',
84
+ subject: 'Subj',
85
+ note: 'Note',
86
+ startTime: start,
87
+ duration: 90,
88
+ result: 'Completed',
89
+ aiNote: '',
90
+ transcript: '',
91
+ legs: [],
92
+ additionalSubmission: {},
93
+ composedLogDetails: 'Body',
94
+ existingCallLogDetails: {},
95
+ hashedAccountId: 'h',
96
+ isFromSSCL: false,
97
+ proxyConfig: sampleConfig
98
+ });
99
+
100
+ expect(res.returnMessage.message).toMatch(/updated/i);
101
+ const args = axios.mock.calls[0][0];
102
+ expect(args.method).toBe('PUT');
103
+ expect(args.url).toMatch(/\/activities\/77$/);
104
+ expect(args.data.subject).toBe('Subj');
105
+ expect(args.data.end_date).toBeDefined();
106
+ });
107
+
108
+ test('getLogFormatType returns meta.logFormat or custom', () => {
109
+ expect(proxy.getLogFormatType('x', sampleConfig)).toBe('text/plain');
110
+ expect(proxy.getLogFormatType('x', null)).toBe('custom');
111
+ });
112
+ });
113
+
114
+ describe('proxy connector - more coverage', () => {
115
+ beforeEach(() => {
116
+ axios.mockReset();
117
+ Connector.getProxyConfig.mockReset();
118
+ });
119
+
120
+ test('getAuthType returns apiKey', async () => {
121
+ expect(await proxy.getAuthType()).toBe('apiKey');
122
+ });
123
+
124
+ test('getBasicAuth encodes apiKey with colon', () => {
125
+ const token = proxy.getBasicAuth({ apiKey: 'abc' });
126
+ expect(token).toBe(Buffer.from('abc:').toString('base64'));
127
+ });
128
+
129
+ test('getUserInfo maps id/name/message/platformAdditionalInfo', async () => {
130
+ const cfg = JSON.parse(JSON.stringify(sampleConfig));
131
+ delete cfg.auth; // ensure provided authHeader is used
132
+ Connector.getProxyConfig.mockResolvedValue(cfg);
133
+ axios.mockResolvedValue({ data: { user: { username: 'u1', role: 'admin' }, message: 'OK' } });
134
+
135
+ const res = await proxy.getUserInfo({
136
+ authHeader: 'Basic t',
137
+ hostname: 'host',
138
+ additionalInfo: { foo: 'bar' },
139
+ platform: 'test',
140
+ apiKey: 'k',
141
+ proxyId: 'p1'
142
+ });
143
+
144
+ expect(res.successful).toBe(true);
145
+ expect(res.platformUserInfo.id).toBe('u1-test');
146
+ expect(res.platformUserInfo.name).toBe('u1');
147
+ expect(res.returnMessage.message).toBe('OK');
148
+ expect(res.platformUserInfo.platformAdditionalInfo.userResponse).toEqual({ username: 'u1', role: 'admin' });
149
+ });
150
+
151
+ test('findContact maps list items', async () => {
152
+ Connector.getProxyConfig.mockResolvedValue(sampleConfig);
153
+ axios.mockResolvedValue({ data: { contacts: [ { id: 'c1', name: 'Alice', type: 'Contact', phone: '+1' } ] } });
154
+
155
+ const out = await proxy.findContact({
156
+ user: { accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } },
157
+ authHeader: 'x',
158
+ phoneNumber: '+1',
159
+ overridingFormat: '',
160
+ isExtension: false
161
+ });
162
+
163
+ expect(out.successful).toBe(true);
164
+ expect(out.matchedContactInfo.length).toBe(1);
165
+ expect(out.matchedContactInfo[0].id).toBe('c1');
166
+ });
167
+
168
+ test('createContact maps object response', async () => {
169
+ Connector.getProxyConfig.mockResolvedValue(sampleConfig);
170
+ axios.mockResolvedValue({ data: { id: 'c2', name: 'Bob', type: 'Lead' } });
171
+
172
+ const res = await proxy.createContact({
173
+ user: { accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } },
174
+ authHeader: 'x',
175
+ phoneNumber: '+1',
176
+ newContactName: 'Bob',
177
+ newContactType: 'Lead',
178
+ additionalSubmission: {}
179
+ });
180
+
181
+ expect(res.contactInfo).toEqual({ id: 'c2', name: 'Bob', type: 'Lead' });
182
+ expect(res.returnMessage.messageType).toBe('success');
183
+ });
184
+
185
+ test('createMessageLog maps idPath and updateMessageLog returns success', async () => {
186
+ Connector.getProxyConfig.mockResolvedValue(sampleConfig);
187
+ axios.mockResolvedValueOnce({ data: { activity: { id: 'M1' } } });
188
+
189
+ const create = await proxy.createMessageLog({
190
+ user: { accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } },
191
+ contactInfo: { id: 'c1', name: 'Alice' },
192
+ authHeader: 'x',
193
+ message: { subject: 'S', direction: 'Outbound', from: { phoneNumber: '+1' }, creationTime: Date.now() },
194
+ additionalSubmission: {},
195
+ recordingLink: '',
196
+ faxDocLink: '',
197
+ faxDownloadLink: '',
198
+ imageLink: '',
199
+ videoLink: ''
200
+ });
201
+ expect(create.logId).toBe('M1');
202
+
203
+ axios.mockResolvedValueOnce({ data: { ok: true } });
204
+ const update = await proxy.updateMessageLog({
205
+ user: { accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } },
206
+ contactInfo: { id: 'c1', name: 'Alice' },
207
+ existingMessageLog: { thirdPartyLogId: 'M1' },
208
+ message: { subject: 'S', direction: 'Outbound', from: { phoneNumber: '+1' }, creationTime: Date.now() },
209
+ authHeader: 'x',
210
+ additionalSubmission: {},
211
+ imageLink: '',
212
+ videoLink: ''
213
+ });
214
+ expect(update.returnMessage.message).toMatch(/updated/i);
215
+ const args2 = axios.mock.calls[1][0];
216
+ expect(args2.url).toMatch(/\/activities\/M1$/);
217
+ });
218
+
219
+ test('upsertCallDisposition returns Not supported when op missing', async () => {
220
+ Connector.getProxyConfig.mockResolvedValue(sampleConfig);
221
+ const res = await proxy.upsertCallDisposition({
222
+ user: { accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } },
223
+ existingCallLog: { thirdPartyLogId: 'L1' },
224
+ authHeader: 'x',
225
+ dispositions: []
226
+ });
227
+ expect(res.returnMessage.message).toMatch(/Not supported/);
228
+ });
229
+
230
+ test('getLicenseStatus maps values with custom config', async () => {
231
+ const licenseConfig = {
232
+ requestDefaults: { baseUrl: 'https://api.example.com' },
233
+ operations: {
234
+ getLicenseStatus: {
235
+ method: 'GET',
236
+ url: '/license/{{userId}}',
237
+ responseMapping: {
238
+ isLicenseValidPath: 'body.valid',
239
+ licenseStatusPath: 'body.status',
240
+ licenseStatusDescriptionPath: 'body.desc'
241
+ }
242
+ }
243
+ }
244
+ };
245
+ Connector.getProxyConfig.mockResolvedValue(licenseConfig);
246
+ axios.mockResolvedValue({ data: { valid: true, status: 'Pro', desc: 'All good' } });
247
+ UserModel.findByPk.mockResolvedValue({ id: 'u1', accessToken: 't', platformAdditionalInfo: { proxyId: 'p1' } });
248
+
249
+ const s = await proxy.getLicenseStatus({ userId: 'u1', platform: 'x' });
250
+ expect(s.isLicenseValid).toBe(true);
251
+ expect(s.licenseStatus).toBe('Pro');
252
+ expect(s.licenseStatusDescription).toBe('All good');
253
+ });
254
+
255
+ test('unAuthorize without custom op clears tokens and saves user', async () => {
256
+ Connector.getProxyConfig.mockResolvedValue(sampleConfig); // no unAuthorize op
257
+ const user = { accessToken: 't', refreshToken: 'r', save: jest.fn(), platformAdditionalInfo: { proxyId: 'p1' } };
258
+ const out = await proxy.unAuthorize({ user });
259
+ expect(user.accessToken).toBe('');
260
+ expect(user.refreshToken).toBe('');
261
+ expect(user.save).toHaveBeenCalled();
262
+ expect(out.returnMessage.messageType).toBe('success');
263
+ });
264
+
265
+ test('unAuthorize with custom op triggers request then clears tokens', async () => {
266
+ const cfg = {
267
+ requestDefaults: { baseUrl: 'https://api.example.com' },
268
+ operations: { unAuthorize: { method: 'POST', url: '/logout' } }
269
+ };
270
+ Connector.getProxyConfig.mockResolvedValue(cfg);
271
+ axios.mockResolvedValue({ data: { ok: true } });
272
+ const user = { accessToken: 't', refreshToken: 'r', save: jest.fn(), platformAdditionalInfo: { proxyId: 'p1' } };
273
+ const out = await proxy.unAuthorize({ user });
274
+ expect(axios).toHaveBeenCalledTimes(1);
275
+ expect(out.returnMessage.message).toMatch(/Logged out/);
276
+ expect(user.accessToken).toBe('');
277
+ });
278
+ });
279
+
@@ -0,0 +1,161 @@
1
+ {
2
+ "meta": {
3
+ "name": "",
4
+ "displayName": "",
5
+ "logFormat": "text/plain"
6
+ },
7
+ "auth": {
8
+ "type": "apiKey",
9
+ "scheme": "Basic",
10
+ "credentialTemplate": "{{apiKey}}",
11
+ "encode": "base64",
12
+ "headerName": "Authorization"
13
+ },
14
+ "requestDefaults": {
15
+ "baseUrl": "",
16
+ "timeoutSeconds": 30,
17
+ "defaultHeaders": {
18
+ "Accept": "application/json",
19
+ "X-Secret-Key": "{{secretKey}}"
20
+ }
21
+ },
22
+ "operations": {
23
+ "getUserInfo": {
24
+ "method": "GET",
25
+ "url": "/authentication",
26
+ "responseMapping": {
27
+ "type": "object",
28
+ "idPath": "body.user.username",
29
+ "namePath": "body.user.username",
30
+ "messagePath": "body.message",
31
+ "platformAdditionalInfoPaths": {
32
+ "userResponse": "body.user"
33
+ }
34
+ }
35
+ },
36
+ "findContact": {
37
+ "method": "GET",
38
+ "url": "/contacts",
39
+ "query": {
40
+ "phone": "{{phoneNumber}}"
41
+ },
42
+ "responseMapping": {
43
+ "type": "list",
44
+ "listPath": "body.contacts",
45
+ "item": {
46
+ "idPath": "id",
47
+ "namePath": "name",
48
+ "typePath": "",
49
+ "phonePath": "",
50
+ "additionalInfoPath": ""
51
+ }
52
+ }
53
+ },
54
+ "createContact": {
55
+ "method": "POST",
56
+ "url": "/contacts",
57
+ "headers": {
58
+ "Content-Type": "application/json"
59
+ },
60
+ "body": {
61
+ "name": "{{newContactName}}",
62
+ "type": "{{newContactType}}",
63
+ "phone": "{{phoneNumber}}"
64
+ },
65
+ "responseMapping": {
66
+ "type": "object",
67
+ "idPath": "body.id",
68
+ "namePath": "body.name",
69
+ "typePath": "body.type"
70
+ }
71
+ },
72
+ "createCallLog": {
73
+ "method": "POST",
74
+ "url": "/activities",
75
+ "headers": {
76
+ "Content-Type": "application/json"
77
+ },
78
+ "body": {
79
+ "subject": "{{subject}}",
80
+ "description": "{{composedLogDetails}}",
81
+ "start_date": "{{startTime}}",
82
+ "end_date": "{{endTime}}",
83
+ "activity_code_id": 3,
84
+ "repeats": "never",
85
+ "linked_contacts": [
86
+ { "contact_id": "{{contactInfo.id}}" }
87
+ ]
88
+ },
89
+ "responseMapping": {
90
+ "type": "object",
91
+ "idPath": "body.activity.id"
92
+ }
93
+ },
94
+ "updateCallLog": {
95
+ "method": "PUT",
96
+ "url": "/activities/{{thirdPartyLogId}}",
97
+ "headers": {
98
+ "Content-Type": "application/json"
99
+ },
100
+ "body": {
101
+ "subject": "{{subject}}",
102
+ "description": "{{composedLogDetails}}",
103
+ "start_date": "{{startTime}}",
104
+ "end_date": "{{endTime}}"
105
+ }
106
+ },
107
+ "getCallLog": {
108
+ "method": "GET",
109
+ "url": "/activities/{{thirdPartyLogId}}",
110
+ "headers": {
111
+ "include": "linked_contacts"
112
+ },
113
+ "responseMapping": {
114
+ "type": "object",
115
+ "subjectPath": "body.activity.subject",
116
+ "notePath": "body.activity.note",
117
+ "fullBodyPath": "body.activity.description"
118
+ }
119
+ },
120
+ "createMessageLog": {
121
+ "method": "POST",
122
+ "url": "/activities",
123
+ "headers": {
124
+ "Content-Type": "application/json"
125
+ },
126
+ "body": {
127
+ "subject": "Message with {{contactInfo.name}}",
128
+ "description": "Subject: {{message.subject}}\nDirection: {{message.direction}}\n phoneNumber: {{message.from.phoneNumber}}\nRecording link: {{recordingLink}}\n",
129
+ "start_date": "{{creationTime}}",
130
+ "end_date": "{{creationTime}}",
131
+ "activity_code_id": 3,
132
+ "repeats": "never",
133
+ "linked_contacts": [
134
+ { "contact_id": "{{contactInfo.id}}" }
135
+ ]
136
+ },
137
+ "responseMapping": {
138
+ "type": "object",
139
+ "idPath": "body.activity.id"
140
+ }
141
+ },
142
+ "updateMessageLog": {
143
+ "method": "PUT",
144
+ "url": "/activities/{{thirdPartyLogId}}",
145
+ "headers": {
146
+ "Content-Type": "application/json"
147
+ },
148
+ "body": {
149
+ "subject": "Message with {{contactInfo.name}}",
150
+ "description": "Subject: {{message.subject}}\nDirection: {{message.direction}}\nRecording link: {{recordingLink}}\n",
151
+ "start_date": "{{creationTime}}",
152
+ "end_date": "{{creationTime}}",
153
+ "activity_code_id": 3,
154
+ "repeats": "never",
155
+ "linked_contacts": [
156
+ { "contact_id": "{{contactInfo.id}}" }
157
+ ]
158
+ }
159
+ }
160
+ }
161
+ }
@@ -173,7 +173,7 @@ describe('ConnectorRegistry Interface Registration with Composition', () => {
173
173
  expect(connectorRegistry.hasPlatformInterface('testPlatform', 'testInterface')).toBe(false);
174
174
  });
175
175
 
176
- test('should get connector capabilities correctly', () => {
176
+ test('should get connector capabilities correctly', async () => {
177
177
  const mockInterface = jest.fn();
178
178
  const mockConnector = {
179
179
  getAuthType: () => 'apiKey',
@@ -184,7 +184,7 @@ describe('ConnectorRegistry Interface Registration with Composition', () => {
184
184
  connectorRegistry.registerConnectorInterface('testPlatform', 'customMethod', mockInterface);
185
185
  connectorRegistry.registerConnector('testPlatform', mockConnector);
186
186
 
187
- const capabilities = connectorRegistry.getConnectorCapabilities('testPlatform');
187
+ const capabilities = await connectorRegistry.getConnectorCapabilities('testPlatform');
188
188
 
189
189
  expect(capabilities.platform).toBe('testPlatform');
190
190
  expect(capabilities.originalMethods).toContain('getAuthType');
@@ -248,7 +248,7 @@ describe('ConnectorRegistry Interface Registration with Composition', () => {
248
248
  }).toThrow('Connector not found for platform: nonExistentPlatform');
249
249
  });
250
250
 
251
- test('should handle mixed scenarios correctly', () => {
251
+ test('should handle mixed scenarios correctly', async () => {
252
252
  // Scenario 1: Only interfaces, no connector
253
253
  connectorRegistry.registerConnectorInterface('mixedPlatform', 'interfaceMethod', jest.fn());
254
254
  const interfaceOnly = connectorRegistry.getConnector('mixedPlatform');
@@ -266,6 +266,6 @@ describe('ConnectorRegistry Interface Registration with Composition', () => {
266
266
  const composedConnector = connectorRegistry.getConnector('mixedPlatform');
267
267
  expect(composedConnector.interfaceMethod).toBeDefined();
268
268
  expect(composedConnector.getAuthType).toBeDefined();
269
- expect(composedConnector.getAuthType()).toBe('apiKey');
269
+ expect(await composedConnector.getAuthType()).toBe('apiKey');
270
270
  });
271
271
  });
@@ -57,7 +57,9 @@ describe('Auth Handler', () => {
57
57
  authHeader: 'Basic dGVzdC1hcGkta2V5Og==',
58
58
  hostname: 'test.example.com',
59
59
  additionalInfo: {},
60
- apiKey: 'test-api-key'
60
+ apiKey: 'test-api-key',
61
+ platform: 'testCRM',
62
+ proxyId: undefined
61
63
  });
62
64
  });
63
65