Skip to content

Commit

Permalink
Merge pull request #230 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 8819a2d + d4e7a49 commit 34a3dd3
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/controller/ticketController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ 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);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
new GetTicketVo(info, page, limit),
);
};

public getTicketDetail = async (req: IUserReq) => {
const getTicketsDto = new GetTicketDetailDto(req);
const ticket = await this.ticketService.getTicketDetail(getTicketsDto);
Expand Down
4 changes: 2 additions & 2 deletions src/dto/user/userDetailDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export class UserDetailDto {

constructor(req: IUpdateUserDetailReq) {
this._id = (req.user as IUser).id.toString();
const { name, birthDate, gender, phone, address, avatarPath } = req.body;
const { name, birthDate, gender, phone, address, imgUrl } = req.body;
this._name = name;
this._birthDate = birthDate;
this._gender = gender;
this._phone = phone;
this._address = address;
this._avatarPath = avatarPath;
this._avatarPath = imgUrl;
}
}
5 changes: 4 additions & 1 deletion src/models/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const schema = new Schema<ITicket>(
type: Date,
select: true,
},
writeOffStaffId: userId,
writeOffStaffId: {
type: Types.ObjectId,
select: true,
},
shareCode: { type: String },
},
{ ...schemaOption, ...virtualSchemaOption },
Expand Down
12 changes: 11 additions & 1 deletion src/repository/ticketRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { EditTicketsDTO } from '../dto/ticket/editTicketsDto';
import { CreateShareCodeDTO } from '../dto/ticket/createShareCodeDto';
import { TransferTicketDTO } from '../dto/ticket/transferTicketDto';
import moment from 'moment';
import { createGetTicketPipeline } from '../utils/aggregate/ticket/getTickets.pipeline';
import {
createGetSharedTicketPipeline,
createGetTicketPipeline,
} from '../utils/aggregate/ticket/getTickets.pipeline';
import { GetTicketDetailDto } from '../dto/ticket/getTicketDetailDto';
import { createGetTicketDetailPipeline } from '../utils/aggregate/ticket/getTicketDetail.pipeline';
import { SellTicketDto } from '../dto/ticket/sellTicketDto';
Expand All @@ -33,6 +36,13 @@ export class TicketRepository {
const results = await TicketModel.aggregate(pipeline);
return results[0];
};
public findSharedTickets = async (
ticketFilterDto: GetTicketsDto,
): Promise<IGetTicketsRes> => {
const pipeline = createGetSharedTicketPipeline(ticketFilterDto);
const results = await TicketModel.aggregate(pipeline);
return results[0];
};

public findTransferableTicketByOrderIdAndProductId = async (
sellTicketDto: SellTicketDto,
Expand Down
26 changes: 26 additions & 0 deletions src/routes/ticketRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,32 @@ export class TicketRoute extends BaseRoute {
this.responseHandler(this.controller.getTicketDetail),
);

this.router.get(
'/v1/ticket-shared',
/**
* #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.responseHandler(this.controller.getSharedTickets),
);

this.router.patch(
'/v1/ticket',
/**
Expand Down
3 changes: 3 additions & 0 deletions src/service/ticketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class TicketService {

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

public async createTickets(order: IOrder) {
const orderProducts = order.products.reduce(
Expand Down
2 changes: 1 addition & 1 deletion src/types/user.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ export interface IUpdateUserDetailReq extends IUserReq {
gender: Gender;
phone: string;
address: string;
avatarPath: string;
imgUrl: string;
};
}
84 changes: 84 additions & 0 deletions src/utils/aggregate/ticket/getTickets.pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,87 @@ export const createGetTicketPipeline = ({
{ $unwind: '$metadata' },
];
};

export const createGetSharedTicketPipeline = ({
options,
sort,
limit,
page,
}: GetTicketsDto) => {
return [
{
$match: {
isPublished: { $eq: true },
},
},
{
$lookup: {
localField: 'productId',
from: 'products',
foreignField: '_id',
as: 'product',
pipeline: [
{
$project: options.productSelect,
},
],
},
},
{ $unwind: '$product' },
// Grouping by orderId and productId
{
$group: {
_id: {
orderId: '$orderId',
productId: '$productId',
},
tickets: { $push: '$$ROOT' },
count: { $sum: 1 },
},
},
{
$project: {
_id: 0,
count: 1,
ticket: { $arrayElemAt: ['$tickets', 0] },
},
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: ['$ticket', { count: '$count' }],
},
},
},
{
$facet: {
metadata: [{ $count: 'totalCount' }],
tickets: [
{ $sort: sort },
{ $skip: (page - 1) * limit },
{ $limit: limit },
{
$project: {
count: 1,
...options.ticketSelect,
},
},
],
},
},
{
$set: {
metadata: [
{
$cond: {
if: { $eq: [{ $size: '$metadata' }, 0] },
then: [{ totalCount: 0 }],
else: '$metadata',
},
},
],
},
},
{ $unwind: '$metadata' },
];
};

0 comments on commit 34a3dd3

Please sign in to comment.