task.controller.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Body, Controller, Delete, Get, Param, Post, Put, Req } from '@nestjs/common'
  2. import { PageRequest } from 'src/common/dto/page-request'
  3. import { Task } from './entities/task.entity'
  4. import { TaskService } from './task.service'
  5. import { TaskItem } from './entities/task-item.entity'
  6. import { UsersService } from '../users/users.service'
  7. import { In } from 'typeorm'
  8. @Controller('task')
  9. export class TaskController {
  10. constructor(
  11. private readonly taskService: TaskService,
  12. private readonly userService: UsersService
  13. ) {
  14. }
  15. @Post('/')
  16. async findAllTask(@Req() req, @Body() page: PageRequest<Task>) {
  17. if (req.user.roles.includes('api') || req.user.roles.includes('superApi')) {
  18. const userIds = await this.userService.getInvitesIds(req.user.id)
  19. ;(page.search as any).where.userId = In(userIds)
  20. } else if (!req.user.roles.includes('admin')) {
  21. ;(page.search as any).where.userId = (req.user.id)
  22. }
  23. if ((page.search as any).where.status === 'run') {
  24. (page.search as any).where.status = In(['pending', 'queued', 'cutting'])
  25. }
  26. return await this.taskService.findAllTask(page)
  27. }
  28. @Put('/')
  29. async createTask(@Req() req, @Body() body) {
  30. body.userId = req.user.id
  31. return await this.taskService.createTask(body)
  32. }
  33. @Put('/:id')
  34. async updateTask(@Param('id') id: string, @Req() req, @Body() body) {
  35. return await this.taskService.updateTask(parseInt(id), req.user, body)
  36. }
  37. @Get('/verification/:id')
  38. async balanceVerification(@Param('id') id: string) {
  39. return await this.taskService.balanceVerification(parseInt(id))
  40. }
  41. @Delete('/:id')
  42. async delTask(@Param('id') id: string) {
  43. return await this.taskService.delTask(parseInt(id))
  44. }
  45. @Post('/:id/start')
  46. async startTask(@Param('id') id: string) {
  47. return await this.taskService.startTask(parseInt(id))
  48. }
  49. @Post('/:id/queueCutting')
  50. async queueCutting(@Param('id') id: string) {
  51. return await this.taskService.queueCutting(parseInt(id))
  52. }
  53. @Post('/:id/pause')
  54. async pauseTask(@Param('id') id: string) {
  55. return await this.taskService.pauseTask(parseInt(id))
  56. }
  57. @Get('/:id/forceCompletion')
  58. async forceCompletion(@Param('id') id: string) {
  59. return await this.taskService.forceCompletion(parseInt(id))
  60. }
  61. @Post('/item')
  62. async findAllTaskItem(@Body() page: PageRequest<TaskItem>) {
  63. (page.search as any).where.embed = false
  64. return await this.taskService.findAllTaskItem(page)
  65. }
  66. @Post('/item/:taskId/receipt')
  67. async exportTaskItem(@Param('taskId') taskId: number) {
  68. try {
  69. return await this.taskService.exportTaskItem(taskId)
  70. } catch (error) {
  71. console.error(error)
  72. }
  73. }
  74. @Get('/toBeSentNum/:taskId')
  75. async getTobeSentNum(@Param('taskId') taskId: number) {
  76. return await this.taskService.getToBeSentNum(taskId)
  77. }
  78. @Get('/num/:taskId')
  79. async getSuccessNum(@Param('taskId') taskId: number) {
  80. return await this.taskService.getSuccessNum(taskId)
  81. }
  82. @Post('/export')
  83. async exportTask(@Req() req, @Body() data) {
  84. return await this.taskService.exportTask(req, data)
  85. }
  86. @Get('/statistics')
  87. async homeStatistics(@Req() req) {
  88. return await this.taskService.homeStatistics(req)
  89. }
  90. @Get('/adminStatistics')
  91. async adminStatistics(@Req() req) {
  92. return await this.taskService.adminStatistics()
  93. }
  94. }