From 4d8c2ebb1f9c8fa101364a7add5f875f9eb3842c Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 22 Jun 2021 18:10:22 +0100 Subject: [PATCH] Supported products column for `parse` & `unparse` refs https://github.com/TryGhost/Team/issues/765 Support for multiple products means we can no longer map a members state to a csv row using just the `complimentary_plan` option. Instead we must include the product(s) that a member has. This ensures that we can read and write this data from/to csv files. --- ghost/members-csv/lib/parse.js | 6 ++++++ ghost/members-csv/lib/unparse.js | 12 +++++++++++- ghost/members-csv/test/unparse.test.js | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ghost/members-csv/lib/parse.js b/ghost/members-csv/lib/parse.js index ad9c6e6b25..f8384ec9f8 100644 --- a/ghost/members-csv/lib/parse.js +++ b/ghost/members-csv/lib/parse.js @@ -25,6 +25,12 @@ module.exports = (path, mapping, defaultLabels = []) => { } } + if (header === 'products') { + if (value && typeof value === 'string') { + return value.split(',').map(name => ({name})); + } + } + if (header === 'subscribed') { return value.toLowerCase() !== 'false'; } diff --git a/ghost/members-csv/lib/unparse.js b/ghost/members-csv/lib/unparse.js index 7edcb8cfa1..442799525e 100644 --- a/ghost/members-csv/lib/unparse.js +++ b/ghost/members-csv/lib/unparse.js @@ -12,7 +12,8 @@ const unparse = (members) => { 'stripe_customer_id', 'created_at', 'deleted_at', - 'labels' + 'labels', + 'products' ]); const mappedMembers = members.map((member) => { if (member.error) { @@ -28,6 +29,14 @@ const unparse = (members) => { }).join(','); } + let products = ''; + + if (Array.isArray(member.products)) { + products = member.products.map((product) => { + return product.name; + }).join(','); + } + return { id: member.id, email: member.email, @@ -39,6 +48,7 @@ const unparse = (members) => { created_at: member.created_at, deleted_at: member.deleted_at, labels: labels, + products: products, error: member.error || null }; }); diff --git a/ghost/members-csv/test/unparse.test.js b/ghost/members-csv/test/unparse.test.js index bd55b6da4a..ec1cd486f1 100644 --- a/ghost/members-csv/test/unparse.test.js +++ b/ghost/members-csv/test/unparse.test.js @@ -13,7 +13,7 @@ describe('unparse', function () { should.exist(result); - const expected = `id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels\r\n,email@example.com,Sam Memberino,Early supporter,,,,,,`; + const expected = `id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,products\r\n,email@example.com,Sam Memberino,Early supporter,,,,,,,`; should.equal(result, expected); }); });