api stuff for frontend

This commit is contained in:
Claeb101 2023-03-05 05:12:04 -05:00
parent 73fc637426
commit cd5d252cc2
3 changed files with 76 additions and 1 deletions

73
src/api/session.js Normal file
View File

@ -0,0 +1,73 @@
import * as dotenv from "dotenv";
import { Router } from "express";
import { authorize } from "../lib/authorize.js";
import { summarize } from "../lib/chat.js";
import { db } from "../lib/db.js";
import { getMessages, getMessagesBySession } from "../lib/session.js";
dotenv.config();
const app = Router();
app.get('/', async (req, res) => {
const b = req.query.open
let sessions = [];
if (b == "true") {
sessions = await db.session.findMany({
where: {
operator: undefined
}
})
} else if(b == "false") {
sessions = await db.session.findMany({
where: {
none: {
operator: undefined
}
}
})
} else {
sessions = await db.session.findMany()
}
return res.status(200).json({
sessions
})
})
app.get("/message", async (req, res) => {
const messages = await getMessagesBySession(req.body.sessionId)
return res.status(200).json({
messages
})
})
app.get("/summary", async (req, res) => {
const summary = await summarize(req.body.sessionId)
return res.status(200).json({
summary
})
})
app.get('/transfer', async (req, res) => {
const {authorized, webSession} = authorize(req, res)
if(!authorized) return res.status(401).send(null)
const operator = await db.operator.findUnique({
where: { id: webSession.operatorId}
})
const session = await db.session.update({
where: {
id: req.body.sessionId
},
data: {
operatorPhone: operator.phoneNumber
}
})
return res.status(200).json({
session,
operator
})
})
export default app

View File

@ -1,6 +1,7 @@
import express from "express";
import call from "./api/call.js";
import operator from "./api/operator.js"
import session from "./api/session.js"
import cors from "cors"
const app = express();
@ -10,6 +11,7 @@ app.use(cors())
app.use('/call', call)
app.use('/operator', operator)
app.use('/session', session)
// Create an HTTP server and listen for requests on port 3000
app.listen(8080, () => {

View File

@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import {db} from './db.js'
export const authorize = async (req, res, admin=false) => {
export const authorize = async (req, res) => {
let authorization = null
if(!authorization) authorization = req.headers.authorization
if(!authorization && req.cookies.auth) {