update commission

This commit is contained in:
2024-11-21 22:05:22 +07:00
parent 8c5d81d0aa
commit 8eec1bed55
11 changed files with 336 additions and 121 deletions

View File

@@ -1,10 +1,5 @@
import { Inject, Injectable, Logger } from '@nestjs/common'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
import { Builder, SchemaContext } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { DocumentEvent } from './document.event'
@@ -33,22 +28,44 @@ export class DocumentSchema extends PothosSchema {
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
owner: t.relation('owner'),
ownerId: t.exposeID('ownerId'),
collaborators: t.exposeStringList('collaborators'),
collaborators: t.relation('collaborators'),
isPublic: t.exposeBoolean('isPublic'),
}),
})
}
@PothosRef()
documentCollaborator() {
return this.builder.prismaObject('DocumentCollaborator', {
fields: (t) => ({
documentId: t.exposeID('documentId', { nullable: false }),
userId: t.exposeID('userId', { nullable: false }),
document: t.relation('document', { nullable: false }),
user: t.relation('user', { nullable: false }),
readable: t.exposeBoolean('readable', { nullable: false }),
writable: t.exposeBoolean('writable', { nullable: false }),
}),
})
}
@PothosRef()
documentDelta() {
return this.builder.simpleObject('DocumentDelta', {
fields: (t) => ({
documentId: t.string(),
pageIndex: t.int(),
eventType: t.string(),
documentId: t.string({
nullable: true,
}),
pageIndex: t.int({
nullable: true,
}),
delta: t.field({
type: 'Delta',
nullable: true,
}),
senderId: t.string({
nullable: true,
}),
senderId: t.string(),
}),
})
}
@@ -98,11 +115,7 @@ export class DocumentSchema extends PothosSchema {
if (ctx.isSubscription) throw new Error('Not allowed')
const userId = ctx.http?.me?.id
if (!userId) throw new Error('User not found')
const fileUrl = await this.minio.getFileUrl(
'document',
'document',
'document',
)
const fileUrl = await this.minio.getFileUrl('document', 'document', 'document')
if (!fileUrl) throw new Error('File not found')
const document = await this.prisma.document.create({
...query,
@@ -155,10 +168,7 @@ export class DocumentSchema extends PothosSchema {
delta,
senderId: ctx.http?.me?.id,
}
ctx.http.pubSub.publish(
`${DocumentEvent.CHANGED}.${args.documentId}`,
documentDelta,
)
ctx.http.pubSub.publish(`${DocumentEvent.CHANGED}.${args.documentId}`, documentDelta)
return documentDelta
},
}),
@@ -185,6 +195,32 @@ export class DocumentSchema extends PothosSchema {
return args.data
},
}),
addCollaborator: t.prismaField({
type: this.documentCollaborator(),
args: {
documentId: t.arg({ type: 'String', required: true }),
userId: t.arg({ type: 'String', required: true }),
readable: t.arg({ type: 'Boolean', required: true }),
writable: t.arg({ type: 'Boolean', 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.create({
data: {
documentId: args.documentId,
userId: args.userId,
readable: args.readable,
writable: args.writable,
},
})
},
}),
}))
this.builder.subscriptionFields((t) => ({