events.service.ts154 lines, 3.5 KB
| 1 | import { |
| 2 | CreateEventDto, |
| 3 | EventResponse, |
| 4 | SERVICES_PORTS, |
| 5 | UpdateEventDto, |
| 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 EventsService { |
| 13 | private readonly eventServiceUrl = |
| 14 | process.env.EVENTS_SERVICE_URL || |
| 15 | `http://localhost:${SERVICES_PORTS.EVENTS_SERVICE}`; |
| 16 | |
| 17 | constructor(private readonly httpService: HttpService) {} |
| 18 | |
| 19 | async create( |
| 20 | data: CreateEventDto, |
| 21 | userId: string, |
| 22 | userRole: string, |
| 23 | ): Promise<EventResponse> { |
| 24 | try { |
| 25 | const response = await firstValueFrom( |
| 26 | this.httpService.post<EventResponse>(this.eventServiceUrl, data, { |
| 27 | headers: { 'x-user-id': userId, 'x-user-role': userRole }, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | return response.data; |
| 32 | } catch (error) { |
| 33 | this.handleError(error); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | async findAll(): Promise<EventResponse[]> { |
| 38 | try { |
| 39 | const response = await firstValueFrom( |
| 40 | this.httpService.get<EventResponse[]>(this.eventServiceUrl), |
| 41 | ); |
| 42 | |
| 43 | return response.data; |
| 44 | } catch (error) { |
| 45 | this.handleError(error); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | async findMyEvents(userId: string): Promise<EventResponse[]> { |
| 50 | try { |
| 51 | const response = await firstValueFrom( |
| 52 | this.httpService.get<EventResponse[]>( |
| 53 | `${this.eventServiceUrl}/my-events`, |
| 54 | { |
| 55 | headers: { 'x-user-id': userId }, |
| 56 | }, |
| 57 | ), |
| 58 | ); |
| 59 | |
| 60 | return response.data; |
| 61 | } catch (error) { |
| 62 | this.handleError(error); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | async findOne(id: string): Promise<EventResponse> { |
| 67 | try { |
| 68 | const response = await firstValueFrom( |
| 69 | this.httpService.get<EventResponse>(`${this.eventServiceUrl}/${id}`), |
| 70 | ); |
| 71 | |
| 72 | return response.data; |
| 73 | } catch (error) { |
| 74 | this.handleError(error); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | async update( |
| 79 | id: string, |
| 80 | data: UpdateEventDto, |
| 81 | userId: string, |
| 82 | userRole: string, |
| 83 | ): Promise<EventResponse> { |
| 84 | try { |
| 85 | const response = await firstValueFrom( |
| 86 | this.httpService.put<EventResponse>( |
| 87 | `${this.eventServiceUrl}/${id}`, |
| 88 | data, |
| 89 | { |
| 90 | headers: { 'x-user-id': userId, 'x-user-role': userRole }, |
| 91 | }, |
| 92 | ), |
| 93 | ); |
| 94 | |
| 95 | return response.data; |
| 96 | } catch (error) { |
| 97 | this.handleError(error); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | async publish( |
| 102 | id: string, |
| 103 | userId: string, |
| 104 | userRole: string, |
| 105 | ): Promise<EventResponse> { |
| 106 | try { |
| 107 | const response = await firstValueFrom( |
| 108 | this.httpService.post<EventResponse>( |
| 109 | `${this.eventServiceUrl}/${id}/publish`, |
| 110 | {}, |
| 111 | { |
| 112 | headers: { 'x-user-id': userId, 'x-user-role': userRole }, |
| 113 | }, |
| 114 | ), |
| 115 | ); |
| 116 | |
| 117 | return response.data; |
| 118 | } catch (error) { |
| 119 | this.handleError(error); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | async cancel( |
| 124 | id: string, |
| 125 | userId: string, |
| 126 | userRole: string, |
| 127 | ): Promise<EventResponse> { |
| 128 | try { |
| 129 | const response = await firstValueFrom( |
| 130 | this.httpService.post<EventResponse>( |
| 131 | `${this.eventServiceUrl}/${id}/cancel`, |
| 132 | {}, |
| 133 | { |
| 134 | headers: { 'x-user-id': userId, 'x-user-role': userRole }, |
| 135 | }, |
| 136 | ), |
| 137 | ); |
| 138 | |
| 139 | return response.data; |
| 140 | } catch (error) { |
| 141 | this.handleError(error); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private handleError(error: unknown): never { |
| 146 | const err = error as { |
| 147 | response?: { data: string | object; status: number }; |
| 148 | }; |
| 149 | if (err.response) { |
| 150 | throw new HttpException(err.response.data, err.response.status); |
| 151 | } |
| 152 | throw new HttpException('Something went wrong', 503); |
| 153 | } |
| 154 | } |