mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
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:
parent
ecd2083745
commit
3ee0b813b6
3 changed files with 37 additions and 6 deletions
|
@ -1,6 +1,13 @@
|
||||||
const debug = require('@tryghost/debug')('importer:revue-subscriber');
|
const debug = require('@tryghost/debug')('importer:revue-subscriber');
|
||||||
const BaseImporter = require('./base');
|
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 {
|
class RevueSubscriberImporter extends BaseImporter {
|
||||||
constructor(allDataFromFile) {
|
constructor(allDataFromFile) {
|
||||||
super(allDataFromFile, {
|
super(allDataFromFile, {
|
||||||
|
@ -17,7 +24,31 @@ class RevueSubscriberImporter extends BaseImporter {
|
||||||
async doImport(options, importOptions) {
|
async doImport(options, importOptions) {
|
||||||
debug('doImport', this.modelName, this.dataToImport.length);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,8 +77,8 @@ const buildSubscriberList = (revueData) => {
|
||||||
subscribers.push({
|
subscribers.push({
|
||||||
email: subscriber.email,
|
email: subscriber.email,
|
||||||
name: `${subscriber.first_name} ${subscriber.last_name}`.trim(),
|
name: `${subscriber.first_name} ${subscriber.last_name}`.trim(),
|
||||||
created_at: subscriber.created_at
|
created_at: subscriber.created_at,
|
||||||
|
subscribed: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -119,19 +119,19 @@ describe('Revue Importer', function () {
|
||||||
it('can process a subscriber with only first name', 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'});
|
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 () {
|
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'});
|
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 () {
|
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'});
|
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}]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue