Ghost/ghost/members-csv/lib/unparse.js
Fabien O'Carroll 75d003816e Fixed the importer from overriding properties
refs https://github.com/TryGhost/Team/issues/1202

When importing we were transforming the CSV and add missing columns to
it before storing it in preparation to perform the import. This resulted
in the missing columns being updated for existing members with blank
data.

We've updated the Members CSV parsing library to take an options list of
columns to include, which then allows imports to not include all of the
default columns.
2021-12-01 17:02:30 +02:00

62 lines
1.6 KiB
JavaScript

const _ = require('lodash');
const papaparse = require('papaparse');
const DEFAULT_COLUMNS = [
'id',
'email',
'name',
'note',
'subscribed_to_emails',
'complimentary_plan',
'stripe_customer_id',
'created_at',
'deleted_at',
'labels',
'products'
];
const unparse = (members, columns = DEFAULT_COLUMNS.slice()) => {
const mappedMembers = members.map((member) => {
if (member.error) {
columns.push('error');
}
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(',');
}
let products = '';
if (Array.isArray(member.products)) {
products = member.products.map((product) => {
return product.name;
}).join(',');
}
return {
id: member.id,
email: member.email,
name: member.name,
note: member.note,
subscribed_to_emails: member.subscribed,
complimentary_plan: member.comped || member.complimentary_plan,
stripe_customer_id: _.get(member, 'subscriptions[0].customer.id') || member.stripe_customer_id,
created_at: member.created_at,
deleted_at: member.deleted_at,
labels: labels,
products: products,
error: member.error || null
};
});
return papaparse.unparse(mappedMembers, {
columns
});
};
module.exports = unparse;