From 66a6a522e0bf0b07144a331c6df0313f84fcacdd Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 20 Jul 2021 19:26:46 +0400 Subject: [PATCH] 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 --- core/server/services/members/importer/importer.js | 11 ++++++----- core/server/services/members/service.js | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/core/server/services/members/importer/importer.js b/core/server/services/members/importer/importer.js index 50644d5ffd..ca71fc616d 100644 --- a/core/server/services/members/importer/importer.js +++ b/core/server/services/members/importer/importer.js @@ -6,7 +6,6 @@ const errors = require('@tryghost/errors'); const tpl = require('@tryghost/tpl'); const urlUtils = require('../../../../shared/url-utils'); -const db = require('../../../data/db'); const emailTemplate = require('./email-template'); const messages = { @@ -19,18 +18,20 @@ module.exports = class MembersCSVImporter { * @param {Object} config * @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} 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 {({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._settingsCache = settingsCache; this._getMembersApi = getMembersApi; this._ghostMailer = ghostMailer; this._isSet = isSet; this._addJob = addJob; + this._knex = knex; } /** @@ -128,7 +129,7 @@ module.exports = class MembersCSVImporter { const result = await rows.reduce(async (resultPromise, row) => { const resultAccumulator = await resultPromise; - const trx = await db.knex.transaction(); + const trx = await this._knex.transaction(); const options = { transacting: trx }; diff --git a/core/server/services/members/service.js b/core/server/services/members/service.js index 32f947eaea..8acc59d707 100644 --- a/core/server/services/members/service.js +++ b/core/server/services/members/service.js @@ -134,7 +134,8 @@ const membersService = { () => membersApi, ghostMailer, labsService.isSet, - jobsService.addJob + jobsService.addJob, + db.knex ), stats: new MembersStats({