Ghost/ghost/data-generator/lib/importers/MembersStripeCustomersImporter.js
Simon Backx d2cb23c3fa
Wired up Docker setup script and increased data generation performance (#19420)
ref PROD-233

- Stored whether Docker is used in the config files
- When running `yarn setup`, any existing Docker container will be
reset. Run with `-y` to skip the confirmation step.
- `yarn setup` will always init the database and generate fake data
- Increased amount of default generated data to 100,000 members + 500
posts.
- Made lots of performance improvements in the data generator so we can
generate the default data in ±170s
2024-01-05 13:42:30 +00:00

46 lines
1.6 KiB
JavaScript

const {faker} = require('@faker-js/faker');
const TableImporter = require('./TableImporter');
class MembersStripeCustomersImporter extends TableImporter {
static table = 'members_stripe_customers';
static dependencies = ['members'];
constructor(knex, transaction) {
super(MembersStripeCustomersImporter.table, knex, transaction);
}
async import(quantity) {
const members = await this.transaction.select('id', 'name', 'email', 'created_at', 'status').from('members');
await this.importForEach(members, quantity ? quantity / members.length : 1);
}
generate() {
if (this.model.status !== 'paid') {
// Only 30% of free members should have a stripe customer = have had a subscription in the past or tried to subscribe
// The number should increase the older the member is
const daysSinceMemberCreated = Math.floor((new Date() - new Date(this.model.created_at)) / (1000 * 60 * 60 * 24));
const shouldHaveStripeCustomer = faker.datatype.number({min: 0, max: 100}) < Math.max(Math.min(daysSinceMemberCreated / 30, 30), 5);
if (!shouldHaveStripeCustomer) {
return;
}
}
return {
id: this.fastFakeObjectId(),
member_id: this.model.id,
customer_id: `cus_${faker.random.alphaNumeric(14, {
casing: 'mixed'
})}`,
name: this.model.name,
email: this.model.email,
created_at: this.model.created_at,
created_by: 'unused'
};
}
}
module.exports = MembersStripeCustomersImporter;