feat: add 'second' option to enum billingBy in API_Pricing model and update API usage calculation function
parent
f26045a038
commit
5c81ae2513
|
@ -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)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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:{
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue