Skip to content

Commit

Permalink
Merge pull request #53 from InseeFrLab/feat/sprint22
Browse files Browse the repository at this point in the history
Feat/sprint22
  • Loading branch information
MailineN authored Aug 22, 2024
2 parents d0019e4 + 27ee274 commit 524947a
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 73 deletions.
7 changes: 0 additions & 7 deletions src/assets/surveyData/dataEmptyActivity.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,5 @@
"INPUTED": null,
"PREVIOUS": null,
"COLLECTED": null
},
"ISVALIDATED": {
"EDITED": null,
"FORCED": null,
"INPUTED": null,
"PREVIOUS": null,
"COLLECTED": null
}
}
12 changes: 0 additions & 12 deletions src/assets/surveyData/edtActivitySurvey.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,6 @@
"componentRef": "activityselecter_mainactivity",
"variableType": "COLLECTED"
},
{
"name": "ISVALIDATED",
"values": {
"EDITED": null,
"FORCED": null,
"INPUTED": null,
"PREVIOUS": null,
"COLLECTED": null
},
"componentRef": "",
"variableType": "COLLECTED"
},
{
"name": "ISLOCKED",
"values": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import { isDesktop } from "service/responsive";
import { getActivitiesOrRoutes, surveyReadOnly } from "service/survey-activity-service";
import { getData, getValue, getValueOfData, saveDataLocally } from "service/survey-service";
import { isSurveyCompleted } from "service/survey-state-service";
import { getSurveyIdFromUrl } from "utils/utils";

const ActivityDurationPage = () => {
Expand Down Expand Up @@ -173,7 +172,7 @@ const ActivityDurationPage = () => {
};

const onClose = (forceQuit: boolean) => {
const isCompleted = isSurveyCompleted(idSurvey);
const isCompleted = getValue(idSurvey, FieldNameEnum.ISCOMPLETED, currentIteration) as boolean;
const isCloture = getValue(idSurvey, FieldNameEnum.ISCLOSED) as boolean;
if (!openSnackbar) {
if (!isCompleted) {
Expand Down
14 changes: 7 additions & 7 deletions src/pages/end-survey/EndSurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const EndSurveyPage = () => {
};

const saveDataAndInit = useCallback((surveyData: SurveyData, forceUpdate?: boolean) => {
saveData(idSurvey, surveyData.data, false, forceUpdate).then(() => {
saveData(idSurvey, surveyData, false, forceUpdate).then(() => {
initializeSurveysDatasCache().finally(() => {
setIsModalDisplayed(true);
});
Expand All @@ -101,21 +101,21 @@ const EndSurveyPage = () => {
data: dataWithIsEnvoyed ?? callbackHolder.getData(),
};

const handleSuccess = (surveyDataAnswer: any) => {
surveyData.data.lastRemoteSaveDate = surveyDataAnswer.stateData?.date;
return saveDataAndInit(surveyData, true);
};
// const handleSuccess = (surveyDataAnswer: any) => {
// surveyData.data.lastRemoteSaveDate = surveyDataAnswer.stateData?.date;
// return saveDataAndInit(surveyData, true);
// };

const handleError = () => {
setErrorSubmit(true);
};
if (!isDemoMode && navigator.onLine) {
if (isReviewer()) {
return remotePutSurveyDataReviewer(idSurvey, stateData, surveyData.data)
.then(handleSuccess)
.then(navToHome)
.catch(handleError);
} else {
return remotePutSurveyData(idSurvey, surveyData).then(handleSuccess).catch(handleError);
return remotePutSurveyData(idSurvey, surveyData).then(navToHome).catch(handleError);
}
} else {
return saveDataAndInit(surveyData, true);
Expand Down
46 changes: 40 additions & 6 deletions src/service/api-service/getRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ const remoteGetSurveyData = (
)
.then(response => {
if (response.data.COLLECTED != null) {
const revertedTranformedData = revertTransformedArray(response.data.COLLECTED);
response.data.COLLECTED = revertedTranformedData;
try {
const revertedTranformedData = revertTransformedArray(response.data.COLLECTED);
response.data.COLLECTED = revertedTranformedData;
} catch (error) {
console.error("Error reverting transformed data:", error);
}
resolve(response.data);
}
resolve(response.data);
Expand All @@ -188,6 +192,31 @@ const remoteGetSurveyData = (
});
};

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

const requestGetDataReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
Expand All @@ -200,8 +229,12 @@ const requestGetDataReviewer = (
)
.then(response => {
if (response.data != null) {
const revertedTranformedData = revertTransformedArray(response.data.COLLECTED);
response.data.COLLECTED = revertedTranformedData;
try {
const revertedTranformedData = revertTransformedArray(response.data.COLLECTED);
response.data.COLLECTED = revertedTranformedData;
} catch (error) {
console.error("Error reverting transformed data:", error);
}
resolve(response.data);
} else {
resolve(response.data);
Expand Down Expand Up @@ -249,8 +282,8 @@ const requestGetSurveyDataReviewer = (
idSurvey: string,
setError: (error: ErrorCodeEnum) => void,
): Promise<SurveyData> => {
return requestGetStateReviewer(idSurvey, setError).then((stateData: StateData) => {
return requestGetDataReviewer(idSurvey, setError).then(data => {
return requestGetDataReviewer(idSurvey, setError).then(data => {
return requestGetStateReviewer(idSurvey, setError).then((stateData: StateData) => {
return new Promise(resolve => {
const surveyData: SurveyData = {
stateData: stateData,
Expand Down Expand Up @@ -292,5 +325,6 @@ export {
fetchSurveysSourcesByIds,
fetchUserSurveysInfo,
remoteGetSurveyData,
remoteGetSurveyStateData,
remoteGetSurveyDataReviewer,
};
7 changes: 4 additions & 3 deletions src/service/api-service/putRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const requestPutSurveyData = (
data: SurveyData,
token?: string,
): Promise<SurveyData> => {
console.log("requestPutSurveyData", data);
const collectedData = transformCollectedArray(data?.data?.COLLECTED);
if (data.data) {
data.data.COLLECTED = collectedData;
delete data.data.COLLECTED?.WEEKLYPLANNER;
delete data.data.stateData;
}
console.log("requestPutSurveyData", data);
const stateData = data.stateData;
const putLunaticData = axios.put(
`${stromaeBackOfficeApiBaseUrl}api/survey-unit/${idSurvey}/data`,
Expand Down Expand Up @@ -68,7 +69,7 @@ const remotePutSurveyDataReviewer = (
data: LunaticData,
): Promise<SurveyData> => {
//Temporar check on token validity to avoid 401 error, if not valid, reload page
//#
//
const now = new Date();
const tokenExpiresAt = jwt<JwtPayload>(getUserToken() ?? "").exp;
// * 1000 because tokenExpiresAt is in seconds and now.getTime() in milliseconds
Expand All @@ -95,7 +96,7 @@ const requestPutDataReviewer = (
): Promise<LunaticData> => {
console.log("requestPutDataReviewer", data);
data.COLLECTED = transformCollectedArray(data?.COLLECTED);
//TODO: Find another solution to avoid ui bug
//TODO: Find another solution to avoid ui problem
delete data.COLLECTED?.WEEKLYPLANNER;
return new Promise<LunaticData>(resolve => {
axios
Expand Down
1 change: 0 additions & 1 deletion src/service/loop-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,6 @@ const getCurrentLoopPage = (
return { step: currentLoopSubpage, completed: false };
};

//TODO: Edit this function to send state data with COMPLETED status instead
const setLoopCompleted = (idSurvey: string, iteration: number | undefined, isCompleted: boolean) => {
const completed = setValue(idSurvey, FieldNameEnum.ISCOMPLETED, isCompleted, iteration);
if (completed) {
Expand Down
30 changes: 14 additions & 16 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
import { EdtUserRightsEnum } from "./../enumerations/EdtUserRightsEnum";
import { LunaticData } from "./../interface/lunatic/Lunatic";
import {
fetchSurveysSourcesByIds,
fetchUserSurveysInfo,
remoteGetSurveyData,
remoteGetSurveyDataReviewer,
Expand Down Expand Up @@ -235,23 +234,21 @@ const initDataForSurveys = (setError: (error: ErrorCodeEnum) => void) => {
[SurveysIdsEnum.ACTIVITY_SURVEYS_IDS]: activitySurveysIds,
[SurveysIdsEnum.WORK_TIME_SURVEYS_IDS]: workingTimeSurveysIds,
};
const sources: SourceData = {
[SourcesEnum.ACTIVITY_SURVEY]: edtActivitySurvey,
[SourcesEnum.WORK_TIME_SURVEY]: edtWorkTimeSurvey,
};
const innerPromises: Promise<any>[] = [
getRemoteSavedSurveysDatas(allSurveysIds, setError).then(() => {
return initializeSurveysDatasCache(allSurveysIds);
}),
saveSurveysIds(surveysIds),
fetchSurveysSourcesByIds(
[SourcesEnum.ACTIVITY_SURVEY, SourcesEnum.WORK_TIME_SURVEY],
setError,
).then(sources => {
const inerFetchPromises: Promise<any>[] = [
saveSources(sources),
saveUserSurveysData({ data: userDatas }),
];
return Promise.all(inerFetchPromises);
}),
];
return Promise.all(innerPromises);
const inerFetchPromises: Promise<any>[] = [
saveSources(sources),
saveUserSurveysData({ data: userDatas }),
];
return Promise.all([...innerPromises, ...inerFetchPromises]);
});
} else {
return lunaticDatabase.get(USER_SURVEYS_DATA).then((data: LunaticData | undefined) => {
Expand Down Expand Up @@ -750,7 +747,8 @@ const getData = (idSurvey: string): LunaticData => {
};

const getDataCache = (idSurvey: string) => {
return datas.get(idSurvey) ?? getItemFromSession(idSurvey);
const data = datas.get(idSurvey) ?? getItemFromSession(idSurvey);
return data;
};

const setDataCache = (idSurvey: string, data: LunaticData) => {
Expand Down Expand Up @@ -901,24 +899,23 @@ const saveData = (
if (!navigator.onLine || isDemoMode || localSaveOnly) stateData.date = 0;

if (isChange) {
console.log("SaveRemote data", data.COLLECTED);
console.log("SaveRemote data", data);

data = saveQualityScore(idSurvey, data);
stateData = getSurveyStateData(data);

if (!navigator.onLine) {
stateData.date = 0;
data.stateData = stateData;
return setLocalDatabase(stateData, data, idSurvey);
}

if (!isDemoMode && !localSaveOnly) {
stateData.date = data.lastLocalSaveDate ?? Date.now();
const surveyData: SurveyData = {
stateData: stateData,
data: data,
};
data.lastRemoteSaveDate = stateData.date;
console.log("StateData to save", stateData);

if (isReviewerMode) {
return remotePutSurveyDataReviewer(idSurvey, stateData, data).then(() => {
Expand All @@ -928,6 +925,7 @@ const saveData = (
return setLocalDatabase(stateData, data, idSurvey);
});
} else {
//TODO: TEMP: need to figure out why there is still a state data here
return remotePutSurveyData(idSurvey, surveyData).then(() => {
data.stateData = stateData;
return setLocalDatabase(stateData, data, idSurvey);
Expand Down
29 changes: 10 additions & 19 deletions src/service/survey-state-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,11 @@ const isDemoMode = () => {
return getFlatLocalStorageValue(LocalStorageVariableEnum.IS_DEMO_MODE) === "true";
};

const isSurveyLocked = (idSurvey: string) => {
const isLocked = getValue(idSurvey, FieldNameEnum.ISLOCKED) as boolean;
return (isLocked != null && isLocked) || existVariableEdited(idSurvey);
};

// const surveyValidated = (idSurvey: string) => {
// const isValidated = getValue(idSurvey, FieldNameEnum.ISVALIDATED) as boolean;
// return isValidated != null && isValidated;
// };

// const surveyClosed = (idSurvey: string) => {
// const isClosed = getValue(idSurvey, FieldNameEnum.ISCLOSED) as boolean;
// return isClosed != null && isClosed;
// };

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

const isSurveyClosed = (idSurvey: string) => {
const stateData = getSurveyStateData(getData(idSurvey));
return stateData.state == StateDataStateEnum.COMPLETED;
};
const isSurveyStarted = (idSurvey: string) => {
const stateData = getSurveyStateData(getData(idSurvey));
return stateData.state == StateDataStateEnum.INIT;
Expand All @@ -60,6 +41,16 @@ const isSurveyCompleted = (idSurvey: string) => {
return stateData.state == StateDataStateEnum.COMPLETED;
};

const isSurveyClosed = (idSurvey: string) => {
const isClosed = getValue(idSurvey, FieldNameEnum.ISCLOSED) as boolean;
return isClosed != null && isClosed;
};

const isSurveyLocked = (idSurvey: string) => {
const isLocked = getValue(idSurvey, FieldNameEnum.ISLOCKED) as boolean;
return (isLocked != null && isLocked) || existVariableEdited(idSurvey);
};

const getStatutSurvey = (idSurvey: string) => {
const isLocked = getValue(idSurvey, FieldNameEnum.ISLOCKED) as boolean;
const isValidated = isSurveyValidated(idSurvey);
Expand Down
1 change: 1 addition & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function addItemToSession(idSurvey: string, item: any) {
}

function getItemFromSession(idSurvey: string) {
console.log("getItemFromSession", idSurvey, sessionStorage.getItem(idSurvey));
return JSON.parse(sessionStorage.getItem(idSurvey ?? "") ?? "{}");
}

Expand Down

0 comments on commit 524947a

Please sign in to comment.