fcsc_ipi_frontend/ipi-survey-platform/src/services/establishments/establishmentService.js
2025-10-29 18:26:41 +05:30

95 lines
3.1 KiB
JavaScript

import { getRequest, postRequest, putRequest, deleteRequest } from '@/services/api/CommonService';
import resolveEstablishmentId from '@/services/utils/establishment';
const dashboardEndpoint = '/establishment_dashboard';
const establishmentsEndpoint = '/establishments';
const buildEstablishmentQueryParams = (params = {}) => {
const {
page,
limit,
search,
emirateId,
isicCode,
status,
sortBy,
sortOrder,
export: exportFormat,
} = params;
const query = {};
if (page !== undefined) query.page = page;
if (limit !== undefined) query.limit = limit;
if (search) query.search = search;
if (emirateId) query.emirate_id = emirateId;
if (isicCode) query.isic_code = isicCode;
if (status) query.status = status;
if (sortBy) query.sort_by = sortBy;
if (sortOrder) query.sort_order = sortOrder;
if (exportFormat) query.export = exportFormat;
return query;
};
export const fetchEstablishments = async ({ params, ...config } = {}) => {
const query = buildEstablishmentQueryParams(params);
const response = await getRequest(establishmentsEndpoint, {
...config,
params: { ...(config.params || {}), ...query },
});
return response.data;
};
export const createEstablishment = async (payload, config = {}) => {
const response = await postRequest(establishmentsEndpoint, payload, config);
return response.data;
};
export const deleteEstablishment = async (establishmentId, config = {}) => {
const resolvedId = resolveEstablishmentId(establishmentId);
if (resolvedId === undefined) {
throw new Error('Missing establishment ID for delete request.');
}
const url = `${establishmentsEndpoint}/${resolvedId}`;
const response = await deleteRequest(url, config);
return response.data;
};
export const updateEstablishment = async (establishmentId, payload, config = {}) => {
const resolvedId = resolveEstablishmentId(establishmentId);
if (resolvedId === undefined) {
throw new Error('Missing establishment ID for update request.');
}
const url = `${establishmentsEndpoint}/${resolvedId}`;
const response = await putRequest(url, payload, config);
return response.data;
};
export const fetchEstablishmentDashboard = async (establishmentId, config = {}) => {
const resolvedId = resolveEstablishmentId(establishmentId);
if (resolvedId === undefined) {
throw new Error('Missing establishment ID for dashboard request.');
}
const url = `${dashboardEndpoint}?establishment_id=${resolvedId}`;
const response = await getRequest(url, config);
return response.data;
};
export const fetchEstablishmentDetail = async (establishmentId, config = {}) => {
const resolvedId = resolveEstablishmentId(establishmentId);
if (resolvedId === undefined) {
throw new Error('Missing establishment ID for establishment detail request.');
}
const url = `${establishmentsEndpoint}/${resolvedId}`;
const response = await getRequest(url, config);
return response.data;
};
export default {
fetchEstablishments,
fetchEstablishmentDashboard,
fetchEstablishmentDetail,
createEstablishment,
updateEstablishment,
deleteEstablishment,
};