Преглед на файлове

重构 Sender 相关功能为 TgUser,添加 TgUser 控制器、服务和路由,更新相关 DTO,增强代码一致性和可维护性。

wuyi преди 3 седмици
родител
ревизия
ce1c9a2332

+ 2 - 2
src/app.ts

@@ -18,7 +18,7 @@ import messagesRoutes from './routes/messages.routes'
 import tgMsgSendRoutes from './routes/tg-msg-send.routes'
 import taskRoutes from './routes/task.routes'
 import testRoutes from './routes/test.routes'
-import senderRoutes from './routes/sender.routes'
+import tgUserRoutes from './routes/tg-user.routes'
 import chatGroupRoutes from './routes/chat-group.routes'
 import tgGroupRoutes from './routes/tg-group.routes'
 
@@ -93,7 +93,7 @@ export const createApp = async () => {
   app.register(messagesRoutes, { prefix: '/api/messages' })
   app.register(tgMsgSendRoutes, { prefix: '/api/msg' })
   app.register(taskRoutes, { prefix: '/api/tasks' })
-  app.register(senderRoutes, { prefix: '/api/senders' })
+  app.register(tgUserRoutes, { prefix: '/api/tg-users' })
   app.register(chatGroupRoutes, { prefix: '/api/chat-groups' })
   app.register(tgGroupRoutes, { prefix: '/api/tg-groups' })
   app.register(testRoutes, { prefix: '/api/test' })

+ 1 - 1
src/controllers/fish.controller.ts

@@ -1,6 +1,6 @@
 import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
 import { FishService } from '../services/fish.service'
-import { SenderService } from '../services/sender.service'
+import { SenderService } from '../services/tg-user.service'
 import { ResultEnum } from '../entities/fish.entity'
 import { UserRole } from '../entities/user.entity'
 import {

+ 16 - 16
src/controllers/sender.controller.ts → src/controllers/tg-user.controller.ts

@@ -1,14 +1,14 @@
 import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
 import fs from 'fs/promises'
 import path from 'path'
-import { SenderService } from '../services/sender.service'
-import { CreateSenderBody, UpdateSenderBody, ListSenderQuery } from '../dto/sender.dto'
+import { TgUserService } from '../services/tg-user.service'
+import { CreateSenderBody, UpdateSenderBody, ListSenderQuery } from '../dto/tg-user.dto'
 
-export class SenderController {
-  private senderService: SenderService
+export class TgUserController {
+  private tgUserService: TgUserService
 
   constructor(app: FastifyInstance) {
-    this.senderService = new SenderService(app)
+    this.tgUserService = new TgUserService(app)
   }
 
   async importFromJson(_: FastifyRequest, reply: FastifyReply) {
@@ -51,7 +51,7 @@ export class SenderController {
         }
       }
 
-      const result = await this.senderService.importFromJson(importList)
+      const result = await this.tgUserService.importFromJson(importList)
 
       return reply.send({
         message: '转换完成',
@@ -77,12 +77,12 @@ export class SenderController {
         return reply.code(400).send({ message: '缺少必填字段: id' })
       }
 
-      const existingSender = await this.senderService.findById(id)
+      const existingSender = await this.tgUserService.findById(id)
       if (existingSender) {
         return reply.code(400).send({ message: '发送者已存在' })
       }
 
-      const sender = await this.senderService.create(id, dcId, authKey, sessionStr)
+      const sender = await this.tgUserService.create(id, dcId, authKey, sessionStr)
 
       return reply.code(201).send({
         message: '创建成功',
@@ -99,7 +99,7 @@ export class SenderController {
     try {
       const { id } = request.params
 
-      const sender = await this.senderService.findById(id)
+      const sender = await this.tgUserService.findById(id)
       if (!sender) {
         return reply.code(404).send({ message: '发送者不存在' })
       }
@@ -118,7 +118,7 @@ export class SenderController {
     try {
       const { page = 0, size = 20, delFlag } = request.query
 
-      const result = await this.senderService.findAll(page, size, delFlag)
+      const result = await this.tgUserService.findAll(page, size, delFlag)
 
       return reply.send(result)
     } catch (error) {
@@ -136,12 +136,12 @@ export class SenderController {
         return reply.code(400).send({ message: '缺少必填字段: id' })
       }
 
-      const existingSender = await this.senderService.findById(id)
+      const existingSender = await this.tgUserService.findById(id)
       if (!existingSender) {
         return reply.code(404).send({ message: '发送者不存在' })
       }
 
-      const updatedSender = await this.senderService.update(id, updateData)
+      const updatedSender = await this.tgUserService.update(id, updateData)
 
       return reply.send({
         message: '更新成功',
@@ -158,12 +158,12 @@ export class SenderController {
     try {
       const { id } = request.params
 
-      const existingSender = await this.senderService.findById(id)
+      const existingSender = await this.tgUserService.findById(id)
       if (!existingSender) {
         return reply.code(404).send({ message: '发送者不存在' })
       }
 
-      await this.senderService.delete(id)
+      await this.tgUserService.delete(id)
 
       return reply.send({
         message: '删除成功'
@@ -179,12 +179,12 @@ export class SenderController {
     try {
       const { id } = request.params
 
-      const existingSender = await this.senderService.findById(id)
+      const existingSender = await this.tgUserService.findById(id)
       if (!existingSender) {
         return reply.code(404).send({ message: '发送者不存在' })
       }
 
-      await this.senderService.hardDelete(id)
+      await this.tgUserService.hardDelete(id)
 
       return reply.send({
         message: '永久删除成功'

+ 0 - 0
src/dto/sender.dto.ts → src/dto/tg-user.dto.ts


+ 3 - 3
src/executor/task.executor.ts

@@ -4,7 +4,7 @@ import { TelegramClient, Api } from 'telegram'
 import { Task, TaskStatus } from '../entities/task.entity'
 import { TaskItem, TaskItemStatus } from '../entities/task-item.entity'
 import { TgUser } from '../entities/tg-user.entity'
-import { SenderService } from '../services/sender.service'
+import { TgUserService } from '../services/tg-user.service'
 import { TgClientService } from '../services/tgClient.service'
 import { buildStringSessionByDcIdAndAuthKey } from '../utils/tg.util'
 
@@ -12,7 +12,7 @@ export class TaskExecutor {
   private taskRepo: Repository<Task>
   private taskItemRepo: Repository<TaskItem>
   private senderRepo: Repository<TgUser>
-  private senderService: SenderService
+  private senderService: TgUserService
 
   private readonly defaultSenderSendLimit = 5
   private currentSenderSendLimit = this.defaultSenderSendLimit
@@ -22,7 +22,7 @@ export class TaskExecutor {
 
   constructor(private app: FastifyInstance) {
     const ds = app.dataSource
-    this.senderService = new SenderService(app)
+    this.senderService = new TgUserService(app)
     this.taskRepo = ds.getRepository(Task)
     this.taskItemRepo = ds.getRepository(TaskItem)
     this.senderRepo = ds.getRepository(TgUser)

+ 11 - 11
src/routes/sender.routes.ts → src/routes/tg-user.routes.ts

@@ -1,47 +1,47 @@
 import { FastifyInstance } from 'fastify'
-import { SenderController } from '../controllers/sender.controller'
 import { authenticate, hasRole } from '../middlewares/auth.middleware'
-import { CreateSenderBody, UpdateSenderBody, ListSenderQuery } from '../dto/sender.dto'
+import { CreateSenderBody, UpdateSenderBody, ListSenderQuery } from '../dto/tg-user.dto.js'
 import { UserRole } from '../entities/user.entity'
+import { TgUserController } from '../controllers/tg-user.controller'
 
-export default async function senderRoutes(fastify: FastifyInstance) {
-  const senderController = new SenderController(fastify)
+export default async function tgUserRoutes(fastify: FastifyInstance) {
+  const tgUserController = new TgUserController(fastify)
 
   fastify.post<{ Body: CreateSenderBody }>(
     '/',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.create.bind(senderController)
+    tgUserController.create.bind(tgUserController)
   )
 
   fastify.get<{ Querystring: ListSenderQuery }>(
     '/',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.list.bind(senderController)
+    tgUserController.list.bind(tgUserController)
   )
 
   fastify.get<{ Params: { id: string } }>(
     '/:id',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.getById.bind(senderController)
+    tgUserController.getById.bind(tgUserController)
   )
 
   fastify.post<{ Body: UpdateSenderBody }>(
     '/update',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.update.bind(senderController)
+    tgUserController.update.bind(tgUserController)
   )
 
   fastify.delete<{ Params: { id: string } }>(
     '/:id',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.delete.bind(senderController)
+    tgUserController.delete.bind(tgUserController)
   )
 
   fastify.delete<{ Params: { id: string } }>(
     '/:id/hard',
     { onRequest: [authenticate, hasRole(UserRole.ADMIN)] },
-    senderController.hardDelete.bind(senderController)
+    tgUserController.hardDelete.bind(tgUserController)
   )
 
-  fastify.post('/import/json', senderController.importFromJson.bind(senderController))
+  fastify.post('/import/json', tgUserController.importFromJson.bind(tgUserController))
 }

+ 3 - 3
src/services/test.service.ts

@@ -6,7 +6,7 @@ import bigInt from 'big-integer'
 import { Task, TaskStatus } from '../entities/task.entity'
 import { TaskItem, TaskItemStatus } from '../entities/task-item.entity'
 import { TgUser } from '../entities/tg-user.entity'
-import { SenderService } from './sender.service'
+import { TgUserService } from './tg-user.service'
 import { TgClientService } from './tgClient.service'
 import { ChatGroupService } from './chat-group.service'
 import { buildStringSession, buildStringSessionByDcIdAndAuthKey } from '../utils/tg.util'
@@ -16,7 +16,7 @@ export class TestService {
   private readonly taskRepository: Repository<Task>
   private readonly taskItemRepository: Repository<TaskItem>
   private readonly senderRepository: Repository<TgUser>
-  private readonly senderService: SenderService
+  private readonly senderService: TgUserService
   private readonly tgClientService: TgClientService
   private readonly chatGroupService: ChatGroupService
   private senderSendLimit = 5
@@ -29,7 +29,7 @@ export class TestService {
     this.taskRepository = app.dataSource.getRepository(Task)
     this.taskItemRepository = app.dataSource.getRepository(TaskItem)
     this.senderRepository = app.dataSource.getRepository(TgUser)
-    this.senderService = new SenderService(app)
+    this.senderService = new TgUserService(app)
     this.tgClientService = new TgClientService()
     this.chatGroupService = new ChatGroupService(app)
   }

+ 1 - 1
src/services/sender.service.ts → src/services/tg-user.service.ts

@@ -4,7 +4,7 @@ import { TgUser } from '../entities/tg-user.entity'
 import { PaginationResponse } from '../dto/common.dto'
 import { buildStringSessionByDcIdAndAuthKey } from '../utils/tg.util'
 
-export class SenderService {
+export class TgUserService {
   private senderRepository: Repository<TgUser>
 
   constructor(app: FastifyInstance) {