Enhance NestJS application with SWC builder configuration, add @nestjs/devtools-integration for development support, and refactor various components for improved readability. Update package dependencies and streamline import statements across multiple files.

This commit is contained in:
2024-11-26 04:26:55 +07:00
parent c4e302387f
commit a1ca5c62fb
12 changed files with 1646 additions and 235 deletions

View File

@@ -1,10 +1,8 @@
export enum DocumentEvent {
CREATED = 'document_created',
CHANGED = 'document_changed',
DELETED = 'document_deleted',
SAVED = 'document_saved',
PAGE_CREATED = 'document_page_created',
PAGE_DELETED = 'document_page_deleted',
DOCUMENT_CREATED = 'document_created',
ACTIVE_DOCUMENT_ID_CHANGED = 'document_active_document_id_changed',
}

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'
@@ -99,10 +94,7 @@ export class DocumentSchema extends PothosSchema {
...query,
orderBy: args.orderBy ?? undefined,
where: {
OR: [
{ ownerId: ctx.http.me.id },
{ collaborators: { some: { userId: ctx.http.me.id } } },
],
OR: [{ ownerId: ctx.http.me.id }, { collaborators: { some: { userId: ctx.http.me.id } } }],
},
})
},
@@ -138,11 +130,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,
@@ -211,10 +199,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
},
}),
@@ -275,9 +260,7 @@ export class DocumentSchema extends PothosSchema {
if (
document.ownerId !== ctx.http?.me?.id &&
!document.isPublic &&
!document.collaborators.some(
(c) => c.userId === ctx.http?.me?.id && c.writable,
)
!document.collaborators.some((c) => c.userId === ctx.http?.me?.id && c.writable)
)
throw new Error('User is not owner or collaborator of document')
return await this.prisma.document.update({
@@ -302,8 +285,7 @@ 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,
@@ -342,17 +324,16 @@ export class DocumentSchema extends PothosSchema {
if (!document.isPublic) {
if (
document.ownerId !== ctx.websocket?.me?.id &&
!document.collaborators.some(
(c) => c.userId === ctx.websocket?.me?.id && c.writable,
)
!document.collaborators.some((c) => c.userId === ctx.websocket?.me?.id && c.writable)
)
throw new Error('User is not owner or collaborator of document')
}
return pubSub.asyncIterator([
`${DocumentEvent.CHANGED}.${documentId}`,
`${DocumentEvent.CREATED}.${documentId}`,
`${DocumentEvent.DELETED}.${documentId}`,
`${DocumentEvent.SAVED}.${documentId}`,
`${DocumentEvent.PAGE_CREATED}.${documentId}`,
`${DocumentEvent.PAGE_DELETED}.${documentId}`,
`${DocumentEvent.ACTIVE_DOCUMENT_ID_CHANGED}.${documentId}`,
]) as unknown as AsyncIterable<DocumentDelta>
},

View File

@@ -3,64 +3,12 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common'
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'
import { PrismaService } from 'src/Prisma/prisma.service'
@Injectable()
export class DocumentService {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
private quill: any
constructor(
private readonly prisma: PrismaService,
private readonly minio: MinioService,
public document: JSDOM,
) {
;(async () => {
await this.loadQuill()
})()
}
private async loadQuill() {
if (typeof window === 'undefined' || typeof document === 'undefined') {
const { window } = new JSDOM('<!doctype html><html><body></body></html>')
const { navigator } = window
const { Node } = window
global.window = window
global.document = window.document
global.navigator = navigator
global.Node = Node
}
const { default: Quill } = await import('quill')
this.quill = Quill
}
// TODO: maybe never do :)
async handleOnChange(documentId: string, delta: DocumentDelta) {}
async handleOnSave(documentId: string) {}
async handleOnSync(documentId: string) {}
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>