| 1 | import { prisma } from 'src/db'; |
| 2 | |
| 3 | import { HttpException, HttpStatus |
| 4 | import { ConfigService } |
| 5 | |
| 6 | import { GetAirlineDTO } |
| 7 | import { GetFlightsDTO } |
| 8 | |
| 9 | @Injectable() |
| 10 | export class FlightsService |
| 11 | constructor(private configService |
| 12 | |
| 13 | async getAirline(body |
| 14 | const airline = |
| 15 | where: { |
| 16 | OR: [ |
| 17 | { |
| 18 | iata: { |
| 19 | equals: body.search |
| 20 | mode: 'insensitive', |
| 21 | }, |
| 22 | icao: { |
| 23 | equals: body.search |
| 24 | mode: 'insensitive', |
| 25 | }, |
| 26 | }, |
| 27 | ], |
| 28 | }, |
| 29 | }); |
| 30 | if (!airline) |
| 31 | throw new HttpException( |
| 32 | return airline; |
| 33 | } |
| 34 | |
| 35 | async getFlightsBetweenTwoAirports(query |
| 36 | const { from, |
| 37 | const apiKey = |
| 38 | |
| 39 | if (!apiKey) |
| 40 | throw new HttpException( |
| 41 | 'Internal Server Error', |
| 42 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | const _date = |
| 47 | const dayEndDate = |
| 48 | dayEndDate.setDate(_date |
| 49 | |
| 50 | // First check in database |
| 51 | try { |
| 52 | const existingFlights = |
| 53 | where: { |
| 54 | AND: [ |
| 55 | { |
| 56 | arrival: { |
| 57 | path: ['iataCode'], |
| 58 | string_contains: to, |
| 59 | }, |
| 60 | departure: { |
| 61 | path: ['iataCode'], |
| 62 | string_contains: from, |
| 63 | }, |
| 64 | }, |
| 65 | // Uncomment below for production |
| 66 | // { |
| 67 | // date: { |
| 68 | // gte: _date, |
| 69 | // lt: dayEndDate, |
| 70 | // }, |
| 71 | // }, |
| 72 | ], |
| 73 | }, |
| 74 | orderBy: { |
| 75 | createdAt: 'desc', |
| 76 | }, |
| 77 | }); |
| 78 | if (existingFlights.length |
| 79 | return { |
| 80 | success: true, |
| 81 | date, |
| 82 | route: { from, |
| 83 | flightCount: existingFlights.length, |
| 84 | flights: existingFlights, |
| 85 | }; |
| 86 | } |
| 87 | } catch (error) |
| 88 | console.error('Database query error: |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | const apiUrl = |
| 93 | apiUrl.searchParams.append |
| 94 | apiUrl.searchParams.append |
| 95 | apiUrl.searchParams.append |
| 96 | |
| 97 | const response = |
| 98 | |
| 99 | if (!response.ok |
| 100 | console.log(await |
| 101 | throw new Error( |
| 102 | } |
| 103 | |
| 104 | const flights = |
| 105 | |
| 106 | await Promise.all |
| 107 | flights.data.map |
| 108 | prisma.flights.create |
| 109 | data: { |
| 110 | date: _date, |
| 111 | airline: flight.airline |
| 112 | arrival: flight.arrival |
| 113 | codeshared: flight.codeshared |
| 114 | flight: flight.flight |
| 115 | departure: flight.departure |
| 116 | type: flight.type |
| 117 | status: flight.status |
| 118 | }, |
| 119 | }), |
| 120 | ), |
| 121 | ); |
| 122 | |
| 123 | const routeFlights = |
| 124 | (flight) => |
| 125 | flight.departure?.iataCode |
| 126 | flight.arrival?.iataCode |
| 127 | ); |
| 128 | |
| 129 | return { |
| 130 | success: true, |
| 131 | date, |
| 132 | route: { from, |
| 133 | flightCount: routeFlights.length, |
| 134 | flights: routeFlights, |
| 135 | }; |
| 136 | } catch (error) |
| 137 | console.error('Error fetching flights: |
| 138 | throw new HttpException( |
| 139 | 'Failed to fetch flight data', |
| 140 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 141 | ); |
| 142 | } |
| 143 | } |
| 144 | } |