-
Notifications
You must be signed in to change notification settings - Fork 103
/
util.js
211 lines (198 loc) · 5.45 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const { NetworkError } = require('@rudderstack/integrations-lib');
const myAxios = require('../../../util/myAxios');
const { ENDPOINTS } = require('./config');
const tags = require('../../util/tags');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const { JSON_MIME_TYPE } = require('../../util/constant');
const handleErrorResponse = (error, customErrMessage, expectedErrStatus, defaultStatus = 400) => {
let destResp;
let errMessage = '';
let errorStatus = defaultStatus;
if (error.response && error.response.data) {
destResp = error.response?.data?.externalapierror ?? error.response?.data;
errMessage = JSON.stringify(destResp);
errorStatus = error.response.status;
if (error.response.status === expectedErrStatus) {
return { success: false, err: errMessage };
}
}
throw new NetworkError(
`${customErrMessage}: ${errMessage}`,
errorStatus,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(errorStatus),
},
destResp,
);
};
/**
* Checks if user or account exists
* @param {*} id
* @param {*} Config
* @param {*} objectType
* @returns
*/
const objectExists = async (id, Config, objectType) => {
let url = `${ENDPOINTS.USERS_ENDPOINT}/${id}`;
let err = 'invalid response while searching user';
if (objectType === 'account') {
url = `${ENDPOINTS.ACCOUNTS_ENDPOINT}/${id}`;
err = 'invalid response while searching account';
}
let response;
try {
response = await myAxios.get(
url,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'GET',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (response && response.status === 200) {
return { success: true, err: null };
}
const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
err,
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
response,
);
} catch (error) {
return handleErrorResponse(error, `error while fetching ${objectType}`, 404);
}
};
const createAccount = async (payload, Config) => {
let response;
try {
response = await myAxios.post(
ENDPOINTS.ACCOUNTS_ENDPOINT,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'POST',
endpointPath: '/accounts',
module: 'router',
},
);
if (response && response.status === 201) {
return { success: true, err: null };
}
const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
'invalid response while creating account',
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
response,
);
} catch (error) {
return handleErrorResponse(error, 'error while creating account', 400);
}
};
const updateAccount = async (accountId, payload, Config) => {
let response;
try {
response = await myAxios.put(
`${ENDPOINTS.ACCOUNTS_ENDPOINT}/${accountId}`,
payload,
{
headers: {
'X-APTRINSIC-API-KEY': Config.apiKey,
'Content-Type': JSON_MIME_TYPE,
},
},
{
destType: 'gainsight_px',
feature: 'transformation',
requestMethod: 'PUT',
endpointPath: '/accounts/accountId',
module: 'router',
},
);
if (response && response.status === 204) {
return { success: true, err: null };
}
const defStatus = 400;
const status = response ? response.status || defStatus : defStatus;
throw new NetworkError(
'invalid response while updating account',
status,
{
[tags.TAG_NAMES.ERROR_TYPE]: getDynamicErrorType(status),
},
response,
);
} catch (error) {
// it will only occur if the user does not exist
if (
error.response?.status === 404 &&
error.response?.data?.externalapierror?.status === 'NOT_FOUND'
) {
return { success: false, err: null };
}
return handleErrorResponse(error, 'error while updating account', 400);
}
};
/**
* Performs key mapping for Custom Attributes Object.
* Keys for which mapping is not provided in webapp are dropped.
* @param {*} payload
* @param {*} userCustomFieldsMap
* @returns
*/
const renameCustomFields = (payload, userCustomFieldsMap) => {
const renamedPayload = {};
const mapKeys = Object.keys(userCustomFieldsMap);
Object.keys(payload).forEach((key) => {
if (mapKeys.includes(key)) {
renamedPayload[userCustomFieldsMap[key]] = payload[key];
}
});
return renamedPayload;
};
/**
* Util to stringify object present as value
* for any key in event properties
* @param {*} props
* @returns
*/
const formatEventProps = (props) => {
const formattedObj = {};
Object.keys(props).forEach((key) => {
if (typeof props[key] === 'object') {
formattedObj[key] = JSON.stringify(props[key]);
} else {
formattedObj[key] = props[key];
}
});
return formattedObj;
};
module.exports = {
renameCustomFields,
createAccount,
updateAccount,
objectExists,
formatEventProps,
};