ticket.service.ts134 lines, 3.2 KB
| 1 | import { |
| 2 | CheckInTicketDto, |
| 3 | PurchaseTicketDto, |
| 4 | SERVICES_PORTS, |
| 5 | TicketResponse, |
| 6 | } from '@app/common'; |
| 7 | import { HttpService } from '@nestjs/axios'; |
| 8 | import { HttpException, Injectable } from '@nestjs/common'; |
| 9 | import { firstValueFrom } from 'rxjs'; |
| 10 | |
| 11 | @Injectable() |
| 12 | export class TicketService { |
| 13 | private readonly ticketServiceUrl = |
| 14 | process.env.TICKETS_SERVICE_URL || |
| 15 | `http://localhost:${SERVICES_PORTS.TICKETS_SERVICE}`; |
| 16 | |
| 17 | constructor(private readonly httpService: HttpService) {} |
| 18 | |
| 19 | async purchase( |
| 20 | data: PurchaseTicketDto, |
| 21 | userId: string, |
| 22 | ): Promise<TicketResponse[]> { |
| 23 | try { |
| 24 | const response = await firstValueFrom( |
| 25 | this.httpService.post<TicketResponse[]>( |
| 26 | `${this.ticketServiceUrl}/purchase`, |
| 27 | data, |
| 28 | { |
| 29 | headers: { 'x-user-id': userId }, |
| 30 | }, |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | return response.data; |
| 35 | } catch (error) { |
| 36 | this.handleError(error); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | async findMyTickets(userId: string): Promise<TicketResponse[]> { |
| 41 | try { |
| 42 | const response = await firstValueFrom( |
| 43 | this.httpService.get<TicketResponse[]>( |
| 44 | `${this.ticketServiceUrl}/my-tickets`, |
| 45 | { |
| 46 | headers: { 'x-user-id': userId }, |
| 47 | }, |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | return response.data; |
| 52 | } catch (error) { |
| 53 | this.handleError(error); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | async findOne(id: string, userId: string): Promise<TicketResponse> { |
| 58 | try { |
| 59 | const response = await firstValueFrom( |
| 60 | this.httpService.get<TicketResponse>(`${this.ticketServiceUrl}/${id}`, { |
| 61 | headers: { 'x-user-id': userId }, |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | return response.data; |
| 66 | } catch (error) { |
| 67 | this.handleError(error); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | async cancel(id: string, userId: string): Promise<TicketResponse> { |
| 72 | try { |
| 73 | const response = await firstValueFrom( |
| 74 | this.httpService.post<TicketResponse>( |
| 75 | `${this.ticketServiceUrl}/${id}/cancel`, |
| 76 | {}, |
| 77 | { headers: { 'x-user-id': userId } }, |
| 78 | ), |
| 79 | ); |
| 80 | return response.data; |
| 81 | } catch (error) { |
| 82 | this.handleError(error); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | async checkIn( |
| 87 | data: CheckInTicketDto, |
| 88 | organizerId: string, |
| 89 | ): Promise<TicketResponse> { |
| 90 | try { |
| 91 | const response = await firstValueFrom( |
| 92 | this.httpService.post<TicketResponse>( |
| 93 | `${this.ticketServiceUrl}/check-in`, |
| 94 | data, |
| 95 | { |
| 96 | headers: { 'x-user-id': organizerId }, |
| 97 | }, |
| 98 | ), |
| 99 | ); |
| 100 | return response.data; |
| 101 | } catch (error) { |
| 102 | this.handleError(error); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | async findEventTickets( |
| 107 | eventId: string, |
| 108 | organizerId: string, |
| 109 | ): Promise<TicketResponse[]> { |
| 110 | try { |
| 111 | const response = await firstValueFrom( |
| 112 | this.httpService.get<TicketResponse[]>( |
| 113 | `${this.ticketServiceUrl}/event/${eventId}`, |
| 114 | { |
| 115 | headers: { 'x-user-id': organizerId }, |
| 116 | }, |
| 117 | ), |
| 118 | ); |
| 119 | return response.data; |
| 120 | } catch (error) { |
| 121 | this.handleError(error); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private handleError(error: unknown): never { |
| 126 | const err = error as { |
| 127 | response?: { data: string | object; status: number }; |
| 128 | }; |
| 129 | if (err.response) { |
| 130 | throw new HttpException(err.response.data, err.response.status); |
| 131 | } |
| 132 | throw new HttpException('Something went wrong', 503); |
| 133 | } |
| 134 | } |