2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2022-10-26 19:55:08 +03:00
|
|
|
const {blogStartDate} = require('../utils/blog-info');
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
const sixWeeksLater = new Date(blogStartDate);
|
|
|
|
sixWeeksLater.setDate(sixWeeksLater.getDate() + (7 * 6));
|
|
|
|
|
2022-10-26 19:55:08 +03:00
|
|
|
class StripePricesImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'stripe_prices';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['products', 'stripe_products'];
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(StripePricesImporter.table, knex, transaction);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
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);
|
|
|
|
}
|
2022-10-26 19:55:08 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
setReferencedModel(model) {
|
|
|
|
this.model = model;
|
2022-10-26 19:55:08 +03:00
|
|
|
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;
|