mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-08 05:40:17 -04:00
api stuff for frontend
This commit is contained in:
parent
73fc637426
commit
cd5d252cc2
73
src/api/session.js
Normal file
73
src/api/session.js
Normal 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
|
|
@ -1,6 +1,7 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import call from "./api/call.js";
|
import call from "./api/call.js";
|
||||||
import operator from "./api/operator.js"
|
import operator from "./api/operator.js"
|
||||||
|
import session from "./api/session.js"
|
||||||
import cors from "cors"
|
import cors from "cors"
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
@ -10,6 +11,7 @@ app.use(cors())
|
||||||
|
|
||||||
app.use('/call', call)
|
app.use('/call', call)
|
||||||
app.use('/operator', operator)
|
app.use('/operator', operator)
|
||||||
|
app.use('/session', session)
|
||||||
|
|
||||||
// Create an HTTP server and listen for requests on port 3000
|
// Create an HTTP server and listen for requests on port 3000
|
||||||
app.listen(8080, () => {
|
app.listen(8080, () => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import {db} from './db.js'
|
import {db} from './db.js'
|
||||||
|
|
||||||
export const authorize = async (req, res, admin=false) => {
|
export const authorize = async (req, res) => {
|
||||||
let authorization = null
|
let authorization = null
|
||||||
if(!authorization) authorization = req.headers.authorization
|
if(!authorization) authorization = req.headers.authorization
|
||||||
if(!authorization && req.cookies.auth) {
|
if(!authorization && req.cookies.auth) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user