promotion-link.controller.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { PromotionLinkService } from '../services/promotion-link.service'
  3. import {
  4. CreatePromotionLinkBody,
  5. UpdatePromotionLinkBody,
  6. ListPromotionLinkQuery,
  7. PromotionLinkParams
  8. } from '../dto/promotion-link.dto'
  9. import { LinkType } from '../entities/promotion-link.entity'
  10. import { UserRole } from '../entities/user.entity'
  11. import { TeamService } from '../services/team.service'
  12. import { TeamMembersService } from '../services/team-members.service'
  13. export class PromotionLinkController {
  14. private promotionLinkService: PromotionLinkService
  15. private teamService: TeamService
  16. private teamMembersService: TeamMembersService
  17. constructor(app: FastifyInstance) {
  18. this.promotionLinkService = new PromotionLinkService(app)
  19. this.teamService = new TeamService(app)
  20. this.teamMembersService = new TeamMembersService(app)
  21. }
  22. async create(request: FastifyRequest<{ Body: CreatePromotionLinkBody }>, reply: FastifyReply) {
  23. try {
  24. const user = request.user
  25. if (!user) {
  26. return reply.code(403).send({ message: '用户未登录' })
  27. }
  28. if (user.role === UserRole.TEAM) {
  29. const team = await this.teamService.findByUserId(user.id)
  30. request.body.teamId = team.id
  31. }
  32. const promotionLink = await this.promotionLinkService.create(request.body)
  33. return reply.code(201).send(promotionLink)
  34. } catch (error) {
  35. return reply.code(500).send({ message: '创建推广链接失败', error })
  36. }
  37. }
  38. async findById(request: FastifyRequest<{ Params: PromotionLinkParams }>, reply: FastifyReply) {
  39. try {
  40. const { id } = request.params
  41. const promotionLink = await this.promotionLinkService.findById(id)
  42. return reply.send(promotionLink)
  43. } catch (error) {
  44. return reply.code(404).send({ message: '推广链接不存在' })
  45. }
  46. }
  47. async findAll(request: FastifyRequest<{ Querystring: ListPromotionLinkQuery }>, reply: FastifyReply) {
  48. try {
  49. const user = request.user
  50. if (!user) {
  51. return reply.code(403).send({ message: '用户未登录' })
  52. }
  53. if (user.role === UserRole.PROMOTER) {
  54. const teamMembers = await this.teamMembersService.findByUserId(user.id)
  55. request.query.teamId = teamMembers.teamId
  56. } else if (user.role === UserRole.TEAM) {
  57. const team = await this.teamService.findByUserId(user.id)
  58. request.query.teamId = team.id
  59. }
  60. const result = await this.promotionLinkService.findAll(request.query)
  61. return reply.send(result)
  62. } catch (error) {
  63. return reply.code(500).send({ message: '获取推广链接列表失败', error })
  64. }
  65. }
  66. async update(
  67. request: FastifyRequest<{ Params: PromotionLinkParams; Body: UpdatePromotionLinkBody }>,
  68. reply: FastifyReply
  69. ) {
  70. try {
  71. const { id } = request.params
  72. const updateData = { ...request.body, id }
  73. try {
  74. await this.promotionLinkService.findById(id)
  75. } catch (error) {
  76. return reply.code(404).send({ message: '推广链接不存在' })
  77. }
  78. const updatedLink = await this.promotionLinkService.update(updateData)
  79. return reply.send(updatedLink)
  80. } catch (error) {
  81. return reply.code(500).send({ message: '更新推广链接失败', error })
  82. }
  83. }
  84. async delete(request: FastifyRequest<{ Params: PromotionLinkParams }>, reply: FastifyReply) {
  85. try {
  86. const { id } = request.params
  87. try {
  88. await this.promotionLinkService.findById(id)
  89. } catch (error) {
  90. return reply.code(404).send({ message: '推广链接不存在' })
  91. }
  92. await this.promotionLinkService.delete(id)
  93. return reply.send({ message: '推广链接已删除' })
  94. } catch (error) {
  95. return reply.code(500).send({ message: '删除推广链接失败', error })
  96. }
  97. }
  98. async findByType(request: FastifyRequest<{ Querystring: { type: LinkType } }>, reply: FastifyReply) {
  99. try {
  100. const { type } = request.query
  101. const links = await this.promotionLinkService.findByType(type)
  102. return reply.send(links)
  103. } catch (error) {
  104. return reply.code(500).send({ message: '获取指定类型推广链接失败', error })
  105. }
  106. }
  107. async getStatistics(request: FastifyRequest, reply: FastifyReply) {
  108. try {
  109. const statistics = await this.promotionLinkService.getStatistics()
  110. return reply.send(statistics)
  111. } catch (error) {
  112. return reply.code(500).send({ message: '获取统计数据失败', error })
  113. }
  114. }
  115. }