update api
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -4,6 +4,7 @@ import Delta, { Op } from 'quill-delta'
|
||||
import { MinioService } from '../Minio/minio.service'
|
||||
import { DocumentDelta } from './document.type'
|
||||
import { JSDOM } from 'jsdom'
|
||||
import { Logger } from '@nestjs/common'
|
||||
@Injectable()
|
||||
export class DocumentService {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
@@ -32,11 +33,34 @@ export class DocumentService {
|
||||
this.quill = Quill
|
||||
}
|
||||
// TODO: maybe never do :)
|
||||
async handleOnChange(delta: DocumentDelta) {}
|
||||
async handleOnChange(documentId: string, delta: DocumentDelta) {}
|
||||
|
||||
async handleOnSave() {}
|
||||
async handleOnSave(documentId: string) {}
|
||||
|
||||
async handleOnSync() {}
|
||||
async handleOnSync(documentId: string) {}
|
||||
|
||||
async requestSync() {}
|
||||
async requestSync(documentId: string, page?: number) {
|
||||
// using pubsub to broadcast to all clients
|
||||
// this.pubSub.publish(`document:sync:${documentId}`, {
|
||||
// documentId,
|
||||
// page,
|
||||
// })
|
||||
}
|
||||
|
||||
async clientRequestSave(documentId: string) {
|
||||
// using pubsub to broadcast to all clients
|
||||
// this.pubSub.publish(`document:save:${documentId}`, {
|
||||
// documentId,
|
||||
// })
|
||||
}
|
||||
|
||||
async serverRequestSave(documentId: string) {}
|
||||
|
||||
async generatePreviewImage(documentId: string) {}
|
||||
|
||||
async allPageToDelta(documentId: string) {}
|
||||
|
||||
async toDocx(documentId: string) {}
|
||||
}
|
||||
|
||||
// epess/documents/<id>/<page>
|
||||
|
||||
Reference in New Issue
Block a user