From 2736547b840d9262e7fa7637caa1b98a731ecdaa Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sat, 9 Nov 2024 15:49:06 +0700 Subject: [PATCH] fix wrong logic uploaded file --- src/Graphql/graphql.module.ts | 2 +- src/Message/message.schema.ts | 41 +++++-------------------- src/Message/message.type.ts | 1 - src/Minio/minio.service.ts | 11 +++---- src/UploadedFile/uploadedfile.schema.ts | 7 +++-- src/common/pubsub/pubsub-event.ts | 5 +++ 6 files changed, 23 insertions(+), 44 deletions(-) delete mode 100644 src/Message/message.type.ts create mode 100644 src/common/pubsub/pubsub-event.ts diff --git a/src/Graphql/graphql.module.ts b/src/Graphql/graphql.module.ts index 45bee02..f9c1c83 100644 --- a/src/Graphql/graphql.module.ts +++ b/src/Graphql/graphql.module.ts @@ -39,7 +39,7 @@ import { WorkshopModule } from '../Workshop/workshop.module' import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporganization.module' import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module' import { initContextCache } from '@pothos/core' -import { PubSub } from 'graphql-subscriptions' +import { PubSub, PubSubOptions } from 'graphql-subscriptions' @Global() @Module({ diff --git a/src/Message/message.schema.ts b/src/Message/message.schema.ts index 2fa2f6b..a5e5f9f 100644 --- a/src/Message/message.schema.ts +++ b/src/Message/message.schema.ts @@ -8,7 +8,8 @@ import { import { Builder, SchemaContext } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' import { Message, MessageContextType, MessageType } from '@prisma/client' -import { DateTimeUtils } from 'src/common/utils/datetime.utils' +import { DateTimeUtils } from '../common/utils/datetime.utils' +import { PubSubEvent } from '../common/pubsub/pubsub-event' @Injectable() export class MessageSchema extends PothosSchema { @@ -106,37 +107,6 @@ export class MessageSchema extends PothosSchema { // mutations this.builder.mutationFields((t) => ({ - testSendMessage: t.field({ - type: this.message(), - description: 'Test sending a message.', - resolve: async (_root, _args, ctx) => { - if (ctx.isSubscription) { - throw new Error('Not allowed') - } - ctx.http.pubSub.publish('MESSAGE_SENT', { - id: '1', - senderId: '1', - recipientId: '2', - chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c', - type: MessageType.TEXT, - content: 'Hello, world!', - context: MessageContextType.CHAT, - metadata: {}, - sentAt: DateTimeUtils.now().toJSDate(), - }) - return { - id: '1', - senderId: '1', - recipientId: '2', - chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c', - type: MessageType.TEXT, - content: 'Hello, world!', - context: MessageContextType.CHAT, - metadata: {}, - sentAt: DateTimeUtils.now().toJSDate(), - } - }, - }), sendMessage: t.prismaField({ type: this.message(), description: 'Send a message to a chat room.', @@ -182,7 +152,10 @@ export class MessageSchema extends PothosSchema { ...query, data: args.input, }) - ctx.http.pubSub.publish(`MESSAGE_SENT_${message.chatRoomId}`, message) + ctx.http.pubSub.publish( + `${PubSubEvent.MESSAGE_SENT}.${message.chatRoomId}`, + message, + ) return message }, }), @@ -204,7 +177,7 @@ export class MessageSchema extends PothosSchema { websocket: { pubSub }, } = ctx return pubSub.asyncIterator( - `MESSAGE_SENT_${args.chatRoomId}`, + `${PubSubEvent.MESSAGE_SENT}.${args.chatRoomId}`, ) as unknown as AsyncIterable }, resolve: (payload: Message) => payload, diff --git a/src/Message/message.type.ts b/src/Message/message.type.ts deleted file mode 100644 index 8b13789..0000000 --- a/src/Message/message.type.ts +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/Minio/minio.service.ts b/src/Minio/minio.service.ts index 999e877..3f6e0c7 100644 --- a/src/Minio/minio.service.ts +++ b/src/Minio/minio.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from '@nestjs/common' +import { Inject, Injectable, Logger } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { FileUpload } from 'graphql-upload/processRequest.js' import { Client } from 'minio' @@ -31,8 +31,8 @@ export class MinioService { return { result, filename, mimetype, actualFileName } } - async getFileUrl(id: string, category: string, presignUrl?: string) { - if (!id) { + async getFileUrl(fileName: string, category: string, presignUrl?: string) { + if (!fileName) { return null } let url = null @@ -47,14 +47,13 @@ export class MinioService { url = await this.minioClient.presignedUrl( 'GET', this.configService.get('BUCKET_NAME') ?? 'epess', - `${category}/${id}`, + `${category}/${fileName}`, 60 * 60 * 24 * 7, ) } catch (error) { - console.log(error) + Logger.error(error, 'MinioService') } } - console.log(url) return url } diff --git a/src/UploadedFile/uploadedfile.schema.ts b/src/UploadedFile/uploadedfile.schema.ts index ac85e51..acf4f76 100644 --- a/src/UploadedFile/uploadedfile.schema.ts +++ b/src/UploadedFile/uploadedfile.schema.ts @@ -46,7 +46,7 @@ export class UploadedFileSchema extends PothosSchema { description: 'The URL of the file.', resolve: async (file) => { return await this.minioService.updatePresignUrl( - file.id, + file.fileName, 'files', file.fileUrl, ) @@ -89,7 +89,10 @@ export class UploadedFileSchema extends PothosSchema { if (!file) { throw new Error('File not found') } - const fileUrl = await this.minioService.getFileUrl(file.id, 'files') + const fileUrl = await this.minioService.getFileUrl( + file.fileName, + 'files', + ) if (!fileUrl) { throw new Error('Cannot retrieve file url') } diff --git a/src/common/pubsub/pubsub-event.ts b/src/common/pubsub/pubsub-event.ts new file mode 100644 index 0000000..e8c021c --- /dev/null +++ b/src/common/pubsub/pubsub-event.ts @@ -0,0 +1,5 @@ +export enum PubSubEvent { + SERVICE_CREATED = 'SERVICE_CREATED', + MESSAGE_SENT = 'MESSAGE_SENT', + NOTIFICATION = 'NOTIFICATION', +}