Add order, shipping dock, and transaction routes with CRUD operations and Swagger documentation

- Implemented GET, POST, PUT, and DELETE endpoints for orders, shipping docks, and transactions.
- Added Swagger annotations for API documentation.
- Included error handling for database operations.
- Configured pnpm workspace to ignore built dependencies for sqlite3.
This commit is contained in:
bolade
2025-11-12 11:12:05 +01:00
parent 1703819bda
commit 7b3fd1298d
12 changed files with 3323 additions and 66 deletions
+259
View File
@@ -0,0 +1,259 @@
var express = require("express");
var router = express.Router();
/**
* @swagger
* /api/v1/order:
* get:
* summary: Get all orders
* tags: [Order]
* responses:
* 200:
* description: List of all orders
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* type: array
* items:
* $ref: '#/components/schemas/Order'
*/
// GET all orders
router.get("/", async function (req, res, next) {
try {
const db = req.app.get("db");
const orders = await db.order.findAll();
res.json({ success: true, data: orders });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error fetching orders",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/order/{id}:
* get:
* summary: Get one order by ID
* tags: [Order]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 200:
* description: Order details
* 404:
* description: Order not found
*/
// GET one order by id
router.get("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const order = await db.order.findByPk(req.params.id);
if (!order) {
return res
.status(404)
.json({ success: false, message: "Order not found" });
}
res.json({ success: true, data: order });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error fetching order",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/order:
* post:
* summary: Create a new order
* tags: [Order]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - user_id
* - amount
* - tax
* properties:
* user_id:
* type: integer
* amount:
* type: number
* format: decimal
* tax:
* type: number
* format: decimal
* notes:
* type: string
* status:
* type: integer
* enum: [0, 1]
* default: 0
* description: 0 = not paid, 1 = paid
* responses:
* 201:
* description: Order created successfully
* 400:
* description: Bad request
*/
// POST - Create a new order
router.post("/", async function (req, res, next) {
try {
const db = req.app.get("db");
const { user_id, amount, tax, notes, status } = req.body;
if (!user_id || !amount || tax === undefined) {
return res.status(400).json({
success: false,
message: "user_id, amount, and tax are required",
});
}
const order = await db.order.create({
user_id,
amount,
tax,
notes: notes || null,
status: status !== undefined ? status : 0,
});
res.status(201).json({ success: true, data: order });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error creating order",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/order/{id}:
* put:
* summary: Update an order by ID
* tags: [Order]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* requestBody:
* content:
* application/json:
* schema:
* type: object
* properties:
* user_id:
* type: integer
* amount:
* type: number
* tax:
* type: number
* notes:
* type: string
* status:
* type: integer
* enum: [0, 1]
* responses:
* 200:
* description: Order updated successfully
* 404:
* description: Order not found
* delete:
* summary: Delete an order by ID
* tags: [Order]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* responses:
* 200:
* description: Order deleted successfully
* 404:
* description: Order not found
*/
// PUT - Update an order by id
router.put("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const { user_id, amount, tax, notes, status } = req.body;
const order = await db.order.findByPk(req.params.id);
if (!order) {
return res
.status(404)
.json({ success: false, message: "Order not found" });
}
await order.update({
user_id: user_id !== undefined ? user_id : order.user_id,
amount: amount !== undefined ? amount : order.amount,
tax: tax !== undefined ? tax : order.tax,
notes: notes !== undefined ? notes : order.notes,
status: status !== undefined ? status : order.status,
});
res.json({ success: true, data: order });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error updating order",
error: error.message,
});
}
});
// DELETE - Delete an order by id
router.delete("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const order = await db.order.findByPk(req.params.id);
if (!order) {
return res
.status(404)
.json({ success: false, message: "Order not found" });
}
await order.destroy();
res.json({ success: true, message: "Order deleted successfully" });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error deleting order",
error: error.message,
});
}
});
module.exports = router;
+257
View File
@@ -0,0 +1,257 @@
var express = require("express");
var router = express.Router();
/**
* @swagger
* /api/v1/shipping_dock:
* get:
* summary: Get all shipping docks
* tags: [Shipping Dock]
* responses:
* 200:
* description: List of all shipping docks
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* type: array
* items:
* $ref: '#/components/schemas/ShippingDock'
*/
// GET all shipping docks
router.get("/", async function (req, res, next) {
try {
const db = req.app.get("db");
const shippingDocks = await db.shipping_dock.findAll();
res.json({ success: true, data: shippingDocks });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error fetching shipping docks",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/shipping_dock/{id}:
* get:
* summary: Get one shipping dock by ID
* tags: [Shipping Dock]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Shipping dock ID
* responses:
* 200:
* description: Shipping dock details
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* data:
* $ref: '#/components/schemas/ShippingDock'
* 404:
* description: Shipping dock not found
*/
// GET one shipping dock by id
router.get("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
if (!shippingDock) {
return res
.status(404)
.json({ success: false, message: "Shipping dock not found" });
}
res.json({ success: true, data: shippingDock });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error fetching shipping dock",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/shipping_dock:
* post:
* summary: Create a new shipping dock
* tags: [Shipping Dock]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* properties:
* name:
* type: string
* description: Name of the shipping dock
* status:
* type: integer
* description: Status (1 = active, 0 = inactive)
* enum: [0, 1]
* default: 1
* responses:
* 201:
* description: Shipping dock created successfully
* 400:
* description: Bad request - missing required fields
*/
// POST - Create a new shipping dock
router.post("/", async function (req, res, next) {
try {
const db = req.app.get("db");
const { name, status } = req.body;
if (!name) {
return res
.status(400)
.json({ success: false, message: "Name is required" });
}
const shippingDock = await db.shipping_dock.create({
name,
status: status !== undefined ? status : 1,
});
res.status(201).json({ success: true, data: shippingDock });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error creating shipping dock",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/shipping_dock/{id}:
* put:
* summary: Update a shipping dock by ID
* tags: [Shipping Dock]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Shipping dock ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* status:
* type: integer
* enum: [0, 1]
* responses:
* 200:
* description: Shipping dock updated successfully
* 404:
* description: Shipping dock not found
*/
// PUT - Update a shipping dock by id
router.put("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const { name, status } = req.body;
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
if (!shippingDock) {
return res
.status(404)
.json({ success: false, message: "Shipping dock not found" });
}
await shippingDock.update({
name: name !== undefined ? name : shippingDock.name,
status: status !== undefined ? status : shippingDock.status,
});
res.json({ success: true, data: shippingDock });
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error updating shipping dock",
error: error.message,
});
}
});
/**
* @swagger
* /api/v1/shipping_dock/{id}:
* delete:
* summary: Delete a shipping dock by ID
* tags: [Shipping Dock]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: Shipping dock ID
* responses:
* 200:
* description: Shipping dock deleted successfully
* 404:
* description: Shipping dock not found
*/
// DELETE - Delete a shipping dock by id
router.delete("/:id", async function (req, res, next) {
try {
const db = req.app.get("db");
const shippingDock = await db.shipping_dock.findByPk(req.params.id);
if (!shippingDock) {
return res
.status(404)
.json({ success: false, message: "Shipping dock not found" });
}
await shippingDock.destroy();
res.json({
success: true,
message: "Shipping dock deleted successfully",
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Error deleting shipping dock",
error: error.message,
});
}
});
module.exports = router;
+262
View File
@@ -0,0 +1,262 @@
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;