Skip to content

Commit

Permalink
fix: minor fixes and fix #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Senor101 committed Mar 2, 2024
1 parent 04ed0df commit a94fa50
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/api/v1/controllers/bus.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import throwError from "../utils/throwError.util";

const getBusesForIndividualBusOwnerController = async (req: Request, res: Response, next: NextFunction): Promise<Response | void> => {
try {
const busOwnerID = req.user;
const busOwnerID = res.locals.user.id
const busOwner = await BusOwner.findById(busOwnerID).lean();
if (!busOwner) {
return throwError(req, res, "Invalid Bus Owner", 404);
}
const buses = await Bus.find({ busOwner: busOwnerID }).lean();
return res.status(200).json({
message: "Buses fetched Succesfully",
data: busOwner?.buses
data: buses
});
} catch (error) {
console.error(error)
next(error);
}
};
Expand All @@ -35,9 +37,7 @@ const getIndividualBusController = async (req: Request, res: Response, next: Nex

const getAllBusesController = async (req: Request, res: Response, next: NextFunction) => {
try {
const buses = await Bus.find({
busOwner : res.locals.user
}).lean();
const buses = await Bus.find().lean();
return res.status(200).json({
message: "Buses fetched",
count: buses.length,
Expand All @@ -51,18 +51,18 @@ const getAllBusesController = async (req: Request, res: Response, next: NextFunc
const registerBus = async (req: Request, res: Response, next: NextFunction): Promise<Response | void> => {
try {
const busBody: IBus = req.body;
const busId = res.locals.user.id;
const busOwnerId = res.locals.user.id;
const existingBus = await Bus.findOne({ busNumber: busBody.busNumber }).lean();
if (existingBus) {
return throwError(req, res, "Bus with provided bus number exists.", 409);
}
const newBus = await Bus.create(busBody);
const newBus = await Bus.create({...busBody, busOwner: busOwnerId});
const owner = await BusOwner.findById(busOwnerId)
owner?.buses?.push(newBus)
await owner?.save();
return res.status(201).json({
message: "New Bus registered",
data: {
...newBus,
busOwner:busId
}
data: newBus
});
} catch (error) {
next(error);
Expand Down
1 change: 1 addition & 0 deletions src/api/v1/controllers/dashboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const getBusownerDashboardController = async (req: Request, res: Response
if(!busOwner) return throwError(req, res, "Invalid Bus Owner", 404);

const buses = await Bus.find({busOwner: busOwnerId}).lean();
console.log(buses);
let totalSaleData : {date: Date, amount:number}[] = [];

buses.forEach(bus => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/v1/middlewares/errorhandler.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default async (
res: Response,
next: NextFunction
) => {
console.log(err);
console.error(err);
return res.status((err as any).status || 500).json({
error: err.message || 'Internal Server Error',
});
Expand Down
2 changes: 2 additions & 0 deletions src/api/v1/routes/bus.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const router = Router();

router.get("/", busController.getAllBusesController);

router.get('/owner',validateToken, isBusOwner, busController.getBusesForIndividualBusOwnerController)

router.post("/", validateBusRegister, checkValidation, validateToken, isBusOwner, busController.registerBus);

// update your bus location with request from nodemcu providing latitude and longitude in queries
Expand Down

0 comments on commit a94fa50

Please sign in to comment.