feat: add 'second' option to enum billingBy in API_Pricing model and update API usage calculation function

main
adriano 2024-07-31 10:04:19 -03:00
parent f26045a038
commit 5c81ae2513
3 changed files with 6 additions and 9 deletions

View File

@ -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)
})
}

View File

@ -22,7 +22,7 @@ const apiPricing = new Schema({
},
billingBy:{
type: String,
enum: ['minute', 'character', 'token'],
enum: ['minute', 'character', 'token', 'second'],
required: true,
},
billingUnit:{

View File

@ -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)
}