Compare commits
No commits in common. "f26045a0382f7870c9e061a184a87188527df8cd" and "b96b05ad831b724a192b1c9a215725a1964d6ec7" have entirely different histories.
f26045a038
...
b96b05ad83
10
app.js
10
app.js
|
@ -28,13 +28,13 @@ const notFoundMiddlware = require('./middleware/not-found')
|
||||||
|
|
||||||
//middleware
|
//middleware
|
||||||
app.set('trust proxy', 1)
|
app.set('trust proxy', 1)
|
||||||
// app.use(rateLimiter({
|
app.use(rateLimiter({
|
||||||
// windowMs: 15 * 60 * 1000,
|
windowMs: 15 * 60 * 1000,
|
||||||
// max: 60,
|
max: 60,
|
||||||
// }))
|
}))
|
||||||
|
|
||||||
// Security packages
|
// Security packages
|
||||||
// app.use(helmet())
|
app.use(helmet())
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
app.use(xss())
|
app.use(xss())
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@ const {
|
||||||
const API_Usage = require("../models/API_Usage.js")
|
const API_Usage = require("../models/API_Usage.js")
|
||||||
const billingSumUsage = require("../utils/billingSumUsage.js")
|
const billingSumUsage = require("../utils/billingSumUsage.js")
|
||||||
const moment = require('moment')
|
const moment = require('moment')
|
||||||
const API_Call = require("../models/API_Call.js")
|
|
||||||
const API_Operation = require("../models/API_Operation.js")
|
|
||||||
|
|
||||||
const setApiPricing = async (req, res) => {
|
const setApiPricing = async (req, res) => {
|
||||||
|
|
||||||
|
@ -20,22 +18,15 @@ const setApiPricing = async (req, res) => {
|
||||||
'billingBy',
|
'billingBy',
|
||||||
'billingUnit'])
|
'billingUnit'])
|
||||||
|
|
||||||
const normalizedProvider = provider.trim().toLowerCase()
|
const apiPricing = await API_Pricing.create({
|
||||||
const normalizedProduct = product.trim().toLowerCase()
|
provider: provider.trim().toLowerCase(),
|
||||||
|
product: product.trim().toLowerCase(),
|
||||||
const filter = { provider: normalizedProvider, product: normalizedProduct }
|
|
||||||
const update = {
|
|
||||||
provider: normalizedProvider,
|
|
||||||
product: normalizedProduct,
|
|
||||||
currency,
|
currency,
|
||||||
price,
|
price,
|
||||||
billingBy,
|
billingBy,
|
||||||
billingUnit,
|
billingUnit,
|
||||||
type
|
type
|
||||||
}
|
})
|
||||||
const options = { new: true, upsert: true }
|
|
||||||
|
|
||||||
const apiPricing = await API_Pricing.findOneAndUpdate(filter, update, options)
|
|
||||||
|
|
||||||
res.status(StatusCodes.OK).json({ apiPricing })
|
res.status(StatusCodes.OK).json({ apiPricing })
|
||||||
}
|
}
|
||||||
|
@ -46,15 +37,26 @@ const registerUsage = async (req, res) => {
|
||||||
product,
|
product,
|
||||||
usage,
|
usage,
|
||||||
callerId,
|
callerId,
|
||||||
sessionId,
|
|
||||||
companyId,
|
companyId,
|
||||||
|
quantityOfOperationAttempts,
|
||||||
|
chosenOperation,
|
||||||
|
requestLogsOpenAI,
|
||||||
|
responseErrorLogsOpenAI,
|
||||||
|
quantityOfCallsToFalconFlowAPI,
|
||||||
|
requestLogsFalconFlowAPI,
|
||||||
|
responseErrorLogsFalconFlowAPI,
|
||||||
} = req.body
|
} = req.body
|
||||||
|
|
||||||
mustContainProperties(req, [
|
mustContainProperties(req, [
|
||||||
'companyId',
|
'companyId',
|
||||||
'callerId',
|
'callerId',
|
||||||
'sessionId',
|
'quantityOfOperationAttempts',
|
||||||
|
'chosenOperation',
|
||||||
|
'requestLogsOpenAI',
|
||||||
|
// 'responseErrorLogsOpenAI',
|
||||||
|
'quantityOfCallsToFalconFlowAPI',
|
||||||
|
'requestLogsFalconFlowAPI',
|
||||||
|
// 'responseErrorLogsFalconFlowAPI',
|
||||||
'provider',
|
'provider',
|
||||||
'product',
|
'product',
|
||||||
'usage',
|
'usage',
|
||||||
|
@ -73,7 +75,13 @@ const registerUsage = async (req, res) => {
|
||||||
provider: provider.trim().toLowerCase(),
|
provider: provider.trim().toLowerCase(),
|
||||||
product: product.trim().toLowerCase(),
|
product: product.trim().toLowerCase(),
|
||||||
callerId,
|
callerId,
|
||||||
sessionId,
|
quantityOfOperationAttempts,
|
||||||
|
chosenOperation,
|
||||||
|
requestLogsOpenAI,
|
||||||
|
responseErrorLogsOpenAI,
|
||||||
|
quantityOfCallsToFalconFlowAPI,
|
||||||
|
requestLogsFalconFlowAPI,
|
||||||
|
responseErrorLogsFalconFlowAPI,
|
||||||
usage,
|
usage,
|
||||||
price,
|
price,
|
||||||
billingBy,
|
billingBy,
|
||||||
|
@ -90,142 +98,6 @@ const registerUsage = async (req, res) => {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const registerAPICall = async (req, res) => {
|
|
||||||
const {
|
|
||||||
callerId,
|
|
||||||
companyId,
|
|
||||||
sessionId,
|
|
||||||
type,
|
|
||||||
requestLogs,
|
|
||||||
responseError,
|
|
||||||
quantityOfAPICall
|
|
||||||
} = req.body
|
|
||||||
|
|
||||||
mustContainProperties(req, [
|
|
||||||
'companyId',
|
|
||||||
'callerId',
|
|
||||||
'sessionId'
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
const apiCall = await API_Call.create({
|
|
||||||
callerId,
|
|
||||||
companyId,
|
|
||||||
sessionId,
|
|
||||||
type,
|
|
||||||
requestLogs,
|
|
||||||
responseError,
|
|
||||||
quantityOfAPICall
|
|
||||||
})
|
|
||||||
|
|
||||||
res.status(StatusCodes.OK).json({ apiCall })
|
|
||||||
}
|
|
||||||
|
|
||||||
const registerOperation = async (req, res) => {
|
|
||||||
const {
|
|
||||||
callerId,
|
|
||||||
companyId,
|
|
||||||
sessionId,
|
|
||||||
operation,
|
|
||||||
quantityOfOperationAttempts: quantityOfAttempts,
|
|
||||||
} = req.body
|
|
||||||
|
|
||||||
mustContainProperties(req, [
|
|
||||||
'companyId',
|
|
||||||
'callerId',
|
|
||||||
'sessionId',
|
|
||||||
'operation',
|
|
||||||
])
|
|
||||||
|
|
||||||
const apiOperation = await API_Operation.create({
|
|
||||||
callerId,
|
|
||||||
companyId,
|
|
||||||
sessionId,
|
|
||||||
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 getUsage = async (req, res) => {
|
||||||
|
|
||||||
const { startDate, endDate, companyId, } = req.body
|
const { startDate, endDate, companyId, } = req.body
|
||||||
|
@ -252,8 +124,5 @@ const getUsage = async (req, res) => {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
setApiPricing,
|
setApiPricing,
|
||||||
registerUsage,
|
registerUsage,
|
||||||
registerAPICall,
|
getUsage
|
||||||
registerOperation,
|
|
||||||
getUsage,
|
|
||||||
registerAll
|
|
||||||
}
|
}
|
|
@ -1,36 +0,0 @@
|
||||||
const mongoose = require('../db/connect')
|
|
||||||
|
|
||||||
const { Schema } = mongoose
|
|
||||||
|
|
||||||
const apiCall = new Schema({
|
|
||||||
companyId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
callerId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
sessionId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
type: {
|
|
||||||
type: String,
|
|
||||||
enum: ['IAProvider', 'externalAPI',],
|
|
||||||
},
|
|
||||||
requestLogs: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
responseError: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
quantityOfAPICall: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
|
|
||||||
}, { timestamps: true })
|
|
||||||
|
|
||||||
const API_Call = mongoose.model('API_Call', apiCall)
|
|
||||||
|
|
||||||
module.exports = API_Call
|
|
|
@ -1,31 +0,0 @@
|
||||||
const mongoose = require('../db/connect')
|
|
||||||
|
|
||||||
const { Schema } = mongoose
|
|
||||||
|
|
||||||
const apiOperation = new Schema({
|
|
||||||
companyId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
callerId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
sessionId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
operation: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
quantityOfAttempts: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
|
|
||||||
}, { timestamps: true })
|
|
||||||
|
|
||||||
const API_Operation = mongoose.model('API_Operation', apiOperation)
|
|
||||||
|
|
||||||
module.exports = API_Operation
|
|
|
@ -11,37 +11,33 @@ const apiUsage = new Schema({
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
sessionId: {
|
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,
|
type: String,
|
||||||
required: true,
|
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: {
|
provider: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
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, registerAll} = require('../controllers/apiUsagePricing')
|
const { setApiPricing, registerUsage, getUsage} = 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-operation').post(authorization, registerOperation)
|
|
||||||
router.route('/api-register-all').post(authorization, registerAll)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
46
swagger.yaml
46
swagger.yaml
|
@ -24,82 +24,68 @@ paths:
|
||||||
properties:
|
properties:
|
||||||
companyId:
|
companyId:
|
||||||
type: string
|
type: string
|
||||||
|
description: Company identifier
|
||||||
example: "1"
|
example: "1"
|
||||||
callerId:
|
callerId:
|
||||||
type: string
|
type: string
|
||||||
|
description: Identifier of the person who made the call
|
||||||
example: "17988310949"
|
example: "17988310949"
|
||||||
quantityOfOperationAttempts:
|
quantityOfOperationAttempts:
|
||||||
type: string
|
type: string
|
||||||
|
description: Number of attempts to perform the operation made by the person who made the call
|
||||||
example: "2"
|
example: "2"
|
||||||
chosenOperation:
|
chosenOperation:
|
||||||
type: string
|
type: string
|
||||||
|
description: Operation chosen by the person who made the call
|
||||||
example: "unblokUser"
|
example: "unblokUser"
|
||||||
requestLogsOpenAI:
|
requestLogsOpenAI:
|
||||||
type: string
|
type: string
|
||||||
|
description: Response logs of requests made to openai
|
||||||
example: "{}"
|
example: "{}"
|
||||||
responseErrorLogsOpenAI:
|
responseErrorLogsOpenAI:
|
||||||
type: string
|
type: string
|
||||||
|
description: Openai error request response logs
|
||||||
example: "{}"
|
example: "{}"
|
||||||
quantityOfCallsToFalconFlowAPI:
|
quantityOfCallsToFalconFlowAPI:
|
||||||
type: string
|
type: string
|
||||||
|
description: Number of requests made to third-party api
|
||||||
example: "2"
|
example: "2"
|
||||||
requestLogsFalconFlowAPI:
|
requestLogsFalconFlowAPI:
|
||||||
type: string
|
type: string
|
||||||
|
description: Response logs of requests made to third-party api
|
||||||
example: "{}"
|
example: "{}"
|
||||||
responseErrorLogsFalconFlowAPI:
|
responseErrorLogsFalconFlowAPI:
|
||||||
type: string
|
type: string
|
||||||
|
description: Third-party api error request response logs
|
||||||
example: "{}"
|
example: "{}"
|
||||||
provider:
|
provider:
|
||||||
type: string
|
type: string
|
||||||
|
description: Identifier of the organization providing the AI solution
|
||||||
example: "openai"
|
example: "openai"
|
||||||
product:
|
product:
|
||||||
type: string
|
type: string
|
||||||
|
description: Product provided by the organization that is providing AI solution
|
||||||
example: "whisper"
|
example: "whisper"
|
||||||
usage:
|
usage:
|
||||||
type: string
|
type: integer
|
||||||
description: "Time in seconds"
|
description: "Using the API. The product Whisper should be sent in seconds"
|
||||||
example: "15"
|
example: 15
|
||||||
required:
|
required:
|
||||||
- companyId
|
- companyId
|
||||||
- callerId
|
- callerId
|
||||||
- quantityOfOperationAttempts
|
- quantityOfOperationAttempts
|
||||||
- chosenOperation
|
- chosenOperation
|
||||||
- requestLogsOpenAI
|
- requestLogsOpenAI
|
||||||
- responseErrorLogsOpenAI
|
|
||||||
- quantityOfCallsToFalconFlowAPI
|
- quantityOfCallsToFalconFlowAPI
|
||||||
- requestLogsFalconFlowAPI
|
- requestLogsFalconFlowAPI
|
||||||
- responseErrorLogsFalconFlowAPI
|
|
||||||
- provider
|
- provider
|
||||||
- product
|
- product
|
||||||
- usage
|
- usage
|
||||||
required: true
|
required: true
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: 'Successful response'
|
description: ''
|
||||||
content:
|
headers: {}
|
||||||
application/json:
|
|
||||||
example:
|
|
||||||
apiUsage:
|
|
||||||
companyId: "1"
|
|
||||||
callerId: "17988310949"
|
|
||||||
quantityOfOperationAttempts: "2"
|
|
||||||
chosenOperation: "unblokUser"
|
|
||||||
requestLogsOpenAI: "{}"
|
|
||||||
responseErrorLogsOpenAI: "{}"
|
|
||||||
quantityOfCallsToFalconFlowAPI: "2"
|
|
||||||
requestLogsFalconFlowAPI: "{}"
|
|
||||||
responseErrorLogsFalconFlowAPI: "{}"
|
|
||||||
provider: "openai"
|
|
||||||
product: "whisper"
|
|
||||||
usage: 15
|
|
||||||
price: "0.006"
|
|
||||||
billingBy: "minute"
|
|
||||||
billingUnit: 1
|
|
||||||
total_cost: "0.0015000000"
|
|
||||||
_id: "66a8df390cbb7371c4ade653"
|
|
||||||
createdAt: "2024-07-30T12:40:25.782Z"
|
|
||||||
updatedAt: "2024-07-30T12:40:25.782Z"
|
|
||||||
__v: 0
|
|
||||||
deprecated: false
|
deprecated: false
|
||||||
security:
|
security:
|
||||||
- bearer: []
|
- bearer: []
|
||||||
|
|
Loading…
Reference in New Issue