update some complex logic

This commit is contained in:
2024-11-24 22:25:14 +07:00
parent edcd340d0b
commit 68dabd186a
6 changed files with 141 additions and 24 deletions

View File

@@ -9,7 +9,7 @@ import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
// import { LiveKitRoomService } from 'src/LiveKit/livekit.room.service'
import { v4 as uuidv4 } from 'uuid'
import { Role } from '@prisma/client'
@Injectable()
export class CollaborationSessionSchema extends PothosSchema {
constructor(
@@ -61,16 +61,76 @@ export class CollaborationSessionSchema extends PothosSchema {
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
// get collaboration session by schedule date id, if not exist, create new one
collaborationSession: t.prismaField({
type: this.collaborationSession(),
args: this.builder.generator.findUniqueArgs('CollaborationSession'),
args: {
scheduleDateId: t.arg.string({
description: 'The ID of the schedule date.',
required: true,
}),
},
description:
'Retrieve a single collaboration session by its unique identifier.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.collaborationSession.findUnique({
...query,
where: args.where,
resolve: async (_query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
/* ---------- use case 1 : customer get collaboration session by id --------- */
if (args.scheduleDateId && ctx.http.me?.role === Role.CUSTOMER) {
if (!args.scheduleDateId)
throw new Error('Schedule date ID is required')
return await this.prisma.collaborationSession.findUniqueOrThrow({
where: {
scheduleDateId: args.scheduleDateId,
},
})
}
/* ---------- use case 2 : center mentor get collaboration session by schedule date id --------- */
if (!args.scheduleDateId)
throw new Error('Schedule date ID is required')
if (ctx.http.me?.role !== Role.CENTER_MENTOR)
throw new Error('User not allowed')
const scheduleDate = await this.prisma.scheduleDate.findUniqueOrThrow(
{
where: {
id: args.scheduleDateId,
},
},
)
if (!scheduleDate) throw new Error('Schedule date not found')
// check if order is exist in schedule date
if (!scheduleDate.orderId) throw new Error('Order not found')
const collaborationSession =
await this.prisma.collaborationSession.findUnique({
where: {
scheduleDateId: scheduleDate.id,
},
})
const order = await this.prisma.order.findUnique({
where: {
id: scheduleDate.orderId,
},
})
if (!order) throw new Error('Order not found')
if (!order.chatRoomId) throw new Error('Order chat room not found')
if (!collaborationSession) {
const chatRoom = await this.prisma.chatRoom.findUnique({
where: {
id: order.chatRoomId,
},
})
if (!chatRoom) throw new Error('Chat room not found')
// create new one
const newCollaborationSession =
await this.prisma.collaborationSession.create({
data: {
scheduleDateId: scheduleDate.id,
// assign chat room
chatRoomId: order.chatRoomId,
},
})
return newCollaborationSession // if not exist use case
}
return collaborationSession // if exist use case
},
}),
collaborationSessions: t.prismaField({