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',
|
|
|
|
'products'
|
|
|
|
];
|
2020-06-19 08:23:05 +03:00
|
|
|
|
2021-11-04 20:48:39 +03:00
|
|
|
const unparse = (members, columns = DEFAULT_COLUMNS.slice()) => {
|
2020-06-19 08:23:05 +03:00
|
|
|
const mappedMembers = members.map((member) => {
|
2020-10-27 13:42:35 +03:00
|
|
|
if (member.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 = '';
|
|
|
|
if (typeof member.labels === 'string') {
|
|
|
|
labels = member.labels;
|
|
|
|
} else if (Array.isArray(member.labels)) {
|
|
|
|
labels = member.labels.map((l) => {
|
|
|
|
return typeof l === 'string' ? l : l.name;
|
|
|
|
}).join(',');
|
2020-06-19 08:23:05 +03:00
|
|
|
}
|
|
|
|
|
2021-06-22 20:10:22 +03:00
|
|
|
let products = '';
|
|
|
|
|
|
|
|
if (Array.isArray(member.products)) {
|
|
|
|
products = member.products.map((product) => {
|
|
|
|
return product.name;
|
|
|
|
}).join(',');
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:23:05 +03:00
|
|
|
return {
|
|
|
|
id: member.id,
|
|
|
|
email: member.email,
|
|
|
|
name: member.name,
|
|
|
|
note: member.note,
|
|
|
|
subscribed_to_emails: member.subscribed,
|
2020-10-27 13:42:35 +03:00
|
|
|
complimentary_plan: member.comped || member.complimentary_plan,
|
2021-01-28 20:09:19 +03:00
|
|
|
stripe_customer_id: _.get(member, 'subscriptions[0].customer.id') || member.stripe_customer_id,
|
2020-06-19 08:23:05 +03:00
|
|
|
created_at: member.created_at,
|
|
|
|
deleted_at: member.deleted_at,
|
2020-10-27 13:42:35 +03:00
|
|
|
labels: labels,
|
2021-06-22 20:10:22 +03:00
|
|
|
products: products,
|
2020-10-27 13:42:35 +03:00
|
|
|
error: member.error || null
|
2020-06-19 08:23:05 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-10-27 13:42:35 +03:00
|
|
|
return papaparse.unparse(mappedMembers, {
|
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;
|