Skip to content

Commit

Permalink
refactor few things
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehedi Hasan authored and Mehedi Hasan committed Oct 10, 2024
1 parent 936fe96 commit aa16a31
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 13 deletions.
3 changes: 3 additions & 0 deletions dist/app/modules/Auth/auth.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const signin = (payload) => __awaiter(void 0, void 0, void 0, function* () {
if (!user) {
throw new Error("User not found");
}
if ((user === null || user === void 0 ? void 0 : user.status) === "blocked") {
throw new Error("Your account has been blocked. Please contact support for assistance.");
}
const passwordMatch = yield (0, auth_util_1.isPasswordMatched)(payload.password, user.password);
if (!passwordMatch) {
throw new Error("Password not matched");
Expand Down
27 changes: 26 additions & 1 deletion dist/app/modules/booking/booking.model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Booking = void 0;
const mongoose_1 = require("mongoose");
Expand Down Expand Up @@ -74,7 +83,7 @@ const bookingSchema = new mongoose_1.Schema({
},
drivingLicense: {
type: String,
required: [true, "Driving license is required"],
// required: [true, "Driving license is required"],
}
},
additionalFeatures: {
Expand All @@ -94,4 +103,20 @@ const bookingSchema = new mongoose_1.Schema({
}, {
timestamps: true,
});
// Pre-hook to prevent overlapping bookings
bookingSchema.pre("save", function (next) {
return __awaiter(this, void 0, void 0, function* () {
const conflictingBooking = yield exports.Booking.findOne({
car: this.car,
$or: [
{ date: { $lt: this.returnDate, $gt: this.date } },
{ returnDate: { $gt: this.date, $lt: this.returnDate } },
],
});
if (conflictingBooking) {
throw new Error("The car is already booked during this time.");
}
next();
});
});
exports.Booking = (0, mongoose_1.model)("Booking", bookingSchema);
11 changes: 11 additions & 0 deletions dist/app/modules/car/car.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,22 @@ const deleteACar = (0, catchAsync_1.default)((req, res) => __awaiter(void 0, voi
data: result,
});
}));
const checkCarAvailability = (0, catchAsync_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () {
const queries = req.query;
const result = yield car_service_1.CarServices.checkCarAvailability(queries);
(0, sendResponse_1.default)(res, {
statusCode: http_status_1.default.OK,
success: true,
message: "Cars availability checked successfully",
data: result
});
}));
// ------------------------========================-----------------------------
exports.CarControllers = {
createCar,
getACar,
updateACar,
deleteACar,
getAllCars,
checkCarAvailability
};
5 changes: 5 additions & 0 deletions dist/app/modules/car/car.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ const carSchema = new mongoose_1.Schema({
type: Number,
required: true,
},
currentLocation: {
type: String,
required: true,
default: "Chapainawabganj",
},
}, {
timestamps: true,
});
Expand Down
3 changes: 2 additions & 1 deletion dist/app/modules/car/car.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ const booking_controller_1 = require("../booking/booking.controller");
const router = express_1.default.Router();
router.post("/", (0, auth_1.default)(user_constant_1.USER_ROLE.admin), (0, validateRequest_1.default)(car_validation_1.CarValidation.carValidationSchema), car_controller_1.CarControllers.createCar);
router.get("/", car_controller_1.CarControllers.getAllCars);
router.get("/:id", car_controller_1.CarControllers.getACar);
router.get("/check-availability", car_controller_1.CarControllers.checkCarAvailability);
router.put("/return", (0, validateRequest_1.default)(booking_validation_1.bookingValidations.carReturnValidationSchema), (0, auth_1.default)(user_constant_1.USER_ROLE.admin), booking_controller_1.BookingControllers.returnTheCar);
router.get("/:id", car_controller_1.CarControllers.getACar);
router.put("/:id", (0, validateRequest_1.default)(car_validation_1.CarValidation.updateCarValidationSchema), (0, auth_1.default)(user_constant_1.USER_ROLE.admin), car_controller_1.CarControllers.updateACar);
router.delete("/:id", (0, auth_1.default)(user_constant_1.USER_ROLE.admin), car_controller_1.CarControllers.deleteACar);
exports.CarRoutes = router;
13 changes: 10 additions & 3 deletions dist/app/modules/car/car.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ const deleteACar = (id) => __awaiter(void 0, void 0, void 0, function* () {
});
// get all car from the database
const getAllCars = (payload) => __awaiter(void 0, void 0, void 0, function* () {
// const result = await Car.find();
// return result;
// Create a new QueryBuilder instance for the car query
const carQuery = new QueryBuilder_1.default(car_model_1.Car.find({}), payload)
.search(["features",])
.search(["features"])
.filter()
.sort()
.paginate();
Expand All @@ -59,10 +57,19 @@ const getAllCars = (payload) => __awaiter(void 0, void 0, void 0, function* () {
cars: result,
};
});
// search available cars a car with a new value
const checkCarAvailability = (payload) => __awaiter(void 0, void 0, void 0, function* () {
const result = yield car_model_1.Car.find({
status: "available",
currentLocation: { $regex: payload.searchTerm, $options: "i" },
});
return result;
});
exports.CarServices = {
createCar,
getACar,
updateACar,
deleteACar,
getAllCars,
checkCarAvailability,
};
1 change: 1 addition & 0 deletions dist/app/modules/car/car.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const updateCarValidationSchema = zod_1.z.object({
message: "Number of doors must be a positive number",
})
.optional(),
currentLocation: zod_1.z.string().optional(),
}),
});
exports.CarValidation = {
Expand Down
7 changes: 6 additions & 1 deletion src/app/modules/Auth/auth.services.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { TUser } from "../user/user.interface";
import { User } from "../user/user.model";
import { TsigninUser } from "./auth.interface";
Expand All @@ -24,7 +25,11 @@ const signin = async (payload: TsigninUser) => {
if (!user) {
throw new Error("User not found");
}

if (user?.status === "blocked") {
throw new Error("Your account has been blocked. Please contact support for assistance.");
}


const passwordMatch = await isPasswordMatched(
payload.password,
user.password
Expand Down
17 changes: 16 additions & 1 deletion src/app/modules/booking/booking.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const bookingSchema = new Schema<TBooking>({
},
drivingLicense:{
type: String,
required: [true, "Driving license is required"],
// required: [true, "Driving license is required"],
}
},
additionalFeatures:{
Expand All @@ -96,4 +96,19 @@ const bookingSchema = new Schema<TBooking>({
timestamps: true,
});

// Pre-hook to prevent overlapping bookings
bookingSchema.pre("save", async function (next) {
const conflictingBooking = await Booking.findOne({
car: this.car,
$or: [
{ date: { $lt: this.returnDate, $gt: this.date } },
{ returnDate: { $gt: this.date, $lt: this.returnDate } },
],
});

if (conflictingBooking) {
throw new Error("The car is already booked during this time.");
}
next();
});
export const Booking = model<TBooking>("Booking",bookingSchema)
13 changes: 12 additions & 1 deletion src/app/modules/car/car.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ const deleteACar: RequestHandler =catchAsync(async (req, res) => {
}) ;


const checkCarAvailability=catchAsync(async (req,res) => {

const queries = req.query
const result = await CarServices.checkCarAvailability(queries);

sendResponse(res, {
statusCode: httpStatus.OK,
success: true,
message: "Cars availability checked successfully",
data: result
});
})


// ------------------------========================-----------------------------
Expand All @@ -78,5 +89,5 @@ export const CarControllers = {
updateACar,
deleteACar,
getAllCars,

checkCarAvailability
};
1 change: 1 addition & 0 deletions src/app/modules/car/car.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export interface TCar {
images: string[];
year: number;
noOfDoors: number;
currentLocation?: string;
}
export interface CarModel extends Model<TCar> {}
7 changes: 6 additions & 1 deletion src/app/modules/car/car.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const carSchema = new Schema<TCar, CarModel>(
},
status: {
type: String,
enum: ["available","booked","maintenance"],
enum: ["available", "booked", "maintenance"],
default: "available",
},
features: {
Expand Down Expand Up @@ -74,6 +74,11 @@ const carSchema = new Schema<TCar, CarModel>(
type: Number,
required: true,
},
currentLocation: {
type: String,
required: true,
default: "Chapainawabganj",
},
},
{
timestamps: true,
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/car/car.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ router.post(
CarControllers.createCar
);
router.get("/", CarControllers.getAllCars);
router.get("/:id", CarControllers.getACar);
router.get("/check-availability",CarControllers.checkCarAvailability)
router.put(
"/return",
validateRequest(bookingValidations.carReturnValidationSchema),
auth(USER_ROLE.admin),
BookingControllers.returnTheCar
);
router.get("/:id", CarControllers.getACar);
router.put(
"/:id",
validateRequest(CarValidation.updateCarValidationSchema),
Expand Down
16 changes: 13 additions & 3 deletions src/app/modules/car/car.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ const deleteACar = async (id: string) => {
};
// get all car from the database
const getAllCars = async (payload: Record<string, unknown>) => {
// const result = await Car.find();
// return result;

// Create a new QueryBuilder instance for the car query
const carQuery = new QueryBuilder(Car.find({}), payload)
.search(["features",])
.search(["features"])
.filter()
.sort()
.paginate();
Expand All @@ -56,10 +55,21 @@ const getAllCars = async (payload: Record<string, unknown>) => {
};
};

// search available cars a car with a new value
const checkCarAvailability = async (payload: Record<string, unknown>) => {
const result = await Car.find({
status: "available",
currentLocation: { $regex: payload.searchTerm, $options: "i" },
});

return result;
};

export const CarServices = {
createCar,
getACar,
updateACar,
deleteACar,
getAllCars,
checkCarAvailability,
};
2 changes: 2 additions & 0 deletions src/app/modules/car/car.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const updateCarValidationSchema = z.object({
message: "Number of doors must be a positive number",
})
.optional(),
currentLocation: z.string().optional(),

}),
});

Expand Down

0 comments on commit aa16a31

Please sign in to comment.