-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_postcards.js
More file actions
executable file
·404 lines (357 loc) · 15.2 KB
/
Copy pathcreate_postcards.js
File metadata and controls
executable file
·404 lines (357 loc) · 15.2 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env node
// This script is a bit convoluted but it works well enough
// Load environment variables from .env file
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const https = require('https');
const FormData = require('form-data');
// Configuration
const LOB_API_BASE = 'https://api.lob.com/v1';
const LOB_API_KEY = process.env.LOB_API_KEY;
if (!LOB_API_KEY) {
console.error('Error: LOB_API_KEY environment variable is required');
console.error('Please set it in your .env file');
process.exit(1);
}
// Function to make authenticated POST request to Lob API
function createPostcard(postcardData) {
return new Promise((resolve, reject) => {
const form = new FormData();
// Add all fields to form data
Object.keys(postcardData).forEach(key => {
if (key === 'from' || key === 'to') {
// Handle nested objects
Object.keys(postcardData[key]).forEach(subKey => {
form.append(`${key}[${subKey}]`, postcardData[key][subKey]);
});
} else if (key === 'metadata') {
// Handle metadata object
Object.keys(postcardData[key]).forEach(subKey => {
form.append(`metadata[${subKey}]`, postcardData[key][subKey]);
});
} else if (key === 'front' && postcardData[key]) {
// Handle file upload for front
form.append('front', fs.createReadStream(postcardData[key]));
} else if (key === 'back' && postcardData[key]) {
// Handle file upload for back
form.append('back', fs.createReadStream(postcardData[key]));
} else if (key !== 'front' && key !== 'back') {
// Handle other fields
form.append(key, postcardData[key]);
}
});
const options = {
hostname: 'api.lob.com',
port: 443,
path: '/v1/postcards',
method: 'POST',
rejectUnauthorized: false,
headers: {
'Authorization': `Basic ${Buffer.from(LOB_API_KEY + ':').toString('base64')}`,
...form.getHeaders()
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
const responseData = JSON.parse(data);
resolve({
success: true,
statusCode: res.statusCode,
data: responseData
});
} catch (error) {
reject(new Error(`Failed to parse JSON response: ${error.message}`));
}
} else {
try {
const errorData = JSON.parse(data);
reject({
success: false,
statusCode: res.statusCode,
error: errorData,
rawResponse: data
});
} catch (parseError) {
reject({
success: false,
statusCode: res.statusCode,
error: { message: 'Failed to parse error response' },
rawResponse: data
});
}
}
});
});
req.on('error', (error) => {
reject({
success: false,
error: { message: error.message },
rawResponse: null
});
});
form.pipe(req);
});
}
// Function to parse CSV file
function parseCSV(csvContent) {
const lines = csvContent.trim().split('\n');
const headers = lines[0].split(',').map(header => header.replace(/"/g, ''));
const rows = [];
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const values = lines[i].split(',').map(value => value.replace(/"/g, ''));
const row = {};
headers.forEach((header, index) => {
row[header] = values[index];
});
rows.push(row);
}
}
return { headers, rows };
}
// Function to escape CSV values
function escapeCSV(value) {
if (value === null || value === undefined) {
return '';
}
const stringValue = String(value);
if (stringValue.includes(',') || stringValue.includes('"') || stringValue.includes('\n')) {
return `"${stringValue.replace(/"/g, '""')}"`;
}
return stringValue;
}
// Function to convert object to CSV row
function objectToCSVRow(obj, headers) {
return headers.map(header => escapeCSV(obj[header] || '')).join(',');
}
// Function to convert country names to ISO-3166 codes
// Needed because the request requires the 'US' format, but the response that we're working off returns the full country name
function convertCountryToISO(countryName) {
const countryMap = {
'UNITED STATES': 'US',
'US': 'US',
'USA': 'US'
};
return countryMap[countryName?.toUpperCase()] || countryName;
}
// Function to map CSV row to Lob API postcard data
function mapCSVToPostcardData(row) {
return {
mail_type: row.mail_type,
from: {
name: row.from_name,
address_line1: row.from_address_line1,
address_country: convertCountryToISO(row.from_address_country),
address_state: row.from_address_state,
address_city: row.from_address_city,
address_zip: row.from_address_zip
},
to: {
name: row.to_name,
address_line1: row.to_address_line1,
address_country: convertCountryToISO(row.to_address_country),
address_state: row.to_address_state,
address_city: row.to_address_city,
address_zip: row.to_address_zip
},
description: row.description,
metadata: {
user: row.metadata_user,
campaign_id: row.metadata_campaign_id,
environment: row.metadata_environment,
client: row.metadata_client
},
size: row.size,
front: row.front ? path.resolve(row.front) : undefined,
back: row.back ? path.resolve(row.back) : undefined,
send_date: new Date(Date.now() + 5 * 60 * 1000).toISOString(), // 5 minutes from now
};
}
// Function to write results to CSV
function writeResultsToCSV(filename, results, headers) {
const csvLines = [headers.join(',')];
results.forEach(result => {
csvLines.push(objectToCSVRow(result, headers));
});
fs.writeFileSync(filename, csvLines.join('\n'));
console.log(`✓ Results written to: ${filename}`);
}
// Main function
async function processCSV(inputFilePath) {
try {
console.log(`Processing CSV file: ${inputFilePath}`);
// Read the input CSV
const csvContent = fs.readFileSync(inputFilePath, 'utf8');
const { headers, rows } = parseCSV(csvContent);
console.log(`Found ${rows.length} postcards to create`);
// Generate output filenames
const inputBasename = path.basename(inputFilePath, '.csv');
const successFilename = `${inputBasename}_success.csv`;
const errorFilename = `${inputBasename}_errors.csv`;
const successResults = [];
const errorResults = [];
// Define headers for output CSVs
const successHeaders = [
'original_id',
'new_postcard_id',
// 'status',
'mail_type',
'size',
// 'carrier',
// 'from_name',
// 'from_address_line1',
// 'from_address_city',
// 'from_address_state',
// 'from_address_zip',
// 'from_address_country',
// 'to_name',
// 'to_address_line1',
// 'to_address_city',
// 'to_address_state',
// 'to_address_zip',
// 'to_address_country',
'description',
// 'metadata_user',
// 'metadata_campaign_id',
// 'metadata_environment',
// 'metadata_client',
'date_created',
// 'date_modified',
'send_date',
// 'expected_delivery_date',
// 'completed_at',
'url',
// 'raw_url',
// 'use_type',
// 'fsc',
// 'lob_credits_funding_status',
// 'is_creative_proof',
// 'response_data'
];
const errorHeaders = [
'original_id', 'status_code', 'error_message', 'error_details', 'request_payload',
'raw_response', 'from_name', 'to_name', 'description'
];
// Process each row
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const originalId = row.id;
console.log(`Processing postcard ${i + 1}/${rows.length}: ${originalId}`);
try {
// Map CSV data to Lob API format
const postcardData = mapCSVToPostcardData(row);
// Send POST request
const result = await createPostcard(postcardData);
if (result.success) {
const successResult = {
original_id: originalId,
new_postcard_id: result.data.id,
// status: result.data.status || 'success',
mail_type: result.data.mail_type || '',
size: result.data.size || '',
// carrier: result.data.carrier || '',
// from_name: result.data.from?.name || '',
// from_address_line1: result.data.from?.address_line1 || '',
// from_address_city: result.data.from?.address_city || '',
// from_address_state: result.data.from?.address_state || '',
// from_address_zip: result.data.from?.address_zip || '',
// from_address_country: result.data.from?.address_country || '',
// to_name: result.data.to?.name || '',
// to_address_line1: result.data.to?.address_line1 || '',
// to_address_city: result.data.to?.address_city || '',
// to_address_state: result.data.to?.address_state || '',
// to_address_zip: result.data.to?.address_zip || '',
// to_address_country: result.data.to?.address_country || '',
description: result.data.description || '',
// metadata_user: result.data.metadata?.user || '',
// metadata_campaign_id: result.data.metadata?.campaign_id || '',
// metadata_environment: result.data.metadata?.environment || '',
// metadata_client: result.data.metadata?.client || '',
date_created: result.data.date_created || '',
// date_modified: result.data.date_modified || '',
send_date: result.data.send_date || '',
// expected_delivery_date: result.data.expected_delivery_date || '',
// completed_at: result.data.completed_at || '',
url: result.data.url || '',
// raw_url: result.data.raw_url || '',
// use_type: result.data.use_type || '',
// fsc: result.data.fsc || false,
// lob_credits_funding_status: result.data.lob_credits_funding_status || '',
// is_creative_proof: result.data.is_creative_proof || false,
// response_data: JSON.stringify(result.data)
};
successResults.push(successResult);
console.log(`✓ Successfully created postcard: ${result.data.id}`);
} else {
throw result;
}
} catch (error) {
const errorResult = {
original_id: originalId,
status_code: error.statusCode || 'N/A',
error_message: error.error?.message || error.message || 'Unknown error',
error_details: JSON.stringify(error.error || {}),
request_payload: JSON.stringify(mapCSVToPostcardData(row)),
raw_response: error.rawResponse || '',
from_name: row.from_name || '',
to_name: row.to_name || '',
description: row.description || ''
};
errorResults.push(errorResult);
console.log(`✗ Failed to create postcard: ${error.error?.message || error.message}`);
}
// Add a small delay to avoid rate limiting
// It's not exponential backoff but it's good enough
// This script is slow enough that we don't need to worry about rate limiting
// if (i < rows.length - 1) {
// await new Promise(resolve => setTimeout(resolve, 200));
// }
}
// Write results to CSV files
if (successResults.length > 0) {
writeResultsToCSV(successFilename, successResults, successHeaders);
}
if (errorResults.length > 0) {
writeResultsToCSV(errorFilename, errorResults, errorHeaders);
}
console.log(`\n✓ Processing complete!`);
console.log(`✓ Successfully created: ${successResults.length} postcards`);
console.log(`✗ Failed to create: ${errorResults.length} postcards`);
if (successResults.length > 0) {
console.log(`✓ Success log: ${successFilename}`);
}
if (errorResults.length > 0) {
console.log(`✗ Error log: ${errorFilename}`);
}
} catch (error) {
console.error(`Error processing CSV: ${error.message}`);
process.exit(1);
}
}
// Command line interface
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage: node create_postcards.js <csv_file_path>');
console.log('Example: node create_postcards.js 174_bradley_drive_postcards_original_data.csv');
process.exit(1);
}
const inputFilePath = args[0];
if (!fs.existsSync(inputFilePath)) {
console.error(`Error: File '${inputFilePath}' does not exist`);
process.exit(1);
}
processCSV(inputFilePath);
}
// Run the script
if (require.main === module) {
main();
}
module.exports = { processCSV, createPostcard, mapCSVToPostcardData };