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

This commit is contained in:
luch 2024-04-09 11:46:49 +08:00 committed by GitHub
parent 2930971da7
commit a23fc28f5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 6 deletions

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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);
}
}