feat: added routes to insert all informations

main
adriano 2024-07-30 14:25:33 -03:00
parent 14c0303c78
commit f26045a038
4 changed files with 98 additions and 15 deletions

View File

@ -47,16 +47,14 @@ const registerUsage = async (req, res) => {
usage, usage,
callerId, callerId,
sessionId, sessionId,
companyId, companyId,
quantityOfOperationAttempts,
} = req.body } = req.body
mustContainProperties(req, [ mustContainProperties(req, [
'companyId', 'companyId',
'callerId', 'callerId',
'sessionId', 'sessionId',
'quantityOfOperationAttempts',
'provider', 'provider',
'product', 'product',
'usage', 'usage',
@ -75,8 +73,7 @@ const registerUsage = async (req, res) => {
provider: provider.trim().toLowerCase(), provider: provider.trim().toLowerCase(),
product: product.trim().toLowerCase(), product: product.trim().toLowerCase(),
callerId, callerId,
sessionId, sessionId,
quantityOfOperationAttempts,
usage, usage,
price, price,
billingBy, billingBy,
@ -130,25 +127,105 @@ const registerOperation = async (req, res) => {
companyId, companyId,
sessionId, sessionId,
operation, operation,
quantityOfOperationAttempts: quantityOfAttempts,
} = req.body } = req.body
mustContainProperties(req, [ mustContainProperties(req, [
'companyId', 'companyId',
'callerId', 'callerId',
'sessionId', 'sessionId',
'operation' 'operation',
]) ])
const apiOperation = await API_Operation.create({ const apiOperation = await API_Operation.create({
callerId, callerId,
companyId, companyId,
sessionId, sessionId,
operation operation,
quantityOfAttempts
}) })
res.status(StatusCodes.OK).json({ apiOperation }) res.status(StatusCodes.OK).json({ apiOperation })
} }
const registerAll = async (req, res) => {
const {
callerId,
companyId,
sessionId,
lstUsage,
lstRequest,
lstOperation,
} = req.body
if (lstUsage) {
for (const used of lstUsage) {
const { product, provider, usage } = used
const apiPricing = await API_Pricing.findOne({
provider: provider.trim().toLowerCase(),
product: product.trim().toLowerCase(),
})
if (apiPricing) {
const { price, billingBy, billingUnit } = apiPricing
const apiUsage = await API_Usage.create({
provider: provider.trim().toLowerCase(),
product: product.trim().toLowerCase(),
callerId,
sessionId,
usage,
price,
billingBy,
billingUnit,
companyId,
total_cost: calculateApiUsage(price, billingUnit, usage, billingBy)
})
}
}
}
if (lstRequest) {
for (const request of lstRequest) {
const { type,
requestLogs,
responseError,
quantityOfAPICall } = request
const apiCall = await API_Call.create({
callerId,
companyId,
sessionId,
type,
requestLogs,
responseError,
quantityOfAPICall
})
}
}
if (lstOperation) {
for (const op of lstOperation) {
const { operation, quantityOfOperationAttempts: quantityOfAttempts } = op
const apiOperation = await API_Operation.create({
callerId,
companyId,
sessionId,
operation,
quantityOfAttempts
})
}
}
res.send(StatusCodes.OK)
}
const getUsage = async (req, res) => { const getUsage = async (req, res) => {
const { startDate, endDate, companyId, } = req.body const { startDate, endDate, companyId, } = req.body
@ -177,5 +254,6 @@ module.exports = {
registerUsage, registerUsage,
registerAPICall, registerAPICall,
registerOperation, registerOperation,
getUsage getUsage,
registerAll
} }

View File

@ -19,6 +19,10 @@ const apiOperation = new Schema({
type: String, type: String,
required: true, required: true,
}, },
quantityOfAttempts: {
type: String,
required: true,
},
}, { timestamps: true }) }, { timestamps: true })

View File

@ -15,10 +15,10 @@ const apiUsage = new Schema({
type: String, type: String,
required: true, required: true,
}, },
quantityOfOperationAttempts: { // quantityOfOperationAttempts: {
type: String, // type: String,
required: true, // required: true,
}, // },
// chosenOperation: { // chosenOperation: {
// type: String, // type: String,
// required: true, // required: true,

View File

@ -1,13 +1,14 @@
const express = require('express') const express = require('express')
const router = express.Router() const router = express.Router()
const { authorization, } = require('../middleware/authentication') const { authorization, } = require('../middleware/authentication')
const { setApiPricing, registerUsage, getUsage, registerAPICall, registerOperation} = require('../controllers/apiUsagePricing') const { setApiPricing, registerUsage, getUsage, registerAPICall, registerOperation, registerAll} = require('../controllers/apiUsagePricing')
router.route('/create').post(authorization, setApiPricing) router.route('/create').post(authorization, setApiPricing)
router.route('/usage').post(authorization, registerUsage) router.route('/usage').post(authorization, registerUsage)
router.route('/report').post(authorization, getUsage) router.route('/report').post(authorization, getUsage)
router.route('/api-call').post(authorization, registerAPICall) router.route('/api-call').post(authorization, registerAPICall)
router.route('/api-operation').post(authorization, registerOperation) router.route('/api-operation').post(authorization, registerOperation)
router.route('/api-register-all').post(authorization, registerAll)