2021-06-22 13:07:16 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const membersCSV = require('@tryghost/members-csv');
|
2021-07-19 13:46:38 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-06-22 13:07:16 +03:00
|
|
|
const emailTemplate = require('./email-template');
|
2022-11-28 20:22:10 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-06-22 13:07:16 +03:00
|
|
|
|
2021-07-19 13:46:38 +03:00
|
|
|
const messages = {
|
2022-10-18 09:00:59 +03:00
|
|
|
filenameCollision: 'Filename already exists, please try again.'
|
2021-07-19 13:46:38 +03:00
|
|
|
};
|
|
|
|
|
2023-01-04 13:22:12 +03:00
|
|
|
// The key should correspond to a member model field (unless it's a special purpose field like 'complimentary_plan')
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
// the value should represent an allowed field name coming from user input
|
|
|
|
const DEFAULT_CSV_HEADER_MAPPING = {
|
|
|
|
email: 'email',
|
|
|
|
name: 'name',
|
|
|
|
note: 'note',
|
|
|
|
subscribed_to_emails: 'subscribed',
|
|
|
|
created_at: 'created_at',
|
|
|
|
complimentary_plan: 'complimentary_plan',
|
|
|
|
stripe_customer_id: 'stripe_customer_id',
|
2022-10-24 13:03:44 +03:00
|
|
|
labels: 'labels'
|
2022-10-13 11:16:37 +03:00
|
|
|
};
|
2022-10-24 13:03:44 +03:00
|
|
|
|
2021-06-22 13:07:16 +03:00
|
|
|
module.exports = class MembersCSVImporter {
|
|
|
|
/**
|
2021-07-20 18:34:22 +03:00
|
|
|
* @param {Object} options
|
2021-07-20 18:35:49 +03:00
|
|
|
* @param {string} options.storagePath - The path to store CSV's in before importing
|
2021-07-21 10:48:05 +03:00
|
|
|
* @param {Function} options.getTimezone - function returning currently configured timezone
|
2022-10-25 11:40:28 +03:00
|
|
|
* @param {() => Object} options.getMembersRepository - member model access instance for data access and manipulation
|
2022-10-26 09:20:19 +03:00
|
|
|
* @param {() => Promise<import('@tryghost/tiers/lib/Tier')>} options.getDefaultTier - async function returning default Member Tier
|
2021-07-21 10:49:47 +03:00
|
|
|
* @param {Function} options.sendEmail - function sending an email
|
2021-07-20 18:34:22 +03:00
|
|
|
* @param {(string) => boolean} options.isSet - Method checking if specific feature is enabled
|
2023-01-04 13:22:12 +03:00
|
|
|
* @param {({job, offloaded, name}) => void} options.addJob - Method registering an async job
|
2021-07-20 18:34:22 +03:00
|
|
|
* @param {Object} options.knex - An instance of the Ghost Database connection
|
|
|
|
* @param {Function} options.urlFor - function generating urls
|
2022-04-06 22:12:38 +03:00
|
|
|
* @param {Object} options.context
|
2021-06-22 13:07:16 +03:00
|
|
|
*/
|
2022-10-25 11:40:28 +03:00
|
|
|
constructor({storagePath, getTimezone, getMembersRepository, getDefaultTier, sendEmail, isSet, addJob, knex, urlFor, context}) {
|
2021-07-20 18:35:49 +03:00
|
|
|
this._storagePath = storagePath;
|
2021-07-21 10:48:05 +03:00
|
|
|
this._getTimezone = getTimezone;
|
2022-10-25 11:40:28 +03:00
|
|
|
this._getMembersRepository = getMembersRepository;
|
|
|
|
this._getDefaultTier = getDefaultTier;
|
2021-07-21 10:49:47 +03:00
|
|
|
this._sendEmail = sendEmail;
|
2021-07-20 17:42:57 +03:00
|
|
|
this._isSet = isSet;
|
2021-07-20 18:21:59 +03:00
|
|
|
this._addJob = addJob;
|
2021-07-20 18:26:46 +03:00
|
|
|
this._knex = knex;
|
2021-07-20 18:31:22 +03:00
|
|
|
this._urlFor = urlFor;
|
2022-04-06 22:12:38 +03:00
|
|
|
this._context = context;
|
2021-06-22 13:07:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a CSV file for import
|
|
|
|
* - Maps headers based on headerMapping, this allows for a non standard CSV
|
|
|
|
* to be imported, so long as a mapping exists between it and a standard CSV
|
|
|
|
* - Stores the CSV to be imported in the storagePath
|
|
|
|
* - Creates a MemberImport Job and associated MemberImportBatch's
|
|
|
|
*
|
|
|
|
* @param {string} inputFilePath - The path to the CSV to prepare
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
* @param {Object.<string, string>} [headerMapping] - An object whose keys are headers in the input CSV and values are the header to replace it with
|
|
|
|
* @param {Array<string>} [defaultLabels] - A list of labels to apply to every member
|
2021-06-22 13:07:16 +03:00
|
|
|
*
|
2022-10-13 10:04:22 +03:00
|
|
|
* @returns {Promise<{filePath: string, batches: number, metadata: Object.<string, any>}>} - A promise resolving to the data including filePath of "prepared" CSV
|
2021-06-22 13:07:16 +03:00
|
|
|
*/
|
|
|
|
async prepare(inputFilePath, headerMapping, defaultLabels) {
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
headerMapping = headerMapping || DEFAULT_CSV_HEADER_MAPPING;
|
2022-10-13 09:41:25 +03:00
|
|
|
// @NOTE: investigate why is it "1" and do we even need this concept anymore?
|
2021-06-22 13:07:16 +03:00
|
|
|
const batchSize = 1;
|
|
|
|
|
2021-07-21 10:48:05 +03:00
|
|
|
const siteTimezone = this._getTimezone();
|
2021-06-22 13:07:16 +03:00
|
|
|
const currentTime = moment().tz(siteTimezone).format('YYYY-MM-DD HH:mm:ss.SSS');
|
|
|
|
const outputFileName = `Members Import ${currentTime}.csv`;
|
|
|
|
const outputFilePath = path.join(this._storagePath, '/', outputFileName);
|
|
|
|
|
|
|
|
const pathExists = await fs.pathExists(outputFilePath);
|
|
|
|
|
|
|
|
if (pathExists) {
|
2022-02-15 15:27:22 +03:00
|
|
|
throw new errors.DataImportError({message: tpl(messages.filenameCollision)});
|
2021-06-22 13:07:16 +03:00
|
|
|
}
|
|
|
|
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
// completely rely on explicit user input for header mappings
|
|
|
|
const rows = await membersCSV.parse(inputFilePath, headerMapping, defaultLabels);
|
2021-11-04 20:48:39 +03:00
|
|
|
const columns = Object.keys(rows[0]);
|
2021-06-22 13:07:16 +03:00
|
|
|
const numberOfBatches = Math.ceil(rows.length / batchSize);
|
2021-11-04 20:48:39 +03:00
|
|
|
const mappedCSV = membersCSV.unparse(rows, columns);
|
2021-06-22 13:07:16 +03:00
|
|
|
|
2022-10-31 11:14:38 +03:00
|
|
|
const hasStripeData = !!(rows.find(function rowHasStripeData(row) {
|
2022-10-31 11:28:21 +03:00
|
|
|
return !!row.stripe_customer_id;
|
2022-10-31 11:14:38 +03:00
|
|
|
}));
|
2021-06-22 13:07:16 +03:00
|
|
|
|
|
|
|
await fs.writeFile(outputFilePath, mappedCSV);
|
|
|
|
|
|
|
|
return {
|
2022-10-13 10:04:22 +03:00
|
|
|
filePath: outputFilePath,
|
2021-06-22 13:07:16 +03:00
|
|
|
batches: numberOfBatches,
|
|
|
|
metadata: {
|
|
|
|
hasStripeData
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs an import of a CSV file
|
|
|
|
*
|
2022-10-13 10:04:22 +03:00
|
|
|
* @param {string} filePath - the path to a "prepared" CSV file
|
2021-06-22 13:07:16 +03:00
|
|
|
*/
|
2022-10-13 10:04:22 +03:00
|
|
|
async perform(filePath) {
|
2022-10-30 18:16:10 +03:00
|
|
|
const rows = await membersCSV.parse(filePath, DEFAULT_CSV_HEADER_MAPPING);
|
2021-06-22 13:07:16 +03:00
|
|
|
|
2022-10-25 11:40:28 +03:00
|
|
|
const defaultTier = await this._getDefaultTier();
|
|
|
|
const membersRepository = await this._getMembersRepository();
|
2021-06-22 14:50:03 +03:00
|
|
|
|
2021-06-22 13:07:16 +03:00
|
|
|
const result = await rows.reduce(async (resultPromise, row) => {
|
|
|
|
const resultAccumulator = await resultPromise;
|
|
|
|
|
2022-10-07 16:45:18 +03:00
|
|
|
// Use doNotReject config to reject `executionPromise` on rollback
|
|
|
|
// https://github.com/knex/knex/blob/master/UPGRADING.md
|
|
|
|
const trx = await this._knex.transaction(undefined, {doNotRejectOnRollback: false});
|
2021-06-22 13:07:16 +03:00
|
|
|
const options = {
|
2022-04-06 22:12:38 +03:00
|
|
|
transacting: trx,
|
|
|
|
context: this._context
|
2021-06-22 13:07:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2023-04-07 09:01:23 +03:00
|
|
|
// 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;
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
const memberValues = {
|
|
|
|
email: row.email,
|
|
|
|
name: row.name,
|
|
|
|
note: row.note,
|
|
|
|
subscribed: row.subscribed,
|
2023-04-07 09:01:23 +03:00
|
|
|
created_at: createdAt,
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
labels: row.labels
|
|
|
|
};
|
2022-10-25 11:40:28 +03:00
|
|
|
const existingMember = await membersRepository.get({email: memberValues.email}, {
|
2021-06-22 13:07:16 +03:00
|
|
|
...options,
|
|
|
|
withRelated: ['labels']
|
|
|
|
});
|
|
|
|
let member;
|
|
|
|
if (existingMember) {
|
|
|
|
const existingLabels = existingMember.related('labels') ? existingMember.related('labels').toJSON() : [];
|
2022-10-25 11:40:28 +03:00
|
|
|
member = await membersRepository.update({
|
Added strict field mapping to member CSV importer
closes https://github.com/TryGhost/Toolbox/issues/430
- The members importer used to import all fields present in the uploaded CSV if the headers match, even if they're not mapped in the UI. This behavior has lead to have misleading consequences and "hidden" features. For example, if the field was present but intentionally left as "Not imported" in the UI the field would still get imported.
- Having a strict list of supported import fields also allows for manageable long-term maintenance of the CSV Import API and detect/communicate changes when they happen.
- The list of the current default field mapping is:
email: 'email',
name: 'name',
note: 'note',
subscribed_to_emails: 'subscribed',
created_at: 'created_at',
complimentary_plan: 'complimentary_plan',
stripe_customer_id: 'stripe_customer_id',
labels: 'labels',
products: 'products'
2022-10-19 13:09:34 +03:00
|
|
|
...memberValues,
|
|
|
|
labels: existingLabels.concat(memberValues.labels)
|
2021-06-22 13:07:16 +03:00
|
|
|
}, {
|
|
|
|
...options,
|
|
|
|
id: existingMember.id
|
|
|
|
});
|
|
|
|
} else {
|
2022-10-25 11:40:28 +03:00
|
|
|
member = await membersRepository.create(memberValues, Object.assign({}, options, {
|
2022-04-13 19:35:24 +03:00
|
|
|
context: {
|
|
|
|
import: true
|
|
|
|
}
|
|
|
|
}));
|
2021-06-22 13:07:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-29 13:41:16 +03:00
|
|
|
if (row.stripe_customer_id && typeof row.stripe_customer_id === 'string') {
|
2022-10-25 11:40:28 +03:00
|
|
|
await membersRepository.linkStripeCustomer({
|
2021-06-22 13:07:16 +03:00
|
|
|
customer_id: row.stripe_customer_id,
|
|
|
|
member_id: member.id
|
|
|
|
}, options);
|
|
|
|
} else if (row.complimentary_plan) {
|
2022-10-25 11:40:28 +03:00
|
|
|
await membersRepository.update({
|
2022-11-08 07:47:00 +03:00
|
|
|
products: [{id: defaultTier.id.toString()}]
|
2022-05-25 11:24:56 +03:00
|
|
|
}, {
|
|
|
|
...options,
|
|
|
|
id: member.id
|
|
|
|
});
|
2021-06-23 12:19:10 +03:00
|
|
|
}
|
|
|
|
|
2021-06-22 13:07:16 +03:00
|
|
|
await trx.commit();
|
|
|
|
return {
|
|
|
|
...resultAccumulator,
|
|
|
|
imported: resultAccumulator.imported + 1
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
// The model layer can sometimes throw arrays of errors
|
2021-07-19 13:46:38 +03:00
|
|
|
const errorList = [].concat(error);
|
|
|
|
const errorMessage = errorList.map(({message}) => message).join(', ');
|
2021-06-22 13:07:16 +03:00
|
|
|
await trx.rollback();
|
|
|
|
return {
|
|
|
|
...resultAccumulator,
|
|
|
|
errors: [...resultAccumulator.errors, {
|
|
|
|
...row,
|
|
|
|
error: errorMessage
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, Promise.resolve({
|
|
|
|
imported: 0,
|
|
|
|
errors: []
|
|
|
|
}));
|
|
|
|
|
|
|
|
return {
|
|
|
|
total: result.imported + result.errors.length,
|
|
|
|
...result
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
generateCompletionEmail(result, data) {
|
2021-07-20 18:31:22 +03:00
|
|
|
const siteUrl = new URL(this._urlFor('home', null, true));
|
|
|
|
const membersUrl = new URL('members', this._urlFor('admin', null, true));
|
2021-06-22 13:07:16 +03:00
|
|
|
if (data.importLabel) {
|
|
|
|
membersUrl.searchParams.set('label', data.importLabel.slug);
|
|
|
|
}
|
|
|
|
return emailTemplate({result, siteUrl, membersUrl, ...data});
|
|
|
|
}
|
|
|
|
|
|
|
|
generateErrorCSV(result) {
|
|
|
|
const errorsWithFormattedMessages = result.errors.map((row) => {
|
|
|
|
const formattedError = row.error
|
|
|
|
.replace(
|
|
|
|
'Value in [members.email] cannot be blank.',
|
|
|
|
'Missing email address'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
'Value in [members.note] exceeds maximum length of 2000 characters.',
|
|
|
|
'"Note" exceeds maximum length of 2000 characters'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
'Value in [members.subscribed] must be one of true, false, 0 or 1.',
|
|
|
|
'Value in "Subscribed to emails" must be "true" or "false"'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
'Validation (isEmail) failed for email',
|
|
|
|
'Invalid email address'
|
|
|
|
)
|
|
|
|
.replace(
|
|
|
|
/No such customer:[^,]*/,
|
|
|
|
'Could not find Stripe customer'
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...row,
|
|
|
|
error: formattedError
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return membersCSV.unparse(errorsWithFormattedMessages);
|
|
|
|
}
|
|
|
|
|
2022-08-23 22:19:30 +03:00
|
|
|
/**
|
|
|
|
* Send email with attached CSV containing error rows info
|
|
|
|
*
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {String} config.emailRecipient - email recipient for error file
|
|
|
|
* @param {String} config.emailSubject - email subject
|
|
|
|
* @param {String} config.emailContent - html content of email
|
|
|
|
* @param {String} config.errorCSV - error CSV content
|
|
|
|
* @param {Object} config.emailSubject - email subject
|
|
|
|
* @param {Object} config.importLabel -
|
|
|
|
* @param {String} config.importLabel.name - label name
|
|
|
|
*/
|
|
|
|
async sendErrorEmail({emailRecipient, emailSubject, emailContent, errorCSV, importLabel}) {
|
|
|
|
await this._sendEmail({
|
|
|
|
to: emailRecipient,
|
|
|
|
subject: emailSubject,
|
|
|
|
html: emailContent,
|
|
|
|
forceTextContent: true,
|
|
|
|
attachments: [{
|
|
|
|
filename: `${importLabel.name} - Errors.csv`,
|
|
|
|
content: errorCSV,
|
|
|
|
contentType: 'text/csv',
|
|
|
|
contentDisposition: 'attachment'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-22 13:07:16 +03:00
|
|
|
/**
|
|
|
|
* Processes CSV file and imports member&label records depending on the size of the imported set
|
|
|
|
*
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {String} config.pathToCSV - path where imported csv with members records is stored
|
|
|
|
* @param {Object} config.headerMapping - mapping of CSV headers to member record fields
|
|
|
|
* @param {Object} [config.globalLabels] - labels to be applied to whole imported members set
|
|
|
|
* @param {Object} config.importLabel -
|
|
|
|
* @param {String} config.importLabel.name - label name
|
|
|
|
* @param {Object} config.user
|
|
|
|
* @param {String} config.user.email - calling user email
|
|
|
|
* @param {Object} config.LabelModel - instance of Ghosts Label model
|
2022-10-19 13:33:47 +03:00
|
|
|
* @param {Boolean} config.forceInline - allows to force performing imports not in a job (used in test environment)
|
2022-11-28 20:22:10 +03:00
|
|
|
* @param {{testImportThreshold: () => Promise<void>}} config.verificationTrigger
|
2021-06-22 13:07:16 +03:00
|
|
|
*/
|
2022-11-28 20:22:10 +03:00
|
|
|
async process({pathToCSV, headerMapping, globalLabels, importLabel, user, LabelModel, forceInline, verificationTrigger}) {
|
2021-07-23 19:29:19 +03:00
|
|
|
const meta = {};
|
2021-06-22 13:07:16 +03:00
|
|
|
const job = await this.prepare(pathToCSV, headerMapping, globalLabels);
|
|
|
|
|
2021-07-28 18:13:48 +03:00
|
|
|
meta.originalImportSize = job.batches;
|
2021-07-23 19:29:19 +03:00
|
|
|
|
2022-10-19 13:33:47 +03:00
|
|
|
if ((job.batches <= 500 && !job.metadata.hasStripeData) || forceInline) {
|
2022-10-13 10:04:22 +03:00
|
|
|
const result = await this.perform(job.filePath);
|
2021-06-22 13:07:16 +03:00
|
|
|
const importLabelModel = result.imported ? await LabelModel.findOne(importLabel) : null;
|
2022-11-28 20:22:10 +03:00
|
|
|
await verificationTrigger.testImportThreshold();
|
2021-07-23 19:29:19 +03:00
|
|
|
|
2021-06-22 13:07:16 +03:00
|
|
|
return {
|
2021-07-23 19:29:19 +03:00
|
|
|
meta: Object.assign(meta, {
|
2021-06-22 13:07:16 +03:00
|
|
|
stats: {
|
|
|
|
imported: result.imported,
|
|
|
|
invalid: result.errors
|
|
|
|
},
|
|
|
|
import_label: importLabelModel
|
2021-07-23 19:29:19 +03:00
|
|
|
})
|
2021-06-22 13:07:16 +03:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const emailRecipient = user.email;
|
2021-07-20 18:21:59 +03:00
|
|
|
this._addJob({
|
2021-06-22 13:07:16 +03:00
|
|
|
job: async () => {
|
2022-11-28 20:22:10 +03:00
|
|
|
try {
|
|
|
|
const result = await this.perform(job.filePath);
|
|
|
|
const importLabelModel = result.imported ? await LabelModel.findOne(importLabel) : null;
|
|
|
|
const emailContent = this.generateCompletionEmail(result, {
|
|
|
|
emailRecipient,
|
|
|
|
importLabel: importLabelModel ? importLabelModel.toJSON() : null
|
|
|
|
});
|
|
|
|
const errorCSV = this.generateErrorCSV(result);
|
|
|
|
const emailSubject = result.imported > 0 ? 'Your member import is complete' : 'Your member import was unsuccessful';
|
|
|
|
await this.sendErrorEmail({
|
|
|
|
emailRecipient,
|
|
|
|
emailSubject,
|
|
|
|
emailContent,
|
|
|
|
errorCSV,
|
|
|
|
importLabel
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
logging.error('Error in members import job');
|
|
|
|
logging.error(e);
|
|
|
|
}
|
2021-06-22 13:07:16 +03:00
|
|
|
|
2022-11-28 20:22:10 +03:00
|
|
|
// Still check verification triggers in case of errors (e.g., email sending failed)
|
|
|
|
try {
|
|
|
|
await verificationTrigger.testImportThreshold();
|
|
|
|
} catch (e) {
|
|
|
|
logging.error('Error in members import job when testing import threshold');
|
|
|
|
logging.error(e);
|
|
|
|
}
|
2021-06-22 13:07:16 +03:00
|
|
|
},
|
2023-01-04 13:22:12 +03:00
|
|
|
offloaded: false,
|
|
|
|
name: 'members-import'
|
2021-06-22 13:07:16 +03:00
|
|
|
});
|
|
|
|
|
2021-07-23 19:29:19 +03:00
|
|
|
return {
|
|
|
|
meta
|
|
|
|
};
|
2021-06-22 13:07:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|