diff --git a/ghost/core/core/server/data/importer/importers/data/revue-subscriber.js b/ghost/core/core/server/data/importer/importers/data/revue-subscriber.js index d30a8157ce..f75301be62 100644 --- a/ghost/core/core/server/data/importer/importers/data/revue-subscriber.js +++ b/ghost/core/core/server/data/importer/importers/data/revue-subscriber.js @@ -1,6 +1,13 @@ const debug = require('@tryghost/debug')('importer:revue-subscriber'); const BaseImporter = require('./base'); +const papaparse = require('papaparse'); +const path = require('path'); +const fs = require('fs-extra'); + +const config = require('../../../../../shared/config'); +const models = require('../../../../models'); + class RevueSubscriberImporter extends BaseImporter { constructor(allDataFromFile) { super(allDataFromFile, { @@ -17,7 +24,31 @@ class RevueSubscriberImporter extends BaseImporter { async doImport(options, importOptions) { debug('doImport', this.modelName, this.dataToImport.length); - return super.doImport(options, importOptions); + // Don't do anything if there is no data to import + if (this.dataToImport.length === 0) { + return Promise.resolve(); + } + + // required here rather than top-level to avoid pulling in before it's initialized during boot + const membersService = require('../../../../services/members'); + + const importLabel = importOptions.importTag ? importOptions.importTag.replace(/^#/, '') : null; + + const outputFileName = `Converted ${importLabel}.csv`; + const outputFilePath = path.join(config.getContentPath('data'), '/', outputFileName); + const csvData = papaparse.unparse(this.dataToImport); + + const memberImporterOptions = { + pathToCSV: outputFilePath, + globalLabels: [{name: importLabel}], + importLabel: {name: importLabel}, + LabelModel: models.Label, + forceInline: true + }; + + await fs.writeFile(outputFilePath, csvData); + + return membersService.processImport(memberImporterOptions); } } diff --git a/ghost/importer-revue/lib/importer-revue.js b/ghost/importer-revue/lib/importer-revue.js index fa98c2ca48..afafcbdca7 100644 --- a/ghost/importer-revue/lib/importer-revue.js +++ b/ghost/importer-revue/lib/importer-revue.js @@ -77,8 +77,8 @@ const buildSubscriberList = (revueData) => { subscribers.push({ email: subscriber.email, name: `${subscriber.first_name} ${subscriber.last_name}`.trim(), - created_at: subscriber.created_at - + created_at: subscriber.created_at, + subscribed: true }); }); diff --git a/ghost/importer-revue/test/importer-revue.test.js b/ghost/importer-revue/test/importer-revue.test.js index 169d868bd0..791107c4aa 100644 --- a/ghost/importer-revue/test/importer-revue.test.js +++ b/ghost/importer-revue/test/importer-revue.test.js @@ -119,19 +119,19 @@ describe('Revue Importer', function () { it('can process a subscriber with only first name', function () { const result = RevueImporter.importSubscribers({subscribers: 'email,first_name,last_name,created_at\njoe@bloggs.me,Joe,"",2022-12-01 01:02:03.123457'}); - assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe', created_at: '2022-12-01 01:02:03.123457'}]); + assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe', created_at: '2022-12-01 01:02:03.123457', subscribed: true}]); }); it('can process a subscriber with first and last name', function () { const result = RevueImporter.importSubscribers({subscribers: 'email,first_name,last_name,created_at\njoe@bloggs.me,Joe,Bloggs,2022-12-01 01:02:03.123457'}); - assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe Bloggs', created_at: '2022-12-01 01:02:03.123457'}]); + assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe Bloggs', created_at: '2022-12-01 01:02:03.123457', subscribed: true}]); }); it('can process multiple subscribers', function () { const result = RevueImporter.importSubscribers({subscribers: 'email,first_name,last_name,created_at\njoe@bloggs.me,Joe,Bloggs,2022-12-01 01:02:03.123457\njo@bloggs.me,Jo,Bloggs,2022-12-01 01:02:04.123457'}); - assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe Bloggs', created_at: '2022-12-01 01:02:03.123457'},{email: 'jo@bloggs.me', name: 'Jo Bloggs', created_at: '2022-12-01 01:02:04.123457'}]); + assert.deepEqual(result, [{email: 'joe@bloggs.me', name: 'Joe Bloggs', created_at: '2022-12-01 01:02:03.123457', subscribed: true},{email: 'jo@bloggs.me', name: 'Jo Bloggs', created_at: '2022-12-01 01:02:04.123457', subscribed: true}]); }); });