Skip to content

Commit

Permalink
refactor: new survey-unit endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MailineN committed Jul 5, 2024
1 parent a70dbed commit 1404bb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
44 changes: 27 additions & 17 deletions src/service/api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,35 @@ const requestPutSurveyData = (
data: SurveyData,
token?: string,
): Promise<SurveyData> => {
const lunaticData = token ? transformCollectedArray(data.data?.COLLECTED) : data.data?.COLLECTED;
console.log("data", data);
const collectedData = token ? transformCollectedArray(data.data?.COLLECTED) : data.data?.COLLECTED;
if (data.data) {
data.data.COLLECTED = collectedData;
}

if (token) {
console.log("lunaticData has been converted");
}
const surveyData: SurveyData = {
data: lunaticData,
stateData: data.stateData,
};
return new Promise(resolve => {
axios
.put(
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey,
surveyData,
getHeader(stromaeBackOfficeApiBaseUrl, token),
)
.then(() => {
return resolve(data);
});
});
const stateData = data.stateData;
const putLunaticData = axios.put(
`${stromaeBackOfficeApiBaseUrl}api/survey-unit/${idSurvey}/data`,
data,
getHeader(stromaeBackOfficeApiBaseUrl, token),
);

const putStateData = axios.put(
`${stromaeBackOfficeApiBaseUrl}api/survey-unit/${idSurvey}/state-data`,
stateData,
getHeader(stromaeBackOfficeApiBaseUrl, token),
);

return Promise.all([putLunaticData, putStateData])
.then(() => {
return data;
})
.catch(error => {
throw error;
});
};

const remotePutSurveyData = (idSurvey: string, data: SurveyData): Promise<SurveyData> => {
Expand Down Expand Up @@ -349,7 +359,7 @@ const remoteGetSurveyData = (
return new Promise(resolve => {
axios
.get(
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey,
stromaeBackOfficeApiBaseUrl + "api/survey-unit/" + idSurvey + "/data",
getHeader(stromaeBackOfficeApiBaseUrl),
)
.then(response => {
Expand Down
6 changes: 3 additions & 3 deletions src/service/survey-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ const initializeSurveysIdsDataModeReviewer = (
});
};

const initializeData = (remoteSurveyData: SurveyData, surveyId: string) => {
const initializeData = (remoteSurveyData: SurveyData, idSurvey: string) => {
const regexp = new RegExp(process.env.REACT_APP_HOUSE_REFERENCE_REGULAR_EXPRESSION || "");
let surveyData: LunaticData = {
COLLECTED: {},
Expand All @@ -490,10 +490,10 @@ const initializeData = (remoteSurveyData: SurveyData, surveyId: string) => {
id: "",
lastLocalSaveDate: Date.now(),
};
surveyData.houseReference = surveyId?.replace(regexp, "");
surveyData.houseReference = idSurvey?.replace(regexp, "");
surveyData.CALCULATED = {};
surveyData.EXTERNAL = {};
surveyData.COLLECTED = remoteSurveyData.data?.COLLECTED ?? getDataEmpty(surveyId);
surveyData.COLLECTED = remoteSurveyData.data?.COLLECTED ?? getDataEmpty(idSurvey);
surveyData.lastLocalSaveDate =
remoteSurveyData.data?.lastLocalSaveDate ?? remoteSurveyData.data.stateData?.date;
surveyData.stateData = remoteSurveyData.stateData;
Expand Down

0 comments on commit 1404bb1

Please sign in to comment.