diff --git a/server/src/modules/message/controllers/messagePushingTask.controller.ts b/server/src/modules/message/controllers/messagePushingTask.controller.ts index c3a03559..f1b4066c 100644 --- a/server/src/modules/message/controllers/messagePushingTask.controller.ts +++ b/server/src/modules/message/controllers/messagePushingTask.controller.ts @@ -47,10 +47,21 @@ export class MessagePushingTaskController { req, @Body() createMessagePushingTaskDto: CreateMessagePushingTaskDto, ) { + let data; + try { + data = await CreateMessagePushingTaskDto.validate( + createMessagePushingTaskDto, + ); + } catch (error) { + throw new HttpException( + `参数错误: ${error.message}`, + EXCEPTION_CODE.PARAMETER_ERROR, + ); + } const userId = req.user._id; const messagePushingTask = await this.messagePushingTaskService.create({ - ...createMessagePushingTaskDto, + ...data, ownerId: userId, }); return { @@ -72,13 +83,19 @@ export class MessagePushingTaskController { req, @Query() query: QueryMessagePushingTaskListDto, ) { - const userId = req.user._id; - if (!query?.surveyId && !query?.triggerHook && !userId) { - throw new HttpException('参数错误', EXCEPTION_CODE.PARAMETER_ERROR); + let data; + try { + data = await QueryMessagePushingTaskListDto.validate(query); + } catch (error) { + throw new HttpException( + `参数错误: ${error.message}`, + EXCEPTION_CODE.PARAMETER_ERROR, + ); } + const userId = req.user._id; const list = await this.messagePushingTaskService.findAll({ - surveyId: query.surveyId, - hook: query.triggerHook, + surveyId: data.surveyId, + hook: data.triggerHook, ownerId: userId, }); return { diff --git a/server/src/modules/message/dto/createMessagePushingTask.dto.ts b/server/src/modules/message/dto/createMessagePushingTask.dto.ts index fbd690d5..5e1d0ca6 100644 --- a/server/src/modules/message/dto/createMessagePushingTask.dto.ts +++ b/server/src/modules/message/dto/createMessagePushingTask.dto.ts @@ -1,4 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; +import Joi from 'joi'; import { MESSAGE_PUSHING_TYPE, MESSAGE_PUSHING_HOOK, @@ -27,4 +28,19 @@ export class CreateMessagePushingTaskDto { default: [], }) surveys?: string[]; + + static async validate(data) { + return await Joi.object({ + name: Joi.string().required(), + type: Joi.string().allow(null).default(MESSAGE_PUSHING_TYPE.HTTP), + pushAddress: Joi.string().required(), + triggerHook: Joi.string() + .allow(null) + .default(MESSAGE_PUSHING_HOOK.RESPONSE_INSERTED), + surveys: Joi.array() + .items(Joi.string().required()) + .allow(null) + .default([]), + }).validateAsync(data); + } } diff --git a/server/src/modules/message/dto/queryMessagePushingTaskList.dto.ts b/server/src/modules/message/dto/queryMessagePushingTaskList.dto.ts index 5f8196ca..b25932ed 100644 --- a/server/src/modules/message/dto/queryMessagePushingTaskList.dto.ts +++ b/server/src/modules/message/dto/queryMessagePushingTaskList.dto.ts @@ -1,4 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; +import Joi from 'joi'; import { MESSAGE_PUSHING_HOOK } from 'src/enums/messagePushing'; export class QueryMessagePushingTaskListDto { @@ -7,4 +8,11 @@ export class QueryMessagePushingTaskListDto { @ApiProperty({ description: 'hook名称', required: false }) triggerHook?: MESSAGE_PUSHING_HOOK; + + static validate(data) { + return Joi.object({ + surveyId: Joi.string().required(), + triggerHook: Joi.string().required(), + }).validateAsync(data); + } }