bling bling

This commit is contained in:
2024-11-25 19:46:23 +07:00
parent c063f1a10e
commit 84a09375da
6 changed files with 152 additions and 39 deletions

View File

@@ -91,10 +91,21 @@ export class CollaborationSessionSchema extends PothosSchema {
})
/* ---------- 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
// if collaboratorsIds not include current user id, add it
if (
!collaborationSession.collaboratorsIds.includes(ctx.http.me?.id)
) {
collaborationSession.collaboratorsIds.push(ctx.http.me?.id)
await this.prisma.collaborationSession.update({
where: {
id: collaborationSession.id,
},
data: {
collaboratorsIds: collaborationSession.collaboratorsIds,
},
})
}
throw new Error('User not allowed')
return collaborationSession
}
/* ---------- use case 2 : center mentor get collaboration session by schedule date id --------- */
if (
@@ -144,6 +155,7 @@ export class CollaborationSessionSchema extends PothosSchema {
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
@@ -182,28 +194,49 @@ export class CollaborationSessionSchema extends PothosSchema {
}))
this.builder.mutationFields((t) => ({
createCollaborationSession: t.prismaField({
// update active document id
updateActiveDocumentId: t.prismaField({
type: this.collaborationSession(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('CollaborationSession'),
activeDocumentId: t.arg.string({
description: 'The ID of the active document.',
required: true,
}),
collaborationSessionId: t.arg.string({
description: 'The ID of the collaboration session.',
required: true,
}),
},
description: 'Create a new collaboration session.',
resolve: async (query, _root, args, _ctx, _info) => {
// for test only !!!
if (args.input.chatRoom.create) {
args.input.chatRoom.create.id = uuidv4()
}
// call livekit room service to create room
// this.liveKitRoomService.createServiceMeetingRoom(
// args.input.chatRoom.create?.id ?? '',
// )
return await this.prisma.collaborationSession.create({
...query,
data: args.input,
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,
)
)
throw new Error('User not allowed')
return await this.prisma.collaborationSession.update({
where: {
id: args.collaborationSessionId,
},
data: {
activeDocumentId: args.activeDocumentId,
},
})
},
}),

View File

@@ -30,33 +30,43 @@ export class CronService {
`Found ${schedules.length} schedules to update`,
'checkScheduleDateStatus',
)
// get all collaboration sessions
const collaborationSessions =
await this.prisma.collaborationSession.findMany({
where: {
collaboratorsIds: {
hasEvery: schedules.map((s) => s.id),
},
},
})
const updates = schedules
.map((schedule) => {
if (schedule.status === ScheduleDateStatus.NOT_STARTED) {
const collaborationSession = collaborationSessions.find(
(session) => session.scheduleDateId === schedule.id,
)
if (!collaborationSession) {
return {
id: schedule.id,
status: ScheduleDateStatus.MISSING_MENTOR,
}
}
if (
schedule.status === ScheduleDateStatus.IN_PROGRESS &&
schedule.participantIds.length === 1
) {
if (collaborationSession.collaboratorsIds.length === 1) {
return {
id: schedule.id,
status: ScheduleDateStatus.MISSING_CUSTOMER,
}
}
if (
schedule.status === ScheduleDateStatus.IN_PROGRESS &&
schedule.participantIds.length === 2
) {
if (collaborationSession.collaboratorsIds.length === 2) {
return {
id: schedule.id,
status: ScheduleDateStatus.COMPLETED,
}
}
return null
})
.filter((update) => update !== null)

View File

@@ -6,4 +6,5 @@ export enum DocumentEvent {
PAGE_CREATED = 'document_page_created',
PAGE_DELETED = 'document_page_deleted',
DOCUMENT_CREATED = 'document_created',
ACTIVE_DOCUMENT_ID_CHANGED = 'document_active_document_id_changed',
}

View File

@@ -92,13 +92,17 @@ export class DocumentSchema extends PothosSchema {
myDocuments: t.prismaField({
type: [this.document()],
args: this.builder.generator.findManyArgs('Document'),
resolve: async (query, _parent, _args, ctx: SchemaContext) => {
resolve: async (query, _parent, args, ctx: SchemaContext) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http?.me?.id) throw new Error('User not found')
return await this.prisma.document.findMany({
...query,
orderBy: args.orderBy ?? undefined,
where: {
ownerId: ctx.http?.me?.id,
OR: [
{ ownerId: ctx.http.me.id },
{ collaborators: { some: { userId: ctx.http.me.id } } },
],
},
})
},
@@ -215,7 +219,7 @@ export class DocumentSchema extends PothosSchema {
},
}),
updateDocument: t.field({
eventUpdateDocument: t.field({
type: this.documentDelta(),
args: {
data: t.arg({
@@ -237,6 +241,52 @@ export class DocumentSchema extends PothosSchema {
return args.data
},
}),
updateDocument: t.prismaField({
type: this.document(),
args: {
documentId: t.arg({ type: 'String', required: true }),
data: t.arg({
type: this.builder.generator.getUpdateInput('Document', [
'id',
'ownerId',
'createdAt',
'updatedAt',
'collaborationSessionId',
'collaborationSession',
'owner',
'fileUrl',
'previewImageUrl',
]),
required: true,
}),
},
resolve: async (query, _parent, args, ctx: SchemaContext) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http?.me?.id) throw new Error('User not found')
// check if user is owner or collaborator
const document = await this.prisma.document.findUnique({
where: { id: args.documentId },
include: {
collaborators: true,
},
})
if (!document) throw new Error('Document not found')
if (
document.ownerId !== ctx.http?.me?.id &&
!document.isPublic &&
!document.collaborators.some(
(c) => c.userId === ctx.http?.me?.id && c.writable,
)
)
throw new Error('User is not owner or collaborator of document')
return await this.prisma.document.update({
...query,
where: { id: args.documentId },
data: args.data,
})
},
}),
addCollaborator: t.prismaField({
type: this.documentCollaborator(),
args: {
@@ -275,16 +325,35 @@ export class DocumentSchema extends PothosSchema {
required: true,
}),
},
subscribe: (_, args, ctx: SchemaContext) => {
subscribe: async (_, args, ctx: SchemaContext) => {
if (!ctx.isSubscription) throw new Error('Not allowed')
const {
websocket: { pubSub },
} = ctx
const documentId = args.documentId
// check user permission
const document = await this.prisma.document.findUnique({
where: { id: documentId },
include: {
collaborators: true,
},
})
if (!document) throw new Error('Document not found')
if (!document.isPublic) {
if (
document.ownerId !== ctx.websocket?.me?.id &&
!document.collaborators.some(
(c) => c.userId === ctx.websocket?.me?.id && c.writable,
)
)
throw new Error('User is not owner or collaborator of document')
}
return pubSub.asyncIterator([
`${DocumentEvent.CHANGED}.${args.documentId}`,
`${DocumentEvent.CREATED}.${args.documentId}`,
`${DocumentEvent.DELETED}.${args.documentId}`,
`${DocumentEvent.SAVED}.${args.documentId}`,
`${DocumentEvent.CHANGED}.${documentId}`,
`${DocumentEvent.CREATED}.${documentId}`,
`${DocumentEvent.DELETED}.${documentId}`,
`${DocumentEvent.SAVED}.${documentId}`,
`${DocumentEvent.ACTIVE_DOCUMENT_ID_CHANGED}.${documentId}`,
]) as unknown as AsyncIterable<DocumentDelta>
},
resolve: async (payload: DocumentDelta, _args, ctx: SchemaContext) => {

File diff suppressed because one or more lines are too long