diff --git a/controllers/apiUsagePricing.js b/controllers/apiUsagePricing.js index 46b8b6f..d86a228 100644 --- a/controllers/apiUsagePricing.js +++ b/controllers/apiUsagePricing.js @@ -47,16 +47,14 @@ const registerUsage = async (req, res) => { usage, callerId, sessionId, - companyId, - quantityOfOperationAttempts, + companyId, } = req.body mustContainProperties(req, [ 'companyId', 'callerId', - 'sessionId', - 'quantityOfOperationAttempts', + 'sessionId', 'provider', 'product', 'usage', @@ -75,8 +73,7 @@ const registerUsage = async (req, res) => { provider: provider.trim().toLowerCase(), product: product.trim().toLowerCase(), callerId, - sessionId, - quantityOfOperationAttempts, + sessionId, usage, price, billingBy, @@ -130,25 +127,105 @@ const registerOperation = async (req, res) => { companyId, sessionId, operation, + quantityOfOperationAttempts: quantityOfAttempts, } = req.body - + mustContainProperties(req, [ 'companyId', 'callerId', 'sessionId', - 'operation' + 'operation', ]) const apiOperation = await API_Operation.create({ callerId, companyId, sessionId, - operation + operation, + quantityOfAttempts }) 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 { startDate, endDate, companyId, } = req.body @@ -177,5 +254,6 @@ module.exports = { registerUsage, registerAPICall, registerOperation, - getUsage + getUsage, + registerAll } \ No newline at end of file diff --git a/models/API_Operation.js b/models/API_Operation.js index 68b9260..2b1bafc 100644 --- a/models/API_Operation.js +++ b/models/API_Operation.js @@ -19,6 +19,10 @@ const apiOperation = new Schema({ type: String, required: true, }, + quantityOfAttempts: { + type: String, + required: true, + }, }, { timestamps: true }) diff --git a/models/API_Usage.js b/models/API_Usage.js index 2976524..262a402 100644 --- a/models/API_Usage.js +++ b/models/API_Usage.js @@ -15,10 +15,10 @@ const apiUsage = new Schema({ type: String, required: true, }, - quantityOfOperationAttempts: { - type: String, - required: true, - }, + // quantityOfOperationAttempts: { + // type: String, + // required: true, + // }, // chosenOperation: { // type: String, // required: true, diff --git a/routes/apiPriceRoute.js b/routes/apiPriceRoute.js index e06423e..e02c353 100644 --- a/routes/apiPriceRoute.js +++ b/routes/apiPriceRoute.js @@ -1,13 +1,14 @@ const express = require('express') const router = express.Router() 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('/usage').post(authorization, registerUsage) router.route('/report').post(authorization, getUsage) router.route('/api-call').post(authorization, registerAPICall) router.route('/api-operation').post(authorization, registerOperation) +router.route('/api-register-all').post(authorization, registerAll)