added service file

This commit is contained in:
Malini 2025-12-18 12:50:24 +05:30
parent fad60efb61
commit db816b4879

View File

@ -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 { try {
const response = await getRequest( const response = await getRequest(
'/submissions/getPreviousForecastData', "/submissions/getPreviousForecastData",
{ {
params: { params: {
establishment_id: establishmentId, establishment_id: establishmentId,
quarter: quarter, quarter,
year: year, year,
product_id: productId 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) { } catch (error) {
console.error('Error fetching previous forecast data:', error); console.error("Error fetching previous forecast data:", error);
// Instead of throwing, return null to handle the error gracefully
return null; return null;
} }
}; };
export const getBeforePreviousData = async ( export const getBeforePreviousData = async (
establishmentId, establishmentId,
currentQuarter, quarter,
currentYear, year,
productId productId
) => { ) => {
try { try {
if (!establishmentId || !currentQuarter || !currentYear || !productId) { if (!establishmentId || !quarter || !year || !productId) {
throw new Error("Missing required parameters"); 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( const response = await getRequest(
"/submissions/getBeforePreviousData", "/submissions/getBeforePreviousData",
{ {
params: { params: {
establishment_id: establishmentId, establishment_id: establishmentId,
current_quarter: beforePrevQuarter, current_quarter: quarter,
current_year: beforePrevYear, current_year: year,
product_id: productId product_id: productId,
} },
} }
); );
return response?.data ?? response; return response?.data ?? null;
} catch (error) { } catch (error) {
console.error("Error fetching before-previous data:", error); console.error("Error fetching data:", error);
return null; return null;
} }
}; };
export const getSubmissionHistoryByEstablishment = async (establishmentId, config = {}) => { export const getSubmissionHistoryByEstablishment = async (establishmentId, config = {}) => {
try { try {
if (!establishmentId) { if (!establishmentId) {
@ -236,4 +237,5 @@ export default {
getProductSubmissionHistory, getProductSubmissionHistory,
getBeforePreviousData getBeforePreviousData
}; };