Refactor CollaborationSession and Document schemas to improve code clarity and functionality. Enhance error handling in collaboration session retrieval and update logic. Introduce new request sync events in Document schema and update Minio service for document page management. Adjust cron job to use current date for future schedule date checks. Update user schema for better error handling and improve service descriptions in order schema. Remove global decorators from Workshop modules for better module encapsulation.

This commit is contained in:
2024-11-27 06:48:49 +07:00
parent 51bafcfe29
commit 77f44a891f
11 changed files with 176 additions and 113 deletions

View File

@@ -1,10 +1,5 @@
import { Inject, Injectable } from '@nestjs/common'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos'
import { Inject, Injectable, Logger } from '@nestjs/common'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
// import { LiveKitRoomService } from 'src/LiveKit/livekit.room.service'
@@ -71,8 +66,7 @@ export class CollaborationSessionSchema extends PothosSchema {
required: true,
}),
},
description:
'Retrieve a single collaboration session by its unique identifier.',
description: 'Retrieve a single collaboration session by its unique identifier.',
resolve: async (_query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Cannot get your info')
@@ -83,18 +77,15 @@ export class CollaborationSessionSchema extends PothosSchema {
})
if (!scheduleDate) throw new Error('Schedule date not found')
let collaborationSession: CollaborationSession | null = null
collaborationSession =
await this.prisma.collaborationSession.findUnique({
where: {
scheduleDateId: scheduleDate.id,
},
})
collaborationSession = await this.prisma.collaborationSession.findUnique({
where: {
scheduleDateId: scheduleDate.id,
},
})
/* ---------- use case 1 : customer get collaboration session by id --------- */
if (ctx.http.me?.role === Role.CUSTOMER && collaborationSession) {
// if collaboratorsIds not include current user id, add it
if (
!collaborationSession.collaboratorsIds.includes(ctx.http.me?.id)
) {
if (!collaborationSession.collaboratorsIds.includes(ctx.http.me?.id)) {
collaborationSession.collaboratorsIds.push(ctx.http.me?.id)
await this.prisma.collaborationSession.update({
where: {
@@ -108,19 +99,12 @@ export class CollaborationSessionSchema extends PothosSchema {
return collaborationSession
}
/* ---------- use case 2 : center mentor get collaboration session by schedule date id --------- */
if (
ctx.http.me.role !== Role.CENTER_MENTOR &&
ctx.http.me.role !== Role.CENTER_OWNER
) {
if (!collaborationSession)
throw new Error(
'Mentor does not created collaboration session yet',
)
if (ctx.http.me.role !== Role.CENTER_MENTOR && ctx.http.me.role !== Role.CENTER_OWNER) {
if (!collaborationSession) throw new Error('Mentor does not created collaboration session yet')
throw new Error('User not allowed')
}
// check if user is participant
if (!scheduleDate.participantIds.includes(ctx.http.me.id))
throw new Error('User not allowed')
if (!scheduleDate.participantIds.includes(ctx.http.me.id)) throw new Error('User not allowed')
// check if order is exist in schedule date
if (!scheduleDate.orderId) throw new Error('Order not found')
const order = await this.prisma.order.findUnique({
@@ -149,15 +133,14 @@ export class CollaborationSessionSchema extends PothosSchema {
})
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: chatRoom.id,
collaboratorsIds: [ctx.http.me.id],
},
})
const newCollaborationSession = await this.prisma.collaborationSession.create({
data: {
scheduleDateId: scheduleDate.id,
// assign chat room
chatRoomId: chatRoom.id,
collaboratorsIds: [ctx.http.me.id],
},
})
// case after start time and before end time, mark as late
if (now > startTime && now < endTime) {
// mark as late
@@ -179,8 +162,7 @@ export class CollaborationSessionSchema extends PothosSchema {
collaborationSessions: t.prismaField({
type: [this.collaborationSession()],
args: this.builder.generator.findManyArgs('CollaborationSession'),
description:
'Retrieve a list of collaboration sessions with optional filtering, ordering, and pagination.',
description: 'Retrieve a list of collaboration sessions with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.collaborationSession.findMany({
...query,
@@ -207,30 +189,23 @@ export class CollaborationSessionSchema extends PothosSchema {
required: true,
}),
},
description:
'Update the active document ID for a collaboration session.',
description: 'Update the active document ID for a collaboration session.',
resolve: async (_query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http.me) throw new Error('Cannot get your info')
// check permission
const collaborationSession =
await this.prisma.collaborationSession.findUnique({
where: {
id: args.collaborationSessionId,
},
include: {
scheduleDate: true,
},
})
if (!collaborationSession)
throw new Error('Collaboration session not found')
if (
!collaborationSession.scheduleDate.participantIds.includes(
ctx.http.me.id,
)
)
const collaborationSession = await this.prisma.collaborationSession.findUnique({
where: {
id: args.collaborationSessionId,
},
include: {
scheduleDate: true,
},
})
if (!collaborationSession) throw new Error('Collaboration session not found')
if (!collaborationSession.scheduleDate.participantIds.includes(ctx.http.me.id))
throw new Error('User not allowed')
return await this.prisma.collaborationSession.update({
const updatedCollaborationSession = await this.prisma.collaborationSession.update({
where: {
id: args.collaborationSessionId,
},
@@ -238,8 +213,38 @@ export class CollaborationSessionSchema extends PothosSchema {
activeDocumentId: args.activeDocumentId,
},
})
ctx.http.pubSub.publish(`collaborationSessionUpdated:${collaborationSession.id}`, updatedCollaborationSession)
Logger.log(`Collaboration session updated: ${updatedCollaborationSession.id}`, 'updateActiveDocumentId')
return updatedCollaborationSession
},
}),
}))
this.builder.subscriptionFields((t) => ({
collaborationSessionUpdated: t.field({
type: this.collaborationSession(),
description: 'Subscribe to collaboration session updates.',
args: {
collaborationSessionId: t.arg.string({
description: 'The ID of the collaboration session.',
required: true,
}),
},
subscribe: async (_parent, args, ctx) => {
if (!ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.websocket.me) throw new Error('Cannot get your info')
const collaborationSession = await this.prisma.collaborationSession.findUnique({
where: {
id: args.collaborationSessionId,
},
})
if (!collaborationSession) throw new Error('Collaboration session not found')
return ctx.websocket.pubSub.asyncIterator(
`collaborationSessionUpdated:${collaborationSession.id}`,
) as unknown as AsyncIterable<CollaborationSession>
},
resolve: async (payload: CollaborationSession) => payload,
}),
}))
}
}