feat: 消息模块新增参数校验 (#95)

This commit is contained in:
luch 2024-04-09 11:46:49 +08:00 committed by sudoooooo
parent c5489daac3
commit be81e2a863
3 changed files with 47 additions and 6 deletions

View File

@ -47,10 +47,21 @@ export class MessagePushingTaskController {
req, req,
@Body() createMessagePushingTaskDto: CreateMessagePushingTaskDto, @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 userId = req.user._id;
const messagePushingTask = await this.messagePushingTaskService.create({ const messagePushingTask = await this.messagePushingTaskService.create({
...createMessagePushingTaskDto, ...data,
ownerId: userId, ownerId: userId,
}); });
return { return {
@ -72,13 +83,19 @@ export class MessagePushingTaskController {
req, req,
@Query() query: QueryMessagePushingTaskListDto, @Query() query: QueryMessagePushingTaskListDto,
) { ) {
const userId = req.user._id; let data;
if (!query?.surveyId && !query?.triggerHook && !userId) { try {
throw new HttpException('参数错误', EXCEPTION_CODE.PARAMETER_ERROR); 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({ const list = await this.messagePushingTaskService.findAll({
surveyId: query.surveyId, surveyId: data.surveyId,
hook: query.triggerHook, hook: data.triggerHook,
ownerId: userId, ownerId: userId,
}); });
return { return {

View File

@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import Joi from 'joi';
import { import {
MESSAGE_PUSHING_TYPE, MESSAGE_PUSHING_TYPE,
MESSAGE_PUSHING_HOOK, MESSAGE_PUSHING_HOOK,
@ -27,4 +28,19 @@ export class CreateMessagePushingTaskDto {
default: [], default: [],
}) })
surveys?: string[]; 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);
}
} }

View File

@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import Joi from 'joi';
import { MESSAGE_PUSHING_HOOK } from 'src/enums/messagePushing'; import { MESSAGE_PUSHING_HOOK } from 'src/enums/messagePushing';
export class QueryMessagePushingTaskListDto { export class QueryMessagePushingTaskListDto {
@ -7,4 +8,11 @@ export class QueryMessagePushingTaskListDto {
@ApiProperty({ description: 'hook名称', required: false }) @ApiProperty({ description: 'hook名称', required: false })
triggerHook?: MESSAGE_PUSHING_HOOK; triggerHook?: MESSAGE_PUSHING_HOOK;
static validate(data) {
return Joi.object({
surveyId: Joi.string().required(),
triggerHook: Joi.string().required(),
}).validateAsync(data);
}
} }