Updated to use proper members importer for Revue (#16018)

refs:
5f90baf6fe

- The OG implementation of importing revue subscribers was very naive
- This sures it up to use our proper member importer, which makes sure
everything works perfectly:
  - adds an import label
  - ensures members are subscribed to newsletters

Co-authored-by: Kevin Ansfield <kevin@lookingsideways.co.uk>
This commit is contained in:
Hannah Wolfe 2022-12-15 20:29:08 +00:00 committed by GitHub
parent ecd2083745
commit 3ee0b813b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -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);
}
}

View File

@ -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
});
});

View File

@ -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}]);
});
});