This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (93 loc) · 2.8 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
var fs = require('fs');
var _ = require('lodash');
var configNominal = require('./config/config.example');
var configBespoke = require('./config/config');
var config = _.extend({}, configNominal, configBespoke);
var FastSet = require("collections/fast-set");
var util = require('util');
var Promise = require('bluebird');
var stripe = require('stripe')(config.stripeAPIKey);
stripe.setApiVersion('2015-07-13');
var lineReader = require('line-reader');
var outgoingPromises = [];
var servicedCustomers = new FastSet();
function induce(callback) {
callback(config.readAllLines || ++linesRead < config.linesToRead);
};
function reducePromiseBuffer(callback) {
Promise.all(outgoingPromises)
.then(function(tuples) {
console.log("Serializing batch…");
var mappedResults = _.map(_.where(tuples,
{
success: true
}),
function(tuple) {
var result = tuple.result;
var cardsSet = result.data;
var customerID = tuple.customerID;
console.log(util.format(
"Customer '%s' has %d cards.",
customerID,
cardsSet.length));
if(result.has_more) {
console.warn(util.format(
"Customer '%s' has more cards than were returned in the query (which showed only %d)!",
customerID,
cardsSet.length
));
}
var cardStrings = _.reduce(cardsSet, function(accumulator, card) {
var fields = _.map(config.cardFields, function(field) {
return card[field];
})
var serializedIterand = fields.join(config.columnDelimiter);
console.log(util.format("Found card (%s).", fields.join(", ")));
return accumulator.concat(serializedIterand);
}, []);
return cardStrings.join(config.lineDelimiter);
});
var fileAppend = mappedResults.join(config.lineDelimiter)+config.lineDelimiter;
fs.appendFile(config.outputFile, fileAppend, function (err) {
if (err) {
throw err;
}
});
outgoingPromises = [];
induce(callback);
});
};
if (fs.statSync(config.outputFile)) {
fs.truncateSync(config.outputFile);
}
var linesRead = 0;
lineReader.eachLine(config.inputFile, function eachLine(line, last, callback) {
var customerID = line;
if (servicedCustomers.has(customerID)) {
return callback(true);
}
servicedCustomers.add(customerID);
console.log(util.format("Requesting cards for customer '%s'…", customerID));
outgoingPromises.push(stripe.customers.listCards(customerID)
.then(function(result) {
return {
success: true,
result: result,
customerID: customerID
};
})
.catch(function(err) {
console.warn(util.format("Error whilst fetching cards for customer '%s'. Error was: %s", customerID, err));
return {
success: false
}
})
);
if (outgoingPromises.length >= config.promiseBatchSize) {
return reducePromiseBuffer(callback);
}
return induce(callback);
})
.then(function allLinesDone() {
reducePromiseBuffer(function() {});
});