From 5a3fc441dca4a06cc1f61bacfdbabd7649cbcd7c Mon Sep 17 00:00:00 2001 From: Rob McCormack <56261775+robertmccormackbconline@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:41:08 +0000 Subject: [PATCH] DST-18026 / DST-18027 (#877) * DST-18026 / DST-18027 : Add/Update user screen - probation.staff.updated notification * DST-18026 : Test for probation.staff.updated domain event --- .../notification/HmppsDomainEventType.java | 3 +- .../service/impl/UserEntityServiceImpl.java | 15 +++++++++- src/main/resources/data.sql | 1 + .../controller/UserControllerUpdateTest.java | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/uk/co/bconline/ndelius/model/notification/HmppsDomainEventType.java b/src/main/java/uk/co/bconline/ndelius/model/notification/HmppsDomainEventType.java index a4a7a828..17ab538a 100644 --- a/src/main/java/uk/co/bconline/ndelius/model/notification/HmppsDomainEventType.java +++ b/src/main/java/uk/co/bconline/ndelius/model/notification/HmppsDomainEventType.java @@ -7,7 +7,8 @@ @Getter public enum HmppsDomainEventType { - UMT_USERNAME_CHANGED("probation-user.username.changed", "The username for a probation user has been changed"); + UMT_USERNAME_CHANGED("probation-user.username.changed", "The username for a probation user has been changed"), + UMT_STAFF_UPDATED("probation.staff.updated","A staff members details have been updated"); private final String eventType; private final String eventDescription; diff --git a/src/main/java/uk/co/bconline/ndelius/service/impl/UserEntityServiceImpl.java b/src/main/java/uk/co/bconline/ndelius/service/impl/UserEntityServiceImpl.java index c4e5d97d..72601d79 100644 --- a/src/main/java/uk/co/bconline/ndelius/service/impl/UserEntityServiceImpl.java +++ b/src/main/java/uk/co/bconline/ndelius/service/impl/UserEntityServiceImpl.java @@ -15,12 +15,14 @@ import uk.co.bconline.ndelius.model.entity.StaffEntity; import uk.co.bconline.ndelius.model.entity.UserEntity; import uk.co.bconline.ndelius.model.entity.export.UserExportEntity; +import uk.co.bconline.ndelius.model.notification.HmppsDomainEventType; import uk.co.bconline.ndelius.repository.db.ChangeNoteRepository; import uk.co.bconline.ndelius.repository.db.ProbationAreaUserRepository; import uk.co.bconline.ndelius.repository.db.SearchResultRepository; import uk.co.bconline.ndelius.repository.db.StaffRepository; import uk.co.bconline.ndelius.repository.db.StaffTeamRepository; import uk.co.bconline.ndelius.repository.db.UserEntityRepository; +import uk.co.bconline.ndelius.service.DomainEventService; import uk.co.bconline.ndelius.service.UserEntityService; import uk.co.bconline.ndelius.transformer.SearchResultTransformer; import uk.co.bconline.ndelius.util.SearchUtils; @@ -28,6 +30,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; @@ -59,6 +62,7 @@ public class UserEntityServiceImpl implements UserEntityService { private final StaffTeamRepository staffTeamRepository; private final ChangeNoteRepository changeNoteRepository; private final SearchResultTransformer searchResultTransformer; + private final DomainEventService domainEventService; @Autowired public UserEntityServiceImpl( @@ -68,7 +72,8 @@ public UserEntityServiceImpl( ProbationAreaUserRepository probationAreaUserRepository, StaffTeamRepository staffTeamRepository, ChangeNoteRepository changeNoteRepository, - SearchResultTransformer searchResultTransformer) { + SearchResultTransformer searchResultTransformer, + DomainEventService domainEventService) { this.repository = repository; this.staffRepository = staffRepository; this.searchResultRepository = searchResultRepository; @@ -76,6 +81,7 @@ public UserEntityServiceImpl( this.staffTeamRepository = staffTeamRepository; this.changeNoteRepository = changeNoteRepository; this.searchResultTransformer = searchResultTransformer; + this.domainEventService = domainEventService; } @Override @@ -199,6 +205,13 @@ public UserEntity save(UserEntity user) { log.debug("Updating user history"); changeNoteRepository.saveAll(user.getHistory()); + // Send Domain event if user has staff code + if (user.getStaff() != null && user.getStaff().getCode() != null && !user.getStaff().getCode().isEmpty()) + { + val additionalInformation = Map.of("staffCode", user.getStaff().getCode()); + domainEventService.insertDomainEvent(HmppsDomainEventType.UMT_STAFF_UPDATED, additionalInformation); + } + log.debug("Finished saving user to database in {}ms", MILLIS.between(t, LocalDateTime.now())); return savedUser; } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 812d21ad..3842446e 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -53,6 +53,7 @@ INSERT INTO R_STANDARD_REFERENCE_LIST (STANDARD_REFERENCE_LIST_ID, CODE_VALUE, C INSERT INTO R_STANDARD_REFERENCE_LIST (STANDARD_REFERENCE_LIST_ID, CODE_VALUE, CODE_DESCRIPTION, SELECTABLE, REFERENCE_DATA_MASTER_ID) VALUES (STANDARD_REFERENCE_LIST_ID_SEQ.NEXTVAL, 'GRADE3', 'Grade 3', 'N', (SELECT REFERENCE_DATA_MASTER_ID FROM R_REFERENCE_DATA_MASTER WHERE CODE_SET_NAME = 'OFFICER GRADE')); INSERT INTO R_REFERENCE_DATA_MASTER (REFERENCE_DATA_MASTER_ID, CODE_SET_NAME) VALUES (REFERENCE_DATA_MASTER_ID_SEQ.NEXTVAL, 'DOMAIN EVENT TYPE'); INSERT INTO R_STANDARD_REFERENCE_LIST (STANDARD_REFERENCE_LIST_ID, CODE_VALUE, CODE_DESCRIPTION, SELECTABLE, REFERENCE_DATA_MASTER_ID) VALUES (STANDARD_REFERENCE_LIST_ID_SEQ.NEXTVAL, 'probation-user.username.changed', 'probation-user.username.changed', 'Y', (SELECT REFERENCE_DATA_MASTER_ID FROM R_REFERENCE_DATA_MASTER WHERE CODE_SET_NAME = 'DOMAIN EVENT TYPE')); +INSERT INTO R_STANDARD_REFERENCE_LIST (STANDARD_REFERENCE_LIST_ID, CODE_VALUE, CODE_DESCRIPTION, SELECTABLE, REFERENCE_DATA_MASTER_ID) VALUES (STANDARD_REFERENCE_LIST_ID_SEQ.NEXTVAL, 'probation.staff.updated', 'probation.staff.updated', 'Y', (SELECT REFERENCE_DATA_MASTER_ID FROM R_REFERENCE_DATA_MASTER WHERE CODE_SET_NAME = 'DOMAIN EVENT TYPE')); -- Users/Staff INSERT INTO STAFF (STAFF_ID, ROW_VERSION, FORENAME, FORENAME2, SURNAME, OFFICER_CODE, STAFF_GRADE_ID, START_DATE, END_DATE) VALUES (STAFF_ID_SEQ.NEXTVAL, 0, 'dummy', NULL, 'staff', 'N01A000', (SELECT STANDARD_REFERENCE_LIST_ID FROM R_STANDARD_REFERENCE_LIST WHERE CODE_VALUE = 'GRADE1'), CURRENT_TIMESTAMP-10, NULL); diff --git a/src/test/java/uk/co/bconline/ndelius/controller/UserControllerUpdateTest.java b/src/test/java/uk/co/bconline/ndelius/controller/UserControllerUpdateTest.java index 4165fb4c..258aca63 100644 --- a/src/test/java/uk/co/bconline/ndelius/controller/UserControllerUpdateTest.java +++ b/src/test/java/uk/co/bconline/ndelius/controller/UserControllerUpdateTest.java @@ -594,4 +594,33 @@ public void previousStaffWithAFutureStartDateIsEndDatedCorrectly() throws Except assertEquals(LocalDate.now().minusDays(1), previousStaff.getEndDate()); assertEquals(LocalDate.now().minusDays(1), previousStaff.getStartDate()); } + + @Test + public void staffCodeAddAndUpdateSendsDomainEventCorrectly() throws Exception + { + int preDomainEventCount = domainEventRepository.findAll().size(); + // Given user with no staff code, no domain events to be sent + User noCodeUser = createUser(mvc, + aValidUser().toBuilder() + .username(nextTestUsername()) + .startDate(LocalDate.now()) + .build()); + assertEquals(preDomainEventCount, domainEventRepository.findAll().size()); + updateUser(mvc, noCodeUser.toBuilder().surname("Update").build()).andExpect(status().isNoContent()); + assertEquals(preDomainEventCount, domainEventRepository.findAll().size()); + + // Given a user with staff code + User codeUser = createUser(mvc, + aValidUser().toBuilder() + .username(nextTestUsername()) + .startDate(LocalDate.now()) + .staffCode("N01A201") + .staffGrade(ReferenceData.builder().code("GRADE 1").description("Grade 1").build()) + .build()); + + assertEquals(preDomainEventCount + 1, domainEventRepository.findAll().size()); + // Updating the user should also update the domainEvent + updateUser(mvc, codeUser.toBuilder().surname("Update").build()).andExpect(status().isNoContent()); + assertEquals(preDomainEventCount + 2, domainEventRepository.findAll().size()); + } }