From 58f23726feeb8e4ddba9e83bea8fcdfb8eec4936 Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Wed, 13 Dec 2023 14:31:07 +0100 Subject: [PATCH] Added recommendation click and subscribe events to data generator ref PROD-244 --- .../RecommendationClickEventsImporter.js | 32 +++++++++++++++++++ .../RecommendationSubscribeEventsImporter.js | 32 +++++++++++++++++++ ghost/data-generator/lib/importers/index.js | 4 ++- 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 ghost/data-generator/lib/importers/RecommendationClickEventsImporter.js create mode 100644 ghost/data-generator/lib/importers/RecommendationSubscribeEventsImporter.js diff --git a/ghost/data-generator/lib/importers/RecommendationClickEventsImporter.js b/ghost/data-generator/lib/importers/RecommendationClickEventsImporter.js new file mode 100644 index 0000000000..972f7ff505 --- /dev/null +++ b/ghost/data-generator/lib/importers/RecommendationClickEventsImporter.js @@ -0,0 +1,32 @@ +const TableImporter = require('./TableImporter'); +const {faker} = require('@faker-js/faker'); +const {luck} = require('../utils/random'); + +class RecommendationClickEventsImporter extends TableImporter { + static table = 'recommendation_click_events'; + static dependencies = ['recommendations', 'members']; + + constructor(knex, transaction) { + super(RecommendationClickEventsImporter.table, knex, transaction); + } + + async import(quantity) { + const recommendations = await this.transaction.select('id', 'created_at').from('recommendations'); + this.members = await this.transaction.select('id').from('members').limit(500); + + await this.importForEach(recommendations, quantity ? quantity / recommendations.length : () => faker.datatype.number({min: 0, max: 50})); + } + + generate() { + // Not unique + const member = luck(30) ? null : faker.helpers.arrayElement(this.members); + return { + id: faker.database.mongodbObjectId(), + recommendation_id: this.model.id, + member_id: member?.id ?? null, + created_at: faker.date.past() + }; + } +} + +module.exports = RecommendationClickEventsImporter; diff --git a/ghost/data-generator/lib/importers/RecommendationSubscribeEventsImporter.js b/ghost/data-generator/lib/importers/RecommendationSubscribeEventsImporter.js new file mode 100644 index 0000000000..75ca15f82d --- /dev/null +++ b/ghost/data-generator/lib/importers/RecommendationSubscribeEventsImporter.js @@ -0,0 +1,32 @@ +const TableImporter = require('./TableImporter'); +const {faker} = require('@faker-js/faker'); +const {luck} = require('../utils/random'); + +class RecommendationSubscribeEventsImporter extends TableImporter { + static table = 'recommendation_subscribe_events'; + static dependencies = ['recommendations', 'members']; + + constructor(knex, transaction) { + super(RecommendationSubscribeEventsImporter.table, knex, transaction); + } + + async import(quantity) { + const recommendations = await this.transaction.select('id', 'created_at').from('recommendations').where('one_click_subscribe', true); + this.members = await this.transaction.select('id').from('members').limit(500); + + await this.importForEach(recommendations, quantity ? quantity / recommendations.length : () => faker.datatype.number({min: 0, max: 50})); + } + + generate() { + // Not unique + const member = luck(1) ? null : faker.helpers.arrayElement(this.members); + return { + id: faker.database.mongodbObjectId(), + recommendation_id: this.model.id, + member_id: member?.id ?? null, + created_at: faker.date.past() + }; + } +} + +module.exports = RecommendationSubscribeEventsImporter; diff --git a/ghost/data-generator/lib/importers/index.js b/ghost/data-generator/lib/importers/index.js index 6d373f40fb..b62f07cd54 100644 --- a/ghost/data-generator/lib/importers/index.js +++ b/ghost/data-generator/lib/importers/index.js @@ -35,5 +35,7 @@ module.exports = [ require('./MembersLabelsImporter'), require('./RolesUsersImporter'), require('./MembersFeedbackImporter'), - require('./RecommendationsImporter') + require('./RecommendationsImporter'), + require('./RecommendationClickEventsImporter'), + require('./RecommendationSubscribeEventsImporter') ];