From 7fd947eabd6826f3e7da617ad1ceae878ed7863b Mon Sep 17 00:00:00 2001 From: Mailine Nguyen <64129348+MailineN@users.noreply.github.com> Date: Mon, 26 Aug 2024 13:46:17 +0200 Subject: [PATCH] fix: getStateDataFromRemote --- src/pages/home-surveyed/HomeSurveyed.tsx | 1 - src/service/api-service/getRemoteData.ts | 27 +----------------------- src/service/survey-service.ts | 21 ++++++++++-------- src/service/survey-state-service.ts | 14 ++++++------ 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/src/pages/home-surveyed/HomeSurveyed.tsx b/src/pages/home-surveyed/HomeSurveyed.tsx index f201b63e..26c603ac 100644 --- a/src/pages/home-surveyed/HomeSurveyed.tsx +++ b/src/pages/home-surveyed/HomeSurveyed.tsx @@ -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"; diff --git a/src/service/api-service/getRemoteData.ts b/src/service/api-service/getRemoteData.ts index 4bb8ff6c..96c51139 100644 --- a/src/service/api-service/getRemoteData.ts +++ b/src/service/api-service/getRemoteData.ts @@ -203,7 +203,6 @@ const remoteGetSurveyStateData = ( getHeader(stromaeBackOfficeApiBaseUrl), ) .then(response => { - console.log("Get stateData", response.data); const stateData: StateData = response.data; resolve(stateData); }) @@ -270,36 +269,12 @@ const requestGetDataReviewer = ( }); }; -const requestGetStateReviewer = ( - idSurvey: string, - setError: (error: ErrorCodeEnum) => void, -): Promise => { - return new Promise(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 => { 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, diff --git a/src/service/survey-service.ts b/src/service/survey-service.ts index a3b48098..1f6cbf18 100644 --- a/src/service/survey-service.ts +++ b/src/service/survey-service.ts @@ -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"; @@ -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, @@ -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 { @@ -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); } } @@ -1725,7 +1728,7 @@ export { getSurveyDate, getSurveysIdsForHousehold, getSurveyRights, - getSurveyStateData, + getLocalSurveyStateData, getTabsData, getUserDatas, getUserDatasActivity, diff --git a/src/service/survey-state-service.ts b/src/service/survey-state-service.ts index 31dfc00e..85cc77d5 100644 --- a/src/service/survey-state-service.ts +++ b/src/service/survey-state-service.ts @@ -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; }; @@ -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, @@ -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, @@ -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 = { @@ -179,7 +179,7 @@ export { isSurveyStarted, isSurveyCompleted, getStatutSurvey, - getSurveyStateData, + getLocalSurveyStateData, lockAllSurveys, lockSurvey, validateSurvey,