2020-06-19 08:23:05 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const papaparse = require('papaparse');
|
2021-11-04 20:48:39 +03:00
|
|
|
const DEFAULT_COLUMNS = [
|
|
|
|
'id',
|
|
|
|
'email',
|
|
|
|
'name',
|
|
|
|
'note',
|
|
|
|
'subscribed_to_emails',
|
|
|
|
'complimentary_plan',
|
|
|
|
'stripe_customer_id',
|
|
|
|
'created_at',
|
|
|
|
'deleted_at',
|
|
|
|
'labels',
|
2022-10-21 12:45:02 +03:00
|
|
|
'tiers'
|
2021-11-04 20:48:39 +03:00
|
|
|
];
|
2020-06-19 08:23:05 +03:00
|
|
|
|
2024-02-06 21:19:16 +03:00
|
|
|
const unparse = (rows, columns = DEFAULT_COLUMNS.slice()) => {
|
2022-01-11 17:26:11 +03:00
|
|
|
columns = columns.map((column) => {
|
|
|
|
if (column === 'subscribed') {
|
|
|
|
return 'subscribed_to_emails';
|
|
|
|
}
|
|
|
|
return column;
|
|
|
|
});
|
2024-02-06 21:19:16 +03:00
|
|
|
const mappedRows = rows.map((row) => {
|
|
|
|
if (row.error && !columns.includes('error')) {
|
2021-11-04 20:48:39 +03:00
|
|
|
columns.push('error');
|
2020-06-19 08:23:05 +03:00
|
|
|
}
|
2020-10-27 13:42:35 +03:00
|
|
|
|
|
|
|
let labels = '';
|
2024-02-06 21:19:16 +03:00
|
|
|
if (typeof row.labels === 'string') {
|
|
|
|
labels = row.labels;
|
|
|
|
} else if (Array.isArray(row.labels)) {
|
|
|
|
labels = row.labels.map((l) => {
|
2020-10-27 13:42:35 +03:00
|
|
|
return typeof l === 'string' ? l : l.name;
|
|
|
|
}).join(',');
|
2020-06-19 08:23:05 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 19:09:38 +03:00
|
|
|
let tiers = '';
|
2021-06-22 20:10:22 +03:00
|
|
|
|
2024-02-06 21:19:16 +03:00
|
|
|
if (Array.isArray(row.tiers)) {
|
|
|
|
tiers = row.tiers.map((tier) => {
|
2022-05-19 19:09:38 +03:00
|
|
|
return tier.name;
|
2021-06-22 20:10:22 +03:00
|
|
|
}).join(',');
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:23:05 +03:00
|
|
|
return {
|
2024-02-06 21:19:16 +03:00
|
|
|
id: row.id,
|
|
|
|
email: row.email,
|
|
|
|
name: row.name,
|
|
|
|
note: row.note,
|
|
|
|
subscribed_to_emails: 'subscribed' in row ? row.subscribed : row.subscribed_to_emails,
|
|
|
|
complimentary_plan: row.comped || row.complimentary_plan,
|
|
|
|
stripe_customer_id: _.get(row, 'subscriptions[0].customer.id') || row.stripe_customer_id,
|
|
|
|
created_at: row.created_at,
|
|
|
|
deleted_at: row.deleted_at,
|
2020-10-27 13:42:35 +03:00
|
|
|
labels: labels,
|
2022-10-21 12:45:02 +03:00
|
|
|
tiers: tiers,
|
2024-02-06 21:19:16 +03:00
|
|
|
import_tier: row.import_tier || null,
|
|
|
|
error: row.error || null
|
2020-06-19 08:23:05 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-02-06 21:19:16 +03:00
|
|
|
return papaparse.unparse(mappedRows, {
|
2024-04-02 10:44:06 +03:00
|
|
|
escapeFormulae: true,
|
2021-11-04 20:48:39 +03:00
|
|
|
columns
|
2020-10-27 13:42:35 +03:00
|
|
|
});
|
2020-06-19 08:23:05 +03:00
|
|
|
};
|
|
|
|
|
2020-06-19 10:23:26 +03:00
|
|
|
module.exports = unparse;
|