update api

This commit is contained in:
2024-11-24 15:12:05 +07:00
parent ccfe7bf1f1
commit 675460c39c
12 changed files with 234 additions and 45 deletions

View File

@@ -1,5 +1,10 @@
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'
@@ -84,6 +89,20 @@ export class DocumentSchema extends PothosSchema {
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
myDocuments: t.prismaField({
type: [this.document()],
args: this.builder.generator.findManyArgs('Document'),
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,
where: {
ownerId: ctx.http?.me?.id,
},
})
},
}),
document: t.prismaField({
type: this.document(),
args: this.builder.generator.findUniqueArgs('Document'),
@@ -115,7 +134,11 @@ 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,
@@ -134,20 +157,36 @@ export class DocumentSchema extends PothosSchema {
createDocument: t.prismaField({
type: this.document(),
args: {
data: t.arg({
type: this.builder.generator.getCreateInput('Document'),
required: true,
input: t.arg({
type: this.builder.generator.getCreateInput('Document', [
'id',
'ownerId',
'createdAt',
'updatedAt',
'collaborators',
'owner',
'fileUrl',
'previewImageUrl',
'name',
]),
required: false,
}),
},
resolve: async (query, _root, args, ctx: SchemaContext) => {
resolve: async (query, _parent, args, ctx: SchemaContext) => {
if (ctx.isSubscription) throw new Error('Not allowed')
const userId = ctx.http?.me?.id
if (!userId) throw new Error('User not found')
return await this.prisma.document.create({
...query,
data: {
...args.data,
// ownerId: userId,
...args.input,
name: args.input?.name ?? 'Untitled',
fileUrl: '',
owner: {
connect: {
id: userId,
},
},
},
})
},
@@ -168,7 +207,10 @@ 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
},
}),
@@ -210,7 +252,8 @@ export class DocumentSchema extends PothosSchema {
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')
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,