Skip to content

Commit

Permalink
Merge pull request #258 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
y0000ga authored Jun 21, 2024
2 parents c617305 + bda7c0c commit 9044923
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/controller/ticketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ITransferTicketReq,
IClaimShareTicketReq,
IDeleteTicketsReq,
IGetOrderInfoReq,
} from '../types/ticket.type';
import { IOrder } from '../models/order';
import { OrderRepository } from '../repository/orderRepository';
Expand Down Expand Up @@ -117,4 +118,16 @@ export class TicketController extends BaseController {
{ tickets },
);
};

public getTicketOrderInfo: TMethod<IGetOrderInfoReq> = async (req) => {
const getOrderInfoVo = await this.ticketService.getOrderInfo(
req.query.orderId!,
req.query.productId!,
);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
getOrderInfoVo,
);
};
}
19 changes: 10 additions & 9 deletions src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ const ItemSchema = new Schema<IItem>(
},
);

ItemSchema.index(
{
productId: 1,
'plan.name': 1,
'plan.discount': 1,
'plan.headCount': 1,
},
{ unique: true },
);
// TODO: 購物車內的商品與方案不重複
// ItemSchema.index(
// {
// productId: 1,
// 'plan.name': 1,
// 'plan.discount': 1,
// 'plan.headCount': 1,
// },
// { unique: true },
// );

export interface ICart extends BaseModel, IUserId {
items: [IItem];
Expand Down
90 changes: 63 additions & 27 deletions src/routes/ticketRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ClaimTransferTicketPipe } from '../validator/ticket/claimTransferTicket
import { DeleteTicketsPipe } from '../validator/ticket/deleteTickets.pipe';
import { GetTicketDetailPipe } from '../validator/ticket/getTicketDetail.pipe';
import { GetSharedTicketPipe } from '../validator/ticket/getSharedTicket.pipe';
import { GetOrderInfoPipe } from '../validator/ticket/getOrderInfo.pipe';

export class TicketRoute extends BaseRoute {
protected controller!: TicketController;
Expand Down Expand Up @@ -146,70 +147,79 @@ export class TicketRoute extends BaseRoute {
this.usePipe(GetTicketPipe),
this.responseHandler(this.controller.getTickets),
);

this.router.get(
'/v1/ticket/:id',
'/v1/ticket-shared',
/**
* #swagger.tags = ['Ticket']
* #swagger.summary = '取得票券詳細資料'
* #swagger.security=[{"Bearer": []}],
* #swagger.summary = '取得已上架分票票券'
*/
/*
#swagger.parameters['id'] = {
in: 'path',
description: '票券 id',
example: 'abcdefg123124',
#swagger.parameters['page'] = {
in: 'query',
required: true,
description: '頁數',
type: 'number',
schema:{
$ref: "#/definitions/CustomPageQuery"
}
}
#swagger.parameters['limit'] = {
in: 'query',
required: false,
description: '每頁資料數',
type: 'number',
schema:{
$ref: "#/definitions/CustomLimitQuery"
}
}
*/
/*
#swagger.responses[200] = {
description:'OK',
schema:{
$ref: "#/definitions/GetTicketDetailSuccess"
$ref: "#/definitions/GetSharedTicketsSuccess"
}
}
*/
UserVerify,
this.usePipe(GetTicketDetailPipe),
this.responseHandler(this.controller.getTicketDetail),
this.usePipe(GetSharedTicketPipe),
this.responseHandler(this.controller.getSharedTickets),
);

this.router.get(
'/v1/ticket-shared',
'/v1/ticket/orderInfo',
/**
* #swagger.tags = ['Ticket']
* #swagger.summary = '取得已上架分票票券'
* #swagger.summary = '取得上架分票票券訂單內容'
*/
/*
#swagger.parameters['page'] = {
#swagger.parameters['orderId'] = {
in: 'query',
required: true,
description: '頁數',
type: 'number',
description: '訂單ID',
type: 'string',
schema:{
$ref: "#/definitions/CustomPageQuery"
$ref: "#/definitions/CustomGetOrderIdQuery"
}
}
#swagger.parameters['limit'] = {
#swagger.parameters['productId'] = {
in: 'query',
required: false,
description: '每頁資料數',
type: 'number',
required: true,
description: '商品ID',
type: 'string',
schema:{
$ref: "#/definitions/CustomLimitQuery"
$ref: "#/definitions/CustomGetProductIdQuery"
}
}
*/
/*
#swagger.responses[200] = {
description:'OK',
schema:{
$ref: "#/definitions/GetSharedTicketsSuccess"
$ref: "#/definitions/GetOrderInfoSuccess"
}
}
*/
this.usePipe(GetSharedTicketPipe),
this.responseHandler(this.controller.getSharedTickets),
this.usePipe(GetOrderInfoPipe),
this.responseHandler(this.controller.getTicketOrderInfo),
);

this.router.patch(
Expand Down Expand Up @@ -359,5 +369,31 @@ export class TicketRoute extends BaseRoute {
this.usePipe(DeleteTicketsPipe),
this.responseHandler(this.controller.deleteTickets),
);
this.router.get(
'/v1/ticket/:id',
/**
* #swagger.tags = ['Ticket']
* #swagger.summary = '取得票券詳細資料'
* #swagger.security=[{"Bearer": []}],
*/
/*
#swagger.parameters['id'] = {
in: 'path',
description: '票券 id',
example: 'abcdefg123124',
}
*/
/*
#swagger.responses[200] = {
description:'OK',
schema:{
$ref: "#/definitions/GetTicketDetailSuccess"
}
}
*/
UserVerify,
this.usePipe(GetTicketDetailPipe),
this.responseHandler(this.controller.getTicketDetail),
);
}
}
18 changes: 18 additions & 0 deletions src/service/ticketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ import { GetSharedTicketsDto } from '../dto/ticket/getSharedTicketsDto';
import { TicketRefundDto } from '../dto/ticket/TicketRefundDto';
import { OrderRepository } from '../repository/orderRepository';
import { ITicket } from '../models/ticket';
import { ProductRepository } from '../repository/productRepository';
import { GetOrderInfoVo } from '../vo/ticket/getOrderInfoVo';
import { IProduct } from '../models/product';

