@app-connect/core 1.7.8 → 1.7.10

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/handlers/auth.js CHANGED
@@ -6,13 +6,18 @@ const { RingCentral } = require('../lib/ringcentral');
6
6
  const adminCore = require('./admin');
7
7
  const { Connector } = require('../models/dynamo/connectorSchema');
8
8
 
9
- async function onOAuthCallback({ platform, hostname, tokenUrl, callbackUri, apiUrl, username, query, proxyId }) {
9
+ async function onOAuthCallback({ platform, hostname, tokenUrl, query }) {
10
+ const callbackUri = query.callbackUri;
11
+ const apiUrl = query.apiUrl;
12
+ const username = query.username;
13
+ const proxyId = query.proxyId;
14
+ const userEmail = query.userEmail;
10
15
  const platformModule = connectorRegistry.getConnector(platform);
11
16
  let proxyConfig = null;
12
17
  if (proxyId) {
13
18
  proxyConfig = await Connector.getProxyConfig(proxyId);
14
19
  }
15
- const oauthInfo = await platformModule.getOauthInfo({ tokenUrl, hostname, rcAccountId: query.rcAccountId, proxyId, proxyConfig });
20
+ const oauthInfo = await platformModule.getOauthInfo({ tokenUrl, hostname, rcAccountId: query.rcAccountId, proxyId, proxyConfig, userEmail });
16
21
 
17
22
  if (oauthInfo.failMessage) {
18
23
  return {
@@ -27,12 +32,13 @@ async function onOAuthCallback({ platform, hostname, tokenUrl, callbackUri, apiU
27
32
  // Some platforms require different oauth queries, this won't affect normal OAuth process unless CRM module implements getOverridingOAuthOption() method
28
33
  let overridingOAuthOption = null;
29
34
  if (platformModule.getOverridingOAuthOption != null) {
30
- overridingOAuthOption = platformModule.getOverridingOAuthOption({ code: callbackUri.split('code=')[1] });
35
+ const code = new URL(callbackUri).searchParams.get('code');
36
+ overridingOAuthOption = platformModule.getOverridingOAuthOption({ code });
31
37
  }
32
38
  const oauthApp = oauth.getOAuthApp(oauthInfo);
33
39
  const { accessToken, refreshToken, expires } = await oauthApp.code.getToken(callbackUri, overridingOAuthOption);
34
40
  const authHeader = `Bearer ${accessToken}`;
35
- const { successful, platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader, tokenUrl, apiUrl, hostname, platform, username, callbackUri, query, proxyId, proxyConfig });
41
+ const { successful, platformUserInfo, returnMessage } = await platformModule.getUserInfo({ authHeader, tokenUrl, apiUrl, hostname, platform, username, callbackUri, query, proxyId, proxyConfig, userEmail });
36
42
 
37
43
  if (successful) {
38
44
  let userInfo = await saveUserInfo({
@@ -4,10 +4,10 @@ const errorMessage = require('../lib/generalErrorMessage');
4
4
  const connectorRegistry = require('../connector/registry');
5
5
  const { Connector } = require('../models/dynamo/connectorSchema');
6
6
  const { DebugTracer } = require('../lib/debugTracer');
7
+ const { AccountDataModel } = require('../models/accountDataModel');
7
8
 
8
- async function findContact({ platform, userId, phoneNumber, overridingFormat, isExtension, tracer }) {
9
+ async function findContact({ platform, userId, phoneNumber, overridingFormat, isExtension, tracer, isForceRefreshAccountData = false }) {
9
10
  tracer?.trace('handler.findContact:entered', { platform, userId, phoneNumber });
10
-
11
11
  try {
12
12
  let user = await UserModel.findOne({
13
13
  where: {
@@ -16,7 +16,7 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat, is
16
16
  }
17
17
  });
18
18
  tracer?.trace('handler.findContact:userFound', { user });
19
-
19
+
20
20
  if (!user || !user.accessToken) {
21
21
  tracer?.trace('handler.findContact:noUser', { userId });
22
22
  return {
@@ -28,6 +28,20 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat, is
28
28
  }
29
29
  };
30
30
  }
31
+ // find cached contact by composite key; findByPk expects raw PK values, so use where clause
32
+ const existingMatchedContactInfo = await AccountDataModel.findOne({
33
+ where: {
34
+ rcAccountId: user.rcAccountId,
35
+ platformName: platform,
36
+ dataKey: `contact-${phoneNumber}`
37
+ }
38
+ })
39
+ if (!isForceRefreshAccountData) {
40
+ if (existingMatchedContactInfo) {
41
+ console.log('found existing matched contact info in account data');
42
+ return { successful: true, returnMessage: null, contact: existingMatchedContactInfo.data, extraDataTracking: null };
43
+ }
44
+ }
31
45
  const proxyId = user.platformAdditionalInfo?.proxyId;
32
46
  let proxyConfig = null;
33
47
  if (proxyId) {
@@ -37,7 +51,7 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat, is
37
51
  const platformModule = connectorRegistry.getConnector(platform);
38
52
  const authType = await platformModule.getAuthType({ proxyId, proxyConfig });
39
53
  tracer?.trace('handler.findContact:authType', { authType });
40
-
54
+
41
55
  let authHeader = '';
42
56
  switch (authType) {
43
57
  case 'oauth':
@@ -52,12 +66,31 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat, is
52
66
  tracer?.trace('handler.findContact:apiKeyAuth', {});
53
67
  break;
54
68
  }
55
-
56
- const { successful, matchedContactInfo, returnMessage, extraDataTracking } = await platformModule.findContact({ user, authHeader, phoneNumber, overridingFormat, isExtension, proxyConfig, tracer });
69
+
70
+ const { successful, matchedContactInfo, returnMessage, extraDataTracking } = await platformModule.findContact({ user, authHeader, phoneNumber, overridingFormat, isExtension, proxyConfig, tracer, isForceRefreshAccountData });
57
71
  tracer?.trace('handler.findContact:platformFindResult', { successful, matchedContactInfo });
58
-
72
+
59
73
  if (matchedContactInfo != null && matchedContactInfo?.filter(c => !c.isNewContact)?.length > 0) {
60
74
  tracer?.trace('handler.findContact:contactsFound', { count: matchedContactInfo.length });
75
+ // save in org data
76
+ // Danger: it does NOT support one RC account mapping to multiple CRM platforms, because contacts will be shared
77
+ if (user.rcAccountId) {
78
+ if(existingMatchedContactInfo)
79
+ {
80
+ await existingMatchedContactInfo.update({
81
+ data: matchedContactInfo
82
+ });
83
+ }
84
+ else{
85
+ await AccountDataModel.create({
86
+ rcAccountId: user.rcAccountId,
87
+ platformName: platform,
88
+ dataKey: `contact-${phoneNumber}`,
89
+ data: matchedContactInfo
90
+ });
91
+ }
92
+ console.log('store new matched contact info in account data');
93
+ }
61
94
  return { successful, returnMessage, contact: matchedContactInfo, extraDataTracking };
62
95
  }
63
96
  else {
@@ -95,7 +128,7 @@ async function findContact({ platform, userId, phoneNumber, overridingFormat, is
95
128
  } catch (e) {
96
129
  console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
97
130
  tracer?.traceError('handler.findContact:error', e, { platform, statusCode: e.response?.status });
98
-
131
+
99
132
  if (e.response?.status === 429) {
100
133
  return {
101
134
  successful: false,
@@ -179,7 +212,7 @@ async function createContact({ platform, userId, phoneNumber, newContactName, ne
179
212
  return { successful: false, returnMessage };
180
213
  }
181
214
  } catch (e) {
182
- console.log(`platform: ${platform} \n${e.stack}`);
215
+ console.error(`platform: ${platform} \n${e.stack}`);
183
216
  if (e.response?.status === 429) {
184
217
  return {
185
218
  successful: false,
package/handlers/log.js CHANGED
@@ -194,7 +194,7 @@ async function getCallLog({ userId, sessionIds, platform, requireDetails }) {
194
194
  }
195
195
  let logs = [];
196
196
  let returnMessage = null;
197
- let extraDataTracking = {};;
197
+ let extraDataTracking = {};
198
198
 
199
199
  // Handle undefined or null sessionIds
200
200
  if (!sessionIds) {
@@ -303,7 +303,7 @@ async function getCallLog({ userId, sessionIds, platform, requireDetails }) {
303
303
  {
304
304
  id: '1',
305
305
  type: 'text',
306
- text: `Please check if your account has permission to CREATE logs.`
306
+ text: `Please check if your account has permission to READ logs.`
307
307
  }
308
308
  ]
309
309
  }
@@ -483,7 +483,7 @@ async function updateCallLog({ platform, userId, incomingData, hashedAccountId,
483
483
  async function createMessageLog({ platform, userId, incomingData }) {
484
484
  try {
485
485
  let returnMessage = null;
486
- let extraDataTracking = {};;
486
+ let extraDataTracking = {};
487
487
  if (incomingData.logInfo.messages.length === 0) {
488
488
  return {
489
489
  successful: false,
@@ -625,7 +625,7 @@ async function createMessageLog({ platform, userId, incomingData }) {
625
625
  return { successful: true, logIds, returnMessage, extraDataTracking };
626
626
  }
627
627
  catch (e) {
628
- console.log(`platform: ${platform} \n${e.stack}`);
628
+ console.error(`platform: ${platform} \n${e.stack}`);
629
629
  if (e.response?.status === 429) {
630
630
  return {
631
631
  successful: false,