Skip to content
2 changes: 2 additions & 0 deletions Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"@aws-sdk/client-s3": "^3.685.0",
"@aws-sdk/s3-request-presigner": "^3.685.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"amadeus": "^11.0.0",
"amadeus-ts": "^5.0.1",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"date-fns": "^4.1.0",
Expand Down
12 changes: 12 additions & 0 deletions Backend/src/Config/amadeus.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dotenv from "dotenv";

const Amadeus = require("amadeus");

dotenv.config();

const amadeus = new Amadeus({
clientId: process.env.AMADEUS_CLIENT_ID!,
clientSecret: process.env.AMADEUS_CLIENT_SECRET!,
});

export default amadeus;
67 changes: 67 additions & 0 deletions Backend/src/Controllers/Flight/flight.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { NextFunction, Request, Response } from "express";

import HttpError from "../../Errors/HttpError";
import { Flight, IFlight } from "../../Models/Flight/flight.model";
import {
FirstFlight,
Itinerary,
Segment,
bookFlightService,
deleteFlight,
searchFlightsApi,
} from "../../Services/Flight/flight.service";

export const bookFlight = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const flight: IFlight = req.body;
const userid = req.headers.userid;
if (!userid) {
throw new HttpError(400, "Tourist ID is required");
// res.status(500).json({ error: "Tourist ID is required" });
}
const bookedFlight = await bookFlightService(flight, userid.toString());

// // Save the flight to the database
// const savedFlight = await bookedFlight.save();
// console.log("First flight saved:", savedFlight);

res.status(201).json(bookedFlight);
} catch (error) {
// return res.status(500).json({ error: "Internal Server Error" });
next(error);
}
};

export const searchFlights = async (req: Request, res: Response) => {
try {
console.log(req.body);
const flights = await searchFlightsApi(req.body);
//console.log("Flights found:", flights);
return res.status(200).json(flights);
} catch (error) {
console.error("Error in searchFlights:", error);
return res.status(500).json({ error: "Internal Server Error" });
}
};

export const deleteFlightController = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const id = req.params.id;
const userid = req.headers.userid;
if (!userid) {
return res.status(500).json({ error: "Tourist ID is required" });
}
await deleteFlight(id, userid.toString());
res.status(200).send("Flight deleted successfully");
} catch (error) {
next(error);
}
};
85 changes: 85 additions & 0 deletions Backend/src/Models/Flight/flight.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Document, Schema, Types, model } from "mongoose";

import { schemaConfig } from "../../Config/schemaConfig";

export interface IFlight extends Document {
ticketType: "one-way" | "round-trip";
departure: {
departureTime: Date;
arrivalTime: Date;
duration: string;
from: string;
to: string;
airLine: string;
flightNumber: string;
};
returnTrip?: {
departureTime: Date;
arrivalTime: Date;
duration: string;
from: string;
to: string;
airLine: string;
flightNumber: string;
};
// segments: {
// leg: number;
// departureTime: Date;
// arrivalTime: Date;
// from: string;
// to: string;
// }[];
price: number;
travelClass: string;
bookedTickets: number;
touristID: Types.ObjectId;
}

const FlightSchema = new Schema<IFlight>(
{
ticketType: {
type: String,
enum: ["one-way", "round-trip"],
required: true,
},
departure: {
// location: { type: String, required: true },
departureTime: { type: Date, required: true },
arrivalTime: { type: Date, required: true },
duration: { type: String, required: true },
from: { type: String, required: true },
to: { type: String, required: true },
airLine: { type: String, required: true },
flightNumber: { type: String, required: true },
},
returnTrip: {
departureTime: { type: Date },
arrivalTime: { type: Date },
duration: { type: String },
from: { type: String },
to: { type: String },
airLine: { type: String },
flightNumber: { type: String },
},
// segments: [
// {
// leg: { type: Number, required: true },
// departureTime: { type: Date, required: true },
// arrivalTime: { type: Date, required: true },
// from: { type: String, required: true },
// to: { type: String, required: true },
// },
// ],
price: { type: Number, required: true },
travelClass: { type: String, required: true },
bookedTickets: { type: Number, required: true },
touristID: {
type: Schema.Types.ObjectId,
// required: true,
ref: "Tourist",
},
},
schemaConfig,
);

export const Flight = model<IFlight>("Flight", FlightSchema);
2 changes: 2 additions & 0 deletions Backend/src/Models/Users/tourist.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ITourist extends Document {
bookedItineraries: Types.ObjectId[];
bookedActivities: Types.ObjectId[];
bookedTransportations: Types.ObjectId[];
bookedFlights: Types.ObjectId[];
purchaseProducts: Types.ObjectId[];
isDeleted?: boolean;
}
Expand Down Expand Up @@ -56,6 +57,7 @@ const touristSchema = new Schema<ITourist>(
{ type: Schema.Types.ObjectId, ref: "Transportation" },
,
],
bookedFlights: [{ type: Schema.Types.ObjectId, ref: "Flight" }],
purchaseProducts: [{ type: Schema.Types.ObjectId, ref: "Product" }],
isDeleted: { type: Boolean, default: false },
},
Expand Down
15 changes: 15 additions & 0 deletions Backend/src/Routes/Flight/flight.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Router } from "express";

import {
bookFlight,
deleteFlightController,
searchFlights,
} from "../../Controllers/Flight/flight.controller";

const productRouter = Router();

productRouter.post("/book", bookFlight);
productRouter.post("/search", searchFlights);
productRouter.delete("/delete/:id", deleteFlightController);

export default productRouter;
Loading