Skip to content

Commit

Permalink
fix: getStateDataFromRemote
Browse files Browse the repository at this point in the history
  • Loading branch information
MailineN committed Aug 26, 2024
1 parent 8bab513 commit 7fd947e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/pages/home-surveyed/HomeSurveyed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
surveysIds,
userDatasMap,
initializeDatas,
createNameSurveyMap,
} from "service/survey-service";
import { isDemoMode, lockAllSurveys, validateAllEmptySurveys } from "service/survey-state-service";
import { getUserRights } from "service/user-service";
Expand Down
27 changes: 1 addition & 26 deletions src/service/api-service/getRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ const remoteGetSurveyStateData = (
getHeader(stromaeBackOfficeApiBaseUrl),
)
.then(response => {
console.log("Get stateData", response.data);
const stateData: StateData = response.data;
resolve(stateData);
})
Expand Down Expand Up @@ -270,36 +269,12 @@ const requestGetDataReviewer = (
});
};

const requestGetStateReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<StateData> => {
return new Promise<StateData>(resolve => {
axios
.get(
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey + "/state-data",
getHeader(stromaeBackOfficeApiBaseUrl),
)
.then(response => {
resolve(response.data);
})
.catch(err => {
console.log(err);
if (err.response?.status === 403) {
setError(ErrorCodeEnum.NO_RIGHTS);
} else {
resolve(initStateData());
}
});
});
};

const requestGetSurveyDataReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<SurveyData> => {
return requestGetDataReviewer(idSurvey, setError).then(data => {
return requestGetStateReviewer(idSurvey, setError).then((stateData: StateData) => {
return remoteGetSurveyStateData(idSurvey, setError).then((stateData: StateData) => {
return new Promise(resolve => {
const surveyData: SurveyData = {
stateData: stateData,
Expand Down
21 changes: 12 additions & 9 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { AuthContextProps } from "oidc-react";
import { NavigateFunction } from "react-router-dom";
import {
fetchReviewerSurveysAssignments,
remoteGetSurveyStateData,
requestGetDataReviewer,
} from "service/api-service/getRemoteData";
import { lunaticDatabase } from "service/lunatic-database";
Expand Down Expand Up @@ -71,7 +72,7 @@ import { remotePutSurveyData, remotePutSurveyDataReviewer } from "./api-service/
import { fetchReferentiels } from "./api-service/getLocalSurveyData";
import { fetchRemoteReferentiels } from "service/api-service/getRemoteData";
import {
getSurveyStateData,
getLocalSurveyStateData,
initStateData,
isDemoMode,
isSurveyClosed,
Expand Down Expand Up @@ -536,15 +537,15 @@ const getRemoteSavedSurveyData = (
return Promise.reject(new Error("Offline"));
}

const urlRemote = isReviewer() ? requestGetDataReviewer : remoteGetSurveyData;

return urlRemote(surveyId, setError)
const getSurveyDataFunction = isReviewer() ? requestGetDataReviewer : remoteGetSurveyData;
const getSurveyStateDataFunction = remoteGetSurveyStateData;
//TODO: Refactor dirty code
return getSurveyDataFunction(surveyId, setError)
.then((remoteSurveyData: any) => {
// TODO: remove any and improve ts types
const surveyData = initializeData(remoteSurveyData, surveyId);
return lunaticDatabase.get(surveyId).then(localSurveyData => {
if (shouldInitData(remoteSurveyData, localSurveyData)) {
const stateData = getSurveyStateData(surveyData);
const stateData = getLocalSurveyStateData(surveyData);
setLocalDatabase(stateData, surveyData, surveyId);
return lunaticDatabase.save(surveyId, surveyData);
} else {
Expand All @@ -555,8 +556,10 @@ const getRemoteSavedSurveyData = (
if (weeklyPlanner) {
remoteSurveyData.COLLECTED.WEEKLYPLANNER = weeklyPlanner;
}
const stateData = getSurveyStateData(surveyData);
setLocalDatabase(stateData, remoteSurveyData, surveyId);
getSurveyStateDataFunction(surveyId, setError).then(stateData => {
setLocalDatabase(stateData, remoteSurveyData, surveyId);
});

return lunaticDatabase.save(surveyId, surveyData);
}
}
Expand Down Expand Up @@ -1725,7 +1728,7 @@ export {
getSurveyDate,
getSurveysIdsForHousehold,
getSurveyRights,
getSurveyStateData,
getLocalSurveyStateData,
getTabsData,
getUserDatas,
getUserDatasActivity,
Expand Down
14 changes: 7 additions & 7 deletions src/service/survey-state-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ const isDemoMode = () => {
};

const isSurveyValidated = (idSurvey: string) => {
const stateData = getSurveyStateData(getData(idSurvey));
const stateData = getLocalSurveyStateData(getData(idSurvey));
return stateData.state == StateDataStateEnum.VALIDATED;
};

const isSurveyStarted = (idSurvey: string) => {
const stateData = getSurveyStateData(getData(idSurvey));
const stateData = getLocalSurveyStateData(getData(idSurvey));
return stateData.state == StateDataStateEnum.INIT;
};

const isSurveyCompleted = (idSurvey: string) => {
const stateData = getSurveyStateData(getData(idSurvey));
const stateData = getLocalSurveyStateData(getData(idSurvey));
return stateData.state == StateDataStateEnum.COMPLETED;
};

Expand All @@ -62,7 +62,7 @@ const getStatutSurvey = (idSurvey: string) => {
} else return StateDataStateEnum.INIT;
};

const getSurveyStateData = (data: LunaticData): StateData => {
const getLocalSurveyStateData = (data: LunaticData): StateData => {
const lastRemoteDate = Date.now();
const stateData: StateData = {
state: data.stateData?.state ?? StateDataStateEnum.INIT,
Expand Down Expand Up @@ -124,7 +124,7 @@ const lockAllSurveys = (idHousehold: string) => {

const validateSurvey = (idSurvey: string) => {
const data = getData(idSurvey);
const stateData = getSurveyStateData(getData(idSurvey));
const stateData = getLocalSurveyStateData(getData(idSurvey));
if (stateData.state != StateDataStateEnum.VALIDATED) {
const validatedStateData: StateData = {
idStateData: stateData.idStateData,
Expand All @@ -144,7 +144,7 @@ const validateAllEmptySurveys = (idHousehold: string) => {

idSurveys.forEach((idSurvey: string) => {
const data = getData(idSurvey || "");
const stateData = getSurveyStateData(data);
const stateData = getLocalSurveyStateData(data);

if (stateData.state != StateDataStateEnum.VALIDATED) {
const validatedStateData: StateData = {
Expand Down Expand Up @@ -179,7 +179,7 @@ export {
isSurveyStarted,
isSurveyCompleted,
getStatutSurvey,
getSurveyStateData,
getLocalSurveyStateData,
lockAllSurveys,
lockSurvey,
validateSurvey,
Expand Down

0 comments on commit 7fd947e

Please sign in to comment.