Skip to content

Commit

Permalink
Merge pull request #225 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Roger13579 authored Jun 15, 2024
2 parents 42e8e07 + f3e6c61 commit d4d6d99
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 24 deletions.
9 changes: 9 additions & 0 deletions src/controller/groupController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LeaveGroupDto } from '../dto/group/leaveGroupDto';
import { GroupFilterDto } from '../dto/group/groupFilterDto';
import { GetGroupVo } from '../vo/group/getGroupVo';
import { Types } from 'mongoose';
import { Request } from 'express';
import { IUser } from '../models/user';
import { TMethod } from '../types/common.type';

Expand Down Expand Up @@ -79,4 +80,12 @@ export class GroupController extends BaseController {
new GetGroupVo(groups),
);
};
public getGroupDetail = async (req: Request) => {
const group = await this.groupService.findGroupDetail(req.params.groupId);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
group,
);
};
}
19 changes: 19 additions & 0 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { GetUserFavoriteDTO } from '../dto/user/getUserFavoriteDto';
import { GetFavoriteVO } from '../vo/user/getFavoriteVo';
import { SellTicketDto } from '../dto/ticket/sellTicketDto';
import { ISellTicketReq } from '../types/ticket.type';
import { GetUserGroupDto } from '../dto/group/getUserGroupDto';
import { GetGroupVo } from '../vo/group/getGroupVo';
import { PaginateDocument, PaginateOptions, PaginateResult } from 'mongoose';
import { IGroup } from '../models/group';
import { IGetUserGroupsReq } from '../types/group.type';

class UserController extends BaseController {
private readonly userService = new UserService();
Expand Down Expand Up @@ -87,6 +92,20 @@ class UserController extends BaseController {
tickets,
);
};

public getUserGroups: TMethod = async (req: IGetUserGroupsReq) => {
const getUserGroupDto = new GetUserGroupDto(req);
const groups = (await this.userService.getUserGroups(
getUserGroupDto,
)) as PaginateResult<
PaginateDocument<IGroup, NonNullable<unknown>, PaginateOptions>
>;
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
new GetGroupVo(groups),
);
};
}

export default UserController;
55 changes: 55 additions & 0 deletions src/dto/group/getUserGroupDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { GroupSortField } from '../../types/group.type';
import { SortOrder } from '../../types/common.type';
import { Request } from 'express';
import { IUser } from '../../models/user';

export class GetUserGroupDto {
private readonly _user: IUser;
private readonly _groupType: string;
private readonly _page: number;
private readonly _limit: number;
private readonly _sort: Record<string, 1 | -1>;

get groupType(): string {
return this._groupType;
}

get user(): IUser {
return this._user;
}

get ownFilter() {
return {
...(this._user && { userId: { $eq: this._user._id } }),
};
}

get ownOptions() {
return {
page: this._page,
limit: this._limit,
sort: this._sort,
};
}

get joinedOptions() {
return {
page: this._page,
limit: this._limit,
sort: this._sort,
};
}

constructor(req: Request) {
const { page, limit, sortField, sortOrder, groupType } = req.query;

this._sort = {
[`${sortField || GroupSortField.createdAt}`]:
sortOrder === SortOrder.asc ? 1 : -1,
};
this._limit = Number(limit);
this._page = Number(page);
this._user = req.user as IUser;
this._groupType = groupType as string;
}
}
16 changes: 11 additions & 5 deletions src/dto/group/groupFilterDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
GroupStatus,
IGetGroupsReq,
} from '../../types/group.type';
import { IUser } from '../../models/user';
import { Types } from 'mongoose';
import { SortOrder } from '../../types/common.type';

