Ghost/ghost/data-generator/lib/tables/members-subscribe-events.js
Sam Lord 28b11e6fed
Added command to generate demo data (#15691)
refs: https://github.com/TryGhost/Toolbox/issues/440

New command to generate demo data, creates data for over 20 tables in
Ghost, suitable for testing most features of the dashboard, as well as
making guided product tours using newsletters, tiers, many posts and
tags.

Usage: `yarn start generate-data`

Optionally, keep your existing posts / tags with: `yarn start generate-data --use-existing-tags --use-existing-posts`
2022-10-26 17:55:08 +01:00

53 lines
1.5 KiB
JavaScript

const TableImporter = require('./base');
const {faker} = require('@faker-js/faker');
const {luck} = require('../utils/random');
class MembersSubscribeEventsImporter extends TableImporter {
constructor(knex, {newsletters, subscriptions}) {
super('members_subscribe_events', knex);
this.newsletters = newsletters;
this.subscriptions = subscriptions;
}
setImportOptions({model}) {
this.model = model;
this.count = 0;
}
generate() {
const count = this.count;
this.count = this.count + 1;
if (count === 1 && this.model.status === 'free') {
return null;
}
let createdAt = faker.date.between(new Date(this.model.created_at), new Date()).toISOString();
let subscribed = luck(80);
// Free newsletter by default
let newsletterId = this.newsletters[0].id;
if (this.model.status === 'paid' && count === 0) {
// Paid newsletter
newsletterId = this.newsletters[1].id;
createdAt = this.subscriptions.find(s => s.member_id === this.model.id).created_at;
subscribed = luck(98);
}
if (!subscribed) {
return null;
}
return {
id: faker.database.mongodbObjectId(),
member_id: this.model.id,
newsletter_id: newsletterId,
subscribed: true,
created_at: createdAt,
source: 'member'
};
}
}
module.exports = MembersSubscribeEventsImporter;