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 {Object} options.knex - An instance of the Ghost Database connection
* @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._getTimezone = getTimezone;
this._getMembersApi = getMembersApi;
@ -34,7 +34,7 @@ module.exports = class MembersCSVImporter {
this._addJob = addJob;
this._knex = knex;
this._urlFor = urlFor;
this._importThreshold = importThreshold;
this._fetchThreshold = fetchThreshold;
}
/**
@ -274,8 +274,9 @@ module.exports = class MembersCSVImporter {
meta.originalImportSize = job.batches;
if (this._isSet('checkEmailList') && this._importThreshold) {
meta.freeze = job.batches > this._importThreshold;
if (this._isSet('checkEmailList')) {
const threshold = await this._fetchThreshold();
meta.freeze = job.batches > threshold;
} else {
meta.freeze = false;
}

View File

@ -73,7 +73,8 @@ describe('Importer', function () {
isSet: isSetStub,
addJob: sinon.stub(),
knex: knexStub,
urlFor: sinon.stub()
urlFor: sinon.stub(),
fetchThreshold: async () => 2
});
const result = await importer.process({
@ -96,7 +97,7 @@ describe('Importer', function () {
should.exist(result.meta.stats.invalid);
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);
result.meta.freeze.should.be.false();
@ -153,7 +154,7 @@ describe('Importer', function () {
addJob: sinon.stub(),
knex: knexStub,
urlFor: sinon.stub(),
importThreshold: 1
fetchThreshold: async () => 1
});
const result = await importer.process({