GWM : before previous data api
This commit is contained in:
parent
805599d3e3
commit
3c8ace0b7e
@ -884,6 +884,89 @@ exports.getPreviousForecastData = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
exports.getBeforePreviousData = async (req, res) => {
|
||||
try {
|
||||
const { establishment_id, current_quarter, current_year, product_id } = req.query;
|
||||
|
||||
if (!establishment_id || !current_quarter || !current_year || !product_id) {
|
||||
return res.status(400).json({ status: "failed", message: "Missing required query params" });
|
||||
}
|
||||
|
||||
// normalize and validate current_quarter (accept "Q1" or "1")
|
||||
const quarterMatch = String(current_quarter).trim().match(/(\d)/);
|
||||
if (!quarterMatch) {
|
||||
return res.status(400).json({ status: "failed", message: "Invalid current_quarter format" });
|
||||
}
|
||||
let currentQuarterNum = parseInt(quarterMatch[1], 10);
|
||||
if (![1, 2, 3, 4].includes(currentQuarterNum)) {
|
||||
return res.status(400).json({ status: "failed", message: "current_quarter must be Q1..Q4 or 1..4" });
|
||||
}
|
||||
|
||||
// normalize and validate year
|
||||
let yearNum = Number(current_year);
|
||||
if (!Number.isInteger(yearNum) || yearNum <= 0) {
|
||||
return res.status(400).json({ status: "failed", message: "Invalid current_year" });
|
||||
}
|
||||
|
||||
// compute the quarter two steps before (before-previous)
|
||||
// e.g. current Q1 -> before-previous = Q3 (year - 1)
|
||||
// current Q4 -> before-previous = Q2 (same year)
|
||||
let beforePrevQuarterNum = currentQuarterNum - 2;
|
||||
let targetYear = yearNum;
|
||||
if (beforePrevQuarterNum <= 0) {
|
||||
beforePrevQuarterNum += 4;
|
||||
targetYear = yearNum - 1;
|
||||
}
|
||||
|
||||
const quarter = `Q${beforePrevQuarterNum}`;
|
||||
const year = String(targetYear);
|
||||
|
||||
console.log(quarter);
|
||||
console.log('-');
|
||||
console.log(year);
|
||||
|
||||
|
||||
// find submission id
|
||||
const submission = await Submission.findOne({
|
||||
where: { establishment_id, quarter, year },
|
||||
});
|
||||
|
||||
if (!submission) {
|
||||
return res.status(404).json({ status: "failed", message: "No previous submission found" });
|
||||
}
|
||||
|
||||
// find submission product row
|
||||
const submissionProduct = await SubmissionProduct.findOne({
|
||||
where: {
|
||||
submission_id: submission.id,
|
||||
product_id
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: Product,
|
||||
as: "product",
|
||||
attributes: ["product_name", "hs_code"]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!submissionProduct) {
|
||||
return res.status(404).json({ status: "failed", message: "No previous product row found" });
|
||||
}
|
||||
|
||||
submissionProduct.dataValues.quarter_periods = getQuarterPeriods(
|
||||
Number(year),
|
||||
quarter
|
||||
);
|
||||
|
||||
return res.status(200).json({ status: "success", data: submissionProduct });
|
||||
|
||||
} catch (err) {
|
||||
return res.status(500).json({ status: "failed", message: err.message });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
exports.getProductSubmissionHistory = async (req, res) => {
|
||||
try {
|
||||
|
||||
|
||||
@ -2207,6 +2207,43 @@ router.post("/getQuarterPeriods",[verifySignature, verifyToken], submissionContr
|
||||
*/
|
||||
router.get("/submissions/getPreviousForecastData",[verifySignature, verifyToken], submissionController.getPreviousForecastData);
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/submissions/getBeforePreviousData:
|
||||
* get:
|
||||
* summary: compute the quarter two steps before (before-previous) based on Establishment + current_quarter + current_year + Product
|
||||
* tags: [Submissions]
|
||||
* security:
|
||||
* - appSignature: []
|
||||
* cookieAuth: [] # or bearerAuth: [] if you use Authorization header
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: establishment_id
|
||||
* required: true
|
||||
* schema: { type: integer }
|
||||
* - in: query
|
||||
* name: current_quarter
|
||||
* required: true
|
||||
* schema: { type: string, enum: [Q1, Q2, Q3, Q4] }
|
||||
* - in: query
|
||||
* name: current_year
|
||||
* required: true
|
||||
* schema: { type: integer }
|
||||
* - in: query
|
||||
* name: product_id
|
||||
* required: true
|
||||
* schema: { type: integer }
|
||||
* responses:
|
||||
* 200:
|
||||
* description: Previous forecast data fetched successfully
|
||||
* 404:
|
||||
* description: Not found
|
||||
* 500:
|
||||
* description: Server error
|
||||
*/
|
||||
router.get("/submissions/getBeforePreviousData",[verifySignature, verifyToken], submissionController.getBeforePreviousData);
|
||||
|
||||
|
||||
/**
|
||||
* @swagger
|
||||
* /api/submissions/getProductSubmissionHistory:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user