From 5c81ae25139ca193f7e5d58bd41805499a700f63 Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 31 Jul 2024 10:04:19 -0300 Subject: [PATCH] feat: add 'second' option to enum billingBy in API_Pricing model and update API usage calculation function --- controllers/apiUsagePricing.js | 4 ++-- models/API_Pricing.js | 2 +- utils/calculateApiUsage.js | 9 +++------ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/controllers/apiUsagePricing.js b/controllers/apiUsagePricing.js index d86a228..6cb8966 100644 --- a/controllers/apiUsagePricing.js +++ b/controllers/apiUsagePricing.js @@ -79,7 +79,7 @@ const registerUsage = async (req, res) => { billingBy, billingUnit, companyId, - total_cost: calculateApiUsage(price, billingUnit, usage, billingBy) + total_cost: calculateApiUsage(price, billingUnit, usage) }) return res.status(StatusCodes.OK).json({ apiUsage }) @@ -182,7 +182,7 @@ const registerAll = async (req, res) => { billingBy, billingUnit, companyId, - total_cost: calculateApiUsage(price, billingUnit, usage, billingBy) + total_cost: calculateApiUsage(price, billingUnit, usage) }) } diff --git a/models/API_Pricing.js b/models/API_Pricing.js index 329d61d..c497b10 100644 --- a/models/API_Pricing.js +++ b/models/API_Pricing.js @@ -22,7 +22,7 @@ const apiPricing = new Schema({ }, billingBy:{ type: String, - enum: ['minute', 'character', 'token'], + enum: ['minute', 'character', 'token', 'second'], required: true, }, billingUnit:{ diff --git a/utils/calculateApiUsage.js b/utils/calculateApiUsage.js index 536ba26..c3f9e5e 100644 --- a/utils/calculateApiUsage.js +++ b/utils/calculateApiUsage.js @@ -1,12 +1,9 @@ function calculateApiUsage(price, billingUnit, usage, billingBy) { - let _billingUnit = +billingUnit - - if (billingBy == 'minute') - _billingUnit = 60 * _billingUnit - - const total_cost = (+usage / _billingUnit) * parseFloat(price) + + + const total_cost = (+usage / +billingUnit) * parseFloat(price) return total_cost.toFixed(10) }