push code chua nhi?

This commit is contained in:
2024-11-11 22:04:39 +07:00
parent 22bc231409
commit ff45e69efd
7 changed files with 115 additions and 16 deletions

View File

@@ -11,8 +11,11 @@ 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 } from '@prisma/client'
import { Message, MessageContextType, MessageType } 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'
@Injectable()
export class UserSchema extends PothosSchema {
constructor(
@@ -50,16 +53,6 @@ export class UserSchema extends PothosSchema {
}),
packageValue: t.exposeFloat('packageValue', {
description: 'The package value of the user.',
validate: (value) => {
if (typeof value !== 'number') {
return false
}
// value can be only 0 to 1
if (value < 0 || value > 1) {
return false
}
return true
},
nullable: true,
}),
role: t.exposeString('role', {
@@ -390,6 +383,45 @@ export class UserSchema extends PothosSchema {
})
},
}),
// send test notification
sendTestNotification: t.field({
type: this.messageSchema.message(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Message'),
required: true,
}),
},
resolve: async (_, args, ctx) => {
if (ctx.isSubscription) {
throw new Error('Not allowed')
}
const me = ctx.http.me
if (!me) {
throw new Error('User not found')
}
// create message
const message = await this.prisma.message.create({
data: {
type: args.input.type,
content: args.input.content,
senderId: me.id,
recipientId: args.input.recipient?.connect?.id ?? null,
chatRoomId: args.input.chatRoom?.connect?.id ?? null,
sentAt: DateTimeUtils.nowAsJSDate(),
context: args.input.context ?? undefined,
metadata: args.input.metadata ?? undefined,
},
})
// publish message
await ctx.http.pubSub.publish(
`${PubSubEvent.NEW_MESSAGE}.${message.recipientId}`,
message,
)
return message
},
}),
}))
// Subscription section
@@ -401,6 +433,7 @@ export class UserSchema extends PothosSchema {
const {
websocket: { pubSub },
} = ctx
Logger.log(ctx.websocket.me?.id, 'Me ID')
return pubSub.asyncIterator([
`${PubSubEvent.NEW_MESSAGE}.${ctx.websocket.me?.id}`,
]) as unknown as AsyncIterable<Message>