Refactored jobs service out of MembersCSVImporter

refs https://github.com/TryGhost/Team/issues/916

-  The refactor was done follow the DI Constructor pattern and prepare module for extraction
This commit is contained in:
Naz 2021-07-20 19:21:59 +04:00 committed by naz
parent 489e470d7b
commit b96a8f4f6b
2 changed files with 7 additions and 4 deletions

View File

@ -8,7 +8,6 @@ const tpl = require('@tryghost/tpl');
const urlUtils = require('../../../../shared/url-utils'); const urlUtils = require('../../../../shared/url-utils');
const db = require('../../../data/db'); const db = require('../../../data/db');
const emailTemplate = require('./email-template'); const emailTemplate = require('./email-template');
const jobsService = require('../../jobs');
const messages = { const messages = {
filenameCollision: 'Filename already exists, please try again.', filenameCollision: 'Filename already exists, please try again.',
@ -22,14 +21,16 @@ module.exports = class MembersCSVImporter {
* @param {Object} settingsCache - An instance of the Ghost Settings Cache * @param {Object} settingsCache - An instance of the Ghost Settings Cache
* @param {Object} ghostMailer - An instance of GhostMailer * @param {Object} ghostMailer - An instance of GhostMailer
* @param {(string) => boolean} isSet - Method checking if specific feature is enabled * @param {(string) => boolean} isSet - Method checking if specific feature is enabled
* @param {({name, at, job, data, offloaded}) => void} addJob - Method registering an async job
* @param {() => Object} getMembersApi * @param {() => Object} getMembersApi
*/ */
constructor(config, settingsCache, getMembersApi, ghostMailer, isSet) { constructor(config, settingsCache, getMembersApi, ghostMailer, isSet, addJob) {
this._storagePath = config.storagePath; this._storagePath = config.storagePath;
this._settingsCache = settingsCache; this._settingsCache = settingsCache;
this._getMembersApi = getMembersApi; this._getMembersApi = getMembersApi;
this._ghostMailer = ghostMailer; this._ghostMailer = ghostMailer;
this._isSet = isSet; this._isSet = isSet;
this._addJob = addJob;
} }
/** /**
@ -280,7 +281,7 @@ module.exports = class MembersCSVImporter {
}; };
} else { } else {
const emailRecipient = user.email; const emailRecipient = user.email;
jobsService.addJob({ this._addJob({
job: async () => { job: async () => {
const result = await this.perform(job.id); const result = await this.perform(job.id);
const importLabelModel = result.imported ? await LabelModel.findOne(importLabel) : null; const importLabelModel = result.imported ? await LabelModel.findOne(importLabel) : null;

View File

@ -15,6 +15,7 @@ const config = require('../../../shared/config');
const ghostVersion = require('@tryghost/version'); const ghostVersion = require('@tryghost/version');
const _ = require('lodash'); const _ = require('lodash');
const {GhostMailer} = require('../mail'); const {GhostMailer} = require('../mail');
const jobsService = require('../jobs');
const messages = { const messages = {
noLiveKeysInDevelopment: 'Cannot use live stripe keys in development. Please restart in production mode.', noLiveKeysInDevelopment: 'Cannot use live stripe keys in development. Please restart in production mode.',
@ -132,7 +133,8 @@ const membersService = {
settingsCache, settingsCache,
() => membersApi, () => membersApi,
ghostMailer, ghostMailer,
labsService.isSet labsService.isSet,
jobsService.addJob
), ),
stats: new MembersStats({ stats: new MembersStats({