feat: complete day 1

This commit is contained in:
Ayobami
2025-07-10 21:08:42 +01:00
parent 1703819bda
commit b277c8182d
13 changed files with 512 additions and 79 deletions
+104
View File
@@ -0,0 +1,104 @@
const express = require("express");
const router = express.Router();
const { Order } = require("../models");
const {
handleError,
handleSuccess,
handleSequelizeError,
deepEqual,
} = require("../utils");
// GET all orders
router.get("/", async (_, res) => {
try {
const orders = await Order.findAll();
handleSuccess(res, orders);
} catch (err) {
handleSequelizeError(err, res);
}
});
// GET one order by id
router.get("/:id", async (req, res) => {
try {
const order = await Order.findByPk(req.params.id);
if (!order) return handleError("Order not found", 404, res);
handleSuccess(res, order);
} catch (err) {
handleSequelizeError(err, res);
}
});
// POST create an order
router.post("/", async (req, res) => {
try {
const { order_id, user_id, shipping_dock_id, amount, notes } = req.body;
if (
order_id === undefined ||
user_id === undefined ||
shipping_dock_id === undefined ||
amount === undefined ||
notes === undefined
) {
return handleError("All fields are required", 400, res);
}
const order = await Order.create(req.body);
handleSuccess(res, order, 201);
} catch (err) {
handleError(err.message, 500, res);
}
});
// PUT update an order
router.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const order = await Order.findByPk(id);
if (!order) return handleError("Order not found", 404, res);
// Only allow updating amount, notes, and status
const allowedFields = ["amount", "notes", "status"];
const changes = {};
for (const key of allowedFields) {
if (
req.body[key] !== undefined &&
!deepEqual(req.body[key], order[key])
) {
changes[key] = req.body[key];
}
}
// Validate status if present
if (
changes.status !== undefined &&
![0, 1].includes(Number(changes.status))
) {
return handleError("Status must be 0 or 1", 400, res);
}
if (Object.keys(changes).length === 0) {
return handleError("No updated fields", 400, res); // No changes
}
await order.update(changes);
handleSuccess(res, await Order.findByPk(id));
} catch (err) {
handleSequelizeError(err, res);
}
});
// DELETE an order
router.delete("/:id", async (req, res) => {
try {
const deleted = await Order.destroy({
where: { id: req.params.id },
});
if (deleted === 0) return handleError("Order not found", 404, res);
handleSuccess(res, null, 204); // 204 No Content
} catch (err) {
handleSequelizeError(err, res);
}
});
module.exports = router;
+94
View File
@@ -0,0 +1,94 @@
const express = require("express");
const router = express.Router();
const { ShippingDock } = require("../models");
const {
handleError,
handleSuccess,
handleSequelizeError,
deepEqual,
} = require("../utils");
// GET all shipping docks
router.get("/", async (_, res) => {
try {
const docks = await ShippingDock.findAll();
handleSuccess(res, docks);
} catch (err) {
handleSequelizeError(err, res);
}
});
// GET one shipping dock by id
router.get("/:id", async (req, res) => {
try {
const dock = await ShippingDock.findByPk(req.params.id);
if (!dock) return handleError("Dock not found", 404, res);
handleSuccess(res, dock);
} catch (err) {
handleSequelizeError(err, res);
}
});
// POST create a shipping dock
router.post("/", async (req, res) => {
try {
const { name } = req.body;
if (!name || !name.trim()) {
return handleError("Name is required and cannot be empty", 400, res);
}
await ShippingDock.create(req.body);
handleSuccess(res, dock, 201);
} catch (err) {
handleError(err.message, 500, res);
}
});
// PUT update a shipping dock
router.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const dock = await ShippingDock.findByPk(id);
if (!dock) return handleError("Dock not found", 404, res);
// Validate status separately
if (
req.body.status !== undefined &&
![0, 1].includes(Number(req.body.status))
) {
return handleError("Status must be 0 or 1", 400, res);
}
// Build changes object dynamically
const changes = {};
for (const key in req.body) {
if (req.body[key] !== undefined && !deepEqual(req.body[key], dock[key])) {
changes[key] = req.body[key];
}
}
if (Object.keys(changes).length === 0) {
return handleError("No updated fields", 400, res); // No changes
}
await dock.update(changes);
handleSuccess(res, await ShippingDock.findByPk(id));
} catch (err) {
handleSequelizeError(err, res);
}
});
// DELETE a shipping dock
router.delete("/:id", async (req, res) => {
try {
const deleted = await ShippingDock.destroy({
where: { id: req.params.id },
});
if (deleted === 0) return handleError("Dock not found", 404, res);
handleSuccess(res, null, 204); // 204 No Content
} catch (err) {
handleSequelizeError(err, res);
}
});
module.exports = router;
+96
View File
@@ -0,0 +1,96 @@
const express = require("express");
const router = express.Router();
const { Transaction } = require("../models");
const {
handleError,
handleSuccess,
handleSequelizeError,
deepEqual,
} = require("../utils");
// GET all transactions
router.get("/", async (_, res) => {
try {
const transactions = await Transaction.findAll();
handleSuccess(res, transactions);
} catch (err) {
handleSequelizeError(err, res);
}
});
// GET one transaction by id
router.get("/:id", async (req, res) => {
try {
const transaction = await Transaction.findByPk(req.params.id);
if (!transaction) return handleError("Transaction not found", 404, res);
handleSuccess(res, transaction);
} catch (err) {
handleSequelizeError(err, res);
}
});
// POST create a transaction
router.post("/", async (req, res) => {
try {
const { order_id, user_id, shipping_dock_id, amount, notes } = req.body;
if (
order_id === undefined ||
user_id === undefined ||
shipping_dock_id === undefined ||
amount === undefined ||
notes === undefined
) {
return handleError("All fields are required", 400, res);
}
const transaction = await Transaction.create(req.body);
handleSuccess(res, transaction, 201);
} catch (err) {
handleError(err.message, 500, res);
}
});
// PUT update a transaction
router.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const transaction = await Transaction.findByPk(id);
if (!transaction) return handleError("Transaction not found", 404, res);
// Only allow updating amount and notes
const allowedFields = ["amount", "notes"];
const changes = {};
for (const key of allowedFields) {
if (
req.body[key] !== undefined &&
!deepEqual(req.body[key], transaction[key])
) {
changes[key] = req.body[key];
}
}
if (Object.keys(changes).length === 0) {
return handleError("No updated fields", 400, res); // No changes
}
await transaction.update(changes);
handleSuccess(res, await Transaction.findByPk(id));
} catch (err) {
handleSequelizeError(err, res);
}
});
// DELETE a transaction
router.delete("/:id", async (req, res) => {
try {
const deleted = await Transaction.destroy({
where: { id: req.params.id },
});
if (deleted === 0) return handleError("Transaction not found", 404, res);
handleSuccess(res, null, 204); // 204 No Content
} catch (err) {
handleSequelizeError(err, res);
}
});
module.exports = router;