-
Notifications
You must be signed in to change notification settings - Fork 103
/
transform.js
158 lines (141 loc) · 5 KB
/
transform.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
const lodash = require('lodash');
const { TransformationError, InstrumentationError } = require('@rudderstack/integrations-lib');
const {
getSuccessRespEvents,
defaultRequestConfig,
defaultPostRequestConfig,
defaultBatchRequestConfig,
handleRtTfSingleEventError,
removeUndefinedAndNullValues,
} = require('../../util');
const { MAX_BATCH_SIZE } = require('./config');
const { EventType } = require('../../../constants');
const { createOrUpdateContactResponseBuilder } = require('./utils');
const { JSON_MIME_TYPE } = require('../../util/constant');
const responseBuilder = (payload) => {
if (payload) {
const response = defaultRequestConfig();
const { listId, action, ...rest } = payload;
response.body.JSON = removeUndefinedAndNullValues(rest);
return {
...response,
listId,
action,
};
}
// fail-safety for developer error
throw new TransformationError('Something went wrong while constructing the payload');
};
const identifyResponseBuilder = (message, destination) => {
const payload = createOrUpdateContactResponseBuilder(message, destination);
return responseBuilder(payload);
};
const processEvent = (message, destination) => {
// Validating if message type is even given or not
if (!message.type) {
throw new InstrumentationError('Event type is required');
}
const messageType = message.type.toLowerCase();
if (messageType === EventType.IDENTIFY) {
return identifyResponseBuilder(message, destination);
}
throw new InstrumentationError(`Event type "${messageType}" is not supported`);
};
const process = (event) => processEvent(event.message, event.destination);
const generateBatchedPaylaodForArray = (events, combination) => {
let batchEventResponse = defaultBatchRequestConfig();
const metadata = [];
// extracting destination from the first event in a batch
const { destination } = events[0];
const { apiKey, apiSecret } = destination.Config;
const token = Buffer.from(`${apiKey}:${apiSecret}`).toString('base64');
const Contacts = [];
// Batch event into destination batch structure
events.forEach((event) => {
Contacts.push(event.message.body.JSON);
metadata.push(event.metadata);
});
const configurations = combination.split('&&');
// first -> listId, second -> action
const [first, second] = configurations;
batchEventResponse.batchedRequest.body.JSON = {
Action: second,
Contacts,
};
batchEventResponse.batchedRequest.endpoint = `https://api.mailjet.com/v3/REST/contactslist/${first}/managemanycontacts`;
batchEventResponse.batchedRequest.method = defaultPostRequestConfig.requestMethod;
batchEventResponse.batchedRequest.headers = {
'Content-Type': JSON_MIME_TYPE,
Authorization: `Basic ${token}`,
};
batchEventResponse = {
...batchEventResponse,
metadata,
destination,
};
return batchEventResponse;
};
const batchEvents = (successRespList) => {
const batchedResponseList = [];
/*
------------- eventGroups ---------------------
"listId1&&Action1" : [{message : {}, metadata : {}, destination: {}}],
"listId1&&Action2": [{message : {}, metadata : {}, destination: {}}],
"listId2&&Action2": [{message : {}, metadata : {}, destination: {}}],
"listId2&&Action1": [{message : {}, metadata : {}, destination: {}}]
*/
const eventGroups = lodash.groupBy(successRespList, (event) => {
const { listId, action } = event.message;
return `${listId}&&${action}`;
});
Object.keys(eventGroups).forEach((combination) => {
const eventChunks = lodash.chunk(eventGroups[combination], MAX_BATCH_SIZE);
// eventChunks = [[e1,e2,e3,..batchSize],[e1,e2,e3,..batchSize]..]
eventChunks.forEach((chunk) => {
const batchEventResponse = generateBatchedPaylaodForArray(chunk, combination);
batchedResponseList.push(
getSuccessRespEvents(
batchEventResponse.batchedRequest,
batchEventResponse.metadata,
batchEventResponse.destination,
true,
),
);
});
});
return batchedResponseList;
};
const processRouterDest = (inputs, reqMetadata) => {
let batchResponseList = [];
const batchErrorRespList = [];
const successRespList = [];
const { destination } = inputs[0];
inputs.forEach((event) => {
try {
if (event.message.statusCode) {
// already transformed event
successRespList.push({
message: event.message,
metadata: event.metadata,
destination,
});
} else {
// if not transformed
const transformedPayload = {
message: process(event),
metadata: event.metadata,
destination,
};
successRespList.push(transformedPayload);
}
} catch (error) {
const errRespEvent = handleRtTfSingleEventError(event, error, reqMetadata);
batchErrorRespList.push(errRespEvent);
}
});
if (successRespList.length > 0) {
batchResponseList = batchEvents(successRespList);
}
return [...batchResponseList, ...batchErrorRespList];
};
module.exports = { process, processRouterDest };