diff --git a/client/src/components/AdministratorNavigationPanel.tsx b/client/src/components/AdministratorNavigationPanel.tsx index 2c5b724..6c0ecb2 100644 --- a/client/src/components/AdministratorNavigationPanel.tsx +++ b/client/src/components/AdministratorNavigationPanel.tsx @@ -63,9 +63,8 @@ export default function AdministratorNavigationPanel() { return (
@@ -184,11 +180,6 @@ export default function AdministratorNavigationPanel() { icon={} onClickRef="schedules" /> - } - onClickRef="#" - />

Users

diff --git a/client/src/pages/CreateSchedulePage.tsx b/client/src/pages/CreateSchedulePage.tsx index e3cb963..5db5f5d 100644 --- a/client/src/pages/CreateSchedulePage.tsx +++ b/client/src/pages/CreateSchedulePage.tsx @@ -22,7 +22,7 @@ const initialValues = { time: "", location: "", postalCode: "", - status: "Up coming" // Set the default status + status: "Upcoming" // Set the default status }; @@ -57,7 +57,7 @@ export default function CreateSchedulePage() { data.dateTime = dateTime.toISOString(); data.location = data.location.trim(); data.postalCode = data.postalCode.trim(); - data.status = "Up coming"; // Set status to "Up coming" explicitly + data.status = "Upcoming"; // Set status to "Up coming" explicitly console.log("Data to be sent:", data); diff --git a/client/src/pages/ManageSchedulePage.tsx b/client/src/pages/ManageSchedulePage.tsx index fe05650..e1dcd3b 100644 --- a/client/src/pages/ManageSchedulePage.tsx +++ b/client/src/pages/ManageSchedulePage.tsx @@ -16,7 +16,7 @@ interface Schedule { const determineStatus = (dateTime: string): string => { const now = dayjs(); const scheduleDateTime = dayjs(dateTime); - return scheduleDateTime.isAfter(now) ? "Up coming" : "Ended"; + return scheduleDateTime.isAfter(now) ? "Upcoming" : "Ended"; }; export default function ManageSchedulePage() { diff --git a/client/src/pages/Ranking.tsx b/client/src/pages/Ranking.tsx index d86e64e..3bfb427 100644 --- a/client/src/pages/Ranking.tsx +++ b/client/src/pages/Ranking.tsx @@ -215,13 +215,11 @@ export default function Ranking() { return; } - // Function to get a random voucher from the available vouchers const getRandomVouchers = (count: number): Voucher[] => { const shuffled = vouchers.sort(() => 0.5 - Math.random()); return shuffled.slice(0, count); }; - // Group users by town council const townCouncilGroups: Record = {}; topUsers.forEach(user => { if (!townCouncilGroups[user.userTownCouncil]) { @@ -230,9 +228,7 @@ export default function Ranking() { townCouncilGroups[user.userTownCouncil].push(user); }); - // Iterate over each town council and assign vouchers for (const [townCouncil, users] of Object.entries(townCouncilGroups)) { - // Sort users by avgBill and pick top 3 const top3 = users.sort((a, b) => b.avgBill - a.avgBill).slice(0, 3); for (let i = 0; i < top3.length; i++) { @@ -240,11 +236,11 @@ export default function Ranking() { let voucherCount = 0; if (i === 0) { - voucherCount = 3; // Top 1 gets 3 vouchers + voucherCount = 3; } else if (i === 1) { - voucherCount = 2; // Top 2 gets 2 vouchers + voucherCount = 2; } else if (i === 2) { - voucherCount = 1; // Top 3 gets 1 voucher + voucherCount = 1; } const vouchersToAssign = getRandomVouchers(voucherCount); @@ -254,16 +250,30 @@ export default function Ranking() { userId: user.userId, voucherId: voucher.id, }); + + try { + const voucherId = voucher.id; + const response = await instance.put(`${config.serverAddress}/vouchers/update-quantity/${voucherId}`, { + quantityToSubtract: 1 + }); + + if (response.status !== 200) { + console.error(`Failed to update voucher quantity for voucherId: ${voucher.id}`); + } + } catch (error) { + console.error(`Error updating voucher quantity for voucherId: ${voucher.id}`, error); + throw new Error(`Failed to update voucher quantity for voucherId: ${voucher.id}`); + } } } } + console.log("Vouchers assigned successfully"); } catch (error) { console.error("Failed to assign vouchers:", error); } }; - const handleGiveVouchers = async () => { try { await assignVouchersToUsers(top3Users); @@ -282,7 +292,6 @@ export default function Ranking() { label: townCouncil, })); - return (
@@ -316,8 +325,6 @@ export default function Ranking() { Rank User Name - Electrical Bill - Water Bill Total Bill Dependents @@ -332,8 +339,6 @@ export default function Ranking() { {index + 1} {data.userName} - ${data.electricalBill.toFixed(2)} - ${data.waterBill.toFixed(2)} ${data.totalBill.toFixed(2)} {data.noOfDependents} ${data.avgBill.toFixed(2)} diff --git a/client/src/pages/SchedulePage.tsx b/client/src/pages/SchedulePage.tsx index 693fdd3..1e6a7f4 100644 --- a/client/src/pages/SchedulePage.tsx +++ b/client/src/pages/SchedulePage.tsx @@ -160,13 +160,13 @@ export default function SchedulePage() { ))} -
{/* Adjust gap size as needed */} -
{/* Flex 1 for equal sizing and padding */} - +
+
+ -

Paper

+

Paper

$0.05 to 0.20/KG

-
    +
    • Cardboard ($0.20/kg)
    • Newspaper and B&W ($0.11/kg)
    • Mix paper ($0.05/kg)
    • @@ -175,11 +175,11 @@ export default function SchedulePage() {
- + -

Electronics

+

Electronics

$2 to 50/KG

-
    +
    • Flat TV ($5++)
    • Laptop ($10++)
    • Smartphone ($10++)
    • @@ -188,11 +188,11 @@ export default function SchedulePage() {
- + -

Clothes

+

Clothes

$0.20/KG

-
    +
    • Shoe
    • Jewellery
    • Bag
    • diff --git a/client/src/pages/UserVouchersPage.tsx b/client/src/pages/UserVouchersPage.tsx index 287bd46..d9628d2 100644 --- a/client/src/pages/UserVouchersPage.tsx +++ b/client/src/pages/UserVouchersPage.tsx @@ -222,7 +222,7 @@ export default function UserVoucherPage() {
- +
diff --git a/server/routes/schedule.js b/server/routes/schedule.js index 3bc9d27..401cc42 100644 --- a/server/routes/schedule.js +++ b/server/routes/schedule.js @@ -111,13 +111,13 @@ router.patch("/:id/status", async (req, res) => { const schedule = await Schedule.findByPk(id); if (!schedule) { - return res.status(404).json({ message: 'Schedule not found' }); + return res.status(404).json({ message: "Schedule not found" }); } const now = dayjs(); const scheduleDateTime = dayjs(schedule.dateTime); - const newStatus = scheduleDateTime.isAfter(now) ? "Up coming" : "Ended"; + const newStatus = scheduleDateTime.isAfter(now) ? "Upcoming" : "Ended"; schedule.status = newStatus; await schedule.save(); diff --git a/server/routes/vouchers.js b/server/routes/vouchers.js index d74cf1d..ae94a76 100644 --- a/server/routes/vouchers.js +++ b/server/routes/vouchers.js @@ -51,7 +51,6 @@ router.post( } ); - router.get("/", async (req, res) => { let condition = {}; let search = req.query.search; @@ -150,5 +149,57 @@ router.delete("/:id", async (req, res) => { } }); +router.patch("/:id/quantity", async (req, res) => { + const { id } = req.params; + const { decrementBy } = req.body; // Expecting the decrement amount in the request body + + try { + const voucher = await Voucher.findById(id); + if (!voucher) { + return res.status(404).send("Voucher not found"); + } + + voucher.quantityAvailable -= decrementBy; + await voucher.save(); + + res.send({ message: "Voucher quantity updated", voucher }); + } catch (error) { + res.status(500).send("Error updating voucher quantity"); + } +}); +router.put("/update-quantity/:id", async (req, res) => { + let id = req.params.id; + + let vouchers = await Voucher.findByPk(id); + if (!vouchers) { + res.sendStatus(404); + return; + } + + try { + let newQuantity = vouchers.quantityAvailable - req.body.quantityToSubtract; + + if (newQuantity < 0) newQuantity = 0; + + let num = await Voucher.update( + { quantityAvailable: newQuantity }, + { where: { id: id } } + ); + + if (num == 1) { + res.json({ + message: "Voucher quantity was updated successfully.", + }); + } else { + res.status(400).json({ + message: `Cannot update voucher with id ${id}.`, + }); + } + } catch (err) { + console.error("Error:", err); + res.status(400).json({ errors: err.errors }); + } +}); + module.exports = router; \ No newline at end of file