From be81e2a86366ea70d4beb3b57bc499a1cce7c556 Mon Sep 17 00:00:00 2001 From: luch <32321690+luch1994@users.noreply.github.com> Date: Tue, 9 Apr 2024 11:46:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B6=88=E6=81=AF=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C=20(#95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../messagePushingTask.controller.ts | 29 +++++++++++++++---- .../dto/createMessagePushingTask.dto.ts | 16 ++++++++++ .../dto/queryMessagePushingTaskList.dto.ts | 8 +++++ 3 files changed, 47 insertions(+), 6 deletions(-) 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); + } }