Răsfoiți Sursa

✨ feat(chat): 完善聊天消息发送者信息关联

- 修改senderId类型从string为number,支持整数ID
- 添加sender关联字段,关联UserEntity
- 在API响应中包含发送者用户信息(不含密码)
- 更新CRUD路由配置,添加sender关联查询

🐛 fix(chat): 修复发送者ID类型不匹配问题

- 修复senderId类型定义错误,从string改为number
- 添加coerce.number()确保输入正确转换为数字类型
- 更新示例值从字符串ID改为数字ID
yourname 6 luni în urmă
părinte
comite
273159bf15

+ 1 - 1
src/server/api/chat-messages/index.ts

@@ -17,7 +17,7 @@ const crudRoutes = createCrudRoutes({
   getSchema: ChatMessageSchema,
   listSchema: ChatMessageSchema,
   searchFields: ['content', 'senderName'],
-  relations: ['file'],
+  relations: ['file', 'sender'],
   middleware: [authMiddleware],
   userTracking: {
     createdByField: 'createdBy',

+ 11 - 3
src/server/modules/chat/chat-message.entity.ts

@@ -1,5 +1,6 @@
 import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
 import { File } from '@/server/modules/files/file.entity';
+import { UserEntity } from '@/server/modules/users/user.entity';
 
 @Entity('chat_messages')
 export class ChatMessage {
@@ -38,12 +39,19 @@ export class ChatMessage {
 
   @Column({ 
     name: 'sender_id', 
-    type: 'varchar', 
-    length: 255, 
+    type: 'int', 
+    unsigned: true, 
     nullable: true, 
     comment: '发送者ID' 
   })
-  senderId!: string | null;
+  senderId!: number | null;
+
+  @ManyToOne(() => UserEntity, { nullable: true })
+  @JoinColumn({ 
+    name: 'sender_id',
+    referencedColumnName: 'id'
+  })
+  sender!: UserEntity | null;
 
   @Column({ 
     name: 'sender_name', 

+ 10 - 6
src/server/modules/chat/chat-message.schema.ts

@@ -1,4 +1,5 @@
 import { z } from '@hono/zod-openapi';
+import { UserSchema } from '@/server/modules/users/user.schema';
 
 // 基础消息Schema
 export const ChatMessageSchema = z.object({
@@ -18,10 +19,13 @@ export const ChatMessageSchema = z.object({
     example: '这是一条文本消息',
     description: '消息内容'
   }),
-  senderId: z.string().nullable().openapi({
-    example: 'user_123',
+  senderId: z.number().int().positive().nullable().openapi({
+    example: 1,
     description: '发送者ID'
   }),
+  sender: UserSchema.omit({ password: true }).nullable().optional().openapi({
+    description: '发送者用户信息'
+  }),
   senderName: z.string().nullable().openapi({
     example: '张三',
     description: '发送者名称'
@@ -75,8 +79,8 @@ export const CreateChatMessageDto = z.object({
     example: '这是一条文本消息',
     description: '消息内容'
   }),
-  senderId: z.string().nullable().optional().openapi({
-    example: 'user_123',
+  senderId: z.coerce.number().int().positive().nullable().optional().openapi({
+    example: 1,
     description: '发送者ID'
   }),
   senderName: z.string().nullable().optional().openapi({
@@ -107,8 +111,8 @@ export const UpdateChatMessageDto = z.object({
     example: '这是一条文本消息',
     description: '消息内容'
   }),
-  senderId: z.string().nullable().optional().openapi({
-    example: 'user_123',
+  senderId: z.coerce.number().int().positive().nullable().optional().openapi({
+    example: 1,
     description: '发送者ID'
   }),
   senderName: z.string().nullable().optional().openapi({