Ghost/ghost/admin/app/components/gh-launch-wizard/finalise.js
Rishabh Garg d16a8781a9 🐛 Fixed sending non-integer prices to tiers api (#2288)
closes https://github.com/TryGhost/Team/issues/1319

Due to how JS implements numbers, it's possible that when we multiple a number with 2 decimal places by 100 that we do not end up with an integer e.g. 9.95 * 100 = 994.999...

This is not a valid price for the API and so we must round it to the nearest integer. We round off prices both at source as well as in ties serializer to make sure we never send non integer prices to API.
2022-03-04 14:48:45 +05:30

63 lines
2.0 KiB
JavaScript

import Component from '@glimmer/component';
import {htmlSafe} from '@ember/template';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default class GhLaunchWizardFinaliseComponent extends Component {
@service feature;
@service notifications;
@service router;
@service settings;
willDestroy() {
super.willDestroy?.(...arguments);
// clear any unsaved settings changes when going back/forward/closing
this.settings.rollbackAttributes();
}
async saveProduct() {
const data = this.args.getData();
this.product = data?.product;
if (this.product) {
const monthlyAmount = Math.round(data.monthlyAmount * 100);
const yearlyAmount = Math.round(data.yearlyAmount * 100);
const currency = data.currency;
const monthlyPrice = {
nickname: 'Monthly',
amount: monthlyAmount,
active: 1,
currency: currency,
interval: 'month',
type: 'recurring'
};
const yearlyPrice = {
nickname: 'Yearly',
amount: yearlyAmount,
active: 1,
currency: currency,
interval: 'year',
type: 'recurring'
};
this.product.set('monthlyPrice', monthlyPrice);
this.product.set('yearlyPrice', yearlyPrice);
const savedProduct = await this.product.save();
return savedProduct;
}
}
@task
*finaliseTask() {
const data = this.args.getData();
if (data?.product) {
yield this.saveProduct();
this.settings.set('editorIsLaunchComplete', true);
yield this.settings.save();
}
this.router.transitionTo('dashboard');
this.notifications.showNotification(
'Launch complete!',
{type: 'success', actions: htmlSafe('<a href="#/posts">Start creating content</a>')}
);
}
}