-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulkRestartRequests.ts
More file actions
246 lines (227 loc) · 7.65 KB
/
bulkRestartRequests.ts
File metadata and controls
246 lines (227 loc) · 7.65 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { join, resolve } from 'node:path';
import { PersistedState } from '@transcend-io/persisted-state';
import { RequestAction, RequestStatus } from '@transcend-io/privacy-types';
import cliProgress from 'cli-progress';
import colors from 'colors';
import * as t from 'io-ts';
import { difference } from 'lodash-es';
import { DEFAULT_TRANSCEND_API } from '../../constants.js';
import { logger } from '../../logger.js';
import { map } from '../bluebird.js';
import {
RequestIdentifier,
buildTranscendGraphQLClient,
createSombraGotInstance,
fetchRequestIdentifiersBatch,
fetchAllRequests,
validateSombraVersion,
} from '../graphql/index.js';
import { SuccessfulRequest } from './constants.js';
import { extractClientError } from './extractClientError.js';
import { restartPrivacyRequest } from './restartPrivacyRequest.js';
/** Minimal state we need to keep a list of requests */
const ErrorRequest = t.intersection([
SuccessfulRequest,
t.type({
error: t.string,
}),
]);
/** Type override */
type ErrorRequest = t.TypeOf<typeof ErrorRequest>;
/** Persist this data between runs of the script */
const CachedRequestState = t.type({
restartedRequests: t.array(SuccessfulRequest),
failingRequests: t.array(ErrorRequest),
});
/**
* Upload a set of privacy requests from CSV
*
* @param options - Options
*/
export async function bulkRestartRequests({
requestReceiptFolder,
auth,
sombraAuth,
requestActions,
requestStatuses,
createdAtBefore,
createdAtAfter,
updatedAtBefore,
updatedAtAfter,
transcendUrl = DEFAULT_TRANSCEND_API,
requestIds = [],
createdAt = new Date(),
silentModeBefore,
sendEmailReceipt = false,
emailIsVerified = true,
copyIdentifiers = false,
skipWaitingPeriod = false,
concurrency = 20,
}: {
/** Actions to filter for */
requestActions: RequestAction[];
/** Statues to filter for */
requestStatuses: RequestStatus[];
/** File where request receipts are stored */
requestReceiptFolder: string;
/** Transcend API key authentication */
auth: string;
/** API URL for Transcend backend */
transcendUrl?: string;
/** Sombra API key authentication */
sombraAuth?: string;
/** Request IDs to filter for */
requestIds?: string[];
/** Whether to re-verify the email when restarting the request */
emailIsVerified?: boolean;
/** Filter for requests that were submitted before this date */
createdAt?: Date;
/** Requests that have been open for this length of time should be marked as silent mode */
silentModeBefore?: Date;
/** Send an email receipt to the restarted requests */
sendEmailReceipt?: boolean;
/** Copy over all identifiers rather than restarting the request only with the core identifier */
copyIdentifiers?: boolean;
/** Skip the waiting period when restarting requests */
skipWaitingPeriod?: boolean;
/** Filter for requests created before this date */
createdAtBefore?: Date;
/** Filter for requests created after this date */
createdAtAfter?: Date;
/** Filter for requests updated before this date */
updatedAtBefore?: Date;
/** Filter for requests updated after this date */
updatedAtAfter?: Date;
/** Concurrency to upload requests at */
concurrency?: number;
}): Promise<void> {
// Time duration
const t0 = new Date().getTime();
// create a new progress bar instance and use shades_classic theme
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
// Create a new state file to store the requests from this run
const cacheFile = join(
requestReceiptFolder,
`tr-request-restart-${new Date().toISOString()}.json`,
);
const state = new PersistedState(cacheFile, CachedRequestState, {
restartedRequests: [],
failingRequests: [],
});
// Create sombra instance to communicate with
const sombra = await createSombraGotInstance(transcendUrl, auth, sombraAuth);
// Find all requests made before createdAt that are in a removing data state
const client = buildTranscendGraphQLClient(transcendUrl, auth);
const allRequests = await fetchAllRequests(client, {
requestIds,
actions: requestActions,
statuses: requestStatuses,
createdAtBefore,
createdAtAfter,
updatedAtBefore,
updatedAtAfter,
});
const requests = allRequests.filter((request) => new Date(request.createdAt) < createdAt);
logger.info(`Found ${requests.length} requests to restart`);
if (copyIdentifiers) {
logger.info('copyIdentifiers detected - All Identifiers will be copied.');
}
if (sendEmailReceipt) {
logger.info('sendEmailReceipt detected - Email receipts will be sent.');
}
if (skipWaitingPeriod) {
logger.info('skipWaitingPeriod detected - Waiting period will be skipped.');
}
// Validate request IDs
if (requestIds.length > 0 && requestIds.length !== requests.length) {
const missingRequests = difference(
requestIds,
requests.map(({ id }) => id),
);
if (missingRequests.length > 0) {
logger.error(
colors.red(`Failed to find the following requests by ID: ${missingRequests.join(',')}.`),
);
process.exit(1);
}
}
let identifiersByRequest: Map<string, RequestIdentifier[]> | undefined;
if (copyIdentifiers) {
await validateSombraVersion(client);
identifiersByRequest = await fetchRequestIdentifiersBatch(sombra, {
requestIds: requests.map((r) => r.id),
});
}
// Map over the requests
let total = 0;
progressBar.start(requests.length, 0);
await map(
requests,
async (request, ind) => {
try {
const requestIdentifiers = copyIdentifiers
? (identifiersByRequest!.get(request.id) ?? [])
: [];
// Make the GraphQL request to restart the request
const requestResponse = await restartPrivacyRequest(
sombra,
{
...request,
// override silent mode
isSilent:
!!silentModeBefore && new Date(request.createdAt) < silentModeBefore
? true
: request.isSilent,
},
{
requestIdentifiers,
skipWaitingPeriod,
sendEmailReceipt,
emailIsVerified,
},
);
// Cache successful upload
const restartedRequests = state.getValue('restartedRequests');
restartedRequests.push({
id: requestResponse.id,
link: requestResponse.link,
rowIndex: ind,
coreIdentifier: requestResponse.coreIdentifier,
attemptedAt: new Date().toISOString(),
});
await state.setValue(restartedRequests, 'restartedRequests');
} catch (err) {
const msg = `${err.message} - ${JSON.stringify(err.response?.body, null, 2)}`;
const clientError = extractClientError(msg);
const failingRequests = state.getValue('failingRequests');
failingRequests.push({
id: request.id,
link: request.link,
rowIndex: ind,
coreIdentifier: request.coreIdentifier,
attemptedAt: new Date().toISOString(),
error: clientError || msg,
});
await state.setValue(failingRequests, 'failingRequests');
}
total += 1;
progressBar.update(total);
},
{ concurrency },
);
progressBar.stop();
const t1 = new Date().getTime();
const totalTime = t1 - t0;
// Log completion time
logger.info(colors.green(`Completed restarting of requests in "${totalTime / 1000}" seconds.`));
// Log errors
if (state.getValue('failingRequests').length > 0) {
logger.error(
colors.red(
`Encountered "${state.getValue('failingRequests').length}" errors. ` +
`See "${resolve(cacheFile)}" to review the error messages and inputs.`,
),
);
process.exit(1);
}
}