0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Changed MembersCSVImporter constructor

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

- The import threshold has to become a dynamic number based on external parameters. Because of this it makes most sense to have it as an async function parameter
- There's a package API change for importThreshold constructor paramter becoming a fetchThreshold async funciton parameter
This commit is contained in:
Naz 2021-08-18 16:09:33 +04:00
parent df8136804d
commit fd7f515055
2 changed files with 10 additions and 8 deletions

View file

@ -23,9 +23,9 @@ module.exports = class MembersCSVImporter {
* @param {({name, at, job, data, offloaded}) => void} options.addJob - Method registering an async job * @param {({name, at, job, data, offloaded}) => void} options.addJob - Method registering an async job
* @param {Object} options.knex - An instance of the Ghost Database connection * @param {Object} options.knex - An instance of the Ghost Database connection
* @param {Function} options.urlFor - function generating urls * @param {Function} options.urlFor - function generating urls
* @param {number} options.importThreshold - threshold to activate freeze flag if reached * @param {() => Promise<number>} options.fetchThreshold - fetches the threshold to activate freeze flag if reached
*/ */
constructor({storagePath, getTimezone, getMembersApi, sendEmail, isSet, addJob, knex, urlFor, importThreshold}) { constructor({storagePath, getTimezone, getMembersApi, sendEmail, isSet, addJob, knex, urlFor, fetchThreshold}) {
this._storagePath = storagePath; this._storagePath = storagePath;
this._getTimezone = getTimezone; this._getTimezone = getTimezone;
this._getMembersApi = getMembersApi; this._getMembersApi = getMembersApi;
@ -34,7 +34,7 @@ module.exports = class MembersCSVImporter {
this._addJob = addJob; this._addJob = addJob;
this._knex = knex; this._knex = knex;
this._urlFor = urlFor; this._urlFor = urlFor;
this._importThreshold = importThreshold; this._fetchThreshold = fetchThreshold;
} }
/** /**
@ -274,8 +274,9 @@ module.exports = class MembersCSVImporter {
meta.originalImportSize = job.batches; meta.originalImportSize = job.batches;
if (this._isSet('checkEmailList') && this._importThreshold) { if (this._isSet('checkEmailList')) {
meta.freeze = job.batches > this._importThreshold; const threshold = await this._fetchThreshold();
meta.freeze = job.batches > threshold;
} else { } else {
meta.freeze = false; meta.freeze = false;
} }

View file

@ -73,7 +73,8 @@ describe('Importer', function () {
isSet: isSetStub, isSet: isSetStub,
addJob: sinon.stub(), addJob: sinon.stub(),
knex: knexStub, knex: knexStub,
urlFor: sinon.stub() urlFor: sinon.stub(),
fetchThreshold: async () => 2
}); });
const result = await importer.process({ const result = await importer.process({
@ -96,7 +97,7 @@ describe('Importer', function () {
should.exist(result.meta.stats.invalid); should.exist(result.meta.stats.invalid);
should.equal(result.meta.import_label, null); should.equal(result.meta.import_label, null);
// freeze not triggered if it's not provided // freeze not triggered if the import is not over the threshold
should.exist(result.meta.freeze); should.exist(result.meta.freeze);
result.meta.freeze.should.be.false(); result.meta.freeze.should.be.false();
@ -153,7 +154,7 @@ describe('Importer', function () {
addJob: sinon.stub(), addJob: sinon.stub(),
knex: knexStub, knex: knexStub,
urlFor: sinon.stub(), urlFor: sinon.stub(),
importThreshold: 1 fetchThreshold: async () => 1
}); });
const result = await importer.process({ const result = await importer.process({