Enhance DocumentSchema and MinioService to support document folder creation. Update document creation logic to include folder setup in Minio after document creation. Refactor createDocumentFolder method to initialize a default page in the document folder. This improves document management and organization within the Minio storage system.

This commit is contained in:
2024-11-28 20:09:37 +07:00
parent e546c348cc
commit abb7e38df3
3 changed files with 29 additions and 42 deletions

View File

@@ -198,7 +198,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')
return await this.prisma.document.create({
const document = await this.prisma.document.create({
...query,
data: {
...args.input,
@@ -211,6 +211,9 @@ export class DocumentSchema extends PothosSchema {
},
},
})
// create document in minio
await this.minio.createDocumentFolder(document.id)
return document
},
}),

View File

@@ -81,9 +81,15 @@ export class MinioService {
async deleteFile(id: string, category: string) {
return await this.minioClient.removeObject(this.configService.get('BUCKET_NAME') ?? 'epess', `${category}/${id}`)
}
// create a folder for a document pattern epess/documents/<id>/<page>
// create a folder for a document pattern epess/documents/<id>/<page> and add page 0 to default
async createDocumentFolder(id: string) {
return await this.minioClient.putObject(this.configService.get('BUCKET_NAME') ?? 'epess', `documents/${id}`, '')
const emptyDelta = JSON.stringify(new Delta().insert('\n'))
const result = await this.minioClient.putObject(
this.configService.get('BUCKET_NAME') ?? 'epess',
`documents/${id}/0`,
emptyDelta,
)
return result
}
async upsertDocumentPage(id: string, page: number, delta: Delta) {

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 } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { ScheduleDateStatus, ScheduleStatus } from '@prisma/client'
@@ -236,8 +231,7 @@ export class ScheduleSchema extends PothosSchema {
schedules: t.prismaField({
type: [this.schedule()],
args: this.builder.generator.findManyArgs('Schedule'),
description:
'Retrieve a list of schedules with optional filtering, ordering, and pagination.',
description: 'Retrieve a list of schedules with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.schedule.findMany({
...query,
@@ -259,9 +253,7 @@ export class ScheduleSchema extends PothosSchema {
}),
},
resolve: async (_parent, args, _context, _info) => {
return await this.scheduleService.createSchedulePreviewForCenter(
args.scheduleConfigInput,
)
return await this.scheduleService.createSchedulePreviewForCenter(args.scheduleConfigInput)
},
}),
@@ -276,17 +268,13 @@ export class ScheduleSchema extends PothosSchema {
resolve: async (_parent, args, _context, _info) => {
// if no scheduleConfig, use default config
if (!args.scheduleConfig) {
args.scheduleConfig = (
await this.appConfigService.getVisibleConfigs()
).reduce((acc, curr) => {
args.scheduleConfig = (await this.appConfigService.getVisibleConfigs()).reduce((acc, curr) => {
// @ts-ignore
acc[curr.key] = curr.value
return acc
}, {} as ScheduleConfigType)
}
return await this.scheduleService.createSchedulePreviewForSingleDay(
args.scheduleConfig,
)
return await this.scheduleService.createSchedulePreviewForSingleDay(args.scheduleConfig)
},
}),
}))
@@ -303,13 +291,7 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
description: 'Create a new schedule.',
args: {
schedule: t.arg({
type: this.builder.generator.getCreateInput('Schedule', [
'id',
'status',
'customerId',
'orderId',
'dates',
]),
type: this.builder.generator.getCreateInput('Schedule', ['id', 'status', 'customerId', 'orderId', 'dates']),
required: true,
}),
},
@@ -319,20 +301,17 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
}
Logger.log('args.schedule', args.schedule)
// generate preview and check if there is any overlapping with other schedules date in same service
const previewSchedule =
await this.scheduleService.createSchedulePreviewForCenter({
startDate: args.schedule.scheduleStart as string,
endDate: args.schedule.scheduleEnd as string,
slots: args.schedule.slots as number[],
days: args.schedule.daysOfWeek as number[],
})
const existingScheduleDates = await this.prisma.scheduleDate.findMany(
{
where: {
serviceId: args.schedule.managedService.connect?.id,
},
const previewSchedule = await this.scheduleService.createSchedulePreviewForCenter({
startDate: args.schedule.scheduleStart as string,
endDate: args.schedule.scheduleEnd as string,
slots: args.schedule.slots as number[],
days: args.schedule.daysOfWeek as number[],
})
const existingScheduleDates = await this.prisma.scheduleDate.findMany({
where: {
serviceId: args.schedule.managedService.connect?.id,
},
)
})
// check if there is any overlapping with existing schedule dates in same service using DateTimeUtils
const isOverlapping = DateTimeUtils.isOverlaps(
previewSchedule.slots.map((slot) => ({
@@ -353,8 +332,7 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
data: args.schedule,
})
// generate schedule dates based on data and config
const scheduleDates =
await this.scheduleService.generateScheduleDates(schedule)
const scheduleDates = await this.scheduleService.generateScheduleDates(schedule)
// update schedule with schedule dates
return await this.prisma.schedule.update({
...query,