const logger = log4js.getLogger(`TicketService`);

export class TicketService {
private readonly ticketRepository: TicketRepository = new TicketRepository();
private readonly orderRepository: OrderRepository = new OrderRepository();
private readonly productRepository: ProductRepository =
new ProductRepository();

public findTickets = async (ticketFilterDto: GetTicketsDto) => {
const { expiredAtFrom, expiredAtTo } = ticketFilterDto;
Expand Down Expand Up @@ -78,6 +83,19 @@ export class TicketService {
return await this.ticketRepository.getTicketDetail(getTicketDetailDto);
};

public getOrderInfo = async (orderId: string, productId: string) => {
const order = (await this.orderRepository.findById(
new Types.ObjectId(orderId),
)) as IOrder;
const product = (await this.productRepository.findById(
new Types.ObjectId(productId),
)) as IProduct;
const holdCount = order.products
.filter((product) => product.productId.toString() === productId)
.map((p) => p.amount)[0];
return new GetOrderInfoVo(holdCount, product);
};

public verifyTickets = async (verifyTicketsDto: VerifyTicketsDTO) => {
const tickets = await this.ticketRepository.verifyTickets(verifyTicketsDto);

Expand Down
4 changes: 4 additions & 0 deletions src/swagger/definition/product/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ export const CustomGetProductTitleQuery = {
example: '很棒的特映會',
};

export const CustomGetProductIdQuery = {
example: '1231432534wefqwef',
};

export const CustomGetProductTypesQuery = {
example: `${ProductType.corporateBooking},${ProductType.openAir}`,
};
Expand Down
13 changes: 13 additions & 0 deletions src/swagger/definition/ticket/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export const SharedTicket = {
$updatedAt: new Date().toISOString(),
$expiredAt: new Date().toISOString(),
};
export const OrderInfo = {
$productName: 'testproduct',
$holdCount: '2',
$price: '350',
$photoPath: 'https://image.com.tw',
};
/**
* @description swagger autogen 可以自動生成,通常用於 response 的 general 資料
*/
Expand Down Expand Up @@ -92,6 +98,13 @@ export const GetSharedTicketsSuccess = {
...PaginationSuccess,
},
};
export const GetOrderInfoSuccess = {
$status: CustomResponseType.OK,
$message: CustomResponseType.OK_MESSAGE,
$data: {
...OrderInfo,
},
};

export const GetTicketDetailSuccess = {
$status: CustomResponseType.OK,
Expand Down
6 changes: 6 additions & 0 deletions src/types/ticket.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export interface IGetTicketsReq extends IUserReq {
isPublished?: string;
};
}
export interface IGetOrderInfoReq extends IUserReq {
query: {
orderId?: string;
productId?: string;
};
}
export interface IGetTicketsRes {
metadata: [
{
Expand Down
18 changes: 18 additions & 0 deletions src/validator/ticket/getOrderInfo.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { query } from 'express-validator';
import { PipeBase } from '../pipe.base';
import { CustomResponseType } from '../../types/customResponseType';

export class GetOrderInfoPipe extends PipeBase {
public transform = () => [
query('orderId')
.exists()
.withMessage(CustomResponseType.FORMAT_ERROR_MESSAGE + 'orderId'),
query('productId')
.exists()
.withMessage(CustomResponseType.FORMAT_ERROR_MESSAGE + 'productId'),
this.validationHandler,
];
constructor() {
super();
}
}
4 changes: 1 addition & 3 deletions src/validator/ticket/useTicket.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export class UseTicketPipe extends PipeBase {
public transform = () => [
param('ticketId')
.exists()
.withMessage(
CustomResponseType.INVALID_TICKET_REFUND_MESSAGE + 'ticketId',
),
.withMessage(CustomResponseType.FORMAT_ERROR_MESSAGE + 'ticketId'),
this.validationHandler,
];
constructor() {
Expand Down
15 changes: 15 additions & 0 deletions src/vo/ticket/getOrderInfoVo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IProduct } from '../../models/product';

export class GetOrderInfoVo {
private productName: string;
private photoPath: string;
private holdCount: number;
private price: number;

constructor(holdCount: number | undefined, product: IProduct) {
this.productName = product.title;
this.photoPath = product.photoPath;
this.holdCount = holdCount != undefined ? holdCount : 0;
this.price = product.price;
}
}

0 comments on commit 9044923

Please sign in to comment.