From c94f47f28cd1d08e2e4fe50f6cd4f66da84145de Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 2 Dec 2024 22:46:46 +0700 Subject: [PATCH] feat: add removeCollaborator mutation to DocumentSchema - Introduced a new mutation to allow document owners to remove collaborators from a document. - Implemented authorization checks to ensure only the document owner can perform this action. - Enhanced error handling for cases where the document is not found or the user is not the owner. --- src/Document/document.schema.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Document/document.schema.ts b/src/Document/document.schema.ts index b781d68..7a20655 100644 --- a/src/Document/document.schema.ts +++ b/src/Document/document.schema.ts @@ -333,6 +333,25 @@ export class DocumentSchema extends PothosSchema { }) }, }), + removeCollaborator: t.prismaField({ + type: this.documentCollaborator(), + args: { + documentId: t.arg({ type: 'String', required: true }), + userId: t.arg({ type: 'String', required: true }), + }, + resolve: async (_, __, args, ctx: SchemaContext) => { + if (ctx.isSubscription) throw new Error('Not allowed') + // check if ctx user is owner of document + const document = await this.prisma.document.findUnique({ + where: { id: args.documentId }, + }) + if (!document) throw new Error('Document not found') + if (document.ownerId !== ctx.http?.me?.id) throw new Error('User is not owner of document') + return await this.prisma.documentCollaborator.delete({ + where: { documentId_userId: { documentId: args.documentId, userId: args.userId } }, + }) + }, + }), })) this.builder.subscriptionFields((t) => ({