Skip to content

Commit

Permalink
fix: create ready com request status when creaing com requests
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras committed Oct 1, 2024
1 parent 45017b0 commit 81b8e72
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>fr.insee.pearljam</groupId>
<artifactId>pearljam-back-office</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>
<name>Pearl-Jam-Back-Office</name>
<description>Back-office services for PearlJam</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.insee.pearljam.api.surveyunit.dto.CommunicationRequestCreateDto;
import fr.insee.pearljam.api.surveyunit.dto.IdentificationDto;
import fr.insee.pearljam.api.surveyunit.dto.SurveyUnitUpdateDto;
import fr.insee.pearljam.domain.campaign.port.userside.DateService;
import fr.insee.pearljam.domain.surveyunit.model.Comment;
import fr.insee.pearljam.domain.surveyunit.model.Identification;
import fr.insee.pearljam.domain.surveyunit.model.communication.CommunicationRequest;
Expand All @@ -24,6 +25,7 @@
public class SurveyUnitUpdateServiceImpl implements SurveyUnitUpdateService {

private final CommunicationRequestRepository communicationRequestRepository;
private final DateService dateService;

@Transactional
@Override
Expand All @@ -36,7 +38,8 @@ public void updateSurveyUnitInfos(SurveyUnit surveyUnit, SurveyUnitUpdateDto sur
surveyUnit.updateComments(commentsToUpdate);
}
if(surveyUnitUpdateDto.communicationRequests() != null) {
List<CommunicationRequest> communicationRequestsToCreate = CommunicationRequestCreateDto.toModel(surveyUnitUpdateDto.communicationRequests());
Long timestamp = dateService.getCurrentTimestamp();
List<CommunicationRequest> communicationRequestsToCreate = CommunicationRequestCreateDto.toModel(surveyUnitUpdateDto.communicationRequests(), timestamp);
communicationRequestRepository.addCommunicationRequests(surveyUnit, communicationRequestsToCreate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public record CommunicationRequestCreateDto(
* Converts a list of communication request DTOs into a list of communication request models.
*
* @param requests the list of communication request DTOs
* @param readyTimestamp ready timestamp of the communication requests (when does the communication request are created in the back)
* @return the list of communication request models
*/
public static List<CommunicationRequest> toModel(List<CommunicationRequestCreateDto> requests) {
public static List<CommunicationRequest> toModel(List<CommunicationRequestCreateDto> requests, Long readyTimestamp) {
return requests.stream()
.map(request ->
CommunicationRequest.create(request.communicationTemplateId(), request.creationTimestamp(), request.reason()))
CommunicationRequest.create(request.communicationTemplateId(), request.creationTimestamp(), readyTimestamp, request.reason()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public record CommunicationRequest(
/**
* Create a communication request for messhugah
* @param communicationTemplateId communication configuration id
* @param creationTimestamp creation date of the communication request
* @param creationTimestamp creation date of the communication request (coming from the front)
* @param readyTimestamp ready timestamp of the communication request (when does the communication request is created in the back)
* @param reason reason why the communication request is created
* @return {@link CommunicationRequest} communication request object
*/
public static CommunicationRequest create(Long communicationTemplateId, Long creationTimestamp, CommunicationRequestReason reason) {
public static CommunicationRequest create(Long communicationTemplateId, Long creationTimestamp, Long readyTimestamp, CommunicationRequestReason reason) {
List<CommunicationRequestStatus> status = new ArrayList<>();
status.add(CommunicationRequestStatus.create(creationTimestamp));
status.add(CommunicationRequestStatus.create(creationTimestamp, CommunicationStatusType.INITIATED));
status.add(CommunicationRequestStatus.create(readyTimestamp, CommunicationStatusType.READY));
return new CommunicationRequest(null, communicationTemplateId, reason, CommunicationRequestEmitter.INTERVIEWER, status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ public record CommunicationRequestStatus(
/**
* Create a CommunicationRequestStatus
* @param timestamp status creation date
* @param statusType status type
* @return {@link CommunicationRequestStatus} communication request status object
*/
public static CommunicationRequestStatus create(Long timestamp) {
return new CommunicationRequestStatus(null, timestamp, CommunicationStatusType.INITIATED);
public static CommunicationRequestStatus create(Long timestamp, CommunicationStatusType statusType) {
return new CommunicationRequestStatus(null, timestamp, statusType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import fr.insee.pearljam.api.utils.AuthenticatedUserTestHelper;
import fr.insee.pearljam.api.utils.MockMvcTestUtils;
import fr.insee.pearljam.api.utils.ScriptConstants;
import fr.insee.pearljam.config.DateServiceConfiguration;
import fr.insee.pearljam.config.FixedDateServiceConfiguration;
import fr.insee.pearljam.domain.surveyunit.model.CommentType;
import fr.insee.pearljam.infrastructure.campaign.entity.VisibilityDB;
import fr.insee.pearljam.infrastructure.campaign.jpa.VisibilityJpaRepository;
Expand Down Expand Up @@ -77,7 +77,7 @@
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@RequiredArgsConstructor
@Import(DateServiceConfiguration.class)
@Import(FixedDateServiceConfiguration.class)
class TestAuthKeyCloak {

private final SurveyUnitService surveyUnitService;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fr/insee/pearljam/api/noAuth/TestNoAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import fr.insee.pearljam.api.repository.ClosingCauseRepository;
import fr.insee.pearljam.api.repository.MessageRepository;
import fr.insee.pearljam.api.utils.ScriptConstants;
import fr.insee.pearljam.config.DateServiceConfiguration;
import fr.insee.pearljam.config.FixedDateServiceConfiguration;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -40,7 +40,7 @@
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@RequiredArgsConstructor
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Import(DateServiceConfiguration.class)
@Import(FixedDateServiceConfiguration.class)
class TestNoAuth {

private final MessageRepository messageRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@TestConfiguration
@Profile("test")
public class DateServiceConfiguration {
public class FixedDateServiceConfiguration {

@Bean
@Primary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ void testCreateCommunicationRequest() {
// Given
Long communicationTemplateId = 1L;
Long creationDate = System.currentTimeMillis();
Long readyDate = System.currentTimeMillis();
CommunicationRequestReason reason = CommunicationRequestReason.REFUSAL;

// When
CommunicationRequest communicationRequest = CommunicationRequest.create(communicationTemplateId, creationDate, reason);
CommunicationRequest communicationRequest = CommunicationRequest.create(communicationTemplateId, creationDate, readyDate, reason);

// Then
assertNotNull(communicationRequest);
Expand All @@ -25,7 +26,8 @@ void testCreateCommunicationRequest() {
assertEquals(reason, communicationRequest.reason());
assertEquals(CommunicationRequestEmitter.INTERVIEWER, communicationRequest.emitter());
assertNotNull(communicationRequest.status());
assertEquals(1, communicationRequest.status().size());
assertEquals(2, communicationRequest.status().size());
assertEquals(creationDate, communicationRequest.status().getFirst().date());
assertEquals(readyDate, communicationRequest.status().getLast().date());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import fr.insee.pearljam.api.domain.*;
import fr.insee.pearljam.api.service.impl.SurveyUnitUpdateServiceImpl;
import fr.insee.pearljam.api.surveyunit.dto.*;
import fr.insee.pearljam.domain.campaign.port.userside.DateService;
import fr.insee.pearljam.domain.campaign.service.dummy.FixedDateService;
import fr.insee.pearljam.domain.surveyunit.model.CommentType;
import fr.insee.pearljam.domain.surveyunit.model.communication.*;
import fr.insee.pearljam.domain.surveyunit.model.question.*;
Expand All @@ -28,11 +30,13 @@ class SurveyUnitUpdateServiceImplTest {
private SurveyUnit surveyUnit;
private SurveyUnitUpdateDto surveyUnitDto;
private CommunicationTemplateDB communicationTemplate;
private DateService dateService;

@BeforeEach
void setup() {
dateService = new FixedDateService();
communicationRequestFakeRepository = new CommunicationRequestFakeRepository();
surveyUnitService = new SurveyUnitUpdateServiceImpl(communicationRequestFakeRepository);
surveyUnitService = new SurveyUnitUpdateServiceImpl(communicationRequestFakeRepository, dateService);
surveyUnit = new SurveyUnit("id", true, true, null,
null, null, null, null, null);

Expand Down Expand Up @@ -74,13 +78,19 @@ void testUpdateCommunication01() {
1L,
CommunicationRequestReason.UNREACHABLE,
CommunicationRequestEmitter.INTERVIEWER,
List.of(tuple(null, 12345678910L, CommunicationStatusType.INITIATED))
List.of(
tuple(null, 12345678910L, CommunicationStatusType.INITIATED),
tuple(null, dateService.getCurrentTimestamp(), CommunicationStatusType.READY)
)
),
tuple(null,
2L,
CommunicationRequestReason.REFUSAL,
CommunicationRequestEmitter.INTERVIEWER,
List.of(tuple(null, 1234567891011L, CommunicationStatusType.INITIATED))
List.of(
tuple(null, 1234567891011L, CommunicationStatusType.INITIATED),
tuple(null, dateService.getCurrentTimestamp(), CommunicationStatusType.READY)
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ActiveProfiles("auth")
@ActiveProfiles(profiles = {"auth", "test"})
@AutoConfigureMockMvc
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ActiveProfiles("auth")
@ActiveProfiles(profiles = {"auth", "test"})
@AutoConfigureMockMvc
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@ActiveProfiles("auth")
@ActiveProfiles(profiles = {"auth", "test"})
@AutoConfigureMockMvc
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;

@ActiveProfiles("auth")
@ActiveProfiles(profiles = {"auth", "test"})
@AutoConfigureMockMvc
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import fr.insee.pearljam.api.service.SurveyUnitService;
import fr.insee.pearljam.api.utils.AuthenticatedUserTestHelper;
import fr.insee.pearljam.api.utils.ScriptConstants;
import fr.insee.pearljam.config.FixedDateServiceConfiguration;
import fr.insee.pearljam.domain.campaign.port.userside.DateService;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -24,10 +27,11 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ActiveProfiles("auth")
@ActiveProfiles(profiles = {"auth", "test"})
@AutoConfigureMockMvc
@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(FixedDateServiceConfiguration.class)
@Transactional
class SurveyUnitIT {

Expand All @@ -37,6 +41,9 @@ class SurveyUnitIT {
@Autowired
private SurveyUnitService surveyUnitService;

@Autowired
private DateService dateService;

@Test
void testGetAllSurveyUnits() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/api/survey-units")
Expand Down Expand Up @@ -283,6 +290,7 @@ void testGetSurveyUnitDetail() throws Exception {
@Test
@Sql(value = ScriptConstants.REINIT_SQL_SCRIPT, executionPhase = AFTER_TEST_METHOD)
void testPutSurveyUnitDetail() throws Exception {
long currentTimestamp = dateService.getCurrentTimestamp();
String updateJson = """
{
"id":"20",
Expand Down Expand Up @@ -494,6 +502,11 @@ void testPutSurveyUnitDetail() throws Exception {
{
"date":1721903754305,
"status":"INITIATED"
},
{
"date":""" + currentTimestamp + """
,
"status":"READY"
}
]
},
Expand All @@ -516,6 +529,11 @@ void testPutSurveyUnitDetail() throws Exception {
{
"date":1721903754405,
"status":"INITIATED"
},
{
"date":""" + currentTimestamp + """
,
"status":"READY"
}
]
},
Expand Down

0 comments on commit 81b8e72

Please sign in to comment.