update manyyyyyyyyy

This commit is contained in:
2024-11-22 15:06:49 +07:00
parent 8eec1bed55
commit d8a3894aba
10 changed files with 272 additions and 101 deletions

View File

@@ -1,8 +1,18 @@
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 { ChatRoomType, Message, MessageContextType, MessageType } from '@prisma/client'
import {
ChatRoomType,
Message,
MessageContextType,
MessageType,
} from '@prisma/client'
import { DateTimeUtils } from '../common/utils/datetime.utils'
import { PubSubEvent } from '../common/pubsub/pubsub-event'
@@ -75,7 +85,8 @@ export class MessageSchema extends PothosSchema {
}),
messages: t.prismaField({
type: [this.message()],
description: 'Retrieve a list of messages with optional filtering, ordering, and pagination.',
description:
'Retrieve a list of messages with optional filtering, ordering, and pagination.',
args: this.builder.generator.findManyArgs('Message'),
resolve: async (query, _root, args) => {
return await this.prisma.message.findMany({
@@ -107,7 +118,15 @@ export class MessageSchema extends PothosSchema {
description: 'Send a message to a chat room.',
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Message', ['id', 'senderId', 'sender', 'sentAt', 'context', 'recipient', 'recipientId']),
type: this.builder.generator.getCreateInput('Message', [
'id',
'senderId',
'sender',
'sentAt',
'context',
'recipient',
'recipientId',
]),
description: 'The message to send.',
required: true,
}),
@@ -149,8 +168,6 @@ export class MessageSchema extends PothosSchema {
if (!args.input.content || args.input.content.trim() === '') {
throw new Error('Content cannot be empty')
}
args.input.context = messageContext
args.input.context = messageContext
const lastActivity = DateTimeUtils.now()
const message = await this.prisma.$transaction(async (tx) => {
const message = await tx.message.create({
@@ -167,10 +184,16 @@ export class MessageSchema extends PothosSchema {
})
return message
})
ctx.http.pubSub.publish(`${PubSubEvent.MESSAGE_SENT}.${message.chatRoomId}`, message)
ctx.http.pubSub.publish(
`${PubSubEvent.MESSAGE_SENT}.${message.chatRoomId}`,
message,
)
// publish to new message subscribers
userIds.forEach((userId: string) => {
ctx.http.pubSub.publish(`${PubSubEvent.NEW_MESSAGE}.${userId}`, message)
ctx.http.pubSub.publish(
`${PubSubEvent.NEW_MESSAGE}.${userId}`,
message,
)
})
return message
},
@@ -192,7 +215,9 @@ export class MessageSchema extends PothosSchema {
const {
websocket: { pubSub },
} = ctx
return pubSub.asyncIterator([`${PubSubEvent.MESSAGE_SENT}.${args.chatRoomId}`]) as unknown as AsyncIterable<Message>
return pubSub.asyncIterator([
`${PubSubEvent.MESSAGE_SENT}.${args.chatRoomId}`,
]) as unknown as AsyncIterable<Message>
},
resolve: (payload: Message) => payload,
}),