From db816b4879aa5265aae9ab6ec877be25331fe463 Mon Sep 17 00:00:00 2001 From: Malini Date: Thu, 18 Dec 2025 12:50:24 +0530 Subject: [PATCH] added service file --- .../services/submissions/submissionService.js | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/ipi-survey-platform/src/services/submissions/submissionService.js b/ipi-survey-platform/src/services/submissions/submissionService.js index 3038d47..35a0abf 100644 --- a/ipi-survey-platform/src/services/submissions/submissionService.js +++ b/ipi-survey-platform/src/services/submissions/submissionService.js @@ -84,69 +84,70 @@ export const getQuarterPeriods = async (currentYear, currentQuarter) => { } }; -export const getPreviousForecastData = async (establishmentId, quarter, year, productId) => { +export const getPreviousForecastData = async ( + establishmentId, + quarter, + year, + productId +) => { try { const response = await getRequest( - '/submissions/getPreviousForecastData', + "/submissions/getPreviousForecastData", { params: { establishment_id: establishmentId, - quarter: quarter, - year: year, - product_id: productId - } + quarter, + year, + product_id: productId, + }, } ); - // Check if response has data property and return it, otherwise return the response as is - return response.data || response; + + // Always return only backend data + return response?.data || null; + } catch (error) { - console.error('Error fetching previous forecast data:', error); - // Instead of throwing, return null to handle the error gracefully + console.error("Error fetching previous forecast data:", error); return null; } }; + export const getBeforePreviousData = async ( establishmentId, - currentQuarter, - currentYear, + quarter, + year, productId ) => { try { - if (!establishmentId || !currentQuarter || !currentYear || !productId) { + if (!establishmentId || !quarter || !year || !productId) { throw new Error("Missing required parameters"); } - const q = Number(currentQuarter.replace("Q", "")); - let beforePrevQuarter = `Q${q - 2}`; - let beforePrevYear = currentYear; - - if (q - 2 <= 0) { - beforePrevQuarter = `Q${4 + (q - 2)}`; - beforePrevYear = currentYear - 1; - } - const response = await getRequest( "/submissions/getBeforePreviousData", { params: { establishment_id: establishmentId, - current_quarter: beforePrevQuarter, - current_year: beforePrevYear, - product_id: productId - } + current_quarter: quarter, + current_year: year, + product_id: productId, + }, } ); - return response?.data ?? response; + return response?.data ?? null; + } catch (error) { - console.error("Error fetching before-previous data:", error); + console.error("Error fetching data:", error); return null; } }; + + export const getSubmissionHistoryByEstablishment = async (establishmentId, config = {}) => { try { if (!establishmentId) { @@ -236,4 +237,5 @@ export default { getProductSubmissionHistory, getBeforePreviousData }; + \ No newline at end of file