mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-13 08:10:17 -04:00
session lib funcs
This commit is contained in:
parent
93195e4dc3
commit
c9ad7424e2
|
@ -14,8 +14,28 @@ model Session {
|
||||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
|
|
||||||
startedAt DateTime @default(now())
|
startedAt DateTime @default(now())
|
||||||
|
transferedAt DateTime?
|
||||||
endedAt DateTime?
|
endedAt DateTime?
|
||||||
|
|
||||||
callId String @unique
|
callId String @unique
|
||||||
phoneNumber String @unique
|
callerPhone String @unique
|
||||||
|
receiverPhone String? @unique
|
||||||
|
messages Message[]
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Role {
|
||||||
|
USER
|
||||||
|
BOT
|
||||||
|
REP
|
||||||
|
}
|
||||||
|
|
||||||
|
model Message {
|
||||||
|
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||||
|
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
role Role
|
||||||
|
content String
|
||||||
|
|
||||||
|
session Session @relation(fields: [sessionId], references: [id])
|
||||||
|
sessionId String @db.ObjectId
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,75 @@
|
||||||
import { db } from "./db";
|
import { Role } from "@prisma/client";
|
||||||
|
import { db } from "./db.js";
|
||||||
|
|
||||||
const session = () => {
|
export const createSession = async (callId, callerPhone) => {
|
||||||
db.session.create({})
|
return await db.session.create({
|
||||||
|
data: {
|
||||||
|
callId: callId,
|
||||||
|
callerPhone: callerPhone
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const findSessionByCallId = async (callId) => {
|
||||||
|
return await db.session.findUnique({
|
||||||
|
where: {
|
||||||
|
callId: callId
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const transferSession = async (callId, receiverPhone) => {
|
||||||
|
return await db.session.update({
|
||||||
|
where: {
|
||||||
|
callId: callId
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
transferedAt: new Date(),
|
||||||
|
receiverPhone: receiverPhone
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const endSession = async (callId) => {
|
||||||
|
return await db.session.update({
|
||||||
|
where: {
|
||||||
|
callId: callId
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
endedAt: new Date()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const addMessage = async (callId, role, content) => {
|
||||||
|
const session = await findSessionByCallId(callId)
|
||||||
|
return await db.message.create({
|
||||||
|
data: {
|
||||||
|
sessionId: session.id,
|
||||||
|
role: role,
|
||||||
|
content: content
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getMessages = async (callId) => {
|
||||||
|
return await db.message.findMany({
|
||||||
|
where: {
|
||||||
|
session: {
|
||||||
|
callId: callId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
orderBy: [
|
||||||
|
{
|
||||||
|
createdAt: 'asc'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// const main = async () => {
|
||||||
|
// // await addMessage("ofijse", Role.USER, "wefoij")
|
||||||
|
// console.log(await getMessages("ofijse"))
|
||||||
|
// }
|
||||||
|
|
||||||
|
// main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user