|
1 | 1 | package fr.insee.pearljam.api.service.impl; |
2 | 2 |
|
3 | | -import fr.insee.pearljam.api.domain.ContactOutcomeType; |
4 | | -import fr.insee.pearljam.api.domain.IdentificationConfiguration; |
5 | | -import fr.insee.pearljam.api.domain.SurveyUnit; |
6 | | -import fr.insee.pearljam.api.service.SurveyUnitUpdateService; |
| 3 | +import fr.insee.pearljam.api.domain.*; |
7 | 4 | import fr.insee.pearljam.api.surveyunit.dto.CommentDto; |
8 | 5 | import fr.insee.pearljam.api.surveyunit.dto.CommunicationRequestCreateDto; |
9 | 6 | import fr.insee.pearljam.api.surveyunit.dto.ContactOutcomeDto; |
10 | 7 | import fr.insee.pearljam.api.surveyunit.dto.SurveyUnitUpdateDto; |
11 | 8 | import fr.insee.pearljam.api.surveyunit.dto.identification.IdentificationDto; |
| 9 | +import fr.insee.pearljam.domain.campaign.port.userside.DateService; |
12 | 10 | import fr.insee.pearljam.domain.campaign.model.Visibility; |
13 | 11 | import fr.insee.pearljam.domain.campaign.model.communication.CommunicationMedium; |
14 | 12 | import fr.insee.pearljam.domain.campaign.model.communication.CommunicationTemplate; |
15 | 13 | import fr.insee.pearljam.domain.campaign.port.serverside.CommunicationTemplateRepository; |
16 | | -import fr.insee.pearljam.domain.campaign.port.userside.DateService; |
17 | 14 | import fr.insee.pearljam.domain.campaign.port.userside.VisibilityService; |
18 | 15 | import fr.insee.pearljam.domain.exception.CommunicationTemplateNotFoundException; |
19 | 16 | import fr.insee.pearljam.domain.exception.VisibilityNotFoundException; |
20 | 17 | import fr.insee.pearljam.domain.surveyunit.model.Comment; |
21 | 18 | import fr.insee.pearljam.domain.surveyunit.model.ContactOutcome; |
22 | 19 | import fr.insee.pearljam.domain.surveyunit.model.Identification; |
23 | 20 | import fr.insee.pearljam.domain.surveyunit.model.communication.CommunicationRequest; |
| 21 | +import fr.insee.pearljam.api.service.SurveyUnitUpdateService; |
24 | 22 | import fr.insee.pearljam.domain.surveyunit.port.serverside.CommunicationRequestRepository; |
| 23 | +import fr.insee.pearljam.infrastructure.surveyunit.entity.identification.IdentificationDB; |
| 24 | +import java.util.Optional; |
25 | 25 | import lombok.RequiredArgsConstructor; |
26 | 26 | import lombok.extern.slf4j.Slf4j; |
27 | 27 | import org.springframework.stereotype.Service; |
28 | 28 | import org.springframework.transaction.annotation.Transactional; |
29 | | - |
30 | 29 | import java.util.List; |
31 | 30 | import java.util.Set; |
32 | 31 | import java.util.stream.Collectors; |
|
36 | 35 | @Slf4j |
37 | 36 | public class SurveyUnitUpdateServiceImpl implements SurveyUnitUpdateService { |
38 | 37 |
|
39 | | - private final CommunicationRequestRepository communicationRequestRepository; |
40 | | - private final CommunicationTemplateRepository communicationTemplateRepository; |
41 | | - private final VisibilityService visibilityService; |
42 | | - private final DateService dateService; |
| 38 | + private final CommunicationRequestRepository communicationRequestRepository; |
| 39 | + private final CommunicationTemplateRepository communicationTemplateRepository; |
| 40 | + private final VisibilityService visibilityService; |
| 41 | + private final DateService dateService; |
| 42 | + |
| 43 | + @Transactional |
| 44 | + @Override |
| 45 | + public void updateSurveyUnitInfos(SurveyUnit surveyUnit, SurveyUnitUpdateDto surveyUnitUpdateDto) { |
| 46 | + if(surveyUnitUpdateDto.comments() != null) { |
| 47 | + Set<Comment> commentsToUpdate = surveyUnitUpdateDto.comments().stream() |
| 48 | + .map(commentDto -> CommentDto.toModel(surveyUnit.getId(), commentDto)) |
| 49 | + .collect(Collectors.toSet()); |
| 50 | + |
| 51 | + surveyUnit.updateComments(commentsToUpdate); |
| 52 | + } |
| 53 | + if(surveyUnitUpdateDto.communicationRequests() != null) { |
| 54 | + Long timestamp = dateService.getCurrentTimestamp(); |
| 55 | + List<CommunicationRequest> communicationRequestsToCreate = |
| 56 | + surveyUnitUpdateDto.communicationRequests() |
| 57 | + .stream() |
| 58 | + .map(communicationRequestCreateDto -> getNewCommunicationRequest(communicationRequestCreateDto, surveyUnit, timestamp)) |
| 59 | + .toList(); |
| 60 | + communicationRequestRepository.addCommunicationRequests(surveyUnit, communicationRequestsToCreate); |
| 61 | + } |
| 62 | + IdentificationConfiguration identificationConfiguration = surveyUnit.getCampaign().getIdentificationConfiguration(); |
43 | 63 |
|
44 | | - @Transactional |
45 | | - @Override |
46 | | - public void updateSurveyUnitInfos(SurveyUnit surveyUnit, SurveyUnitUpdateDto surveyUnitUpdateDto) { |
47 | | - if (surveyUnitUpdateDto.comments() != null) { |
48 | | - Set<Comment> commentsToUpdate = surveyUnitUpdateDto.comments().stream() |
49 | | - .map(commentDto -> CommentDto.toModel(surveyUnit.getId(), commentDto)) |
50 | | - .collect(Collectors.toSet()); |
| 64 | + Identification identification = Optional.ofNullable(surveyUnitUpdateDto.identification()) |
| 65 | + .map(idDto -> IdentificationDto.toModel(idDto, identificationConfiguration)) |
| 66 | + .orElseGet(() -> IdentificationDB.toModel(surveyUnit.getIdentification())); |
51 | 67 |
|
52 | | - surveyUnit.updateComments(commentsToUpdate); |
53 | | - } |
54 | | - if (surveyUnitUpdateDto.communicationRequests() != null) { |
55 | | - Long timestamp = dateService.getCurrentTimestamp(); |
56 | | - List<CommunicationRequest> communicationRequestsToCreate = |
57 | | - surveyUnitUpdateDto.communicationRequests() |
58 | | - .stream() |
59 | | - .map(communicationRequestCreateDto -> getNewCommunicationRequest(communicationRequestCreateDto, surveyUnit, timestamp)) |
60 | | - .toList(); |
61 | | - communicationRequestRepository.addCommunicationRequests(surveyUnit, communicationRequestsToCreate); |
62 | | - } |
63 | | - IdentificationConfiguration identificationConfiguration = |
64 | | - surveyUnit.getCampaign().getIdentificationConfiguration(); |
65 | | - Identification identification = IdentificationDto.toModel(surveyUnitUpdateDto.identification(), |
66 | | - identificationConfiguration); |
67 | | - surveyUnit.updateIdentification(identification); |
| 68 | + surveyUnit.updateIdentification(identification); |
68 | 69 |
|
69 | | - //update ContactOutcome |
70 | | - ContactOutcome contactOutcome = ContactOutcomeDto.toModel(surveyUnit.getId(), |
71 | | - surveyUnitUpdateDto.contactOutcome()); |
72 | | - contactOutcome = convertDeprecatedContactOutcomeValue(contactOutcome); |
73 | | - surveyUnit.updateContactOutcome(contactOutcome); |
74 | | - } |
| 70 | + //update ContactOutcome |
| 71 | + ContactOutcome contactOutcome = ContactOutcomeDto.toModel(surveyUnit.getId(), |
| 72 | + surveyUnitUpdateDto.contactOutcome()); |
| 73 | + contactOutcome = convertDeprecatedContactOutcomeValue(contactOutcome); |
| 74 | + surveyUnit.updateContactOutcome(contactOutcome); |
| 75 | + } |
75 | 76 |
|
76 | | - // when DCD and DUU values are not used anymore => to be removed |
77 | | - private ContactOutcome convertDeprecatedContactOutcomeValue(ContactOutcome contactOutcome) { |
78 | | - if (contactOutcome == null) { |
79 | | - return null; |
80 | | - } |
81 | | - return switch (contactOutcome.type()) { |
82 | | - case DCD -> new ContactOutcome(contactOutcome.id(), contactOutcome.date(), ContactOutcomeType.NOA, |
83 | | - contactOutcome.totalNumberOfContactAttempts(), contactOutcome.surveyUnitId()); |
84 | | - case DUU -> new ContactOutcome(contactOutcome.id(), contactOutcome.date(), ContactOutcomeType.DUK, |
85 | | - contactOutcome.totalNumberOfContactAttempts(), contactOutcome.surveyUnitId()); |
86 | | - case INA, REF, IMP, UCD, UTR, ALA, DUK, NUH, NOA -> contactOutcome; |
87 | | - }; |
88 | | - } |
| 77 | + // when DCD and DUU values are not used anymore => to be removed |
| 78 | + private ContactOutcome convertDeprecatedContactOutcomeValue(ContactOutcome contactOutcome) { |
| 79 | + if (contactOutcome == null) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return switch (contactOutcome.type()) { |
| 83 | + case DCD -> new ContactOutcome(contactOutcome.id(), contactOutcome.date(), ContactOutcomeType.NOA, |
| 84 | + contactOutcome.totalNumberOfContactAttempts(), contactOutcome.surveyUnitId()); |
| 85 | + case DUU -> new ContactOutcome(contactOutcome.id(), contactOutcome.date(), ContactOutcomeType.DUK, |
| 86 | + contactOutcome.totalNumberOfContactAttempts(), contactOutcome.surveyUnitId()); |
| 87 | + case INA, REF, IMP, UCD, UTR, ALA, DUK, NUH, NOA -> contactOutcome; |
| 88 | + }; |
| 89 | + } |
89 | 90 |
|
90 | | - /** |
91 | | - * This method checks the validity of a communication request |
92 | | - * |
93 | | - * @param communicationRequestToCreate communication request to create |
94 | | - * @param surveyUnit the survey unit to update |
95 | | - * @return a new communication request |
96 | | - */ |
97 | | - private CommunicationRequest getNewCommunicationRequest(CommunicationRequestCreateDto communicationRequestToCreate |
98 | | - , SurveyUnit surveyUnit, Long readyTimestamp) { |
99 | | - String campaignId = surveyUnit.getCampaign().getId(); |
100 | | - CommunicationTemplate communicationTemplate = communicationTemplateRepository |
101 | | - .findCommunicationTemplate(communicationRequestToCreate.communicationTemplateId(), campaignId) |
102 | | - .orElseThrow(CommunicationTemplateNotFoundException::new); |
| 91 | + /** |
| 92 | + * This method checks the validity of a communication request |
| 93 | + * @param communicationRequestToCreate communication request to create |
| 94 | + * @param surveyUnit the survey unit to update |
| 95 | + * @return a new communication request |
| 96 | + */ |
| 97 | + private CommunicationRequest getNewCommunicationRequest(CommunicationRequestCreateDto communicationRequestToCreate, SurveyUnit surveyUnit, Long readyTimestamp) { |
| 98 | + String campaignId = surveyUnit.getCampaign().getId(); |
| 99 | + CommunicationTemplate communicationTemplate = communicationTemplateRepository |
| 100 | + .findCommunicationTemplate(communicationRequestToCreate.communicationTemplateId(), campaignId) |
| 101 | + .orElseThrow(CommunicationTemplateNotFoundException::new); |
103 | 102 |
|
104 | | - if (!communicationTemplate.medium().equals(CommunicationMedium.LETTER)) { |
105 | | - return CommunicationRequest.create( |
106 | | - communicationRequestToCreate.communicationTemplateId(), |
107 | | - communicationRequestToCreate.creationTimestamp(), |
108 | | - readyTimestamp, |
109 | | - communicationRequestToCreate.reason()); |
110 | | - } |
| 103 | + if(!communicationTemplate.medium().equals(CommunicationMedium.LETTER)) { |
| 104 | + return CommunicationRequest.create( |
| 105 | + communicationRequestToCreate.communicationTemplateId(), |
| 106 | + communicationRequestToCreate.creationTimestamp(), |
| 107 | + readyTimestamp, |
| 108 | + communicationRequestToCreate.reason()); |
| 109 | + } |
111 | 110 |
|
112 | | - Visibility visibility = visibilityService |
113 | | - .findVisibility(campaignId, surveyUnit.getOrganizationUnit().getId()) |
114 | | - .orElseThrow(VisibilityNotFoundException::new); |
| 111 | + Visibility visibility = visibilityService |
| 112 | + .findVisibility(campaignId, surveyUnit.getOrganizationUnit().getId()) |
| 113 | + .orElseThrow(VisibilityNotFoundException::new); |
115 | 114 |
|
116 | 115 |
|
117 | | - if (visibility.useLetterCommunication() != null && visibility.useLetterCommunication()) { |
118 | | - return CommunicationRequest.create( |
119 | | - communicationRequestToCreate.communicationTemplateId(), |
120 | | - communicationRequestToCreate.creationTimestamp(), |
121 | | - readyTimestamp, |
122 | | - communicationRequestToCreate.reason()); |
123 | | - } |
| 116 | + if(visibility.useLetterCommunication() != null && visibility.useLetterCommunication()) { |
| 117 | + return CommunicationRequest.create( |
| 118 | + communicationRequestToCreate.communicationTemplateId(), |
| 119 | + communicationRequestToCreate.creationTimestamp(), |
| 120 | + readyTimestamp, |
| 121 | + communicationRequestToCreate.reason()); |
| 122 | + } |
124 | 123 |
|
125 | | - // if the communication request is a letter communication request, but the visibility doesn't admit it, |
126 | | - // create a cancelled communication request |
127 | | - return CommunicationRequest.createCancelled( |
128 | | - communicationRequestToCreate.communicationTemplateId(), |
129 | | - communicationRequestToCreate.creationTimestamp(), |
130 | | - dateService.getCurrentTimestamp(), |
131 | | - communicationRequestToCreate.reason()); |
132 | | - } |
| 124 | + // if the communication request is a letter communication request, but the visibility doesn't admit it, |
| 125 | + // create a cancelled communication request |
| 126 | + return CommunicationRequest.createCancelled( |
| 127 | + communicationRequestToCreate.communicationTemplateId(), |
| 128 | + communicationRequestToCreate.creationTimestamp(), |
| 129 | + dateService.getCurrentTimestamp(), |
| 130 | + communicationRequestToCreate.reason()); |
| 131 | + } |
133 | 132 | } |
0 commit comments