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, billingBy,
billingUnit, billingUnit,
companyId, companyId,
total_cost: calculateApiUsage(price, billingUnit, usage, billingBy) total_cost: calculateApiUsage(price, billingUnit, usage)
}) })
return res.status(StatusCodes.OK).json({ apiUsage }) return res.status(StatusCodes.OK).json({ apiUsage })
@ -182,7 +182,7 @@ const registerAll = async (req, res) => {
billingBy, billingBy,
billingUnit, billingUnit,
companyId, 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:{ billingBy:{
type: String, type: String,
enum: ['minute', 'character', 'token'], enum: ['minute', 'character', 'token', 'second'],
required: true, required: true,
}, },
billingUnit:{ billingUnit:{

View File

@ -1,12 +1,9 @@
function calculateApiUsage(price, billingUnit, usage, billingBy) { 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) return total_cost.toFixed(10)
} }