database bi kiet pha banh roi

This commit is contained in:
2024-11-25 14:17:32 +07:00
parent 9515a3d9b3
commit 28d0374435
6 changed files with 55 additions and 60 deletions

View File

@@ -9,7 +9,7 @@ import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service' import { PrismaService } from '../Prisma/prisma.service'
// import { LiveKitRoomService } from 'src/LiveKit/livekit.room.service' // import { LiveKitRoomService } from 'src/LiveKit/livekit.room.service'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { Role } from '@prisma/client' import { CollaborationSession, Role } from '@prisma/client'
import { DateTimeUtils } from 'src/common/utils/datetime.utils' import { DateTimeUtils } from 'src/common/utils/datetime.utils'
@Injectable() @Injectable()
export class CollaborationSessionSchema extends PothosSchema { export class CollaborationSessionSchema extends PothosSchema {
@@ -75,35 +75,31 @@ export class CollaborationSessionSchema extends PothosSchema {
'Retrieve a single collaboration session by its unique identifier.', 'Retrieve a single collaboration session by its unique identifier.',
resolve: async (_query, _root, args, ctx, _info) => { resolve: async (_query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Not allowed') 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.findUnique({ const scheduleDate = await this.prisma.scheduleDate.findUnique({
where: { where: {
id: args.scheduleDateId, id: args.scheduleDateId,
}, },
}) })
if (!scheduleDate) throw new Error('Schedule date not found') if (!scheduleDate) throw new Error('Schedule date not found')
// check if order is exist in schedule date let collaborationSession: CollaborationSession | null = null
if (!scheduleDate.orderId) throw new Error('Order not found') collaborationSession =
const collaborationSession = await this.prisma.collaborationSession.findUniqueOrThrow({
await this.prisma.collaborationSession.findUnique({
where: { where: {
scheduleDateId: scheduleDate.id, scheduleDateId: scheduleDate.id,
}, },
}) })
/* ---------- use case 1 : customer get collaboration session by id --------- */
if (ctx.http.me?.role === Role.CUSTOMER && collaborationSession) {
if (scheduleDate.participantIds.includes(ctx.http.me?.id)) {
return collaborationSession
}
throw new Error('User not allowed')
}
/* ---------- use case 2 : center mentor get collaboration session by schedule date id --------- */
if (ctx.http.me?.role !== Role.CENTER_MENTOR)
throw new Error('Mentor does not created collaboration session yet')
// check if order is exist in schedule date
if (!scheduleDate.orderId) throw new Error('Order not found')
const order = await this.prisma.order.findUnique({ const order = await this.prisma.order.findUnique({
where: { where: {
id: scheduleDate.orderId, id: scheduleDate.orderId,

View File

@@ -85,6 +85,9 @@ export class PayosService {
status: ScheduleStatus.IN_PROGRESS, status: ScheduleStatus.IN_PROGRESS,
}, },
}) })
if (!schedule) {
throw new Error('Schedule not found')
}
// get mentor id from managed service // get mentor id from managed service
const managedService = await tx.managedService.findUniqueOrThrow({ const managedService = await tx.managedService.findUniqueOrThrow({
where: { id: schedule?.managedServiceId }, where: { id: schedule?.managedServiceId },
@@ -111,13 +114,18 @@ export class PayosService {
chatRoomId: chatRoom.id, chatRoomId: chatRoom.id,
}, },
}) })
// update orderId for schedule dates // add participants to schedule dates where customer and mentor are
const customerId = order.userId
// update orderId
await tx.scheduleDate.updateMany({ await tx.scheduleDate.updateMany({
where: { scheduleId: schedule?.id }, where: { scheduleId: schedule.id },
data: { data: {
orderId: order.id, orderId: order.id,
participantIds: [customerId, mentorId],
}, },
}) })
/* --------------- send first message from mentor to customer --------------- */ /* --------------- send first message from mentor to customer --------------- */
await tx.message.create({ await tx.message.create({
data: { data: {

View File

@@ -5,6 +5,7 @@ export interface ScheduleDateInput {
dayOfWeek: number dayOfWeek: number
slot: number slot: number
serviceId: string serviceId: string
participants: string[]
orderId: string | null orderId: string | null
} }

View File

@@ -146,6 +146,20 @@ export class ScheduleSchema extends PothosSchema {
orderId: t.exposeID('orderId', { orderId: t.exposeID('orderId', {
nullable: true, nullable: true,
}), }),
participantIds: t.exposeStringList('participantIds', {
nullable: false,
}),
maxParticipants: t.exposeInt('maxParticipants', {
nullable: false,
}),
lateStart: t.expose('lateStart', {
type: 'DateTime',
nullable: true,
}),
collaborationSession: t.relation('CollaborationSession', {
description: 'The collaboration session of the schedule date.',
nullable: true,
}),
schedule: t.relation('schedule', { schedule: t.relation('schedule', {
description: 'The schedule the schedule date belongs to.', description: 'The schedule the schedule date belongs to.',
}), }),

View File

@@ -68,7 +68,7 @@ export class ScheduleService {
const scheduleEnd = DateTime.fromJSDate(schedule.scheduleEnd) const scheduleEnd = DateTime.fromJSDate(schedule.scheduleEnd)
const slotDuration = parseInt(config.slotDuration) const slotDuration = parseInt(config.slotDuration)
const slotBreakDuration = parseInt(config.slotBreakDuration) const slotBreakDuration = parseInt(config.slotBreakDuration)
// add participants to schedule dates
const scheduleDates: ScheduleDateInput[] = [] const scheduleDates: ScheduleDateInput[] = []
// loop each day from scheduleStart to scheduleEnd // loop each day from scheduleStart to scheduleEnd
@@ -96,6 +96,7 @@ export class ScheduleService {
end: endTime.toISO() ?? '', end: endTime.toISO() ?? '',
dayOfWeek: date.weekday, dayOfWeek: date.weekday,
slot: slot, slot: slot,
participants: [],
serviceId: schedule.managedServiceId, serviceId: schedule.managedServiceId,
orderId: schedule.orderId, orderId: schedule.orderId,
}) })

File diff suppressed because one or more lines are too long