Skip to content

Commit

Permalink
feature: implement rider controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
khayss committed May 25, 2024
1 parent b008e31 commit 673fd88
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 11 deletions.
151 changes: 144 additions & 7 deletions controllers/riderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
riderSignUpSchema,
validateRiderReqBody,
} from "../utils/riderValidations.js";
import { DeliveryModel } from "../models/deliveryModel.js";

export const signupRider = catchErrorFunc(async (req, res) => {
const { address, email, firstname, lastname, password, tel } =
Expand Down Expand Up @@ -123,18 +124,154 @@ export const loginRider = catchErrorFunc(async (req, res) => {
);
});

export const getRider = catchErrorFunc(async (req, res) => {});
export const getRider = catchErrorFunc(async (req, res) => {
const { id } = req.verifiedRider;

export const getAllDeliveries = catchErrorFunc(async (req, res) => {});
const rider = await RiderModel.findById(id, { approvedBy: 0, password: 0 });

export const getDelivery = catchErrorFunc(async (req, res) => {});
res.status(200).json({
success: true,
data: {
rider: rider.toJSON(),
},
});
});

export const getAllDeliveries = catchErrorFunc(async (req, res) => {
const deliveries = await DeliveryModel.find({}, {}, { limit: 20 });

res.status(200).json({ success: true, data: { deliveries } });
});

export const getDelivery = catchErrorFunc(async (req, res) => {
const { id } = req.params;

const delivery = await DeliveryModel.findById(id);

res
.status(200)
.json({ success: true, data: { delivery: delivery.toJSON() } });
});

export const updateAvailability = catchErrorFunc(async (req, res) => {
const { id } = req.verifiedRider;
const { status } = req.params;

const rider = await RiderModel.findById(id);

if (rider.availability == "BUSY")
throw new AppError(
"1404",
400,
"RIDER",
"Rider is busy",
"Rider is currently busy and cannot update availability"
);

let availability;

switch (status) {
case 0:
availability = "UNAVAILABLE";
case 1:
availability = "AVAILABLE";
default:
availability = "UNAVAILABLE";
}
const updatedAvailability = await RiderModel.findByIdAndUpdate(
id,
{
availability,
},
{ new: true }
);

export const updateAvailability = catchErrorFunc(async (req, res) => {});
console.log(updatedAvailability);

export const pickUpDelivery = catchErrorFunc(async (req, res) => {});
res.status(201).json({
success: true,
data: {
message: "rider availabilty updated",
availability: updatedAvailability.availability,
},
});
});

export const confirmDelivery = catchErrorFunc(async (req, res) => {});
export const pickUpDelivery = catchErrorFunc(async (req, res) => {
const { id } = req.verifiedRider;
const { deliveryId } = req.params;

export const reportDeliveryFailure = catchErrorFunc(async (req, res) => {});
const rider = await RiderModel.findById(id);

if (rider.availability != "AVAILABLE")
throw new AppError(
"1400",
400,
"RIDER",
"Rider is not available",
"Rider is not available"
);
const pickedDelivery = await DeliveryModel.findByIdAndUpdate(
deliveryId,
{
status: "DISPATCHED",
rider: id,
},
{ new: true }
);

await RiderModel.findByIdAndUpdate(id, { availability: "BUSY" });
console.log(pickedDelivery);

res.status(201).json({
success: true,
data: {
message: "delivery confirmed",
data: {
pickedDelivery: pickedDelivery.toJSON(),
},
},
});
});

export const confirmDelivery = catchErrorFunc(async (req, res) => {
const { deliveryId } = req.params;
const { id } = req.verifiedRider;

const confirmedDelivery = await DeliveryModel.findByIdAndUpdate(
deliveryId,
{
status: "DELIVERED",
},
{ new: true }
);

await RiderModel.findByIdAndUpdate(id, { availability: "AVAILABLE" });

res.status(201).json({
success: true,
confirmedDelivery: confirmedDelivery.toJSON(),
});
});

export const reportDeliveryFailure = catchErrorFunc(async (req, res) => {
const { id } = req.verifiedRider;
const { deliveryId } = req.params;

const confirmedDelivery = await DeliveryModel.findByIdAndUpdate(
deliveryId,
{
status: "FAILED",
},
{ new: true }
);

await RiderModel.findByIdAndUpdate(id, { availability: "AVAILABLE" });

res.status(201).json({
success: true,
confirmedDelivery: confirmedDelivery.toJSON(),
});
});

export const ex = catchErrorFunc(async (req, res) => {});
8 changes: 4 additions & 4 deletions routes/riderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ riderRouter
.get("/get-rider", getRider)
.get("/get-all-deliveries", getAllDeliveries)
.get("/get-delivery/:id", getDelivery)
.post("/pickup-delivery", pickUpDelivery)
.post("/confirm-delivery", confirmDelivery)
.post("/update-availability", updateAvailability)
.post("/report-delivery-failure", reportDeliveryFailure);
.post("/pickup-delivery/:deliveryId", pickUpDelivery)
.post("/confirm-delivery/:deliveryId", confirmDelivery)
.post("/update-availability/:status", updateAvailability)
.post("/report-delivery-failure/:deliveryId", reportDeliveryFailure);

0 comments on commit 673fd88

Please sign in to comment.