Skip to content

Commit

Permalink
983 reschedule action Rules (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks91 authored Dec 12, 2023
1 parent dfde610 commit 803aceb
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -108,6 +109,7 @@ public List<ActionRuleDto> findActionRulesByCollex(
actionRuleDTO.setPackCode(actionRule.getEmailTemplate().getPackCode());
}

actionRuleDTO.setActionRuleId(actionRule.getId());
actionRuleDTO.setType(actionRule.getType());
actionRuleDTO.setCollectionExerciseId(actionRule.getCollectionExercise().getId());
actionRuleDTO.setPhoneNumberColumn(actionRule.getPhoneNumberColumn());
Expand All @@ -129,18 +131,7 @@ public ResponseEntity<UUID> insertActionRules(
@Value("#{request.getAttribute('userEmail')}") String createdBy) {

CollectionExercise collectionExercise =
collectionExerciseRepository
.findById(actionRuleDTO.getCollectionExerciseId())
.orElseThrow(
() -> {
log.with("collectionExerciseId", actionRuleDTO.getCollectionExerciseId())
.with("httpStatus", HttpStatus.BAD_REQUEST)
.with("userEmail", createdBy)
.warn("Failed to insert action rule, collection exercise not found");
return new ResponseStatusException(
HttpStatus.BAD_REQUEST, "Collection exercise not found");
});

getCollectionExercise(actionRuleDTO.getCollectionExerciseId(), createdBy);
UserGroupAuthorisedActivityType userActivity;

ExportFileTemplate exportFileTemplate = null;
Expand Down Expand Up @@ -247,4 +238,68 @@ public ResponseEntity<UUID> insertActionRules(

return new ResponseEntity<>(actionRule.getId(), HttpStatus.CREATED);
}

/*
* Updates triggerDateTime
*/
@PutMapping
@Transactional
public ResponseEntity<UUID> updateActionRules(
@RequestBody() ActionRuleDto actionRuleDTO,
@Value("#{request.getAttribute('userEmail')}") String createdBy) {

CollectionExercise collectionExercise =
getCollectionExercise(actionRuleDTO.getCollectionExerciseId(), createdBy);

UserGroupAuthorisedActivityType userActivity =
switch (actionRuleDTO.getType()) {
case EXPORT_FILE -> CREATE_EXPORT_FILE_ACTION_RULE;
case OUTBOUND_TELEPHONE -> CREATE_OUTBOUND_PHONE_ACTION_RULE;
case FACE_TO_FACE -> CREATE_FACE_TO_FACE_ACTION_RULE;
case DEACTIVATE_UAC -> CREATE_DEACTIVATE_UAC_ACTION_RULE;
case SMS -> CREATE_SMS_ACTION_RULE;
case EMAIL -> CREATE_EMAIL_ACTION_RULE;
case EQ_FLUSH -> CREATE_EQ_FLUSH_ACTION_RULE;
default -> throw new IllegalStateException(
"Unexpected value: " + actionRuleDTO.getType());
};

userIdentity.checkUserPermission(createdBy, collectionExercise.getSurvey(), userActivity);

ActionRule actionRule =
collectionExercise.getActionRules().stream()
.filter(action -> action.getId().equals(actionRuleDTO.getActionRuleId()))
.findAny()
.orElseThrow(
() -> {
log.with("actionRuleId", actionRuleDTO.getActionRuleId())
.with("collectionExerciseId", actionRuleDTO.getCollectionExerciseId())
.with("httpStatus", HttpStatus.NOT_FOUND)
.with("userEmail", createdBy)
.warn("Failed to update action rule, action rule not found");
return new ResponseStatusException(
HttpStatus.NOT_FOUND, "Action Rule Id not found");
});

actionRule.setTriggerDateTime(actionRuleDTO.getTriggerDateTime());

actionRuleRepository.saveAndFlush(actionRule);

return new ResponseEntity<>(actionRule.getId(), HttpStatus.OK);
}

private CollectionExercise getCollectionExercise(UUID uuid, String createdBy)
throws ResponseStatusException {
return collectionExerciseRepository
.findById(uuid)
.orElseThrow(
() -> {
log.with("collectionExerciseId", uuid)
.with("httpStatus", HttpStatus.NOT_FOUND)
.with("userEmail", createdBy)
.warn("Failed to edit action rules, collection exercise not found");
return new ResponseStatusException(
HttpStatus.NOT_FOUND, "Collection exercise not found");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@Data
public class ActionRuleDto {

private UUID actionRuleId;

private UUID collectionExerciseId;

private String packCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ void testActionRuleEndpoints() {
actionRuleDto.setTriggerDateTime(OffsetDateTime.now());
return actionRuleDto;
});

integrationTestHelper.testPut(
port,
UserGroupAuthorisedActivityType.CREATE_EMAIL_ACTION_RULE,
(bundle) -> "actionRules",
(bundle) -> {
ActionRuleDto actionRuleDto = new ActionRuleDto();
actionRuleDto.setType(ActionRuleType.EMAIL);
actionRuleDto.setActionRuleId(bundle.getActionRuleId());
actionRuleDto.setCollectionExerciseId(bundle.getCollexId());
actionRuleDto.setTriggerDateTime(OffsetDateTime.now());
return actionRuleDto;
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class BundleOfUsefulTestStuff {
private UUID surveyId;
private UUID collexId;
private UUID caseId;
private UUID actionRuleId;
private String qid;
private String exportFileTemplatePackCode;
private String smsTemplatePackCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import static org.assertj.core.api.Assertions.fail;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import uk.gov.ons.ssdc.common.model.entity.ActionRule;
import uk.gov.ons.ssdc.common.model.entity.ActionRuleType;
import uk.gov.ons.ssdc.common.model.entity.Case;
import uk.gov.ons.ssdc.common.model.entity.CollectionExercise;
import uk.gov.ons.ssdc.common.model.entity.CollectionInstrumentSelectionRule;
Expand All @@ -28,6 +33,7 @@
import uk.gov.ons.ssdc.common.model.entity.UserGroupPermission;
import uk.gov.ons.ssdc.common.validation.ColumnValidator;
import uk.gov.ons.ssdc.common.validation.Rule;
import uk.gov.ons.ssdc.supporttool.model.repository.ActionRuleRepository;
import uk.gov.ons.ssdc.supporttool.model.repository.CaseRepository;
import uk.gov.ons.ssdc.supporttool.model.repository.CollectionExerciseRepository;
import uk.gov.ons.ssdc.supporttool.model.repository.EmailTemplateRepository;
Expand All @@ -48,6 +54,9 @@ public class IntegrationTestHelper {

private final SurveyRepository surveyRepository;
private final CollectionExerciseRepository collectionExerciseRepository;

private final ActionRuleRepository actionRuleRepository;

private final ExportFileTemplateRepository exportFileTemplateRepository;
private final SmsTemplateRepository smsTemplateRepository;
private final EmailTemplateRepository emailTemplateRepository;
Expand All @@ -66,6 +75,7 @@ public class IntegrationTestHelper {
public IntegrationTestHelper(
SurveyRepository surveyRepository,
CollectionExerciseRepository collectionExerciseRepository,
ActionRuleRepository actionRuleRepository,
ExportFileTemplateRepository exportFileTemplateRepository,
SmsTemplateRepository smsTemplateRepository,
EmailTemplateRepository emailTemplateRepository,
Expand All @@ -78,6 +88,7 @@ public IntegrationTestHelper(
UserGroupPermissionRepository userGroupPermissionRepository) {
this.surveyRepository = surveyRepository;
this.collectionExerciseRepository = collectionExerciseRepository;
this.actionRuleRepository = actionRuleRepository;
this.exportFileTemplateRepository = exportFileTemplateRepository;
this.smsTemplateRepository = smsTemplateRepository;
this.emailTemplateRepository = emailTemplateRepository;
Expand Down Expand Up @@ -154,6 +165,31 @@ public void testPost(
}
}

public void testPut(
int port,
UserGroupAuthorisedActivityType activity,
BundleUrlGetter bundleUrlGetter,
BundlePostObjectGetter bundlePostObjectGetter) {
setUpTestUserPermission(activity);
BundleOfUsefulTestStuff bundle = getTestBundle();

String url = String.format("http://localhost:%d/api/%s", port, bundleUrlGetter.getUrl(bundle));
Object objectToPost = bundlePostObjectGetter.getObject(bundle);

restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(objectToPost), String.class);

deleteAllPermissions();
restoreDummyUserAndOtherGubbins(bundle); // Restore the user etc so that user tests still work
try {
restTemplate.exchange(url, HttpMethod.PUT, new HttpEntity<>(objectToPost), String.class);
fail("PUT API call was not forbidden, but should have been");
} catch (HttpClientErrorException expectedException) {
assertThat(expectedException.getStatusCode())
.as("PUT is FORBIDDEN")
.isEqualTo(HttpStatus.FORBIDDEN);
}
}

public void testDelete(
int port, UserGroupAuthorisedActivityType activity, BundleUrlGetter bundleUrlGetter) {
setUpTestUserPermission(activity);
Expand Down Expand Up @@ -203,6 +239,7 @@ private BundleOfUsefulTestStuff getTestBundle() {
new CollectionInstrumentSelectionRule[] {
new CollectionInstrumentSelectionRule(0, null, "test instrument", null)
});

collectionExercise = collectionExerciseRepository.saveAndFlush(collectionExercise);

Case caze = new Case();
Expand Down Expand Up @@ -247,6 +284,22 @@ private BundleOfUsefulTestStuff getTestBundle() {
UserGroupAdmin userGroupAdmin = setupDummyGroupAdmin(UUID.randomUUID(), user, group);
UserGroupPermission userGroupPermission = setupDummyGroupPermission(UUID.randomUUID(), group);

ActionRule actionRule = new ActionRule();
actionRule.setId(UUID.randomUUID());
actionRule.setCollectionExercise(collectionExercise);
actionRule.setType(ActionRuleType.EMAIL);

actionRule.setClassifiers("sample ->> 'ORG_SIZE' = 'HUGE'");

actionRule.setTriggerDateTime(OffsetDateTime.now());
actionRule.setCreatedBy("TEST_USER");
actionRule.setEmailTemplate(emailTemplate);
actionRule.setEmailColumn("emailAddress");

actionRuleRepository.saveAndFlush(actionRule);

collectionExercise.setActionRules(List.of(actionRule));

BundleOfUsefulTestStuff bundle = new BundleOfUsefulTestStuff();
bundle.setSurveyId(survey.getId());
bundle.setCollexId(collectionExercise.getId());
Expand All @@ -261,6 +314,7 @@ private BundleOfUsefulTestStuff getTestBundle() {
bundle.setGroupAdminId(userGroupAdmin.getId());
bundle.setGroupPermissionId(userGroupPermission.getId());
bundle.setSecondGroupId(secondGroup.getId());
bundle.setActionRuleId(collectionExercise.getActionRules().get(0).getId());

return bundle;
}
Expand Down
Loading

0 comments on commit 803aceb

Please sign in to comment.