Skip to content

Commit ed944db

Browse files
committed
feat: make campaignId optionnal to save edited data
1 parent 21cbc94 commit ed944db

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/main/java/fr/insee/genesis/controller/rest/responses/ResponseController.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
import java.nio.file.Paths;
5858
import java.time.LocalDateTime;
5959
import java.util.ArrayList;
60+
import java.util.HashSet;
6061
import java.util.List;
62+
import java.util.Set;
6163
import java.util.stream.Stream;
6264

6365
@RequestMapping(path = "/responses" )
@@ -324,9 +326,18 @@ public ResponseEntity<List<SurveyUnitSimplified>> getLatestForInterrogationList(
324326
public ResponseEntity<Object> saveEditedVariables(
325327
@RequestBody SurveyUnitInputDto surveyUnitInputDto
326328
){
329+
//Code quality : we need to put all that logic out of this controller
327330
//Parse metadata
328331
//Try to look for DDI first, if no DDI found looks for lunatic components
329332
List<GenesisError> errors = new ArrayList<>();
333+
//We need to retrieve campaignId
334+
HashSet<String> campaignIds = findCampaignId(surveyUnitInputDto);
335+
if (campaignIds.size() != 1){
336+
return ResponseEntity.status(500).body("Impossible to assign one campaignId to that response");
337+
}
338+
// If the size is equal to 1 we get this campaignId
339+
String campaignId = campaignIds.iterator().next();
340+
surveyUnitInputDto.setCampaignId(campaignId);
330341
VariablesMap variablesMap = metadataService.readMetadatas(surveyUnitInputDto.getCampaignId(),
331342
surveyUnitInputDto.getMode().getModeName(), fileUtils, errors);
332343
if(variablesMap == null){
@@ -373,6 +384,15 @@ public ResponseEntity<Object> saveEditedVariables(
373384
return ResponseEntity.ok(SUCCESS_MESSAGE);
374385
}
375386

387+
private HashSet<String> findCampaignId(SurveyUnitInputDto surveyUnitInputDto) {
388+
List<SurveyUnitModel> responses = surveyUnitService.findByIdsInterrogationAndQuestionnaire(surveyUnitInputDto.getInterrogationId(), surveyUnitInputDto.getQuestionnaireId());
389+
HashSet<String> campaignIds = new HashSet<>();
390+
for(SurveyUnitModel response : responses){
391+
campaignIds.add(response.getCampaignId());
392+
}
393+
return campaignIds;
394+
}
395+
376396
//Utilities
377397
/**
378398
* Process a campaign with a specific mode
@@ -489,7 +509,6 @@ private ResponseEntity<Object> processXmlFileSequentially(Path filepath, Mode mo
489509

490510
log.info("Saved {} survey units updates from Xml file {}", suCount, filepath.getFileName());
491511
}
492-
493512
return ResponseEntity.ok().build();
494513
}
495514

0 commit comments

Comments
 (0)