2020-06-19 08:23:05 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const papaparse = require('papaparse');
|
|
|
|
|
|
|
|
const unparse = (members) => {
|
2020-10-27 13:42:35 +03:00
|
|
|
const columns = new Set([
|
|
|
|
'id',
|
|
|
|
'email',
|
|
|
|
'name',
|
|
|
|
'note',
|
|
|
|
'subscribed_to_emails',
|
|
|
|
'complimentary_plan',
|
|
|
|
'stripe_customer_id',
|
|
|
|
'created_at',
|
|
|
|
'deleted_at',
|
|
|
|
'labels'
|
|
|
|
]);
|
2020-06-19 08:23:05 +03:00
|
|
|
const mappedMembers = members.map((member) => {
|
2020-10-27 13:42:35 +03:00
|
|
|
if (member.error) {
|
|
|
|
columns.add('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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
stripe_customer_id: _.get(member, 'stripe.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,
|
|
|
|
error: member.error || null
|
2020-06-19 08:23:05 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-10-27 13:42:35 +03:00
|
|
|
return papaparse.unparse(mappedMembers, {
|
|
|
|
columns: Array.from(columns.values())
|
|
|
|
});
|
2020-06-19 08:23:05 +03:00
|
|
|
};
|
|
|
|
|
2020-06-19 10:23:26 +03:00
|
|
|
module.exports = unparse;
|