var express = require("express"); var router = express.Router(); /** * @swagger * /api/v1/transaction: * get: * summary: Get all transactions * tags: [Transaction] * responses: * 200: * description: List of all transactions * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * data: * type: array * items: * $ref: '#/components/schemas/Transaction' */ // GET all transactions router.get("/", async function (req, res, next) { try { const db = req.app.get("db"); const transactions = await db.transaction.findAll(); res.json({ success: true, data: transactions }); } catch (error) { console.error(error); res.status(500).json({ success: false, message: "Error fetching transactions", error: error.message, }); } }); /** * @swagger * /api/v1/transaction/{id}: * get: * summary: Get one transaction by ID * tags: [Transaction] * parameters: * - in: path * name: id * required: true * schema: * type: integer * responses: * 200: * description: Transaction details * 404: * description: Transaction not found */ // GET one transaction by id router.get("/:id", async function (req, res, next) { try { const db = req.app.get("db"); const transaction = await db.transaction.findByPk(req.params.id); if (!transaction) { return res .status(404) .json({ success: false, message: "Transaction not found" }); } res.json({ success: true, data: transaction }); } catch (error) { console.error(error); res.status(500).json({ success: false, message: "Error fetching transaction", error: error.message, }); } }); /** * @swagger * /api/v1/transaction: * post: * summary: Create a new transaction * tags: [Transaction] * requestBody: * required: true * content: * application/json: * schema: * type: object * required: * - order_id * - user_id * - shipping_dock_id * - amount * properties: * order_id: * type: integer * user_id: * type: integer * shipping_dock_id: * type: integer * amount: * type: number * format: decimal * notes: * type: string * responses: * 201: * description: Transaction created successfully * 400: * description: Bad request */ // POST - Create a new transaction router.post("/", async function (req, res, next) { try { const db = req.app.get("db"); const { order_id, user_id, shipping_dock_id, amount, notes } = req.body; if (!order_id || !user_id || !shipping_dock_id || !amount) { return res.status(400).json({ success: false, message: "order_id, user_id, shipping_dock_id, and amount are required", }); } const transaction = await db.transaction.create({ order_id, user_id, shipping_dock_id, amount, notes: notes || null, }); res.status(201).json({ success: true, data: transaction }); } catch (error) { console.error(error); res.status(500).json({ success: false, message: "Error creating transaction", error: error.message, }); } }); /** * @swagger * /api/v1/transaction/{id}: * put: * summary: Update a transaction by ID * tags: [Transaction] * parameters: * - in: path * name: id * required: true * schema: * type: integer * requestBody: * content: * application/json: * schema: * type: object * properties: * order_id: * type: integer * user_id: * type: integer * shipping_dock_id: * type: integer * amount: * type: number * notes: * type: string * responses: * 200: * description: Transaction updated successfully * 404: * description: Transaction not found * delete: * summary: Delete a transaction by ID * tags: [Transaction] * parameters: * - in: path * name: id * required: true * schema: * type: integer * responses: * 200: * description: Transaction deleted successfully * 404: * description: Transaction not found */ // PUT - Update a transaction by id router.put("/:id", async function (req, res, next) { try { const db = req.app.get("db"); const { order_id, user_id, shipping_dock_id, amount, notes } = req.body; const transaction = await db.transaction.findByPk(req.params.id); if (!transaction) { return res .status(404) .json({ success: false, message: "Transaction not found" }); } await transaction.update({ order_id: order_id !== undefined ? order_id : transaction.order_id, user_id: user_id !== undefined ? user_id : transaction.user_id, shipping_dock_id: shipping_dock_id !== undefined ? shipping_dock_id : transaction.shipping_dock_id, amount: amount !== undefined ? amount : transaction.amount, notes: notes !== undefined ? notes : transaction.notes, }); res.json({ success: true, data: transaction }); } catch (error) { console.error(error); res.status(500).json({ success: false, message: "Error updating transaction", error: error.message, }); } }); // DELETE - Delete a transaction by id router.delete("/:id", async function (req, res, next) { try { const db = req.app.get("db"); const transaction = await db.transaction.findByPk(req.params.id); if (!transaction) { return res .status(404) .json({ success: false, message: "Transaction not found" }); } await transaction.destroy(); res.json({ success: true, message: "Transaction deleted successfully", }); } catch (error) { console.error(error); res.status(500).json({ success: false, message: "Error deleting transaction", error: error.message, }); } }); module.exports = router;