Fixed generating duplicate newsletter names

refs https://github.com/TryGhost/DevOps/issues/119

- this switches away from using a static list of names in favor of ones
  generated by faker, so we don't run into duplicate names
- also minor code re-arranging
This commit is contained in:
Daniel Lockyer 2023-12-07 12:18:49 +01:00 committed by Daniel Lockyer
parent 8197a740b1
commit 739c690a29

View File

@ -6,21 +6,23 @@ const {slugify} = require('@tryghost/string');
class NewslettersImporter extends TableImporter { class NewslettersImporter extends TableImporter {
static table = 'newsletters'; static table = 'newsletters';
static dependencies = []; static dependencies = [];
defaultQuantity = 2; defaultQuantity = 2;
sortOrder = 0;
constructor(knex, transaction) { constructor(knex, transaction) {
super(NewslettersImporter.table, knex, transaction); super(NewslettersImporter.table, knex, transaction);
this.sortOrder = 0;
// TODO: Use random names if we ever need more than 2 newsletters
this.names = ['Regular premium', 'Occasional freebie'];
} }
generate() { generate() {
const name = this.names.shift(); const name = `${faker.commerce.productAdjective()} ${faker.word.noun()}`;
const sortOrder = this.sortOrder; const sortOrder = this.sortOrder;
this.sortOrder = this.sortOrder + 1; this.sortOrder = this.sortOrder + 1;
const weekAfter = new Date(blogStartDate); const weekAfter = new Date(blogStartDate);
weekAfter.setDate(weekAfter.getDate() + 7); weekAfter.setDate(weekAfter.getDate() + 7);
return { return {
id: faker.database.mongodbObjectId(), id: faker.database.mongodbObjectId(),
uuid: faker.datatype.uuid(), uuid: faker.datatype.uuid(),