2020-06-19 09:24:51 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-10-27 13:44:06 +03:00
|
|
|
const pump = require('pump');
|
2020-07-08 10:39:37 +03:00
|
|
|
const papaparse = require('papaparse');
|
2020-06-19 09:24:51 +03:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
module.exports = (path, mapping, defaultLabels = []) => {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
const csvFileStream = fs.createReadStream(path);
|
|
|
|
const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, {
|
|
|
|
header: true,
|
|
|
|
transformHeader(_header) {
|
|
|
|
let header = _header;
|
|
|
|
if (mapping && Reflect.has(mapping, _header)) {
|
|
|
|
header = mapping[_header];
|
|
|
|
}
|
|
|
|
if (header === 'subscribed_to_emails') {
|
|
|
|
return 'subscribed';
|
|
|
|
}
|
|
|
|
return header;
|
|
|
|
},
|
|
|
|
transform(value, header) {
|
|
|
|
if (header === 'labels') {
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
return value.split(',').map(name => ({name}));
|
|
|
|
}
|
2020-06-30 16:27:11 +03:00
|
|
|
}
|
2020-06-19 09:24:51 +03:00
|
|
|
|
2021-06-22 20:10:22 +03:00
|
|
|
if (header === 'products') {
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
return value.split(',').map(name => ({name}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 15:39:58 +03:00
|
|
|
if (header === 'subscribed') {
|
2020-10-27 13:44:06 +03:00
|
|
|
return value.toLowerCase() !== 'false';
|
|
|
|
}
|
2020-06-19 09:24:51 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
if (header === 'complimentary_plan') {
|
|
|
|
return value.toLowerCase() === 'true';
|
|
|
|
}
|
2020-08-17 08:57:49 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
if (value === '') {
|
|
|
|
return null;
|
2020-08-17 08:57:49 +03:00
|
|
|
}
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
if (value === 'undefined') {
|
|
|
|
return null;
|
2020-06-19 09:24:51 +03:00
|
|
|
}
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
if (value.toLowerCase() === 'false') {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-19 08:58:33 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
if (value.toLowerCase() === 'true') {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-19 08:58:33 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
const rows = [];
|
|
|
|
const parsedCSVStream = pump(csvFileStream, csvParserStream, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(rows);
|
|
|
|
});
|
2020-06-30 16:27:11 +03:00
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
parsedCSVStream.on('data', (row) => {
|
|
|
|
if (row.labels) {
|
|
|
|
row.labels = row.labels.concat(defaultLabels);
|
|
|
|
} else {
|
|
|
|
row.labels = defaultLabels;
|
|
|
|
}
|
|
|
|
rows.push(row);
|
|
|
|
});
|
|
|
|
});
|
2020-06-19 08:58:33 +03:00
|
|
|
};
|
|
|
|
|
2020-10-27 13:44:06 +03:00
|
|
|
// @TODO do we need this???
|
|
|
|
module.exports.readCSV = ({path, mapping, defaultLabels}) => module.exports(path, mapping, defaultLabels);
|