@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.
@@ -1,194 +1,194 @@
1
- // const Op = require('sequelize').Op;
2
- const { CallLogModel } = require('../models/callLogModel');
3
- // const { MessageLogModel } = require('../models/messageLogModel');
4
- const { UserModel } = require('../models/userModel');
5
- const oauth = require('../lib/oauth');
6
- // const userCore = require('../handlers/user');
7
- const errorMessage = require('../lib/generalErrorMessage');
8
- const adapterRegistry = require('../adapter/registry');
9
-
10
- async function upsertCallDisposition({ platform, userId, sessionId, dispositions, additionalSubmission, userSettings }) {
11
- try {
12
- const log = await CallLogModel.findOne({
13
- where: {
14
- sessionId
15
- }
16
- });
17
- if (!log) {
18
- return {
19
- successful: false,
20
- returnMessage: {
21
- message: `Cannot find log`,
22
- messageType: 'warning',
23
- ttl: 3000
24
- }
25
- }
26
- }
27
- let user = await UserModel.findByPk(userId);
28
- if (!user) {
29
- return {
30
- successful: false,
31
- returnMessage: {
32
- message: `Cannot find user`,
33
- messageType: 'warning',
34
- ttl: 3000
35
- }
36
- }
37
- }
38
- const platformModule = adapterRegistry.getAdapter(platform);
39
- const authType = platformModule.getAuthType();
40
- let authHeader = '';
41
- switch (authType) {
42
- case 'oauth':
43
- const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
44
- user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
45
- authHeader = `Bearer ${user.accessToken}`;
46
- break;
47
- case 'apiKey':
48
- const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
49
- authHeader = `Basic ${basicAuth}`;
50
- break;
51
- }
52
- const { logId, returnMessage, extraDataTracking } = await platformModule.upsertCallDisposition({
53
- user,
54
- existingCallLog: log,
55
- authHeader,
56
- dispositions
57
- });
58
- return { successful: !!logId, logId, returnMessage, extraDataTracking };
59
- }
60
- catch (e) {
61
- console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
62
- if (e.response?.status === 429) {
63
- return {
64
- successful: false,
65
- returnMessage: errorMessage.rateLimitErrorMessage({ platform })
66
- };
67
- }
68
- else if (e.response?.status >= 400 && e.response?.status < 410) {
69
- return {
70
- successful: false,
71
- returnMessage: errorMessage.authorizationErrorMessage({ platform }),
72
- extraDataTracking: {
73
- statusCode: e.response?.status,
74
- }
75
- };
76
- }
77
- return {
78
- successful: false,
79
- returnMessage:
80
- {
81
- message: `Error dispositioning call log`,
82
- messageType: 'warning',
83
- details: [
84
- {
85
- title: 'Details',
86
- items: [
87
- {
88
- id: '1',
89
- type: 'text',
90
- text: `Please check if your account has permission to UPDATE logs.`
91
- }
92
- ]
93
- }
94
- ],
95
- ttl: 5000
96
- }
97
- };
98
- }
99
- }
100
-
101
- // async function upsertMessageDisposition({ platform, userId, conversationLogId, dispositions, additionalSubmission, userSettings }) {
102
- // try {
103
- // const existingSameDateMessageLog = await MessageLogModel.findOne({
104
- // where: {
105
- // conversationLogId
106
- // }
107
- // });
108
- // if (!existingSameDateMessageLog) {
109
- // return {
110
- // successful: false,
111
- // returnMessage: {
112
- // message: `Cannot find log`,
113
- // messageType: 'warning',
114
- // ttl: 3000
115
- // }
116
- // }
117
- // }
118
- // let user = await UserModel.findByPk(userId);
119
- // if (!user) {
120
- // return {
121
- // successful: false,
122
- // returnMessage: {
123
- // message: `Cannot find user`,
124
- // messageType: 'warning',
125
- // ttl: 3000
126
- // }
127
- // }
128
- // }
129
- // const platformModule = adapterRegistry.getAdapter(platform);
130
- // const authType = platformModule.getAuthType();
131
- // let authHeader = '';
132
- // switch (authType) {
133
- // case 'oauth':
134
- // const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
135
- // user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
136
- // authHeader = `Bearer ${user.accessToken}`;
137
- // break;
138
- // case 'apiKey':
139
- // const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
140
- // authHeader = `Basic ${basicAuth}`;
141
- // break;
142
- // }
143
- // const { logId, returnMessage, extraDataTracking } = await platformModule.upsertMessageDisposition({
144
- // user,
145
- // existingMessageLog: existingSameDateMessageLog,
146
- // authHeader,
147
- // dispositions
148
- // });
149
- // return { successful: !!logId, logId, returnMessage, extraDataTracking };
150
- // }
151
- // catch (e) {
152
- // console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
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 dispositioning message log`,
173
- // messageType: 'warning',
174
- // details: [
175
- // {
176
- // title: 'Details',
177
- // items: [
178
- // {
179
- // id: '1',
180
- // type: 'text',
181
- // text: `Please check if your account has correct permissions.`
182
- // }
183
- // ]
184
- // }
185
- // ],
186
- // ttl: 5000
187
- // }
188
- // };
189
- // }
190
- // }
191
-
192
-
193
- exports.upsertCallDisposition = upsertCallDisposition;
1
+ // const Op = require('sequelize').Op;
2
+ const { CallLogModel } = require('../models/callLogModel');
3
+ // const { MessageLogModel } = require('../models/messageLogModel');
4
+ const { UserModel } = require('../models/userModel');
5
+ const oauth = require('../lib/oauth');
6
+ // const userCore = require('../handlers/user');
7
+ const errorMessage = require('../lib/generalErrorMessage');
8
+ const adapterRegistry = require('../adapter/registry');
9
+
10
+ async function upsertCallDisposition({ platform, userId, sessionId, dispositions, additionalSubmission, userSettings }) {
11
+ try {
12
+ const log = await CallLogModel.findOne({
13
+ where: {
14
+ sessionId
15
+ }
16
+ });
17
+ if (!log) {
18
+ return {
19
+ successful: false,
20
+ returnMessage: {
21
+ message: `Cannot find log`,
22
+ messageType: 'warning',
23
+ ttl: 3000
24
+ }
25
+ }
26
+ }
27
+ let user = await UserModel.findByPk(userId);
28
+ if (!user) {
29
+ return {
30
+ successful: false,
31
+ returnMessage: {
32
+ message: `Cannot find user`,
33
+ messageType: 'warning',
34
+ ttl: 3000
35
+ }
36
+ }
37
+ }
38
+ const platformModule = adapterRegistry.getAdapter(platform);
39
+ const authType = platformModule.getAuthType();
40
+ let authHeader = '';
41
+ switch (authType) {
42
+ case 'oauth':
43
+ const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
44
+ user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
45
+ authHeader = `Bearer ${user.accessToken}`;
46
+ break;
47
+ case 'apiKey':
48
+ const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
49
+ authHeader = `Basic ${basicAuth}`;
50
+ break;
51
+ }
52
+ const { logId, returnMessage, extraDataTracking } = await platformModule.upsertCallDisposition({
53
+ user,
54
+ existingCallLog: log,
55
+ authHeader,
56
+ dispositions
57
+ });
58
+ return { successful: !!logId, logId, returnMessage, extraDataTracking };
59
+ }
60
+ catch (e) {
61
+ console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
62
+ if (e.response?.status === 429) {
63
+ return {
64
+ successful: false,
65
+ returnMessage: errorMessage.rateLimitErrorMessage({ platform })
66
+ };
67
+ }
68
+ else if (e.response?.status >= 400 && e.response?.status < 410) {
69
+ return {
70
+ successful: false,
71
+ returnMessage: errorMessage.authorizationErrorMessage({ platform }),
72
+ extraDataTracking: {
73
+ statusCode: e.response?.status,
74
+ }
75
+ };
76
+ }
77
+ return {
78
+ successful: false,
79
+ returnMessage:
80
+ {
81
+ message: `Error dispositioning call log`,
82
+ messageType: 'warning',
83
+ details: [
84
+ {
85
+ title: 'Details',
86
+ items: [
87
+ {
88
+ id: '1',
89
+ type: 'text',
90
+ text: `Please check if your account has permission to UPDATE logs.`
91
+ }
92
+ ]
93
+ }
94
+ ],
95
+ ttl: 5000
96
+ }
97
+ };
98
+ }
99
+ }
100
+
101
+ // async function upsertMessageDisposition({ platform, userId, conversationLogId, dispositions, additionalSubmission, userSettings }) {
102
+ // try {
103
+ // const existingSameDateMessageLog = await MessageLogModel.findOne({
104
+ // where: {
105
+ // conversationLogId
106
+ // }
107
+ // });
108
+ // if (!existingSameDateMessageLog) {
109
+ // return {
110
+ // successful: false,
111
+ // returnMessage: {
112
+ // message: `Cannot find log`,
113
+ // messageType: 'warning',
114
+ // ttl: 3000
115
+ // }
116
+ // }
117
+ // }
118
+ // let user = await UserModel.findByPk(userId);
119
+ // if (!user) {
120
+ // return {
121
+ // successful: false,
122
+ // returnMessage: {
123
+ // message: `Cannot find user`,
124
+ // messageType: 'warning',
125
+ // ttl: 3000
126
+ // }
127
+ // }
128
+ // }
129
+ // const platformModule = adapterRegistry.getAdapter(platform);
130
+ // const authType = platformModule.getAuthType();
131
+ // let authHeader = '';
132
+ // switch (authType) {
133
+ // case 'oauth':
134
+ // const oauthApp = oauth.getOAuthApp((await platformModule.getOauthInfo({ tokenUrl: user?.platformAdditionalInfo?.tokenUrl, hostname: user?.hostname })));
135
+ // user = await oauth.checkAndRefreshAccessToken(oauthApp, user);
136
+ // authHeader = `Bearer ${user.accessToken}`;
137
+ // break;
138
+ // case 'apiKey':
139
+ // const basicAuth = platformModule.getBasicAuth({ apiKey: user.accessToken });
140
+ // authHeader = `Basic ${basicAuth}`;
141
+ // break;
142
+ // }
143
+ // const { logId, returnMessage, extraDataTracking } = await platformModule.upsertMessageDisposition({
144
+ // user,
145
+ // existingMessageLog: existingSameDateMessageLog,
146
+ // authHeader,
147
+ // dispositions
148
+ // });
149
+ // return { successful: !!logId, logId, returnMessage, extraDataTracking };
150
+ // }
151
+ // catch (e) {
152
+ // console.error(`platform: ${platform} \n${e.stack} \n${JSON.stringify(e.response?.data)}`);
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 dispositioning message log`,
173
+ // messageType: 'warning',
174
+ // details: [
175
+ // {
176
+ // title: 'Details',
177
+ // items: [
178
+ // {
179
+ // id: '1',
180
+ // type: 'text',
181
+ // text: `Please check if your account has correct permissions.`
182
+ // }
183
+ // ]
184
+ // }
185
+ // ],
186
+ // ttl: 5000
187
+ // }
188
+ // };
189
+ // }
190
+ // }
191
+
192
+
193
+ exports.upsertCallDisposition = upsertCallDisposition;
194
194
  // exports.upsertMessageDisposition = upsertMessageDisposition;