Skip to content

Commit

Permalink
chore: cleanup & edit survey model to add "S_" prefix by default
Browse files Browse the repository at this point in the history
  • Loading branch information
MailineN committed Aug 10, 2024
1 parent 090c1e2 commit e2ecb43
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 852 deletions.
199 changes: 96 additions & 103 deletions src/assets/surveyData/dataEmptyWeeklyPlanner.json

Large diffs are not rendered by default.

966 changes: 249 additions & 717 deletions src/assets/surveyData/edtWorkTimeSurvey.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ActivityLocationPage = () => {
},
referentiel: getPlaceRef(),
};
console.log("ActivityLocationPage referentiel", specifiquesProps.referentiel);
//console.log("ActivityLocationPage referentiel", specifiquesProps.referentiel);
return (
<LoopSurveyPageStep
currentPage={EdtRoutesNameEnum.ACTIVITY_LOCATION}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const MainActivityPage = () => {
},
nextClickCallback: (routeToGoal: boolean) => {
const codeActivity = getValueOfActivity(callbackHolder.getData(), currentIteration) ?? "";
console.log("codeActivity", codeActivity);
//console.log("codeActivity", codeActivity);
const skip = filtrePage(EdtRoutesNameEnum.MAIN_ACTIVITY_GOAL, codeActivity);
if (routeToGoal && !skip) {
saveAndLoopNavigate(
Expand Down
6 changes: 3 additions & 3 deletions src/pages/work-time/weekly-planner/WeeklyPlanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
getNavigatePath,
getOrchestratorPage,
navFullPath,
saveAndNav,
saveAndNavLocally,
setEnviro,
} from "service/navigation-service";
Expand Down Expand Up @@ -133,6 +132,7 @@ const WeeklyPlannerPage = () => {
getArrayFromSession("DATES")) as string[];
const currentDateIndex = dates.indexOf(response.date);
const dataResponse = getData(idSurveyResponse);
console.log("Save Duration", dataCopy);
if (
!isReviewer() &&
dataResponse.COLLECTED?.[FieldNameEnum.FIRSTNAME].COLLECTED ==
Expand Down Expand Up @@ -238,7 +238,7 @@ const WeeklyPlannerPage = () => {
const validateAndNav = (): void => {
if (displayDayOverview) {
if (isPlaceWorkDisplayed) {
saveDataLocally(idSurvey, callbackHolder.getData());
saveData(idSurvey, callbackHolder.getData());
setDisplayDayOverview(true);
setIsPlaceWorkDisplayed(false);
isPlaceWorkDisplayed = false;
Expand Down Expand Up @@ -304,7 +304,7 @@ const WeeklyPlannerPage = () => {
<SurveyPage
idSurvey={idSurvey}
validate={useCallback(() => validateAndNav(), [displayDayOverview])}
onNavigateBack={useCallback(() => validateAndNav(), [displayDayOverview])}
onNavigateBack={useCallback(() => saveAndNavLocally(idSurvey), [displayDayOverview])}
onPrevious={useCallback(() => saveAndNavLocally(idSurvey), [])}
onEdit={useCallback(() => onEdit(), [])}
onHelp={onHelp}
Expand Down
10 changes: 1 addition & 9 deletions src/service/api-service/getRemoteData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getUserToken, isReviewer } from "../user-service";
import { AuthContextProps } from "oidc-react";
import { NomenclatureActivityOption } from "@inseefrlab/lunatic-edt";
import { ReferentielsEnum } from "enumerations/ReferentielsEnum";
import { revertTransformedArray } from "utils/utils";

export const edtOrganisationApiBaseUrl = process.env.REACT_APP_EDT_ORGANISATION_API_BASE_URL;
export const stromaeBackOfficeApiBaseUrl = process.env.REACT_APP_STROMAE_BACK_OFFICE_API_BASE_URL;
Expand Down Expand Up @@ -85,15 +86,6 @@ const fetchRemoteReferentiels = (setError: (error: ErrorCodeEnum) => void): Prom
});
};

const revertTransformedArray = (dataAct: any) => {
const revertedDataAct: { [key: string]: any } = {};
Object.keys(dataAct).forEach(key => {
const revertedKey = key.startsWith("S_") ? key.substring(2) : key;
revertedDataAct[revertedKey] = dataAct[key];
});
return revertedDataAct;
};

const fetchUserSurveysInfo = (setError: (error: ErrorCodeEnum) => void): Promise<UserSurveys[]> => {
return new Promise(resolve => {
axios
Expand Down
14 changes: 3 additions & 11 deletions src/service/api-service/putRemoteData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//TODO: fix any

import axios from "axios";
import { StateDataStateEnum } from "enumerations/StateDataStateEnum";
import { SurveyData, StateData } from "interface/entity/Api";
Expand All @@ -9,21 +7,14 @@ import { getUserToken, getAuth } from "service/user-service";
import { stromaeBackOfficeApiBaseUrl, getHeader } from "./getRemoteData";
import jwt, { JwtPayload } from "jwt-decode";
import { logout } from "service/auth-service";

const transformCollectedArray = (dataAct: any) => {
const transformedDataAct: { [key: string]: any } = {};
Object.keys(dataAct).forEach(key => {
const transformedKey: string = /^\d/.test(key) ? `S_${key}` : key;
transformedDataAct[transformedKey] = dataAct[key];
});
return transformedDataAct;
};
import { transformCollectedArray } from "utils/utils";

const requestPutSurveyData = (
idSurvey: string,
data: SurveyData,
token?: string,
): Promise<SurveyData> => {
console.log("requestPutSurveyData", data);
const collectedData = transformCollectedArray(data?.data?.COLLECTED);
if (data.data) {
data.data.COLLECTED = collectedData;
Expand Down Expand Up @@ -102,6 +93,7 @@ const requestPutDataReviewer = (
data: LunaticData,
token?: string,
): Promise<LunaticData> => {
console.log("requestPutDataReviewer", data);
data.COLLECTED = transformCollectedArray(data?.COLLECTED);
delete data.COLLECTED?.WEEKLYPLANNER;
return new Promise<LunaticData>(resolve => {
Expand Down
1 change: 0 additions & 1 deletion src/service/navigation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ const closeFormularieAndNav = (idSurvey: string, route: string) => {
* we need to make the call twice to be able to retrieve the current state of the database
*/
const validate = (idSurvey: string): Promise<void | LunaticData> => {
console.log("validate");
return saveData(idSurvey, _callbackHolder.getData() ?? getData(idSurvey), true).then(() => {
return saveData(idSurvey, _callbackHolder.getData() ?? getData(idSurvey), false);
});
Expand Down
7 changes: 6 additions & 1 deletion src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
getArrayFromSession,
getItemFromSession,
groupBy,
revertTransformedArray,
transformCollectedArray,

Check warning on line 49 in src/service/survey-service.ts

View workflow job for this annotation

GitHub Actions / test_lint

'transformCollectedArray' is defined but never used
} from "utils/utils";
import {
edtWorkTimeSurvey,
Expand Down Expand Up @@ -746,6 +748,8 @@ const getData = (idSurvey: string): LunaticData => {
const modifyCollected = modifyIndividualCollected(idSurvey);
const emptyData = getDataCache(idSurvey) ?? createDataEmpty(idSurvey ?? "");
const data = modifyCollected || emptyData;
const revertedCollected = revertTransformedArray(data.COLLECTED);
data.COLLECTED = revertedCollected;
return data;
};

Expand Down Expand Up @@ -1007,7 +1011,8 @@ const saveData = (
if (!navigator.onLine || isDemoMode || localSaveOnly) stateData.date = 0;

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

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

Expand Down
51 changes: 46 additions & 5 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,28 @@ function difference(origObj: any, newObj: any) {
return changes(newObj, origObj);
}

/**
* Merges specific fields from one object into another.
*
* This function merges the "variables" and "components" fields from the new object into the original object.
* Both objects must be valid and of type object.
*
* @param origObj - The original object to merge into.
* @param newObj - The new object containing fields to merge.
* @returns The merged object.
* @throws Will throw an error if either origObj or newObj is not a valid object.
*/
function mergeObjects(origObj: any, newObj: any): any {
// Validate input objects
if (!origObj || !newObj || typeof origObj !== "object" || typeof newObj !== "object") {
throw new Error("Both origObj and newObj must be valid objects.");
}

// Fields to merge
const fieldsToMerge = ["variables", "components"];

fieldsToMerge.forEach(field => {
// Check if the field exists and is an array in origObj
if (Array.isArray(origObj[field])) {
// Ensure the field exists in newObj; if not, initialize as an empty array
newObj[field] = newObj[field] || [];

// Merge the array from origObj into newObj without duplicates
origObj[field].forEach((item: any) => {
if (!newObj[field].some((newItem: any) => isEqual(newItem, item))) {
newObj[field].push(item);
Expand All @@ -200,6 +206,39 @@ function mergeObjects(origObj: any, newObj: any): any {
return newObj;
}

/**
* Reverts a transformed array back to its original structure.
* (Platine Back Office data transformation)
* @param transformedArray - The array that has been transformed.
* @param transformationLogic - A function that defines how to revert the transformation.
* @returns The original array structure before transformation.
* @throws Will throw an error if the transformedArray is not an array .
*/
const revertTransformedArray = (dataAct: any) => {
const revertedDataAct: { [key: string]: any } = {};
Object.keys(dataAct).forEach(key => {
const revertedKey = key.startsWith("S_") ? key.substring(2) : key;
revertedDataAct[revertedKey] = dataAct[key];
});
return revertedDataAct;
};

/**
* Transforms the keys of an object by prefixing keys that start with a digit.
* (Required for Platine Back Office data extract)
*
* @param dataAct - The input object whose keys need to be transformed.
* @returns The transformed object with prefixed keys.
*/
const transformCollectedArray = (dataAct: any) => {
const transformedDataAct: { [key: string]: any } = {};
Object.keys(dataAct).forEach(key => {
const transformedKey: string = /^\d/.test(key) ? `S_${key}` : key;
transformedDataAct[transformedKey] = dataAct[key];
});
return transformedDataAct;
};

export {
addArrayToSession,
addItemToSession,
Expand All @@ -219,4 +258,6 @@ export {
sumAllOfArray,
difference,
mergeObjects,
revertTransformedArray,
transformCollectedArray,
};

0 comments on commit e2ecb43

Please sign in to comment.