From af1ff7902e6e68851ac1558e0a0c570fd5412213 Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Thu, 6 Apr 2023 23:01:23 -0700 Subject: [PATCH] Fixed member import with created_at in the future (#16580) closes TryGhost/Team#2793 - if a member is imported with a created_at in the future, the member will not appear in the members list in admin - this commit updates created_at to the current date if it is in the future upon import --- ghost/members-importer/lib/importer.js | 6 +++++- .../test/fixtures/special-cases.csv | 3 +++ ghost/members-importer/test/importer.test.js | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ghost/members-importer/test/fixtures/special-cases.csv diff --git a/ghost/members-importer/lib/importer.js b/ghost/members-importer/lib/importer.js index a8e49b7a78..66fc4e353c 100644 --- a/ghost/members-importer/lib/importer.js +++ b/ghost/members-importer/lib/importer.js @@ -124,12 +124,16 @@ module.exports = class MembersCSVImporter { }; try { + // If the member is created in the future, set created_at to now + // Members created in the future will not appear in admin members list + // Refs https://github.com/TryGhost/Team/issues/2793 + const createdAt = moment(row.created_at).isAfter(moment()) ? moment().toDate() : row.created_at; const memberValues = { email: row.email, name: row.name, note: row.note, subscribed: row.subscribed, - created_at: row.created_at, + created_at: createdAt, labels: row.labels }; const existingMember = await membersRepository.get({email: memberValues.email}, { diff --git a/ghost/members-importer/test/fixtures/special-cases.csv b/ghost/members-importer/test/fixtures/special-cases.csv new file mode 100644 index 0000000000..e1bb559ffd --- /dev/null +++ b/ghost/members-importer/test/fixtures/special-cases.csv @@ -0,0 +1,3 @@ +id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,tiers +634e48e056ef99c6a7af5850,timetraveler@example.com,"From the cuture","a note",true,true,,9999-10-18T06:34:08.000Z,,"user import label",This is a Bronze Tier + diff --git a/ghost/members-importer/test/importer.test.js b/ghost/members-importer/test/importer.test.js index 9dc114ee66..71ee0a4dca 100644 --- a/ghost/members-importer/test/importer.test.js +++ b/ghost/members-importer/test/importer.test.js @@ -324,5 +324,24 @@ describe('Importer', function () { assert.equal(result.imported, 2); assert.equal(result.errors.length, 0); }); + + it ('handles various special cases', async function () { + const importer = buildMockImporterInstance(); + + const result = await importer.perform(`${csvPath}/special-cases.csv`); + + // CASE: Member has created_at in the future + // The member will not appear in the members list in admin + // In this case, we should overwrite create_at to current timestamp + // Refs: https://github.com/TryGhost/Team/issues/2793 + assert.equal(membersRepositoryStub.create.args[0][0].email, 'timetraveler@example.com'); + assert.equal(membersRepositoryStub.create.args[0][0].subscribed, true); + assert.notDeepEqual(membersRepositoryStub.create.args[0][0].created_at, '9999-10-18T06:34:08.000Z'); + assert.equal(membersRepositoryStub.create.args[0][0].created_at <= new Date(), true); + + assert.equal(result.total, 1); + assert.equal(result.imported, 1); + assert.equal(result.errors.length, 0); + }); }); });