Skip to content

Commit

Permalink
Merge pull request #277 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Roger13579 authored Jun 23, 2024
2 parents 529131b + 1f71da0 commit 25ec773
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
23 changes: 18 additions & 5 deletions src/dto/ticket/createShareCodeDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import moment from 'moment';

export class CreateShareCodeDTO {
private readonly _userId: Types.ObjectId;
private readonly _ticketId: Types.ObjectId;
private readonly _orderId: Types.ObjectId;
private readonly _productId: Types.ObjectId;
private _ticketId: Types.ObjectId | undefined;
private _shareCode: string = '';

get shareCode() {
Expand All @@ -17,12 +19,23 @@ export class CreateShareCodeDTO {
this._shareCode = value;
}

get ticketId(): Types.ObjectId | undefined {
return this._ticketId;
}

set ticketId(value: Types.ObjectId) {
this._ticketId = value;
}

get userId() {
return this._userId;
}

get ticketId() {
return this._ticketId;
get orderId() {
return this._orderId;
}
get productId() {
return this._productId;
}

get availableFilter(): FilterQuery<ITicket> {
Expand All @@ -36,8 +49,8 @@ export class CreateShareCodeDTO {

constructor(req: ITransferTicketReq) {
const { user, body } = req;

this._userId = new Types.ObjectId((user as IUser)._id as string);
this._ticketId = new Types.ObjectId(body.ticketId as string);
this._orderId = new Types.ObjectId(body.orderId as string);
this._productId = new Types.ObjectId(body.productId as string);
}
}
18 changes: 15 additions & 3 deletions src/service/ticketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,23 @@ export class TicketService {
};

public updateShareCode = async (createShareCodeDto: CreateShareCodeDTO) => {
const { ticketId } = createShareCodeDto;
createShareCodeDto.shareCode = await this.genShareCode(ticketId);
const { orderId, productId, userId } = createShareCodeDto;
const tickets =
(await this.ticketRepository.findTransferableTicketByOrderIdAndProductId(
userId,
orderId,
productId,
)) as ITicket[];
if (!tickets || tickets.length < 2) {
throwError(
CustomResponseType.TICKET_NOT_ENOUGH_MESSAGE,
CustomResponseType.TICKET_NOT_ENOUGH,
);
}
createShareCodeDto.ticketId = tickets[0]._id;
createShareCodeDto.shareCode = await this.genShareCode(tickets[0]._id);
const ticket =
await this.ticketRepository.updateShareCode(createShareCodeDto);

if (!ticket) {
throwError(
CustomResponseType.TRANSFER_TICKET_CREATE_ERROR_MESSAGE +
Expand Down
11 changes: 9 additions & 2 deletions src/swagger/definition/ticket/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const CustomRefundTicketsReason = {

const propName = {
productId: '商品 id',
orderId: '訂單 id',
userId: '擁有者 id',
ticketId: '票券 id',
status: '票券狀態',
Expand Down Expand Up @@ -59,6 +60,11 @@ const property = {
description: propName.ticketId,
example: ticket.ticketId,
},
orderId: {
type: 'string',
description: propName.ticketId,
example: ticket.ticketId,
},
status: {
type: 'string',
enum: validEditStatus,
Expand Down Expand Up @@ -120,9 +126,10 @@ export const CustomEditTicketsObj = {

export const CustomCreateShareCodeObj = {
type: 'object',
required: ['ticketId'],
required: ['orderId', 'productId'],
properties: {
ticketId: property.ticketId,
orderId: property.orderId,
productId: property.productId,
},
};

Expand Down
3 changes: 2 additions & 1 deletion src/types/ticket.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export enum TicketProcess {

export interface ITransferTicketReq extends IUserReq {
body: {
ticketId: string;
orderId: string;
productId: string;
};
}
export interface ISellTicketReq extends IUserReq {
Expand Down
8 changes: 6 additions & 2 deletions src/validator/ticket/TransferTicket.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import { CustomResponseType } from '../../types/customResponseType';
export class TransferTicketPipe extends PipeBase {
public transform = () => [
this.nonEmptyStringValidation(
body('ticketId'),
CustomResponseType.TRANSFER_TICKET_CREATE_ERROR_MESSAGE + 'ticketId',
body('orderId'),
CustomResponseType.TRANSFER_TICKET_CREATE_ERROR_MESSAGE + 'orderId',
),
this.nonEmptyStringValidation(
body('productId'),
CustomResponseType.TRANSFER_TICKET_CREATE_ERROR_MESSAGE + 'productId',
),
this.validationHandler,
];
Expand Down

0 comments on commit 25ec773

Please sign in to comment.