mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Extracted members csv input serialization logic into separate module
refs a6b5a82f09
- This is prep work for extraction into members repository.
This commit is contained in:
parent
a6b5a82f09
commit
5a6ce5abfe
3 changed files with 45 additions and 34 deletions
|
@ -9,7 +9,6 @@ const membersService = require('../../services/members');
|
||||||
const settingsCache = require('../../services/settings/cache');
|
const settingsCache = require('../../services/settings/cache');
|
||||||
const {i18n} = require('../../lib/common');
|
const {i18n} = require('../../lib/common');
|
||||||
const logging = require('../../../shared/logging');
|
const logging = require('../../../shared/logging');
|
||||||
const fsLib = require('../../lib/fs');
|
|
||||||
const db = require('../../data/db');
|
const db = require('../../data/db');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
@ -400,7 +399,6 @@ const members = {
|
||||||
method: 'add'
|
method: 'add'
|
||||||
},
|
},
|
||||||
async query(frame) {
|
async query(frame) {
|
||||||
let filePath = frame.file.path;
|
|
||||||
let imported = {
|
let imported = {
|
||||||
count: 0
|
count: 0
|
||||||
};
|
};
|
||||||
|
@ -410,43 +408,14 @@ const members = {
|
||||||
};
|
};
|
||||||
let duplicateStripeCustomerIdCount = 0;
|
let duplicateStripeCustomerIdCount = 0;
|
||||||
|
|
||||||
const columnsToExtract = [{
|
|
||||||
name: 'email',
|
|
||||||
lookup: /^email/i
|
|
||||||
}, {
|
|
||||||
name: 'name',
|
|
||||||
lookup: /name/i
|
|
||||||
}, {
|
|
||||||
name: 'note',
|
|
||||||
lookup: /note/i
|
|
||||||
}, {
|
|
||||||
name: 'subscribed_to_emails',
|
|
||||||
lookup: /subscribed_to_emails/i
|
|
||||||
}, {
|
|
||||||
name: 'stripe_customer_id',
|
|
||||||
lookup: /stripe_customer_id/i
|
|
||||||
}, {
|
|
||||||
name: 'complimentary_plan',
|
|
||||||
lookup: /complimentary_plan/i
|
|
||||||
}, {
|
|
||||||
name: 'labels',
|
|
||||||
lookup: /labels/i
|
|
||||||
}, {
|
|
||||||
name: 'created_at',
|
|
||||||
lookup: /created_at/i
|
|
||||||
}];
|
|
||||||
|
|
||||||
// NOTE: custom labels have to be created in advance otherwise there are conflicts
|
// NOTE: custom labels have to be created in advance otherwise there are conflicts
|
||||||
// when processing member creation in parallel later on in import process
|
// when processing member creation in parallel later on in import process
|
||||||
const importSetLabels = serializeMemberLabels(frame.data.labels);
|
const importSetLabels = serializeMemberLabels(frame.data.labels);
|
||||||
await createLabels(importSetLabels, frame.options);
|
await createLabels(importSetLabels, frame.options);
|
||||||
|
|
||||||
return fsLib.readCSV({
|
return Promise.resolve().then(() => {
|
||||||
path: filePath,
|
const sanitized = sanitizeInput(frame.data.members);
|
||||||
columnsToExtract: columnsToExtract
|
duplicateStripeCustomerIdCount = frame.data.members.length - sanitized.length;
|
||||||
}).then((result) => {
|
|
||||||
const sanitized = sanitizeInput(result);
|
|
||||||
duplicateStripeCustomerIdCount = result.length - sanitized.length;
|
|
||||||
invalid.count += duplicateStripeCustomerIdCount;
|
invalid.count += duplicateStripeCustomerIdCount;
|
||||||
|
|
||||||
if (duplicateStripeCustomerIdCount) {
|
if (duplicateStripeCustomerIdCount) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:members');
|
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:members');
|
||||||
|
const {parse} = require('./utils/members-import-csv');
|
||||||
|
|
||||||
function defaultRelations(frame) {
|
function defaultRelations(frame) {
|
||||||
if (frame.options.withRelated) {
|
if (frame.options.withRelated) {
|
||||||
|
@ -42,5 +43,10 @@ module.exports = {
|
||||||
edit(apiConfig, frame) {
|
edit(apiConfig, frame) {
|
||||||
debug('edit');
|
debug('edit');
|
||||||
this.add(apiConfig, frame);
|
this.add(apiConfig, frame);
|
||||||
|
},
|
||||||
|
|
||||||
|
async importCSV(apiConfig, frame) {
|
||||||
|
debug('importCSV');
|
||||||
|
frame.data.members = await parse(frame.file.path);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
const fsLib = require('../../../../../../lib/fs');
|
||||||
|
|
||||||
|
const parse = async (filePath) => {
|
||||||
|
const columnsToExtract = [{
|
||||||
|
name: 'email',
|
||||||
|
lookup: /^email/i
|
||||||
|
}, {
|
||||||
|
name: 'name',
|
||||||
|
lookup: /name/i
|
||||||
|
}, {
|
||||||
|
name: 'note',
|
||||||
|
lookup: /note/i
|
||||||
|
}, {
|
||||||
|
name: 'subscribed_to_emails',
|
||||||
|
lookup: /subscribed_to_emails/i
|
||||||
|
}, {
|
||||||
|
name: 'stripe_customer_id',
|
||||||
|
lookup: /stripe_customer_id/i
|
||||||
|
}, {
|
||||||
|
name: 'complimentary_plan',
|
||||||
|
lookup: /complimentary_plan/i
|
||||||
|
}, {
|
||||||
|
name: 'labels',
|
||||||
|
lookup: /labels/i
|
||||||
|
}, {
|
||||||
|
name: 'created_at',
|
||||||
|
lookup: /created_at/i
|
||||||
|
}];
|
||||||
|
|
||||||
|
return await fsLib.readCSV({
|
||||||
|
path: filePath,
|
||||||
|
columnsToExtract: columnsToExtract
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.parse = parse;
|
Loading…
Add table
Reference in a new issue