| 1 | import { GenericErrorEntity } from 'src/common/entites/generic-error.entity'; |
| 2 | import { Auth } |
| 3 | |
| 4 | import { |
| 5 | BadRequestException, |
| 6 | Body, |
| 7 | Controller, |
| 8 | Delete, |
| 9 | Get, |
| 10 | InternalServerErrorException, |
| 11 | Param, |
| 12 | Post, |
| 13 | Put, |
| 14 | Query, |
| 15 | UnauthorizedException, |
| 16 | } from '@nestjs/common'; |
| 17 | import { |
| 18 | ApiBadRequestResponse, |
| 19 | ApiBearerAuth, |
| 20 | ApiInternalServerErrorResponse, |
| 21 | ApiNotFoundResponse, |
| 22 | ApiOperation, |
| 23 | ApiParam, |
| 24 | ApiQuery, |
| 25 | ApiResponse, |
| 26 | ApiTags, |
| 27 | ApiUnauthorizedResponse, |
| 28 | } from '@nestjs/swagger'; |
| 29 | import { User } |
| 30 | |
| 31 | import { GetFlightTrackDTO } |
| 32 | import { GetFlightDTO } |
| 33 | import { GetFlightSearchDTO } |
| 34 | import { CreateFlightBookingDTO } |
| 35 | import { UpdateFlightBookingDTO } |
| 36 | import { FlightTrackResponseEntity } |
| 37 | import { |
| 38 | FlightResponseEntity, |
| 39 | FlightSearchResponseEntity, |
| 40 | } from './entities/flight.entity'; |
| 41 | import { FlightBookingEntity } |
| 42 | import { FlightService } |
| 43 | |
| 44 | @Controller('flight') |
| 45 | @ApiTags('Flight') |
| 46 | @ApiBearerAuth('JWT-auth') |
| 47 | export class FlightController |
| 48 | constructor(private readonly |
| 49 | |
| 50 | @Get('track' |
| 51 | @ApiOperation({ |
| 52 | summary: 'Get flight track/path', |
| 53 | description: |
| 54 | 'Get the flight path with all position data points for a specific flight', |
| 55 | }) |
| 56 | @ApiQuery({ |
| 57 | name: 'iata', |
| 58 | required: true, |
| 59 | type: String, |
| 60 | description: 'IATA flight number', |
| 61 | example: 'UA123', |
| 62 | }) |
| 63 | @ApiQuery({ |
| 64 | name: 'icao', |
| 65 | required: true, |
| 66 | type: String, |
| 67 | description: 'ICAO flight number', |
| 68 | example: 'UAL123', |
| 69 | }) |
| 70 | @ApiQuery({ |
| 71 | name: 'date', |
| 72 | required: false, |
| 73 | type: String, |
| 74 | description: |
| 75 | 'Flight date (ISO format). Defaults to current date if not provided', |
| 76 | example: '2024-03-20', |
| 77 | }) |
| 78 | @ApiResponse({ |
| 79 | status: 200, |
| 80 | description: 'Flight track data retrieved successfully', |
| 81 | type: FlightTrackResponseEntity, |
| 82 | }) |
| 83 | @ApiUnauthorizedResponse({ |
| 84 | description: 'User is not authenticated', |
| 85 | type: GenericErrorEntity, |
| 86 | }) |
| 87 | @ApiBadRequestResponse({ |
| 88 | description: 'Invalid request parameters or flight not found', |
| 89 | type: GenericErrorEntity, |
| 90 | }) |
| 91 | @ApiInternalServerErrorResponse({ |
| 92 | description: 'Internal server error', |
| 93 | type: GenericErrorEntity, |
| 94 | }) |
| 95 | @ApiQuery({ |
| 96 | type: String, |
| 97 | name: 'timezone', |
| 98 | description: 'The timezone from which the request is being made.', |
| 99 | required: true, |
| 100 | }) |
| 101 | async getFlightTrack(@ |
| 102 | return await this |
| 103 | } |
| 104 | |
| 105 | @Get('search' |
| 106 | @ApiOperation({ |
| 107 | summary: 'Search flights', |
| 108 | description: |
| 109 | 'Search for flights by identifier and optional date. Returns lightweight flight summaries for selection.', |
| 110 | }) |
| 111 | @ApiQuery({ |
| 112 | name: 'iata', |
| 113 | required: true, |
| 114 | type: String, |
| 115 | description: 'IATA flight number', |
| 116 | example: 'UA123', |
| 117 | }) |
| 118 | @ApiQuery({ |
| 119 | name: 'icao', |
| 120 | required: true, |
| 121 | type: String, |
| 122 | description: 'ICAO flight number', |
| 123 | example: 'UAL123', |
| 124 | }) |
| 125 | @ApiQuery({ |
| 126 | name: 'date', |
| 127 | required: false, |
| 128 | type: String, |
| 129 | description: |
| 130 | 'Flight date (ISO format). Filters flights by date if provided', |
| 131 | example: '2024-03-20', |
| 132 | }) |
| 133 | @ApiQuery({ |
| 134 | type: String, |
| 135 | name: 'timezone', |
| 136 | description: 'The timezone from which the request is being made.', |
| 137 | required: true, |
| 138 | }) |
| 139 | @ApiResponse({ |
| 140 | status: 200, |
| 141 | description: 'Flight search results', |
| 142 | type: FlightSearchResponseEntity, |
| 143 | }) |
| 144 | @ApiUnauthorizedResponse({ |
| 145 | description: 'User is not authenticated', |
| 146 | type: GenericErrorEntity, |
| 147 | }) |
| 148 | @ApiBadRequestResponse({ |
| 149 | description: 'Invalid request parameters', |
| 150 | type: GenericErrorEntity, |
| 151 | }) |
| 152 | @ApiInternalServerErrorResponse({ |
| 153 | description: 'Internal server error', |
| 154 | type: GenericErrorEntity, |
| 155 | }) |
| 156 | async searchFlights( |
| 157 | @Query() query |
| 158 | @Auth() auth |
| 159 | ) { |
| 160 | return await this |
| 161 | } |
| 162 | |
| 163 | @Get() |
| 164 | @ApiOperation({ |
| 165 | summary: 'Get flight details', |
| 166 | description: |
| 167 | 'Get detailed information about a specific flight including real-time tracking data. Use faFlightId to fetch a specific flight after searching.', |
| 168 | }) |
| 169 | @ApiQuery({ |
| 170 | name: 'iata', |
| 171 | required: true, |
| 172 | type: String, |
| 173 | description: 'IATA flight number', |
| 174 | example: 'UA123', |
| 175 | }) |
| 176 | @ApiQuery({ |
| 177 | name: 'icao', |
| 178 | required: true, |
| 179 | type: String, |
| 180 | description: 'ICAO flight number', |
| 181 | example: 'UAL123', |
| 182 | }) |
| 183 | @ApiQuery({ |
| 184 | name: 'date', |
| 185 | required: false, |
| 186 | type: String, |
| 187 | description: |
| 188 | 'Flight date (ISO format). Defaults to current date if not provided', |
| 189 | example: '2024-03-20', |
| 190 | }) |
| 191 | @ApiQuery({ |
| 192 | type: String, |
| 193 | name: 'timezone', |
| 194 | description: 'The timezone from which the request is being made.', |
| 195 | required: true, |
| 196 | }) |
| 197 | @ApiQuery({ |
| 198 | type: Boolean, |
| 199 | default: false, |
| 200 | required: false, |
| 201 | name: 'forceUpdate', |
| 202 | description: |
| 203 | 'Force update the flight data(used to refresh the flight data)', |
| 204 | }) |
| 205 | @ApiQuery({ |
| 206 | type: String, |
| 207 | required: false, |
| 208 | name: 'faFlightId', |
| 209 | description: |
| 210 | 'FlightAware flight ID to fetch a specific flight. Use this after searching for flights to get details of a selected flight.', |
| 211 | }) |
| 212 | @ApiResponse({ |
| 213 | status: 200, |
| 214 | description: 'Flight details retrieved successfully', |
| 215 | type: FlightResponseEntity, |
| 216 | }) |
| 217 | @ApiUnauthorizedResponse({ |
| 218 | description: 'User is not authenticated', |
| 219 | type: GenericErrorEntity, |
| 220 | }) |
| 221 | @ApiBadRequestResponse({ |
| 222 | description: 'Invalid request parameters', |
| 223 | type: GenericErrorEntity, |
| 224 | }) |
| 225 | @ApiInternalServerErrorResponse({ |
| 226 | description: 'Internal server error', |
| 227 | type: GenericErrorEntity, |
| 228 | }) |
| 229 | async getFlight(@ |
| 230 | return await this |
| 231 | } |
| 232 | |
| 233 | @Post('booking' |
| 234 | @ApiOperation({ |
| 235 | summary: 'Create a flight booking', |
| 236 | description: 'Create a new booking for a flight with booking details', |
| 237 | }) |
| 238 | @ApiResponse({ |
| 239 | status: 201, |
| 240 | description: 'Flight booking created successfully', |
| 241 | type: FlightBookingEntity, |
| 242 | }) |
| 243 | @ApiBadRequestResponse({ |
| 244 | description: 'Invalid request parameters or flight not found', |
| 245 | type: GenericErrorEntity, |
| 246 | }) |
| 247 | @ApiUnauthorizedResponse({ |
| 248 | description: 'User is not authenticated', |
| 249 | type: GenericErrorEntity, |
| 250 | }) |
| 251 | @ApiInternalServerErrorResponse({ |
| 252 | description: 'Internal server error', |
| 253 | type: GenericErrorEntity, |
| 254 | }) |
| 255 | async createFlightBooking( |
| 256 | @Body() createBookingDto |
| 257 | @Auth() auth |
| 258 | ) { |
| 259 | return await this |
| 260 | createBookingDto, |
| 261 | auth.id, |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | @Put('booking/:bookingId' |
| 266 | @ApiOperation({ |
| 267 | summary: 'Update a flight booking', |
| 268 | description: 'Update an existing flight booking', |
| 269 | }) |
| 270 | @ApiParam({ |
| 271 | name: 'bookingId', |
| 272 | description: 'ID of the booking to update', |
| 273 | example: 'booking_abc123', |
| 274 | }) |
| 275 | @ApiResponse({ |
| 276 | status: 200, |
| 277 | description: 'Flight booking updated successfully', |
| 278 | type: FlightBookingEntity, |
| 279 | }) |
| 280 | @ApiBadRequestResponse({ |
| 281 | description: 'Invalid request parameters', |
| 282 | type: GenericErrorEntity, |
| 283 | }) |
| 284 | @ApiNotFoundResponse({ |
| 285 | description: 'Booking not found', |
| 286 | type: GenericErrorEntity, |
| 287 | }) |
| 288 | @ApiUnauthorizedResponse({ |
| 289 | description: 'User is not authenticated', |
| 290 | type: GenericErrorEntity, |
| 291 | }) |
| 292 | @ApiInternalServerErrorResponse({ |
| 293 | description: 'Internal server error', |
| 294 | type: GenericErrorEntity, |
| 295 | }) |
| 296 | async updateFlightBooking( |
| 297 | @Param('bookingId' |
| 298 | @Body() updateBookingDto |
| 299 | @Auth() auth |
| 300 | ) { |
| 301 | return await this |
| 302 | bookingId, |
| 303 | updateBookingDto, |
| 304 | auth.id, |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | @Get('booking/:bookingId' |
| 309 | @ApiOperation({ |
| 310 | summary: 'Get a flight booking', |
| 311 | description: 'Get details of a specific flight booking', |
| 312 | }) |
| 313 | @ApiParam({ |
| 314 | name: 'bookingId', |
| 315 | description: 'ID of the booking to retrieve', |
| 316 | example: 'booking_abc123', |
| 317 | }) |
| 318 | @ApiResponse({ |
| 319 | status: 200, |
| 320 | description: 'Flight booking retrieved successfully', |
| 321 | type: FlightBookingEntity, |
| 322 | }) |
| 323 | @ApiNotFoundResponse({ |
| 324 | description: 'Booking not found', |
| 325 | type: GenericErrorEntity, |
| 326 | }) |
| 327 | @ApiUnauthorizedResponse({ |
| 328 | description: 'User is not authenticated', |
| 329 | type: GenericErrorEntity, |
| 330 | }) |
| 331 | @ApiInternalServerErrorResponse({ |
| 332 | description: 'Internal server error', |
| 333 | type: GenericErrorEntity, |
| 334 | }) |
| 335 | async getFlightBooking( |
| 336 | @Param('bookingId' |
| 337 | @Auth() auth |
| 338 | ) { |
| 339 | return await this |
| 340 | } |
| 341 | |
| 342 | @Get('booking/flight/:flightId' |
| 343 | @ApiOperation({ |
| 344 | summary: 'Get all bookings for a flight', |
| 345 | description: 'Get all bookings associated with a specific flight', |
| 346 | }) |
| 347 | @ApiParam({ |
| 348 | name: 'flightId', |
| 349 | description: 'ID of the flight', |
| 350 | example: 'flight_abc123', |
| 351 | }) |
| 352 | @ApiResponse({ |
| 353 | status: 200, |
| 354 | description: 'Flight bookings retrieved successfully', |
| 355 | type: [FlightBookingEntity], |
| 356 | }) |
| 357 | @ApiBadRequestResponse({ |
| 358 | description: 'Flight not found', |
| 359 | type: GenericErrorEntity, |
| 360 | }) |
| 361 | @ApiUnauthorizedResponse({ |
| 362 | description: 'User is not authenticated', |
| 363 | type: GenericErrorEntity, |
| 364 | }) |
| 365 | @ApiInternalServerErrorResponse({ |
| 366 | description: 'Internal server error', |
| 367 | type: GenericErrorEntity, |
| 368 | }) |
| 369 | async getFlightBookingsByFlightId( |
| 370 | @Param('flightId' |
| 371 | @Auth() auth |
| 372 | ) { |
| 373 | return await this |
| 374 | flightId, |
| 375 | auth.id, |
| 376 | ); |
| 377 | } |
| 378 | |
| 379 | @Delete('booking/:bookingId' |
| 380 | @ApiOperation({ |
| 381 | summary: 'Delete a flight booking', |
| 382 | description: 'Delete a flight booking', |
| 383 | }) |
| 384 | @ApiParam({ |
| 385 | name: 'bookingId', |
| 386 | description: 'ID of the booking to delete', |
| 387 | example: 'booking_abc123', |
| 388 | }) |
| 389 | @ApiResponse({ |
| 390 | status: 200, |
| 391 | description: 'Flight booking deleted successfully', |
| 392 | schema: { |
| 393 | type: 'object', |
| 394 | properties: { |
| 395 | message: { |
| 396 | type: 'string', |
| 397 | example: 'Booking deleted successfully', |
| 398 | }, |
| 399 | }, |
| 400 | }, |
| 401 | }) |
| 402 | @ApiNotFoundResponse({ |
| 403 | description: 'Booking not found', |
| 404 | type: GenericErrorEntity, |
| 405 | }) |
| 406 | @ApiUnauthorizedResponse({ |
| 407 | description: 'User is not authenticated', |
| 408 | type: GenericErrorEntity, |
| 409 | }) |
| 410 | @ApiInternalServerErrorResponse({ |
| 411 | description: 'Internal server error', |
| 412 | type: GenericErrorEntity, |
| 413 | }) |
| 414 | async deleteFlightBooking( |
| 415 | @Param('bookingId' |
| 416 | @Auth() auth |
| 417 | ) { |
| 418 | return await this |
| 419 | } |
| 420 | } |