Skip to content

Commit

Permalink
Merge pull request #245 from Roger13579/fix/group-request-parameter-c…
Browse files Browse the repository at this point in the history
…heck

Fix/group request parameter check
  • Loading branch information
Roger13579 authored Jun 19, 2024
2 parents e5eff9d + 39310f0 commit c0a4371
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/controller/groupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class GroupController extends BaseController {
};
public joinGroup: TMethod<TJoinGroupReq> = async (req) => {
const joinGroupDto = new JoinGroupDto(req);
await this.groupService.joinGroup(joinGroupDto);
const result = await this.groupService.joinGroup(joinGroupDto);
if (result) {
return this.formatResponse(result.message, result.code, {});
}
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
Expand Down
6 changes: 3 additions & 3 deletions src/dto/group/groupFilterDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class GroupFilterDto {
...(this._participantCount && {
amount: { $eq: this._participantCount },
}),
...(this._haveTicket !== undefined && { haveTicket: this._haveTicket }),
...(this._haveTicket && { haveTicket: this._haveTicket }),
...((this._startAt || this._endAt) && {
time: {
...(this._endAt && { $lte: this._endAt }),
Expand Down Expand Up @@ -83,8 +83,8 @@ export class GroupFilterDto {
this._limit = Number(limit);
this._page = Number(page);
this._participantCount = Number(participantCount);
this._movieTitle = movieTitle?.split(',');
this._theater = theater?.split(',');
this._movieTitle = movieTitle ? movieTitle.split(',') : undefined;
this._theater = theater ? theater.split(',') : undefined;
this._haveTicket =
haveTicket === undefined ? undefined : haveTicket === 'true';
this._startAt = startAt ? moment(startAt).toDate() : undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/service/groupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export class GroupService {
(userId) => userId.toString() === joinGroupDto.userId.toString(),
);
if (matchGroup.length > 0) {
throwError(
CustomResponseType.GROUP_ALREADY_JOINED_MESSAGE,
CustomResponseType.GROUP_ALREADY_JOINED,
);
return {
code: CustomResponseType.GROUP_ALREADY_JOINED,
message: CustomResponseType.GROUP_ALREADY_JOINED_MESSAGE,
};
}
// 檢查人數是否已滿
const groupSize = group.amount;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export const virtualSchemaOption = {
},
},
};

export const nullableOption = { nullable: true, checkFalsy: true };
26 changes: 8 additions & 18 deletions src/validator/group/getGroup.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../types/group.type';
import { OptionType, TCustomValidator } from '../index.type';
import { SortOrder } from '../../types/common.type';
import { booleanStrings } from '../../utils/constants';
import { booleanStrings, nullableOption } from '../../utils/constants';

export class GetGroupsPipe extends PipeBase {
private validateStartAt: TCustomValidator = (value, { req }) => {
Expand All @@ -30,54 +30,44 @@ export class GetGroupsPipe extends PipeBase {
query('page'),
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'page',
),
query('title')
.optional()
.isString()
.withMessage(CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'title'),
query('hasTicket')
.optional()
.optional(nullableOption)
.isIn(booleanStrings)
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'hasTicket',
),
query('movieTitle')
.optional()
.isString()
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'movieTitle',
),
query('status')
.optional()
.optional(nullableOption)
.isIn(Object.keys(GroupStatus))
.withMessage(CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'status'),
query('participantCount')
.optional()
.optional(nullableOption)
.isInt({ min: 1 })
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'participantCount',
),
query('startAt')
.optional()
.optional(nullableOption)
.custom(this.validateDate)
.custom(this.validateStartAt)
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'startAtTo',
),
query('endAt')
.optional()
.optional(nullableOption)
.custom(this.validateDate)
.custom(this.validateEndAt)
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'startAtTo',
),
query('sortField')
.optional()
.optional(nullableOption)
.custom(this.validateOption(OptionType.item, GroupSortField))
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'sortField',
),
query('sortOrder')
.optional()
.optional(nullableOption)
.custom(this.validateOption(OptionType.item, SortOrder))
.withMessage(
CustomResponseType.INVALID_GROUP_FILTER_MESSAGE + 'sortOrder',
Expand Down
4 changes: 2 additions & 2 deletions src/validator/pipe.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export abstract class PipeBase {
*/
protected validateDate = (value: string) => {
const date = new Date(value);
return date instanceof Date && !isNaN(date.getTime());
return !isNaN(date.getTime());
};

protected validationHandler: TMethod<IUserReq, void> = (req, res, next) => {
Expand Down Expand Up @@ -155,7 +155,7 @@ export abstract class PipeBase {

/**
* 驗證:多個屬性不可同時存在
* @param propNames - 需要檢查的屬性名稱數組
* @param params - 需要檢查的屬性名稱數組
*/
protected validateExclusiveQuery =
(params: IValidateExclusiveQueryParams): TCustomValidator<unknown> =>
Expand Down

0 comments on commit c0a4371

Please sign in to comment.