Refactored db dependency 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:26:46 +04:00 committed by naz
parent fdfd185835
commit 99560ca901

View File

@ -6,7 +6,6 @@ const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
const urlUtils = require('../../../../shared/url-utils'); const urlUtils = require('../../../../shared/url-utils');
const db = require('../../../data/db');
const emailTemplate = require('./email-template'); const emailTemplate = require('./email-template');
const messages = { const messages = {
@ -19,18 +18,20 @@ module.exports = class MembersCSVImporter {
* @param {Object} config * @param {Object} config
* @param {string} config.storagePath - The path to store CSV's in before importing * @param {string} config.storagePath - The path to store CSV's in before importing
* @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} getMembersApi
* @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 {({name, at, job, data, offloaded}) => void} addJob - Method registering an async job
* @param {() => Object} getMembersApi * @param {Object} knex - An instance of the Ghost Database connection
*/ */
constructor(config, settingsCache, getMembersApi, ghostMailer, isSet, addJob) { constructor(config, settingsCache, getMembersApi, ghostMailer, isSet, addJob, knex) {
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; this._addJob = addJob;
this._knex = knex;
} }
/** /**
@ -128,7 +129,7 @@ module.exports = class MembersCSVImporter {
const result = await rows.reduce(async (resultPromise, row) => { const result = await rows.reduce(async (resultPromise, row) => {
const resultAccumulator = await resultPromise; const resultAccumulator = await resultPromise;
const trx = await db.knex.transaction(); const trx = await this._knex.transaction();
const options = { const options = {
transacting: trx transacting: trx
}; };