mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-02 08:13:34 +03:00
4ff467794f
refs: https://github.com/TryGhost/DevOps/issues/11 This is a pretty huge commit, but the relevant points are: * Each importer no longer needs to be passed a set of data, it just gets the data it needs * Each importer specifies its dependencies, so that the order of import can be determined at runtime using a topological sort * The main data generator function can just tell each importer to import the data it has This makes working on the data generator much easier. Some other benefits are: * Batched importing, massively speeding up the whole process * `--tables` to set the exact tables you want to import, and specify the quantity of each
70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
const {faker} = require('@faker-js/faker');
|
|
const TableImporter = require('./TableImporter');
|
|
const {blogStartDate} = require('../utils/blog-info');
|
|
|
|
const sixWeeksLater = new Date(blogStartDate);
|
|
sixWeeksLater.setDate(sixWeeksLater.getDate() + (7 * 6));
|
|
|
|
class StripePricesImporter extends TableImporter {
|
|
static table = 'stripe_prices';
|
|
static dependencies = ['products', 'stripe_products'];
|
|
|
|
constructor(knex, transaction) {
|
|
super(StripePricesImporter.table, knex, transaction);
|
|
}
|
|
|
|
async import() {
|
|
const stripeProducts = await this.transaction.select('id', 'stripe_product_id', 'product_id').from('stripe_products');
|
|
this.products = await this.transaction.select('id', 'monthly_price', 'yearly_price').from('products');
|
|
|
|
await this.importForEach(stripeProducts, 2);
|
|
}
|
|
|
|
setReferencedModel(model) {
|
|
this.model = model;
|
|
this.count = 0;
|
|
}
|
|
|
|
generate() {
|
|
const count = this.count;
|
|
this.count = this.count + 1;
|
|
|
|
const relatedProduct = this.products.find(product => product.id === this.model.product_id);
|
|
|
|
if (count === 1 && relatedProduct.monthly_price === null) {
|
|
// Only single complimentary price (yearly)
|
|
return null;
|
|
}
|
|
|
|
const billingCycle = {
|
|
nickname: 'Monthly',
|
|
interval: 'month',
|
|
type: 'recurring',
|
|
currency: 'usd',
|
|
amount: relatedProduct.monthly_price
|
|
};
|
|
if (count === 1) {
|
|
billingCycle.nickname = 'Yearly';
|
|
billingCycle.interval = 'year';
|
|
billingCycle.amount = relatedProduct.yearly_price;
|
|
} else if (relatedProduct.monthly_price === null) {
|
|
billingCycle.nickname = 'Complimentary';
|
|
billingCycle.interval = 'year';
|
|
billingCycle.amount = 0;
|
|
}
|
|
|
|
return Object.assign({}, {
|
|
id: faker.database.mongodbObjectId(),
|
|
stripe_price_id: faker.datatype.hexadecimal({
|
|
length: 64,
|
|
prefix: ''
|
|
}),
|
|
stripe_product_id: this.model.stripe_product_id,
|
|
active: true,
|
|
created_at: faker.date.between(blogStartDate, sixWeeksLater)
|
|
}, billingCycle);
|
|
}
|
|
}
|
|
|
|
module.exports = StripePricesImporter;
|