Skip to content

Commit

Permalink
feat: add isPublished param into findTransferableTicket function
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger13579 committed Jul 3, 2024
1 parent 10df7b4 commit 50483c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
11 changes: 8 additions & 3 deletions src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions src/repository/ticketRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
});
Expand Down
34 changes: 16 additions & 18 deletions src/service/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions src/vo/ticket/getTransferableTicketVo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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;
this.photoPath = product.photoPath;
this.theater = product.theater;
this.startAt = product.startAt;
this.expiredAt = group[0].expiredAt;
this.amount = group.length - 1;
this.amount = amount;
}
}

0 comments on commit 50483c9

Please sign in to comment.