From 4eeed0d32aac905a58b8b1c7ba34ec3c50f18975 Mon Sep 17 00:00:00 2001 From: Rish Date: Wed, 12 Feb 2020 11:03:16 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20"undefined"=20values=20i?= =?UTF-8?q?n=20member=20csv=20export?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue We missed handling `undefined` values for fields during csv export for memebrs, which causes csv entries as `undefined` for fields that don't exist. It also added need for extra handling of `undefined` entries during csv import. This PR fixes the bug by properly handling empty/undefined values in export --- core/server/api/canary/members.js | 9 ++++++--- core/server/lib/fs/format-csv.js | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/server/api/canary/members.js b/core/server/api/canary/members.js index 7b7f667271..b54c45bfc7 100644 --- a/core/server/api/canary/members.js +++ b/core/server/api/canary/members.js @@ -18,8 +18,11 @@ const decorateWithSubscriptions = async function (member) { }); }; -// NOTE: this method should not exist at all and needs to be cleaned up -// it was created due to a bug in how CSV is currently created for exports +/** NOTE: this method should not exist at all and needs to be cleaned up + it was created due to a bug in how CSV is currently created for exports + Export bug was fixed in 3.6 but method exists to handle older csv exports with undefined +**/ + const cleanupUndefined = (obj) => { for (let key in obj) { if (obj[key] === 'undefined') { @@ -31,7 +34,7 @@ const cleanupUndefined = (obj) => { // NOTE: this method can be removed once unique constraints are introduced ref.: https://github.com/TryGhost/Ghost/blob/e277c6b/core/server/data/schema/schema.js#L339 const sanitizeInput = (members) => { const customersMap = members.reduce((acc, member) => { - if (member.stripe_customer_id) { + if (member.stripe_customer_id && member.stripe_customer_id !== 'undefined') { if (acc[member.stripe_customer_id]) { acc[member.stripe_customer_id] += 1; } else { diff --git a/core/server/lib/fs/format-csv.js b/core/server/lib/fs/format-csv.js index 85228dc893..acf4f6b96f 100644 --- a/core/server/lib/fs/format-csv.js +++ b/core/server/lib/fs/format-csv.js @@ -10,7 +10,7 @@ module.exports = function formatCSV(data, fields) { for (i = 0; i < fields.length; i = i + 1) { field = fields[i]; - csv += entry[field] !== null ? entry[field] : ''; + csv += (entry[field] !== null && entry[field] !== undefined) ? entry[field] : ''; if (i !== fields.length - 1) { csv += ','; }