Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* DST-18026 / DST-18027 : Add/Update user screen - probation.staff.updated notification

* DST-18026 : Test for probation.staff.updated domain event
  • Loading branch information
robertmccormackbconline authored Jan 16, 2025
1 parent 5fc15f5 commit 5a3fc44
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
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;

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;
Expand Down Expand Up @@ -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(
Expand All @@ -68,14 +72,16 @@ public UserEntityServiceImpl(
ProbationAreaUserRepository probationAreaUserRepository,
StaffTeamRepository staffTeamRepository,
ChangeNoteRepository changeNoteRepository,
SearchResultTransformer searchResultTransformer) {
SearchResultTransformer searchResultTransformer,
DomainEventService domainEventService) {
this.repository = repository;
this.staffRepository = staffRepository;
this.searchResultRepository = searchResultRepository;
this.probationAreaUserRepository = probationAreaUserRepository;
this.staffTeamRepository = staffTeamRepository;
this.changeNoteRepository = changeNoteRepository;
this.searchResultTransformer = searchResultTransformer;
this.domainEventService = domainEventService;
}

@Override
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 5a3fc44

Please sign in to comment.