Skip to content

Commit

Permalink
Merge pull request #58 from jeevan-aj/update
Browse files Browse the repository at this point in the history
completed filter and sort :rocket
  • Loading branch information
jeevan-aj authored May 4, 2024
2 parents 7b9a557 + 994e631 commit 2de940b
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 348 deletions.
82 changes: 75 additions & 7 deletions backend/controllers/userControllers/userBookingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Booking from "../../models/BookingModel.js";
import { errorHandler } from "../../utils/error.js";
import Razorpay from "razorpay";
import { availableAtDate } from "../../services/checkAvailableVehicle.js";
import Vehicle from "../../models/vehicleModel.js";

export const BookCar = async (req, res, next) => {
try {
Expand Down Expand Up @@ -52,8 +53,6 @@ export const BookCar = async (req, res, next) => {
}
};



//createing razorpay instance
export const razorpayOrder = async (req, res, next) => {
try {
Expand Down Expand Up @@ -91,9 +90,7 @@ export const getVehiclesWithoutBooking = async (req, res, next) => {
return next(errorHandler(409, "pickup District and location needed"));

if (!pickupDate || !dropOffDate)
return next(
errorHandler(409, "pickup , dropffdate is required")
);
return next(errorHandler(409, "pickup , dropffdate is required"));

// Check if pickupDate is before dropOffDate
if (pickupDate >= dropOffDate)
Expand All @@ -112,7 +109,10 @@ export const getVehiclesWithoutBooking = async (req, res, next) => {
}

const availableVehicles = vehiclesAvailableAtDate.filter(
(cur) => cur.district === pickUpDistrict && cur.location == pickUpLocation && cur.isDeleted === "false"
(cur) =>
cur.district === pickUpDistrict &&
cur.location == pickUpLocation &&
cur.isDeleted === "false"
);

if (!availableVehicles) {
Expand All @@ -135,6 +135,74 @@ export const getVehiclesWithoutBooking = async (req, res, next) => {
};


// filtering vehicles
export const filterVehicles = async (req, res, next) => {
try {
if (!req.body) {
next(errorHandler(401, "bad request no body"));
return;
}
const transformedData = req.body;
if (!transformedData) {
next(errorHandler(401, "select filter option first"));
}
const generateMatchStage = (data) => {
const carTypes = [];
data.forEach((cur) => {
if (cur.type === "car_type") {
// Extract the first key of the object and push it into 'cartypes' array
const firstKey = Object.keys(cur).find((key) => key !== "type");
if (firstKey) {
carTypes.push(firstKey);
}
}
});

const transmitions = [];
data.forEach((cur) => {
// If the current element has type equal to 'transmition'
if (cur.type === "transmition") {
// Iterate through each key of the current element
Object.keys(cur).forEach((key) => {
// Exclude the 'type' key and push only keys with truthy values into 'transmitions' array
if (key !== "type" && cur[key]) {
transmitions.push(key);
}
});
}
});


return {
$match: {
$and: [
carTypes.length > 0 ? { car_type: { $in: carTypes } } : null,
transmitions.length > 0
? { transmition: { $in: transmitions } }
: null,
].filter((condition) => condition !== null), // Remove null conditions
},
};
};

const matchStage = generateMatchStage(transformedData);

const filteredVehicles = await Vehicle.aggregate([matchStage]);
if (!filteredVehicles) {
next(errorHandler(401, "no vehicles found"));
return;
}
res.status(200).json({
status: "success",
data: {
filteredVehicles,
},
});
} catch (error) {
console.log(error);
next(errorHandler(500, "internal server error in fiilterVehicles"));
}
};

// -----------------------------------

Expand Down Expand Up @@ -198,4 +266,4 @@ export const getVehiclesWithoutBooking = async (req, res, next) => {
// }
// };

// ---------------------
// ---------------------
3 changes: 2 additions & 1 deletion backend/routes/userRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { verifyToken } from "../utils/verifyUser.js";
import { updateUser ,deleteUser , signOut , test} from "../controllers/userControllers/userController.js";
import { listAllVehicles, searchCar, showVehicleDetails } from "../controllers/userControllers/userAllVehiclesController.js";
import { editUserProfile } from "../controllers/userControllers/userProfileController.js";
import { BookCar, razorpayOrder, getVehiclesWithoutBooking } from "../controllers/userControllers/userBookingController.js";
import { BookCar, razorpayOrder, getVehiclesWithoutBooking, filterVehicles } from "../controllers/userControllers/userBookingController.js";


const router = express.Router()
Expand All @@ -21,6 +21,7 @@ router.post('/getVehiclesWithoutBooking',getVehiclesWithoutBooking)
// router.post('/checkAvailability',checkAvailability)
router.post('/razorpay',razorpayOrder)
router.post('/bookCar',BookCar)
router.post('/filterVehicles',filterVehicles)



Expand Down
Loading

0 comments on commit 2de940b

Please sign in to comment.