94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
|
|
const express = require("express");
|
||
|
|
const router = express.Router();
|
||
|
|
const db = require("../models");
|
||
|
|
const ReportService = require("../services/ReportService");
|
||
|
|
|
||
|
|
// GET /api/v1/report/sale?month=1&year=2022
|
||
|
|
router.get("/sale", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { month, year, from_date, to_date } = req.query;
|
||
|
|
if (month && year) {
|
||
|
|
const result = await ReportService.getMonthlySales(db.order, month, year);
|
||
|
|
return res.json(result);
|
||
|
|
} else if (from_date && to_date) {
|
||
|
|
const result = await ReportService.getSalesByDateRange(
|
||
|
|
db.order,
|
||
|
|
from_date,
|
||
|
|
to_date
|
||
|
|
);
|
||
|
|
return res.json(result);
|
||
|
|
} else {
|
||
|
|
return res.status(400).json({ error: "Missing required parameters" });
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
res.status(500).json({ error: error.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// GET /api/v1/report/monthly?year=2022
|
||
|
|
router.get("/monthly", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { year } = req.query;
|
||
|
|
if (!year) return res.status(400).json({ error: "year is required" });
|
||
|
|
const result = await ReportService.getMonthlySalesByYear(db.order, year);
|
||
|
|
res.json(result);
|
||
|
|
} catch (error) {
|
||
|
|
res.status(500).json({ error: error.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// GET /api/v1/report/user?year=2022&user_id=1
|
||
|
|
router.get("/user", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { year, user_id } = req.query;
|
||
|
|
if (!year || !user_id)
|
||
|
|
return res.status(400).json({ error: "year and user_id are required" });
|
||
|
|
const result = await ReportService.getMonthlySalesByUser(
|
||
|
|
db.order,
|
||
|
|
year,
|
||
|
|
user_id
|
||
|
|
);
|
||
|
|
res.json(result);
|
||
|
|
} catch (error) {
|
||
|
|
res.status(500).json({ error: error.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// GET /api/v1/report/shipping_dock?year=2022&shipping_dock_id=1
|
||
|
|
router.get("/shipping_dock", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { year, shipping_dock_id } = req.query;
|
||
|
|
if (!year || !shipping_dock_id)
|
||
|
|
return res
|
||
|
|
.status(400)
|
||
|
|
.json({ error: "year and shipping_dock_id are required" });
|
||
|
|
const result = await ReportService.getMonthlySalesByShippingDock(
|
||
|
|
db.order,
|
||
|
|
year,
|
||
|
|
shipping_dock_id
|
||
|
|
);
|
||
|
|
res.json(result);
|
||
|
|
} catch (error) {
|
||
|
|
res.status(500).json({ error: error.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// GET /api/v1/report/user/count?year=2022&user_id=1
|
||
|
|
router.get("/user/count", async (req, res) => {
|
||
|
|
try {
|
||
|
|
const { year, user_id } = req.query;
|
||
|
|
if (!year || !user_id)
|
||
|
|
return res.status(400).json({ error: "year and user_id are required" });
|
||
|
|
const result = await ReportService.getOrderCountByUser(
|
||
|
|
db.order,
|
||
|
|
year,
|
||
|
|
user_id
|
||
|
|
);
|
||
|
|
res.json(result);
|
||
|
|
} catch (error) {
|
||
|
|
res.status(500).json({ error: error.message });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|