mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2025-12-19 05:47:46 +00:00
subscription update-quantities validate min voice call sessions (#521)
* subscription update-quantities validate min voice call sessions * subscription update-quantities validate min voice call sessions * fixed review comment
This commit is contained in:
@@ -17,6 +17,7 @@ const {
|
||||
} = require('../../utils/stripe-utils');
|
||||
const {setupFreeTrial} = require('./utils');
|
||||
const sysError = require('../error');
|
||||
const Product = require('../../models/product');
|
||||
const actions = [
|
||||
'upgrade-to-paid',
|
||||
'downgrade-to-free',
|
||||
@@ -24,6 +25,8 @@ const actions = [
|
||||
'update-quantities'
|
||||
];
|
||||
|
||||
const MIN_VOICE_CALL_SESSION_QUANTITY = 5;
|
||||
|
||||
const handleError = async(logger, method, res, err) => {
|
||||
if ('StatusError' === err.name) {
|
||||
const text = await err.text();
|
||||
@@ -146,6 +149,24 @@ const upgradeToPaidPlan = async(req, res) => {
|
||||
|
||||
await handleSubscriptionOutcome(req, res, subscription);
|
||||
};
|
||||
|
||||
const validateProductQuantities = async(products) => {
|
||||
// validate voice call session minimums
|
||||
const productModel = new Product();
|
||||
const availableProducts = await productModel.retrieveAll();
|
||||
const voiceCallSessionsProductSid =
|
||||
availableProducts.find((p) => p.category === 'voice_call_session')?.product_sid;
|
||||
if (voiceCallSessionsProductSid) {
|
||||
const invalid = products.find((p) => {
|
||||
return (p.product_sid === voiceCallSessionsProductSid &&
|
||||
(typeof p.quantity !== 'number' || p.quantity < MIN_VOICE_CALL_SESSION_QUANTITY));
|
||||
});
|
||||
if (invalid) {
|
||||
throw new DbErrorBadRequest('invalid voice call session value, minimum is ' +
|
||||
MIN_VOICE_CALL_SESSION_QUANTITY);
|
||||
}
|
||||
}
|
||||
};
|
||||
const downgradeToFreePlan = async(req, res) => {
|
||||
const logger = req.app.locals.logger;
|
||||
const {account_sid} = req.user;
|
||||
@@ -291,11 +312,11 @@ router.post('/', async(req, res) => {
|
||||
if ('update-payment-method' === action && typeof payment_method_id !== 'string') {
|
||||
throw new DbErrorBadRequest('missing payment_method_id');
|
||||
}
|
||||
if ('upgrade-to-paid' === action && (!Array.isArray(products) || 0 === products.length)) {
|
||||
if (['update-quantities', 'upgrade-to-paid'].includes(action)) {
|
||||
if ((!Array.isArray(products) || 0 === products.length)) {
|
||||
throw new DbErrorBadRequest('missing products');
|
||||
}
|
||||
if ('update-quantities' === action && (!Array.isArray(products) || 0 === products.length)) {
|
||||
throw new DbErrorBadRequest('missing products');
|
||||
await validateProductQuantities(products);
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
|
||||
Reference in New Issue
Block a user