export class GroupFilterDto {
Expand All @@ -20,12 +18,10 @@ export class GroupFilterDto {
private readonly _page: number;
private readonly _limit: number;
private readonly _sort: Record<string, 1 | -1>;
private readonly _userId: Types.ObjectId | undefined;

get filter() {
const titleRegex = this._title ? new RegExp(this._title) : undefined;
return {
...(this._userId && { userId: { $eq: this._userId } }),
...(titleRegex && { title: { $regex: titleRegex } }),
...(this._movieTitle && { movieTitle: { $in: this._movieTitle } }),
...(this._status && { status: { $eq: this._status } }),
Expand All @@ -48,6 +44,17 @@ export class GroupFilterDto {
page: this._page,
limit: this._limit,
sort: this._sort,
projection: {
title: 1,
placeholderImg: 1,
theater: 1,
movieTitle: 1,
status: 1,
time: 1,
amount: 1,
haveTicket: 1,
content: 1,
},
};
}

Expand Down Expand Up @@ -82,6 +89,5 @@ export class GroupFilterDto {
haveTicket === undefined ? undefined : haveTicket === 'true';
this._startAt = startAt ? moment(startAt).toDate() : undefined;
this._endAt = endAt ? moment(endAt).toDate() : undefined;
this._userId = req.user !== undefined ? (req.user as IUser).id : undefined;
}
}
22 changes: 22 additions & 0 deletions src/repository/groupRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PaginateDocument,
PaginateOptions,
Types,
FilterQuery,
} from 'mongoose';
import { JoinGroupDto } from '../dto/group/joinGroupDto';
import { LeaveGroupDto } from '../dto/group/leaveGroupDto';
Expand All @@ -21,6 +22,27 @@ export class GroupRepository {
return GroupModel.findById(groupId);
}

public async findByIds(
groupIds: string[],
option: PaginateOptions | undefined,
) {
return GroupModel.paginate(
{
_id: {
$in: groupIds,
},
},
option,
);
}

public async findByUserId(
filter: FilterQuery<IGroup> | undefined,
option: PaginateOptions | undefined,
) {
return GroupModel.paginate(filter, option);
}

public async updateGroup(updateGroupDto: UpdateGroupDto) {
return GroupModel.findByIdAndUpdate(
{ _id: new Types.ObjectId(updateGroupDto.groupId) },
Expand Down
27 changes: 24 additions & 3 deletions src/routes/groupRoute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseRoute } from './baseRoute';
import { GroupController } from '../controller/groupController';
import { UserCheck, UserVerify } from '../middleware/userVerify';
import { UserVerify } from '../middleware/userVerify';
import { CreateGroupPipe } from '../validator/group/createGroup.pipe';
import { UpdateGroupPipe } from '../validator/group/updateGroup.pipe';
import { JoinGroupPipe } from '../validator/group/joinGroup.pipe';
Expand Down Expand Up @@ -163,12 +163,34 @@ export class GroupRoute extends BaseRoute {
UserVerify,
this.responseHandler(this.controller.deleteGroup),
);
this.router.get(
'/v1/group/:groupId',
/**
* #swagger.tags = ['Group']
* #swagger.summary = '取得揪團詳細'
*/
/*
#swagger.parameters['groupId'] ={
in:'path',
description:'揪團ID',
required: true,
type: 'string'
}
*/
/**
#swagger.responses[200] = {
description: 'OK',
schema: {
$ref: '#/definitions/GetGroupDetailSuccess' }
}
*/
this.responseHandler(this.controller.getGroupDetail),
);
this.router.get(
'/v1/group',
/**
* #swagger.tags = ['Group']
* #swagger.summary = '取得揪團列表'
* #swagger.security=[{"Bearer": []}],
*/
/*
#swagger.parameters['limit'] = {
Expand Down Expand Up @@ -283,7 +305,6 @@ export class GroupRoute extends BaseRoute {
}
}
*/
UserCheck,
this.usePipe(GetGroupsPipe),
this.responseHandler(this.controller.getGroups),
);
Expand Down
51 changes: 51 additions & 0 deletions src/routes/userRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseRoute } from './baseRoute';
import UserController from '../controller/userController';
import { UserVerify } from '../middleware/userVerify';
import { GetUserFavoritePipe } from '../validator/user/getUserFavorite.pipe';
import { GetUserGroupPipe } from '../validator/group/getUserGroup.pipe';

export class UserRoute extends BaseRoute {
protected controller!: UserController;
Expand Down Expand Up @@ -223,5 +224,55 @@ export class UserRoute extends BaseRoute {
UserVerify,
this.responseHandler(this.controller.getTransferableTicket),
);
this.router.get(
'/v1/user/groups',
/**
* #swagger.tags = ['User']
* #swagger.summary = '取得用戶我的揪團'
* #swagger.security=[{"Bearer": []}]
*/
/*
#swagger.parameters['groupType'] = {
in: 'query',
required: true,
description: '已建立 or 已參加',
type: 'string',
enum: ['own', 'joined'],
schema:{
$ref: "#/definitions/CustomGetGroupTypeQuery"
}
}
#swagger.parameters['page'] = {
in: 'query',
required: true,
description: '頁數',
type: 'number',
schema:{
$ref: "#/definitions/CustomPageQuery"
}
}
#swagger.parameters['sortOrder'] = {
in: 'query',
required: false,
description: '排序順序',
type: 'string',
enum: ["asc", "desc"],
schema:{
$ref: "#/definitions/CustomSortOrderQuery"
}
}
*/
/*
#swagger.responses[200]={
description:'OK',
schema:{
$ref:'#/definitions/GetUserGroupSuccess'
}
}
*/
UserVerify,
this.usePipe(GetUserGroupPipe),
this.responseHandler(this.controller.getUserGroups),
);
}
}
4 changes: 4 additions & 0 deletions src/service/groupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ export class GroupService {
public async findGroups(groupFilterDto: GroupFilterDto) {
return await this.groupRepository.findGroups(groupFilterDto);
}

public async findGroupDetail(groupId: string) {
return await this.groupRepository.findById(new Types.ObjectId(groupId));
}
}
24 changes: 11 additions & 13 deletions src/service/productService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,18 @@ export class ProductService {
favorite.productId.toString(),
);
}
result.docs = await Promise.all(
result.docs.map(async (doc) => {
let isFavorite = false;
if (user) {
if (favoriteProductIds.includes(doc._id.toString())) {
isFavorite = true;
}
result.docs = result.docs.map((doc) => {
let isFavorite = false;
if (user) {
if (favoriteProductIds.includes(doc._id.toString())) {
isFavorite = true;
}
return {
...doc.toObject(),
isFavorite: isFavorite,
} as ProductDocumentWithFavorite;
}),
);
}
return {
...doc.toObject(),
isFavorite: isFavorite,
} as ProductDocumentWithFavorite;
});
return result;
};

Expand Down
Loading

0 comments on commit d4d6d99

Please sign in to comment.