-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (132 loc) · 3.99 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
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 getAutomation = async (environment, locale, template_id) => {
const headers = getHeaders(locale);
const body_template = getAutomationTemplateGraphQL(template_id);
const body_nodes = getAutomationNodesGraphQL(template_id);
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 updateAutomation = async (environment, locale, nodes, template) => {
const headers = getHeaders(locale);
const body_nodes = saveAutomationV2GraphQL(nodes, template);
const body_template = saveAutomationV2TemplateQl(nodes, template);
const response_nodes = await fetch(graphqlEndpoint(environment)[locale], {
method: 'POST',
headers,
body: JSON.stringify(body_nodes).replace(
'api.courier.com',
'api.eu.courier.com'
),
});
const response_template = await fetch(
graphqlEndpoint(environment)[locale],
{
method: 'POST',
headers,
body: 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
);
const saved = await updateAutomation(
environment,
'eu',
nodes,
template
);
console.log(
`Saved - ${get(saved, [
'template',
'data',
'automationsV2',
'saveTemplate',
'name',
])} - (${automation.id})`
);
}
};
// 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');
syncAutomations('test');