toi bi ngu

This commit is contained in:
2024-11-17 20:27:33 +07:00
parent bb0eed1851
commit 3430971449
14 changed files with 675 additions and 82 deletions

View File

@@ -11,11 +11,13 @@ import { DocumentEvent } from './document.event'
import { Document } from '@prisma/client'
import { DocumentDelta } from './document.type'
import Delta from 'quill-delta'
import { MinioService } from 'src/Minio/minio.service'
@Injectable()
export class DocumentSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minio: MinioService,
) {
super()
}
@@ -46,6 +48,7 @@ export class DocumentSchema extends PothosSchema {
delta: t.field({
type: 'Delta',
}),
senderId: t.string(),
}),
})
}
@@ -88,6 +91,30 @@ export class DocumentSchema extends PothosSchema {
})
},
}),
newDocument: t.field({
type: this.document(),
args: {},
resolve: async (query, _args, ctx, _info) => {
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',
)
if (!fileUrl) throw new Error('File not found')
const document = await this.prisma.document.create({
...query,
data: {
name: 'Untitled',
fileUrl,
ownerId: userId,
},
})
return document
},
}),
}))
this.builder.mutationFields((t) => ({
@@ -99,10 +126,16 @@ export class DocumentSchema extends PothosSchema {
required: true,
}),
},
resolve: async (query, _root, args) => {
resolve: async (query, _root, 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,
data: {
...args.data,
// ownerId: userId,
},
})
},
}),
@@ -120,6 +153,7 @@ export class DocumentSchema extends PothosSchema {
documentId: args.documentId,
pageIndex: args.pageIndex,
delta,
senderId: ctx.http?.me?.id,
}
ctx.http.pubSub.publish(
`${DocumentEvent.CHANGED}.${args.documentId}`,
@@ -142,10 +176,12 @@ export class DocumentSchema extends PothosSchema {
const {
http: { pubSub },
} = ctx
pubSub.publish(
`${DocumentEvent.CHANGED}.${args.data.documentId}`,
args.data,
)
const senderId = ctx.http?.me?.id
if (!senderId) throw new Error('User not found')
pubSub.publish(`${DocumentEvent.CHANGED}.${args.data.documentId}`, {
...args.data,
senderId,
})
return args.data
},
}),
@@ -172,7 +208,11 @@ export class DocumentSchema extends PothosSchema {
`${DocumentEvent.SAVED}.${args.documentId}`,
]) as unknown as AsyncIterable<DocumentDelta>
},
resolve: async (payload: DocumentDelta) => payload,
resolve: async (payload: DocumentDelta, _args, ctx: SchemaContext) => {
if (!ctx.isSubscription) throw new Error('Not allowed')
if (payload.senderId === ctx.websocket?.me?.id) return
return payload
},
}),
}))
}