2020-06-19 09:24:51 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const csvParser = require('csv-parser');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
const readCSV = (options) => {
|
|
|
|
const columnsToExtract = options.columnsToExtract || [];
|
|
|
|
let results = [];
|
|
|
|
const rows = [];
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
const readFile = fs.createReadStream(options.path);
|
|
|
|
|
|
|
|
readFile.on('err', function (err) {
|
|
|
|
reject(err);
|
|
|
|
})
|
|
|
|
.pipe(csvParser())
|
|
|
|
.on('data', function (row) {
|
|
|
|
rows.push(row);
|
|
|
|
})
|
|
|
|
.on('end', function () {
|
|
|
|
// If CSV is single column - return all values including header
|
|
|
|
const headers = _.keys(rows[0]);
|
|
|
|
|
|
|
|
let result = {};
|
|
|
|
const columnMap = {};
|
|
|
|
if (columnsToExtract.length === 1 && headers.length === 1) {
|
|
|
|
results = _.map(rows, function (value) {
|
|
|
|
result = {};
|
|
|
|
result[columnsToExtract[0].name] = value[headers[0]];
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If there are multiple columns in csv file
|
|
|
|
// try to match headers using lookup value
|
|
|
|
|
|
|
|
_.map(columnsToExtract, function findMatches(column) {
|
|
|
|
_.each(headers, function checkheader(header) {
|
|
|
|
if (column.lookup.test(header)) {
|
|
|
|
columnMap[column.name] = header;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
results = _.map(rows, function evaluateRow(row) {
|
|
|
|
const result = {};
|
|
|
|
_.each(columnMap, function returnMatches(value, key) {
|
|
|
|
result[key] = row[value];
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
resolve(results);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-06-19 08:58:33 +03:00
|
|
|
|
|
|
|
const parse = async (filePath) => {
|
|
|
|
const columnsToExtract = [{
|
|
|
|
name: 'email',
|
|
|
|
lookup: /^email/i
|
|
|
|
}, {
|
|
|
|
name: 'name',
|
|
|
|
lookup: /name/i
|
|
|
|
}, {
|
|
|
|
name: 'note',
|
|
|
|
lookup: /note/i
|
|
|
|
}, {
|
|
|
|
name: 'subscribed_to_emails',
|
|
|
|
lookup: /subscribed_to_emails/i
|
|
|
|
}, {
|
|
|
|
name: 'stripe_customer_id',
|
|
|
|
lookup: /stripe_customer_id/i
|
|
|
|
}, {
|
|
|
|
name: 'complimentary_plan',
|
|
|
|
lookup: /complimentary_plan/i
|
|
|
|
}, {
|
|
|
|
name: 'labels',
|
|
|
|
lookup: /labels/i
|
|
|
|
}, {
|
|
|
|
name: 'created_at',
|
|
|
|
lookup: /created_at/i
|
|
|
|
}];
|
|
|
|
|
2020-06-19 09:24:51 +03:00
|
|
|
return await readCSV({
|
2020-06-19 08:58:33 +03:00
|
|
|
path: filePath,
|
|
|
|
columnsToExtract: columnsToExtract
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-06-19 10:23:26 +03:00
|
|
|
module.exports = parse;
|
2020-06-19 09:24:51 +03:00
|
|
|
module.exports.readCSV = readCSV;
|