forked from bwebs/courier-automation-migration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
195 lines (171 loc) · 6 KB
/
index.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
import fetch from "node-fetch";
import get from "lodash/get.js";
import {
getAutomationNodesGraphQL,
getAutomationTemplateGraphQL,
getHeaders,
saveAutomationV2GraphQL,
saveAutomationV2TemplateQl,
} from "./utils.js";
import { authorizations, graphqlEndpoint } from "./variables.js";
const AUTOMATION_ID_SAFELIST = [
'5a4f9964-7cfc-4ebc-8bc0-89e54b0a5d5a', // Spark Kindle Drip Campaign
'436c453f-ef7c-42b4-bba8-15cf3a5ed3ed', // Spark Kindle Drip Campaign Exit
'9ef909e9-def8-4c74-afd6-2cfc465bb7b5', // Care Spark Kindle Drip Campaign
'9c628c07-cd0e-429e-97b7-b271829e39b0', // Care Spark Kindle Drip Campaign Exit
'01ce8c81-3a67-4316-9d79-d94c2526ebd7', // Scheduling > Nylas V3 Migration
]
const DISABLED_EVENT_PREFIX = 'NOT_AVAILABLE ';
const getAutomation = async (environment, locale, template_id, version) => {
const headers = getHeaders(locale);
const body_template = getAutomationTemplateGraphQL(template_id, version);
const body_nodes = getAutomationNodesGraphQL(template_id, version);
const response_graph = await fetch(graphqlEndpoint(environment)[locale], {
method: "POST",
headers,
body: JSON.stringify(body_template),
});
const template = await response_graph.json();
const response_nodes = await fetch(graphqlEndpoint(environment)[locale], {
method: "POST",
headers,
body: JSON.stringify(body_nodes),
});
const nodes = await response_nodes.json();
return {
template: get(template, "data.automationsV2.template", {}),
nodes: get(nodes, "data.automationsV2.nodes", []),
};
};
const updateVariables = (content) => {
return content.replace(/api\.courier\.com/, "api.eu.courier.com")
.replace(/app\.betterup\.co/, "app.betterup.eu")
.replace(/app\.staging\.betterup\.io/, "app.staging.eu.betterup.io")
.replace(/topic-rex-lb-1225292210\.us-west-2\.elb\.amazonaws\.com/, "topic-rex-lb-1225292210.us-west-2.elb.amazonaws.com");
};
const updateAutomation = async (environment, locale, nodes, template, disable=false) => {
const headers = getHeaders(locale);
const body_nodes = saveAutomationV2GraphQL(nodes, template);
const body_template = saveAutomationV2TemplateQl(nodes, template);
if (disable) {
body_nodes.variables.nodes.forEach(node => {
if (node.type == 'trigger' && node.trigger_type == 'segment') {
const event_id = node.event_id?.replace(DISABLED_EVENT_PREFIX, '') || '';
node.event_id = [DISABLED_EVENT_PREFIX, event_id].join('');
}
});
body_template.variables.nodes.forEach(node => {
if (node.type == 'trigger' && node.trigger_type == 'segment') {
const event_id = node.event_id.replace(DISABLED_EVENT_PREFIX, '');
node.event_id = [DISABLED_EVENT_PREFIX, event_id].join('');
}
});
}
// console.log("body_nodes", body_nodes.variables);
// console.log("body_template", body_template.variables);
const response_nodes = await fetch(graphqlEndpoint(environment)[locale], {
method: "POST",
headers,
body: updateVariables(JSON.stringify(body_nodes)),
});
const response_template = await fetch(graphqlEndpoint(environment)[locale], {
method: "POST",
headers,
body: updateVariables(JSON.stringify(body_template)),
});
return {
nodes: await response_nodes.json(),
template: await response_template.json(),
};
};
const syncAutomations = async (environment) => {
const automations = await fetch(graphqlEndpoint(environment)["us"], {
headers: getHeaders("us"),
method: "POST",
body: JSON.stringify({
variables: {},
query: `{
automationTemplates {
nodes {
name
id
template
templateId
createdAt
updatedAt
publishedAt
__typename
}
__typename
}
automationsV2 {
templates {
templates
__typename
}
__typename
}
}
`,
}),
});
const automation_data = await automations.json();
const automation_ids = get(
automation_data,
["data", "automationsV2", "templates", "templates"],
[],
);
for (let i = 0; i < automation_ids.length; i++) {
const automation = automation_ids[i];
const { template, nodes } = await getAutomation(
environment,
"us",
automation.id,
'v0'
);
const disable = !AUTOMATION_ID_SAFELIST.includes(automation.id);
const saved = await updateAutomation(environment, "eu", nodes, template, disable);
const name = get(saved, [
"template",
"data",
"automationsV2",
"saveTemplate",
"name",
]);
const version = get(saved, [
"template",
"data",
"automationsV2",
"saveTemplate",
"version",
]);
console.log(''); // Add a newline for readability
if (name) {
console.log(`Saved - ${name} - ${version} - (${automation.id})`);
} else {
const nodeErrors = get(saved, ["nodes", "errors"]);
const templateErrors = get(saved, ["template", "errors"]);
console.log(`Failed - ${automation.id} - ${automation.name}`);
console.log(JSON.stringify(nodeErrors));
console.log(JSON.stringify(templateErrors));
}
}
};
// check JWT expiration
const checkJWTExpiration = (locale) => {
const payload = JSON.parse(
Buffer.from(authorizations[locale].split(".")[1], "base64").toString(
"utf-8",
),
);
const now = Math.floor(Date.now() / 1000);
if (payload.exp < now) {
throw Error("JWT expired: " + locale);
}
};
checkJWTExpiration("us");
checkJWTExpiration("eu");
const environment = process.argv.slice(2)[0] || "test";
console.log('Syncing automations for environment:', environment);
syncAutomations(environment);
// getAutomation(environment, 'us', '5a4f9964-7cfc-4ebc-8bc0-89e54b0a5d5a', 'v0');