super bug

This commit is contained in:
2024-11-18 15:34:32 +07:00
parent 3430971449
commit 88941394eb
6 changed files with 186 additions and 382 deletions

View File

@@ -1,21 +1,33 @@
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 { clerkClient } from '@clerk/express'
import { UnauthorizedException } from '@nestjs/common'
import { MailService } from '../Mail/mail.service'
import { MessageSchema } from 'src/Message/message.schema'
import { Message, MessageContextType, MessageType } from '@prisma/client'
import { ChatRoom, Message, MessageContextType, MessageType, Role } from '@prisma/client'
import { PubSubEvent } from 'src/common/pubsub/pubsub-event'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { JsonValue } from '@prisma/client/runtime/library'
import { z } from 'zod'
type UserWithChatRooms = {
name: string
id: string
email: string
phoneNumber: string | null
bankBin: string | null
bankAccountNumber: string | null
packageValue: number
role: Role
avatarUrl: string | null
createdAt: Date
updatedAt: Date
customerChatRoom?: ChatRoom[]
mentorChatRoom?: ChatRoom[]
}
@Injectable()
export class UserSchema extends PothosSchema {
constructor(
@@ -148,18 +160,39 @@ export class UserSchema extends PothosSchema {
}),
me: t.prismaField({
description: 'Retrieve the current user in context.',
type: this.user(),
type: this.user() || 'Json',
args: {
includeChatRoom: t.arg({ type: 'Boolean', required: false }),
},
resolve: async (_query, _root, _args, ctx) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}
return ctx.http.me
let user = ctx.http.me as UserWithChatRooms
if (!user?.name) {
throw new Error('User not found')
}
if (_args.includeChatRoom) {
const customerChatRoom = await this.prisma.chatRoom.findMany({
where: {
OR: [{ customerId: ctx.http.me?.id }, { mentorId: ctx.http.me?.id }],
},
distinct: ['id'],
orderBy: {
lastActivity: 'desc',
},
})
user = {
...user,
customerChatRoom,
}
}
return user
},
}),
users: t.prismaField({
description:
'Retrieve a list of users with optional filtering, ordering, and pagination.',
description: 'Retrieve a list of users with optional filtering, ordering, and pagination.',
type: [this.user()],
args: this.builder.generator.findManyArgs('User'),
resolve: async (query, _root, args) => {
@@ -291,10 +324,9 @@ export class UserSchema extends PothosSchema {
}
const buffer = Buffer.concat(chunks)
const { id: userId, imageUrl } =
await clerkClient.users.updateUserProfileImage(id, {
file: new Blob([buffer]),
})
const { id: userId, imageUrl } = await clerkClient.users.updateUserProfileImage(id, {
file: new Blob([buffer]),
})
await this.prisma.user.update({
where: { id: userId },
data: {
@@ -371,14 +403,9 @@ export class UserSchema extends PothosSchema {
throw new Error(`User ${args.email} not found`)
}
// send email
await this.mailService.sendTemplateEmail(
[args.email],
'Thông báo chọn lựa quản trị viên cho người điều hành',
'ModeratorInvitation',
{
USER_NAME: user.name,
},
)
await this.mailService.sendTemplateEmail([args.email], 'Thông báo chọn lựa quản trị viên cho người điều hành', 'ModeratorInvitation', {
USER_NAME: user.name,
})
return 'Invited'
})
},
@@ -415,10 +442,7 @@ export class UserSchema extends PothosSchema {
},
})
// publish message
await ctx.http.pubSub.publish(
`${PubSubEvent.NEW_MESSAGE}.${message.recipientId}`,
message,
)
await ctx.http.pubSub.publish(`${PubSubEvent.NEW_MESSAGE}.${message.recipientId}`, message)
return message
},
}),
@@ -433,9 +457,7 @@ export class UserSchema extends PothosSchema {
const {
websocket: { pubSub },
} = ctx
return pubSub.asyncIterator([
`${PubSubEvent.NEW_MESSAGE}.${ctx.websocket.me?.id}`,
]) as unknown as AsyncIterable<Message>
return pubSub.asyncIterator([`${PubSubEvent.NEW_MESSAGE}.${ctx.websocket.me?.id}`]) as unknown as AsyncIterable<Message>
},
resolve: async (payload: Message) => payload,
}),