Skip to content

Commit

Permalink
Merge pull request #233 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Roger13579 authored Jun 16, 2024
2 parents 34a3dd3 + e489f98 commit c6f43a6
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/controller/ticketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { EditTicketsDTO } from '../dto/ticket/editTicketsDto';
import { CreateShareCodeDTO } from '../dto/ticket/createShareCodeDto';
import { TransferTicketDTO } from '../dto/ticket/transferTicketDto';
import { GetTicketDetailDto } from '../dto/ticket/getTicketDetailDto';
import { GetSharedTicketsDto } from '../dto/ticket/getSharedTicketsDto';

export class TicketController extends BaseController {
private readonly ticketService = new TicketService();
Expand Down Expand Up @@ -47,9 +48,10 @@ export class TicketController extends BaseController {
};

public getSharedTickets: TMethod<IGetTicketsReq> = async (req) => {
const getTicketsDto = new GetTicketsDto(req);
const { page, limit } = getTicketsDto;
const info = await this.ticketService.findSharedTickets(getTicketsDto);
const getSharedTicketsDto = new GetSharedTicketsDto(req);
const { page, limit } = getSharedTicketsDto;
const info =
await this.ticketService.findSharedTickets(getSharedTicketsDto);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
Expand Down
57 changes: 57 additions & 0 deletions src/dto/ticket/getSharedTicketsDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { IGetTicketsReq, SharedTicketSortField } from '../../types/ticket.type';
import { SortOrder } from '../../types/common.type';

export class GetSharedTicketsDto {
private readonly _page: number;
private readonly _limit: number;
private readonly _sort: Record<string, 1 | -1>;
get page() {
return this._page;
}

get limit() {
return this._limit;
}

get sort() {
return this._sort;
}

get options() {
const productSelect = {
title: 1,
photoPath: 1,
price: 1,
theater: 1,
startAt: 1,
};
const ticketSelect = {
_id: 1,
productId: 1,
userId: 1,
orderId: 1,
status: 1,
isPublished: 1,
expiredAt: 1,
createdAt: 1,
updatedAt: 1,
product: 1,
};
return {
productSelect,
ticketSelect,
page: this._page,
limit: this._limit,
sort: this._sort,
};
}
constructor(req: IGetTicketsReq) {
const { page, limit, sortField, sortOrder } = req.query;
this._sort = {
[`${sortField || SharedTicketSortField.updatedAt}`]:
sortOrder === SortOrder.desc ? 1 : -1,
};
this._limit = Number(limit);
this._page = Number(page);
}
}
5 changes: 3 additions & 2 deletions src/repository/ticketRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { GetTicketDetailDto } from '../dto/ticket/getTicketDetailDto';
import { createGetTicketDetailPipeline } from '../utils/aggregate/ticket/getTicketDetail.pipeline';
import { SellTicketDto } from '../dto/ticket/sellTicketDto';
import { GetSharedTicketsDto } from '../dto/ticket/getSharedTicketsDto';

export class TicketRepository {
public async createTicket(createTicketDto: CreateTicketDto) {
Expand All @@ -37,9 +38,9 @@ export class TicketRepository {
return results[0];
};
public findSharedTickets = async (
ticketFilterDto: GetTicketsDto,
getSharedTicketsDto: GetSharedTicketsDto,
): Promise<IGetTicketsRes> => {
const pipeline = createGetSharedTicketPipeline(ticketFilterDto);
const pipeline = createGetSharedTicketPipeline(getSharedTicketsDto);
const results = await TicketModel.aggregate(pipeline);
return results[0];
};
Expand Down
29 changes: 21 additions & 8 deletions src/routes/ticketRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TransferTicketPipe } from '../validator/ticket/TransferTicket.pipe';
import { ClaimTransferTicketPipe } from '../validator/ticket/claimTransferTicket.pipe';
import { DeleteTicketsPipe } from '../validator/ticket/deleteTickets.pipe';
import { GetTicketDetailPipe } from '../validator/ticket/getTicketDetail.pipe';
import { GetSharedTicketPipe } from '../validator/ticket/getSharedTicket.pipe';

export class TicketRoute extends BaseRoute {
protected controller!: TicketController;
Expand Down Expand Up @@ -177,25 +178,37 @@ export class TicketRoute extends BaseRoute {
'/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(GetSharedTicketPipe),
this.responseHandler(this.controller.getSharedTickets),
);

Expand Down
2 changes: 1 addition & 1 deletion src/routes/userRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class UserRoute extends BaseRoute {
"gender": "none",
"phone": "0912345678",
"address": "aaaabbb",
"avatarPath": "avatarPath"
"imgUrl": "imgimg"
}
}
*/
Expand Down
7 changes: 5 additions & 2 deletions src/service/ticketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Types } from 'mongoose';
import * as crypto from 'node:crypto';
import { TransferTicketDTO } from '../dto/ticket/transferTicketDto';
import { GetTicketDetailDto } from '../dto/ticket/getTicketDetailDto';
import { GetSharedTicketsDto } from '../dto/ticket/getSharedTicketsDto';

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

Expand All @@ -34,8 +35,10 @@ export class TicketService {

return await this.ticketRepository.findTickets(ticketFilterDto);
};
public findSharedTickets = async (ticketFilterDto: GetTicketsDto) => {
return await this.ticketRepository.findSharedTickets(ticketFilterDto);
public findSharedTickets = async (
getSharedTicketsDto: GetSharedTicketsDto,
) => {
return await this.ticketRepository.findSharedTickets(getSharedTicketsDto);
};

public async createTickets(order: IOrder) {
Expand Down
26 changes: 26 additions & 0 deletions src/swagger/definition/ticket/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export const Ticket = {
writeOffStaffId: 'rrr',
giverId: 'iiddidididi',
};

export const SharedTicket = {
$_id: 'asdfasdfasd',
$productId: 'cdscsdcsdc',
$userId: '123235564364567',
$orderId: 'iiddidididi',
$status: TicketStatus.unverified,
$isPublished: false,
$createdAt: new Date().toISOString(),
$updatedAt: new Date().toISOString(),
$expiredAt: new Date().toISOString(),
};
/**
* @description swagger autogen 可以自動生成,通常用於 response 的 general 資料
*/
Expand Down Expand Up @@ -61,6 +73,20 @@ export const GetTicketsSuccess = {
...PaginationSuccess,
},
};
export const GetSharedTicketsSuccess = {
$status: CustomResponseType.OK,
$message: CustomResponseType.OK_MESSAGE,
$data: {
tickets: [
{
...SharedTicket,
product: TicketProductDetail,
count: 2,
},
],
...PaginationSuccess,
},
};

export const GetTicketDetailSuccess = {
$status: CustomResponseType.OK,
Expand Down
4 changes: 4 additions & 0 deletions src/types/ticket.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export enum TicketSortField {
status = 'status',
}

export enum SharedTicketSortField {
updatedAt = 'updatedAt',
}

export interface IVerifyTicketsReq extends IUserReq {
body: {
tickets: {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/aggregate/ticket/getTickets.pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetTicketsDto } from '../../../dto/ticket/getTicketsDto';
import { GetSharedTicketsDto } from '../../../dto/ticket/getSharedTicketsDto';

export const createGetTicketPipeline = ({
productNameRegex,
Expand Down Expand Up @@ -73,7 +74,7 @@ export const createGetSharedTicketPipeline = ({
sort,
limit,
page,
}: GetTicketsDto) => {
}: GetSharedTicketsDto) => {
return [
{
$match: {
Expand Down
21 changes: 21 additions & 0 deletions src/validator/ticket/getSharedTicket.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PipeBase } from '../pipe.base';
import { query } from 'express-validator';
import { CustomResponseType } from '../../types/customResponseType';

export class GetSharedTicketPipe extends PipeBase {
public transform = () => [
this.limitValidation(
query('limit'),
CustomResponseType.INVALID_TICKET_FILTER_MESSAGE + 'limit',
),
this.positiveIntValidation(
query('page'),
CustomResponseType.INVALID_TICKET_FILTER_MESSAGE + 'page',
),
this.validationHandler,
];

constructor() {
super();
}
}

0 comments on commit c6f43a6

Please sign in to comment.