From 0c387a350b9ac95f6720d174f5ef288574e63097 Mon Sep 17 00:00:00 2001 From: adriano Date: Fri, 9 Aug 2024 10:39:04 -0300 Subject: [PATCH] feat: create model for storing WhatsApp Official API usage and add controller to handle API consumption data --- controllers/apiUsagePricing.js | 79 ++++++++++++++++++++++++++++++---- models/API_Pricing.js | 7 ++- models/API_Usage.js | 29 +------------ models/API_Usage_Whatsapp.js | 47 ++++++++++++++++++++ routes/apiPriceRoute.js | 9 +++- 5 files changed, 129 insertions(+), 42 deletions(-) create mode 100644 models/API_Usage_Whatsapp.js diff --git a/controllers/apiUsagePricing.js b/controllers/apiUsagePricing.js index 6cb8966..7730b64 100644 --- a/controllers/apiUsagePricing.js +++ b/controllers/apiUsagePricing.js @@ -9,6 +9,7 @@ const billingSumUsage = require("../utils/billingSumUsage.js") const moment = require('moment') const API_Call = require("../models/API_Call.js") const API_Operation = require("../models/API_Operation.js") +const API_Usage_Whatsapp = require("../models/API_Usage_Whatsapp.js") const setApiPricing = async (req, res) => { @@ -23,7 +24,12 @@ const setApiPricing = async (req, res) => { const normalizedProvider = provider.trim().toLowerCase() const normalizedProduct = product.trim().toLowerCase() - const filter = { provider: normalizedProvider, product: normalizedProduct } + let filter = { provider: normalizedProvider, product: normalizedProduct } + + if (type) { + filter = { ...filter, type } + } + const update = { provider: normalizedProvider, product: normalizedProduct, @@ -47,14 +53,14 @@ const registerUsage = async (req, res) => { usage, callerId, sessionId, - companyId, + companyId, } = req.body mustContainProperties(req, [ 'companyId', 'callerId', - 'sessionId', + 'sessionId', 'provider', 'product', 'usage', @@ -73,7 +79,7 @@ const registerUsage = async (req, res) => { provider: provider.trim().toLowerCase(), product: product.trim().toLowerCase(), callerId, - sessionId, + sessionId, usage, price, billingBy, @@ -90,6 +96,60 @@ const registerUsage = async (req, res) => { } +const registerWhatsappUsage = async (req, res) => { + const { + companyId, + provider, + product, + type, + msgId, + ticketId, + billable, + pricing_model, + } = req.body + + mustContainProperties(req, [ + 'companyId', + 'provider', + 'product', + 'type', + 'msgId', + 'ticketId', + 'billable', + 'pricing_model' + ]) + + const apiPricing = await API_Pricing.findOne({ + provider: provider.trim().toLowerCase(), + product: product.trim().toLowerCase(), + type: type.trim().toLowerCase(), + }) + + + if (apiPricing) { + + const { price } = apiPricing + + const apiUsageWhatsapp = await API_Usage_Whatsapp.create({ + companyId, + provider: provider.trim().toLowerCase(), + product: product.trim().toLowerCase(), + price, + msgId, + ticketId, + billable, + pricing_model, + type + }) + + return res.status(StatusCodes.OK).json({ apiUsageWhatsapp }) + + } + + res.status(StatusCodes.NOT_FOUND).json({ msg: `Price not found for ${product} in the API Pricing table` }) + +} + const registerAPICall = async (req, res) => { const { callerId, @@ -129,12 +189,12 @@ const registerOperation = async (req, res) => { operation, quantityOfOperationAttempts: quantityOfAttempts, } = req.body - + mustContainProperties(req, [ 'companyId', 'callerId', 'sessionId', - 'operation', + 'operation', ]) const apiOperation = await API_Operation.create({ @@ -176,7 +236,7 @@ const registerAll = async (req, res) => { provider: provider.trim().toLowerCase(), product: product.trim().toLowerCase(), callerId, - sessionId, + sessionId, usage, price, billingBy, @@ -223,7 +283,7 @@ const registerAll = async (req, res) => { } } - res.send(StatusCodes.OK) + res.send(StatusCodes.OK) } const getUsage = async (req, res) => { @@ -255,5 +315,6 @@ module.exports = { registerAPICall, registerOperation, getUsage, - registerAll + registerAll, + registerWhatsappUsage } \ No newline at end of file diff --git a/models/API_Pricing.js b/models/API_Pricing.js index c497b10..a7113b3 100644 --- a/models/API_Pricing.js +++ b/models/API_Pricing.js @@ -19,10 +19,10 @@ const apiPricing = new Schema({ price: { type: String, required: true - }, + }, billingBy:{ type: String, - enum: ['minute', 'character', 'token', 'second'], + enum: ['minute', 'character', 'token', 'second', 'hour'], required: true, }, billingUnit:{ @@ -30,8 +30,7 @@ const apiPricing = new Schema({ required: true }, type: { - type: String, - enum: ['input', 'output',], + type: String, } }, { timestamps: true }) diff --git a/models/API_Usage.js b/models/API_Usage.js index 262a402..74aaac0 100644 --- a/models/API_Usage.js +++ b/models/API_Usage.js @@ -14,34 +14,7 @@ const apiUsage = new Schema({ sessionId: { type: String, required: true, - }, - // quantityOfOperationAttempts: { - // type: String, - // required: true, - // }, - // chosenOperation: { - // type: String, - // required: true, - // }, - // requestLogsOpenAI: { - // type: String, - // required: true, - // }, - // responseErrorLogsOpenAI: { - // type: String, - // }, - // quantityOfCallsToFalconFlowAPI: { - // type: String, - // required: true, - // }, - // requestLogsFalconFlowAPI: { - // type: String, - // required: true, - // }, - // responseErrorLogsFalconFlowAPI: { - // type: String, - // required: true, - // }, + }, provider: { type: String, required: true, diff --git a/models/API_Usage_Whatsapp.js b/models/API_Usage_Whatsapp.js new file mode 100644 index 0000000..42889c7 --- /dev/null +++ b/models/API_Usage_Whatsapp.js @@ -0,0 +1,47 @@ +const mongoose = require('../db/connect') + +const { Schema } = mongoose + +const apiUsageWhatsapp = new Schema({ + companyId: { + type: String, + required: true, + }, + msgId: { + type: String, + required: true, + }, + ticketId: { + type: String, + required: true, + }, + provider: { + type: String, + required: true, + }, + product: { + type: String, + required: true, + }, + price: { + type: String, + required: true + }, + billable: { + type: Boolean, + require: true + }, + pricing_model: { + type: String, + default: true + }, + type: { + type: String, + required: true, + }, + +}, { timestamps: true }) + +const API_Usage_Whatsapp = mongoose.model('API_Usage_Whatsapp', apiUsageWhatsapp) + +module.exports = API_Usage_Whatsapp diff --git a/routes/apiPriceRoute.js b/routes/apiPriceRoute.js index e02c353..e3fe30d 100644 --- a/routes/apiPriceRoute.js +++ b/routes/apiPriceRoute.js @@ -1,10 +1,17 @@ const express = require('express') const router = express.Router() const { authorization, } = require('../middleware/authentication') -const { setApiPricing, registerUsage, getUsage, registerAPICall, registerOperation, registerAll} = require('../controllers/apiUsagePricing') +const { setApiPricing, + registerUsage, + getUsage, + registerAPICall, + registerOperation, + registerAll, +registerWhatsappUsage} = require('../controllers/apiUsagePricing') router.route('/create').post(authorization, setApiPricing) router.route('/usage').post(authorization, registerUsage) +router.route('/usage-whatsapp').post(authorization, registerWhatsappUsage) router.route('/report').post(authorization, getUsage) router.route('/api-call').post(authorization, registerAPICall) router.route('/api-operation').post(authorization, registerOperation)