@app-connect/core 0.0.3 → 1.6.4
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/.env.test +5 -5
- package/README.md +434 -425
- package/adapter/mock.js +76 -76
- package/adapter/registry.js +247 -247
- package/handlers/admin.js +62 -60
- package/handlers/auth.js +205 -156
- package/handlers/contact.js +274 -274
- package/handlers/disposition.js +193 -193
- package/handlers/log.js +612 -586
- package/handlers/user.js +101 -101
- package/index.js +1302 -1202
- package/jest.config.js +56 -56
- package/lib/analytics.js +52 -52
- package/lib/callLogComposer.js +550 -451
- package/lib/constants.js +9 -0
- package/lib/encode.js +30 -30
- package/lib/generalErrorMessage.js +41 -41
- package/lib/jwt.js +16 -16
- package/lib/oauth.js +79 -29
- package/lib/util.js +43 -40
- package/models/adminConfigModel.js +17 -17
- package/models/cacheModel.js +23 -23
- package/models/callLogModel.js +27 -27
- package/models/dynamo/lockSchema.js +24 -24
- package/models/dynamo/noteCacheSchema.js +30 -0
- package/models/messageLogModel.js +25 -25
- package/models/sequelize.js +16 -16
- package/models/userModel.js +38 -38
- package/package.json +67 -64
- package/releaseNotes.json +701 -577
- package/test/adapter/registry.test.js +270 -270
- package/test/handlers/auth.test.js +230 -230
- package/test/lib/jwt.test.js +161 -161
- package/test/setup.js +176 -176
package/handlers/contact.js
CHANGED
|
@@ -1,275 +1,275 @@
|
|
|
1
|
-
const oauth = require('../lib/oauth');
|
|
2
|
-
const { UserModel } = require('../models/userModel');
|
|
3
|
-
const errorMessage = require('../lib/generalErrorMessage');
|
|
4
|
-
const adapterRegistry = require('../adapter/registry');
|
|
5
|
-
async function findContact({ platform, userId, phoneNumber, overridingFormat, isExtension }) {
|
|
6
|
-
try {
|
|
7
|
-
let user = await UserModel.findOne({
|
|
8
|
-
where: {
|
|
9
|
-
id: userId,
|
|
10
|
-
platform
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
if (!user || !user.accessToken) {
|
|
14
|
-
return {
|
|
15
|
-
successful: false,
|
|
16
|
-
returnMessage: {
|
|
17
|
-
message: `Contact not found`,
|
|
18
|
-
messageType: 'warning',
|
|
19
|
-
ttl: 5000
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
const platformModule = adapterRegistry.getAdapter(platform);
|
|
24
|
-
const authType = platformModule.getAuthType();
|
|
25
|
-
let authHeader = '';
|
|
26
|
-
switch (authType) {
|
|
27
|
-
case 'oauth':
|
|
28
|
-
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
29
|
-
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
30
|
-
authHeader = `Bearer ${user.accessToken}`;
|
|
31
|
-
break;
|
|
32
|
-
case 'apiKey':
|
|
33
|
-
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
34
|
-
authHeader = `Basic ${basicAuth}`;
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
const { successful, matchedContactInfo, returnMessage, extraDataTracking } = await platformModule.findContact({ user, authHeader, phoneNumber, overridingFormat, isExtension });
|
|
38
|
-
if (matchedContactInfo != null && matchedContactInfo?.filter(c => !c.isNewContact)?.length > 0) {
|
|
39
|
-
return { successful, returnMessage, contact: matchedContactInfo, extraDataTracking };
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
if (returnMessage) {
|
|
43
|
-
return {
|
|
44
|
-
successful,
|
|
45
|
-
returnMessage,
|
|
46
|
-
extraDataTracking,
|
|
47
|
-
contact: matchedContactInfo,
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
successful,
|
|
52
|
-
returnMessage:
|
|
53
|
-
{
|
|
54
|
-
message: `Contact not found`,
|
|
55
|
-
messageType: 'warning',
|
|
56
|
-
details: [{
|
|
57
|
-
title: 'Details',
|
|
58
|
-
items: [
|
|
59
|
-
{
|
|
60
|
-
id: '1',
|
|
61
|
-
type: 'text',
|
|
62
|
-
text: `A contact with the phone number ${phoneNumber} could not be found in your ${platform} account.`
|
|
63
|
-
}
|
|
64
|
-
]
|
|
65
|
-
}],
|
|
66
|
-
ttl: 5000
|
|
67
|
-
},
|
|
68
|
-
contact: matchedContactInfo,
|
|
69
|
-
extraDataTracking
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
} catch (e) {
|
|
73
|
-
console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
|
|
74
|
-
if (e.response?.status === 429) {
|
|
75
|
-
return {
|
|
76
|
-
successful: false,
|
|
77
|
-
returnMessage: errorMessage.rateLimitErrorMessage({ platform }),
|
|
78
|
-
extraDataTracking: {
|
|
79
|
-
statusCode: e.response?.status,
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
84
|
-
return {
|
|
85
|
-
successful: false,
|
|
86
|
-
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
87
|
-
extraDataTracking: {
|
|
88
|
-
statusCode: e.response?.status,
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
return {
|
|
93
|
-
successful: false,
|
|
94
|
-
returnMessage:
|
|
95
|
-
{
|
|
96
|
-
message: `Error finding contacts`,
|
|
97
|
-
messageType: 'warning',
|
|
98
|
-
details: [
|
|
99
|
-
{
|
|
100
|
-
title: 'Details',
|
|
101
|
-
items: [
|
|
102
|
-
{
|
|
103
|
-
id: '1',
|
|
104
|
-
type: 'text',
|
|
105
|
-
text: `Please check if your account has permission to VIEW and LIST contacts`
|
|
106
|
-
}
|
|
107
|
-
]
|
|
108
|
-
}
|
|
109
|
-
],
|
|
110
|
-
ttl: 5000
|
|
111
|
-
},
|
|
112
|
-
extraDataTracking: {
|
|
113
|
-
statusCode: e.response?.status,
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function createContact({ platform, userId, phoneNumber, newContactName, newContactType, additionalSubmission }) {
|
|
120
|
-
try {
|
|
121
|
-
let user = await UserModel.findOne({
|
|
122
|
-
where: {
|
|
123
|
-
id: userId,
|
|
124
|
-
platform
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
if (!user || !user.accessToken) {
|
|
128
|
-
return { successful: false, message: `Contact not found` };
|
|
129
|
-
}
|
|
130
|
-
const platformModule = adapterRegistry.getAdapter(platform);
|
|
131
|
-
const authType = platformModule.getAuthType();
|
|
132
|
-
let authHeader = '';
|
|
133
|
-
switch (authType) {
|
|
134
|
-
case 'oauth':
|
|
135
|
-
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
136
|
-
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
137
|
-
authHeader = `Bearer ${user.accessToken}`;
|
|
138
|
-
break;
|
|
139
|
-
case 'apiKey':
|
|
140
|
-
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
141
|
-
authHeader = `Basic ${basicAuth}`;
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
const { contactInfo, returnMessage, extraDataTracking } = await platformModule.createContact({ user, authHeader, phoneNumber, newContactName, newContactType, additionalSubmission });
|
|
145
|
-
if (contactInfo != null) {
|
|
146
|
-
return { successful: true, returnMessage, contact: contactInfo, extraDataTracking };
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
return { successful: false, returnMessage };
|
|
150
|
-
}
|
|
151
|
-
} catch (e) {
|
|
152
|
-
console.log(`platform: ${platform} \n${e.stack}`);
|
|
153
|
-
if (e.response?.status === 429) {
|
|
154
|
-
return {
|
|
155
|
-
successful: false,
|
|
156
|
-
returnMessage: errorMessage.rateLimitErrorMessage({ platform }),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
160
|
-
return {
|
|
161
|
-
successful: false,
|
|
162
|
-
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
163
|
-
extraDataTracking: {
|
|
164
|
-
statusCode: e.response?.status,
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
successful: false,
|
|
170
|
-
returnMessage:
|
|
171
|
-
{
|
|
172
|
-
message: `Error creating contact`,
|
|
173
|
-
messageType: 'warning',
|
|
174
|
-
details: [
|
|
175
|
-
{
|
|
176
|
-
title: 'Details',
|
|
177
|
-
items: [
|
|
178
|
-
{
|
|
179
|
-
id: '1',
|
|
180
|
-
type: 'text',
|
|
181
|
-
text: `A contact with the phone number ${phoneNumber} could not be created. Make sure you have permission to create contacts in ${platform}.`
|
|
182
|
-
}
|
|
183
|
-
]
|
|
184
|
-
}
|
|
185
|
-
],
|
|
186
|
-
ttl: 5000
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async function findContactWithName({ platform, userId, name }) {
|
|
193
|
-
try {
|
|
194
|
-
let user = await UserModel.findOne({
|
|
195
|
-
where: {
|
|
196
|
-
id: userId,
|
|
197
|
-
platform
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
if (!user || !user.accessToken) {
|
|
201
|
-
return {
|
|
202
|
-
successful: false,
|
|
203
|
-
returnMessage: {
|
|
204
|
-
message: `No contact found with name ${name}`,
|
|
205
|
-
messageType: 'warning',
|
|
206
|
-
ttl: 5000
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
const platformModule = adapterRegistry.getAdapter(platform);
|
|
211
|
-
const authType = platformModule.getAuthType();
|
|
212
|
-
let authHeader = '';
|
|
213
|
-
switch (authType) {
|
|
214
|
-
case 'oauth':
|
|
215
|
-
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
216
|
-
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
217
|
-
authHeader = `Bearer ${user.accessToken}`;
|
|
218
|
-
break;
|
|
219
|
-
case 'apiKey':
|
|
220
|
-
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
221
|
-
authHeader = `Basic ${basicAuth}`;
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
const { successful, matchedContactInfo, returnMessage } = await platformModule.findContactWithName({ user, authHeader, name });
|
|
225
|
-
if (matchedContactInfo != null && matchedContactInfo?.filter(c => !c.isNewContact)?.length > 0) {
|
|
226
|
-
return { successful, returnMessage, contact: matchedContactInfo };
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
if (returnMessage) {
|
|
230
|
-
return {
|
|
231
|
-
successful,
|
|
232
|
-
returnMessage,
|
|
233
|
-
contact: matchedContactInfo,
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return {
|
|
237
|
-
successful,
|
|
238
|
-
returnMessage:
|
|
239
|
-
{
|
|
240
|
-
message: `No contact found with name ${name} `,
|
|
241
|
-
messageType: 'warning',
|
|
242
|
-
ttl: 5000
|
|
243
|
-
},
|
|
244
|
-
contact: matchedContactInfo
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
} catch (e) {
|
|
248
|
-
console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
|
|
249
|
-
if (e.response?.status === 429) {
|
|
250
|
-
return {
|
|
251
|
-
successful: false,
|
|
252
|
-
returnMessage: errorMessage.rateLimitErrorMessage({ platform })
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
256
|
-
return {
|
|
257
|
-
successful: false,
|
|
258
|
-
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
return {
|
|
262
|
-
successful: false,
|
|
263
|
-
returnMessage:
|
|
264
|
-
{
|
|
265
|
-
message: `Error finding contacts`,
|
|
266
|
-
messageType: 'warning',
|
|
267
|
-
ttl: 5000
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
exports.findContact = findContact;
|
|
274
|
-
exports.createContact = createContact;
|
|
1
|
+
const oauth = require('../lib/oauth');
|
|
2
|
+
const { UserModel } = require('../models/userModel');
|
|
3
|
+
const errorMessage = require('../lib/generalErrorMessage');
|
|
4
|
+
const adapterRegistry = require('../adapter/registry');
|
|
5
|
+
async function findContact({ platform, userId, phoneNumber, overridingFormat, isExtension }) {
|
|
6
|
+
try {
|
|
7
|
+
let user = await UserModel.findOne({
|
|
8
|
+
where: {
|
|
9
|
+
id: userId,
|
|
10
|
+
platform
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
if (!user || !user.accessToken) {
|
|
14
|
+
return {
|
|
15
|
+
successful: false,
|
|
16
|
+
returnMessage: {
|
|
17
|
+
message: `Contact not found`,
|
|
18
|
+
messageType: 'warning',
|
|
19
|
+
ttl: 5000
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const platformModule = adapterRegistry.getAdapter(platform);
|
|
24
|
+
const authType = platformModule.getAuthType();
|
|
25
|
+
let authHeader = '';
|
|
26
|
+
switch (authType) {
|
|
27
|
+
case 'oauth':
|
|
28
|
+
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
29
|
+
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
30
|
+
authHeader = `Bearer ${user.accessToken}`;
|
|
31
|
+
break;
|
|
32
|
+
case 'apiKey':
|
|
33
|
+
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
34
|
+
authHeader = `Basic ${basicAuth}`;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
const { successful, matchedContactInfo, returnMessage, extraDataTracking } = await platformModule.findContact({ user, authHeader, phoneNumber, overridingFormat, isExtension });
|
|
38
|
+
if (matchedContactInfo != null && matchedContactInfo?.filter(c => !c.isNewContact)?.length > 0) {
|
|
39
|
+
return { successful, returnMessage, contact: matchedContactInfo, extraDataTracking };
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
if (returnMessage) {
|
|
43
|
+
return {
|
|
44
|
+
successful,
|
|
45
|
+
returnMessage,
|
|
46
|
+
extraDataTracking,
|
|
47
|
+
contact: matchedContactInfo,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
successful,
|
|
52
|
+
returnMessage:
|
|
53
|
+
{
|
|
54
|
+
message: `Contact not found`,
|
|
55
|
+
messageType: 'warning',
|
|
56
|
+
details: [{
|
|
57
|
+
title: 'Details',
|
|
58
|
+
items: [
|
|
59
|
+
{
|
|
60
|
+
id: '1',
|
|
61
|
+
type: 'text',
|
|
62
|
+
text: `A contact with the phone number ${phoneNumber} could not be found in your ${platform} account.`
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
}],
|
|
66
|
+
ttl: 5000
|
|
67
|
+
},
|
|
68
|
+
contact: matchedContactInfo,
|
|
69
|
+
extraDataTracking
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} catch (e) {
|
|
73
|
+
console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
|
|
74
|
+
if (e.response?.status === 429) {
|
|
75
|
+
return {
|
|
76
|
+
successful: false,
|
|
77
|
+
returnMessage: errorMessage.rateLimitErrorMessage({ platform }),
|
|
78
|
+
extraDataTracking: {
|
|
79
|
+
statusCode: e.response?.status,
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
84
|
+
return {
|
|
85
|
+
successful: false,
|
|
86
|
+
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
87
|
+
extraDataTracking: {
|
|
88
|
+
statusCode: e.response?.status,
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
successful: false,
|
|
94
|
+
returnMessage:
|
|
95
|
+
{
|
|
96
|
+
message: `Error finding contacts`,
|
|
97
|
+
messageType: 'warning',
|
|
98
|
+
details: [
|
|
99
|
+
{
|
|
100
|
+
title: 'Details',
|
|
101
|
+
items: [
|
|
102
|
+
{
|
|
103
|
+
id: '1',
|
|
104
|
+
type: 'text',
|
|
105
|
+
text: `Please check if your account has permission to VIEW and LIST contacts`
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
ttl: 5000
|
|
111
|
+
},
|
|
112
|
+
extraDataTracking: {
|
|
113
|
+
statusCode: e.response?.status,
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function createContact({ platform, userId, phoneNumber, newContactName, newContactType, additionalSubmission }) {
|
|
120
|
+
try {
|
|
121
|
+
let user = await UserModel.findOne({
|
|
122
|
+
where: {
|
|
123
|
+
id: userId,
|
|
124
|
+
platform
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
if (!user || !user.accessToken) {
|
|
128
|
+
return { successful: false, message: `Contact not found` };
|
|
129
|
+
}
|
|
130
|
+
const platformModule = adapterRegistry.getAdapter(platform);
|
|
131
|
+
const authType = platformModule.getAuthType();
|
|
132
|
+
let authHeader = '';
|
|
133
|
+
switch (authType) {
|
|
134
|
+
case 'oauth':
|
|
135
|
+
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
136
|
+
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
137
|
+
authHeader = `Bearer ${user.accessToken}`;
|
|
138
|
+
break;
|
|
139
|
+
case 'apiKey':
|
|
140
|
+
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
141
|
+
authHeader = `Basic ${basicAuth}`;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
const { contactInfo, returnMessage, extraDataTracking } = await platformModule.createContact({ user, authHeader, phoneNumber, newContactName, newContactType, additionalSubmission });
|
|
145
|
+
if (contactInfo != null) {
|
|
146
|
+
return { successful: true, returnMessage, contact: contactInfo, extraDataTracking };
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return { successful: false, returnMessage };
|
|
150
|
+
}
|
|
151
|
+
} catch (e) {
|
|
152
|
+
console.log(`platform: ${platform} \n${e.stack}`);
|
|
153
|
+
if (e.response?.status === 429) {
|
|
154
|
+
return {
|
|
155
|
+
successful: false,
|
|
156
|
+
returnMessage: errorMessage.rateLimitErrorMessage({ platform }),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
160
|
+
return {
|
|
161
|
+
successful: false,
|
|
162
|
+
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
163
|
+
extraDataTracking: {
|
|
164
|
+
statusCode: e.response?.status,
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
successful: false,
|
|
170
|
+
returnMessage:
|
|
171
|
+
{
|
|
172
|
+
message: `Error creating contact`,
|
|
173
|
+
messageType: 'warning',
|
|
174
|
+
details: [
|
|
175
|
+
{
|
|
176
|
+
title: 'Details',
|
|
177
|
+
items: [
|
|
178
|
+
{
|
|
179
|
+
id: '1',
|
|
180
|
+
type: 'text',
|
|
181
|
+
text: `A contact with the phone number ${phoneNumber} could not be created. Make sure you have permission to create contacts in ${platform}.`
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
}
|
|
185
|
+
],
|
|
186
|
+
ttl: 5000
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function findContactWithName({ platform, userId, name }) {
|
|
193
|
+
try {
|
|
194
|
+
let user = await UserModel.findOne({
|
|
195
|
+
where: {
|
|
196
|
+
id: userId,
|
|
197
|
+
platform
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
if (!user || !user.accessToken) {
|
|
201
|
+
return {
|
|
202
|
+
successful: false,
|
|
203
|
+
returnMessage: {
|
|
204
|
+
message: `No contact found with name ${name}`,
|
|
205
|
+
messageType: 'warning',
|
|
206
|
+
ttl: 5000
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const platformModule = adapterRegistry.getAdapter(platform);
|
|
211
|
+
const authType = platformModule.getAuthType();
|
|
212
|
+
let authHeader = '';
|
|
213
|
+
switch (authType) {
|
|
214
|
+
case 'oauth':
|
|
215
|
+
const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
|
|
216
|
+
user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
|
|
217
|
+
authHeader = `Bearer ${user.accessToken}`;
|
|
218
|
+
break;
|
|
219
|
+
case 'apiKey':
|
|
220
|
+
const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
|
|
221
|
+
authHeader = `Basic ${basicAuth}`;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
const { successful, matchedContactInfo, returnMessage } = await platformModule.findContactWithName({ user, authHeader, name });
|
|
225
|
+
if (matchedContactInfo != null && matchedContactInfo?.filter(c => !c.isNewContact)?.length > 0) {
|
|
226
|
+
return { successful, returnMessage, contact: matchedContactInfo };
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
if (returnMessage) {
|
|
230
|
+
return {
|
|
231
|
+
successful,
|
|
232
|
+
returnMessage,
|
|
233
|
+
contact: matchedContactInfo,
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
successful,
|
|
238
|
+
returnMessage:
|
|
239
|
+
{
|
|
240
|
+
message: `No contact found with name ${name} `,
|
|
241
|
+
messageType: 'warning',
|
|
242
|
+
ttl: 5000
|
|
243
|
+
},
|
|
244
|
+
contact: matchedContactInfo
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
} catch (e) {
|
|
248
|
+
console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
|
|
249
|
+
if (e.response?.status === 429) {
|
|
250
|
+
return {
|
|
251
|
+
successful: false,
|
|
252
|
+
returnMessage: errorMessage.rateLimitErrorMessage({ platform })
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
else if (e.response?.status >= 400 && e.response?.status < 410) {
|
|
256
|
+
return {
|
|
257
|
+
successful: false,
|
|
258
|
+
returnMessage: errorMessage.authorizationErrorMessage({ platform }),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
successful: false,
|
|
263
|
+
returnMessage:
|
|
264
|
+
{
|
|
265
|
+
message: `Error finding contacts`,
|
|
266
|
+
messageType: 'warning',
|
|
267
|
+
ttl: 5000
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
exports.findContact = findContact;
|
|
274
|
+
exports.createContact = createContact;
|
|
275
275
|
exports.findContactWithName = findContactWithName;
|