From 50483c91f171d7baddb83834a8b5dcea5ac8bbcb Mon Sep 17 00:00:00 2001 From: RogerLi Date: Thu, 4 Jul 2024 00:33:37 +0800 Subject: [PATCH] feat: add isPublished param into findTransferableTicket function --- src/controller/userController.ts | 11 +++++--- src/repository/ticketRepository.ts | 7 +++-- src/service/userService.ts | 34 +++++++++++------------- src/vo/ticket/getTransferableTicketVo.ts | 4 +-- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/controller/userController.ts b/src/controller/userController.ts index 1f3bf1f..26e34c9 100644 --- a/src/controller/userController.ts +++ b/src/controller/userController.ts @@ -4,14 +4,18 @@ import { UserService } from '../service/userService'; import { JWTPayloadDTO } from '../dto/user/jwtPayloadDto'; import { UserDetailVo } from '../vo/userDetailVo'; import { UserDetailDto } from '../dto/user/userDetailDto'; -import { IUserReq, TMethod } from '../types/common.type'; +import { TMethod } from '../types/common.type'; import { IUser } from '../models/user'; import { EditFavoriteDTO } from '../dto/user/editFavoriteDto'; import { IGetUserFavoriteReq, IUpdateUserDetailReq } from '../types/user.type'; import { GetUserFavoriteDTO } from '../dto/user/getUserFavoriteDto'; import { GetFavoriteVO } from '../vo/user/getFavoriteVo'; import { SellTicketDto } from '../dto/ticket/sellTicketDto'; -import { ISellTicketReq, ITicketRefundReq } from '../types/ticket.type'; +import { + IGetTicketsReq, + ISellTicketReq, + ITicketRefundReq, +} from '../types/ticket.type'; import { GetUserGroupDto } from '../dto/group/getUserGroupDto'; import { GetGroupVo } from '../vo/group/getGroupVo'; import { PaginateDocument, PaginateOptions, PaginateResult } from 'mongoose'; @@ -85,9 +89,10 @@ export class UserController extends BaseController { {}, ); }; - public getTransferableTicket = async (req: IUserReq) => { + public getTransferableTicket = async (req: IGetTicketsReq) => { const tickets = await this.userService.getTransferableTicket( (req.user as IUser)._id, + req.query.isPublished, ); return this.formatResponse( CustomResponseType.OK_MESSAGE, diff --git a/src/repository/ticketRepository.ts b/src/repository/ticketRepository.ts index 5573444..0f6031f 100644 --- a/src/repository/ticketRepository.ts +++ b/src/repository/ticketRepository.ts @@ -65,10 +65,13 @@ export class TicketRepository { }); }; - public findTransferableTicket = async (userId: string) => { + public findTransferableTicket = async ( + userId: string, + isPublished: boolean, + ) => { return TicketModel.find({ userId: userId, - isPublished: false, + isPublished: isPublished, status: TicketStatus.unverified, expiredAt: { $gte: new Date() }, }); diff --git a/src/service/userService.ts b/src/service/userService.ts index 42cf76f..6f3a13b 100644 --- a/src/service/userService.ts +++ b/src/service/userService.ts @@ -227,17 +227,6 @@ export class UserService { ); return { accessToken, refreshToken }; } - public generateRefreshJWT(userId: string, accountType: string): string { - const privateKey = process.env.JWT_SECRETS; - const defaultOptions: object = { - expiresIn: process.env.JWT_REFRESH_EXPIRES, - }; - return jwt.sign( - { id: userId, accountType: accountType }, - privateKey, - Object.assign(defaultOptions), - ); - } public async generateForgotPasswordJWT(userId: string) { const privateKey = process.env.JWT_SECRETS; @@ -335,8 +324,15 @@ export class UserService { tickets.splice(0, sellTicketDto.sellAmount), ); }; - public getTransferableTicket = async (userId: string) => { - const tickets = await this.ticketRepository.findTransferableTicket(userId); + public getTransferableTicket = async ( + userId: string, + booleanString: string | undefined, + ) => { + const isPublished = booleanString === 'true'; + const tickets = await this.ticketRepository.findTransferableTicket( + userId, + isPublished, + ); // 查出的ticket依orderId和productId分組 const grouped = tickets.reduce( (acc, ticket) => { @@ -349,16 +345,18 @@ export class UserService { }, {} as { [key: string]: ITicket[] }, ); - // 去除只剩一張的票 - const validGroups = Object.values(grouped).filter( - (group) => group.length > 1, - ); + let validGroups = Object.values(grouped); + // 條件為搜尋未上架票券時才去除只剩一張的票 + if (!isPublished) { + validGroups = validGroups.filter((group) => group.length > 1); + } // 依productId查出商品資訊 const map = Object.values(validGroups).map(async (group) => { const product = (await this.productRepository.findById( new Types.ObjectId(group[0].productId), )) as IProduct; - return new GetTransferableTicketVo(group, product); + const amount = isPublished ? group.length : group.length - 1; + return new GetTransferableTicketVo(group, product, amount); }); return Promise.all(map); }; diff --git a/src/vo/ticket/getTransferableTicketVo.ts b/src/vo/ticket/getTransferableTicketVo.ts index a0cebf7..513e3bf 100644 --- a/src/vo/ticket/getTransferableTicketVo.ts +++ b/src/vo/ticket/getTransferableTicketVo.ts @@ -11,7 +11,7 @@ export class GetTransferableTicketVo { private expiredAt: Date; private amount: number; - constructor(group: ITicket[], product: IProduct) { + constructor(group: ITicket[], product: IProduct, amount: number) { this.orderId = group[0].orderId.toString(); this.productId = product._id.toString(); this.productName = product.title; @@ -19,6 +19,6 @@ export class GetTransferableTicketVo { this.theater = product.theater; this.startAt = product.startAt; this.expiredAt = group[0].expiredAt; - this.amount = group.length - 1; + this.amount = amount; } }