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

@@ -12,7 +12,7 @@ import AuthzPlugin from '@pothos/plugin-authz'
import ErrorsPlugin from '@pothos/plugin-errors'
import type { FileUpload } from 'graphql-upload/processRequest.js'
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js'
import { Injectable } from '@nestjs/common'
import { Injectable, Logger } from '@nestjs/common'
import { PrismaCrudGenerator } from './graphql.generator'
import type PrismaTypes from '../types/pothos.generated'
import PrismaUtils from '@pothos/plugin-prisma-utils'
@@ -107,6 +107,7 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
// optionally customize how errors are formatted
validationError: (zodError, _args, _context, _info) => {
// the default behavior is to just throw the zod error directly
Logger.error(zodError.message, 'Zod Error')
return zodError
},
},

View File

@@ -56,6 +56,7 @@ export class MessageSchema extends PothosSchema {
}),
metadata: t.expose('metadata', {
type: 'Json',
nullable: true,
description: 'The metadata of the message.',
}),
sender: t.relation('sender', {

View File

@@ -0,0 +1,10 @@
import { Inject, Injectable } from '@nestjs/common'
import { PothosSchema } from '@smatch-corp/nestjs-pothos'
import { PrismaService } from 'src/Prisma/prisma.service'
@Injectable()
export class RealtimeSchema extends PothosSchema {
constructor(private readonly prisma: PrismaService) {
super()
}
}

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'
// @ts-ignore
import * as Y from 'yjs'
// import Quil, { Delta } from 'quill'
@Injectable()
export class RealtimeService {
yjs = new Y.Doc()
// constructor(private readonly quil: Quil) {}
}

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>