Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIDM-8901 add caseworkers #377

Merged
merged 8 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public Exception decode(String methodKey, Response response) {
log.error("Failed to process response body.", e);
}

return exception(statusCode, message, responseHeaders, responseBody)
.orElseGet(() -> delegate.decode(methodKey, response));
return exception(statusCode, message, responseHeaders, responseBody).orElseGet(() -> delegate.decode(
methodKey,
response
));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import uk.gov.hmcts.cft.idam.api.v2.common.model.ActivatedUserRequest;
import uk.gov.hmcts.cft.idam.api.v2.common.model.User;
import uk.gov.hmcts.cft.idam.testingsupportapi.repo.model.TestingSession;
import uk.gov.hmcts.cft.idam.testingsupportapi.service.TestingCaseWorkerProfileService;
import uk.gov.hmcts.cft.idam.testingsupportapi.service.TestingSessionService;
import uk.gov.hmcts.cft.idam.testingsupportapi.service.TestingUserProfileService;
import uk.gov.hmcts.cft.rd.model.CaseWorkerProfile;
import uk.gov.hmcts.cft.rd.model.UserProfile;

@RestController
Expand All @@ -25,10 +27,14 @@ public class UserProfileController {

private final TestingUserProfileService testingUserProfileService;

private final TestingCaseWorkerProfileService testingCaseWorkerProfileService;

public UserProfileController(TestingSessionService testingSessionService,
TestingUserProfileService testingUserProfileService) {
TestingUserProfileService testingUserProfileService,
TestingCaseWorkerProfileService testingCaseWorkerProfileService) {
this.testingSessionService = testingSessionService;
this.testingUserProfileService = testingUserProfileService;
this.testingCaseWorkerProfileService = testingCaseWorkerProfileService;
}

@GetMapping("/test/rd/user-profiles/{userId}")
Expand All @@ -39,6 +45,14 @@ public UserProfile getUserProfileById(@AuthenticationPrincipal @Parameter(hidden
return testingUserProfileService.getUserProfileByUserId(userId);
}

@GetMapping("/test/rd/caseworker-profiles/{userId}")
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "bearerAuth")
public CaseWorkerProfile getCaseWorkerProfileById(@AuthenticationPrincipal @Parameter(hidden = true) Jwt principal,
@PathVariable String userId) {
return testingCaseWorkerProfileService.getCaseWorkerProfileById(userId);
}

@PutMapping("/test/cft/users/{userId}")
@ResponseStatus(HttpStatus.OK)
@SecurityRequirement(name = "bearerAuth")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package uk.gov.hmcts.cft.idam.testingsupportapi.properties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "cft.categories")
public class CategoryProperties {

Map<String, List<String>> rolePatterns;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class CleanupReceiver {
public static final String CLEANUP_SERVICE = "cleanup-service";

public static final String CLEANUP_PROFILE = "cleanup-profile";

public static final String CLEANUP_CASEWORKER = "cleanup-caseworker";
private final AdminService adminService;

public CleanupReceiver(AdminService adminService) {
Expand Down Expand Up @@ -63,4 +65,11 @@ public void receiveUserProfile(CleanupEntity entity) {
adminService.cleanupUserProfile(entity);
}

@JmsListener(destination = CLEANUP_CASEWORKER)
public void receiveCaseWorkerProfile(CleanupEntity entity) {
Span.current()
.setAttribute(TraceAttribute.USER_ID, entity.getEntityId());
adminService.cleanupCaseWorkerProfile(entity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.time.ZonedDateTime;
import java.util.List;

import static uk.gov.hmcts.cft.idam.testingsupportapi.repo.model.TestingState.REMOVE_DEPENDENCIES;

@Slf4j
@Service
public class AdminService {
Expand All @@ -29,21 +31,25 @@ public class AdminService {
private final TestingServiceProviderService testingServiceProviderService;
private final TestingSessionService testingSessionService;
private final TestingUserProfileService testingUserProfileService;
private final TestingCaseWorkerProfileService testingCaseWorkerProfileService;
@Value("${cleanup.burner.lifespan}")
private Duration burnerLifespan;
@Value("${cleanup.session.lifespan}")
private Duration sessionLifespan;
private Clock clock;

public AdminService(TestingUserService testingUserService, TestingRoleService testingRoleService,
public AdminService(TestingUserService testingUserService,
TestingRoleService testingRoleService,
TestingServiceProviderService testingServiceProviderService,
TestingSessionService testingSessionService,
TestingUserProfileService testingUserProfileService) {
TestingUserProfileService testingUserProfileService,
TestingCaseWorkerProfileService testingCaseWorkerProfileService) {
this.testingUserService = testingUserService;
this.testingRoleService = testingRoleService;
this.testingServiceProviderService = testingServiceProviderService;
this.testingSessionService = testingSessionService;
this.testingUserProfileService = testingUserProfileService;
this.testingCaseWorkerProfileService = testingCaseWorkerProfileService;
this.clock = Clock.system(ZoneOffset.UTC);
}

Expand All @@ -69,8 +75,8 @@ public void triggerExpiryBurnerUsers() {

ZonedDateTime now = ZonedDateTime.now(clock);

List<TestingEntity> burnerEntities =
testingUserService.getExpiredBurnerUserTestingEntities(now.minus(burnerLifespan));
List<TestingEntity> burnerEntities = testingUserService.getExpiredBurnerUserTestingEntities(now.minus(
burnerLifespan));
if (CollectionUtils.isNotEmpty(burnerEntities)) {
Span.current().setAttribute(TraceAttribute.COUNT, String.valueOf(burnerEntities.size()));
for (TestingEntity burnerEntity : burnerEntities) {
Expand All @@ -95,32 +101,33 @@ public void triggerExpirySessions() {
}

protected void triggerActiveSessionExpiry(ZonedDateTime expiryTime) {
List<TestingSession> expiredSessions =
testingSessionService.getExpiredSessionsByState(expiryTime, TestingState.ACTIVE);
List<TestingSession> expiredSessions = testingSessionService.getExpiredSessionsByState(expiryTime,
TestingState.ACTIVE
);
if (CollectionUtils.isNotEmpty(expiredSessions)) {
Span.current().setAttribute(TraceAttribute.COUNT, String.valueOf(expiredSessions.size()));
for (TestingSession expiredSession : expiredSessions) {
List<TestingEntity> sessionUsers = testingUserService.getTestingEntitiesForSession(expiredSession);
List<TestingEntity> sessionProfiles =
testingUserProfileService.getTestingEntitiesForSession(expiredSession);
if (CollectionUtils.isNotEmpty(sessionUsers) || CollectionUtils.isNotEmpty(sessionProfiles)) {
List<TestingEntity> sessionProfiles = testingUserProfileService.getTestingEntitiesForSession(
expiredSession);
List<TestingEntity> sessionCaseworkers = testingCaseWorkerProfileService.getTestingEntitiesForSession(
expiredSession);
if (CollectionUtils.isNotEmpty(sessionUsers) || CollectionUtils.isNotEmpty(sessionProfiles)
|| CollectionUtils.isNotEmpty(sessionCaseworkers)) {

expiredSession.setState(TestingState.REMOVE_DEPENDENCIES);
expiredSession.setState(REMOVE_DEPENDENCIES);
expiredSession.setLastModifiedDate(ZonedDateTime.now(clock));
testingSessionService.updateSession(expiredSession);

for (TestingEntity sessionUser : sessionUsers) {
testingUserService.requestCleanup(sessionUser);
}
for (TestingEntity sessionProfile : sessionProfiles) {
testingUserProfileService.requestCleanup(sessionProfile);
}
log.info(
"Changed session '{}' with key '{}' from {} to {}",
expiredSession.getId(),
expiredSession.getSessionKey(),
TestingState.ACTIVE,
expiredSession.getState()
sessionUsers.forEach(testingUserService::requestCleanup);
sessionProfiles.forEach(testingUserProfileService::requestCleanup);
sessionCaseworkers.forEach(testingCaseWorkerProfileService::requestCleanup);

log.info("Changed session '{}' with key '{}' from {} to {}",
expiredSession.getId(),
expiredSession.getSessionKey(),
TestingState.ACTIVE,
expiredSession.getState()
);

} else {
Expand All @@ -134,23 +141,29 @@ protected void triggerActiveSessionExpiry(ZonedDateTime expiryTime) {
}

protected void triggerRemoveDependencySessionExpiry(ZonedDateTime expiryTime) {
List<TestingSession> expiredSessions =
testingSessionService.getExpiredSessionsByState(expiryTime, TestingState.REMOVE_DEPENDENCIES);
List<TestingSession> expiredSessions = testingSessionService.getExpiredSessionsByState(expiryTime,
REMOVE_DEPENDENCIES
);
if (CollectionUtils.isNotEmpty(expiredSessions)) {
Span.current().setAttribute(TraceAttribute.COUNT, String.valueOf(expiredSessions.size()));
for (TestingSession expiredSession : expiredSessions) {
List<TestingEntity> sessionUsers = testingUserService.getTestingEntitiesForSession(expiredSession);
List<TestingEntity> sessionProfiles =
testingUserProfileService.getTestingEntitiesForSession(expiredSession);
if (CollectionUtils.isEmpty(sessionUsers) && CollectionUtils.isEmpty(sessionProfiles)) {
List<TestingEntity> sessionProfiles = testingUserProfileService.getTestingEntitiesForSession(
expiredSession);
List<TestingEntity> sessionCaseworkers = testingCaseWorkerProfileService.getTestingEntitiesForSession(
expiredSession);
if (CollectionUtils.isEmpty(sessionUsers) && CollectionUtils.isEmpty(sessionProfiles)
&& CollectionUtils.isEmpty(sessionCaseworkers)) {
testingSessionService.requestCleanup(expiredSession);
log.info("Requested cleanup of session {} after dependency cleanup", expiredSession.getId());
} else {
log.info(
"Session {} still has testing entities, {} user(s) and {} user-profile(s)",
"Session {} still has testing entities, {} user(s), {} user-profile(s), {} caseworker-profile"
+ "(s)",
expiredSession.getId(),
CollectionUtils.size(sessionUsers),
CollectionUtils.size(sessionProfiles)
CollectionUtils.size(sessionProfiles),
CollectionUtils.size(sessionCaseworkers)
);
}
}
Expand All @@ -172,10 +185,9 @@ public void cleanupUser(CleanupEntity userEntity) {
Span.current().setAttribute(TraceAttribute.OUTCOME, "not-found");
}
if (testingUserService.deleteTestingEntityById(userEntity.getTestingEntityId())) {
log.info(
"Removed testing entity with id {}, for user {}",
userEntity.getTestingEntityId(),
userEntity.getEntityId()
log.info("Removed testing entity with id {}, for user {}",
userEntity.getTestingEntityId(),
userEntity.getEntityId()
);
}
}
Expand Down Expand Up @@ -210,10 +222,9 @@ public void cleanupRole(CleanupEntity roleEntity) {
Span.current().setAttribute(TraceAttribute.OUTCOME, "not-found");
}
if (testingRoleService.deleteTestingEntityById(roleEntity.getTestingEntityId())) {
log.info(
"Removed testing entity with id {}, for role {}",
roleEntity.getTestingEntityId(),
roleEntity.getEntityId()
log.info("Removed testing entity with id {}, for role {}",
roleEntity.getTestingEntityId(),
roleEntity.getEntityId()
);
}
}
Expand All @@ -225,10 +236,9 @@ public void cleanupService(CleanupEntity serviceEntity) {
Span.current().setAttribute(TraceAttribute.OUTCOME, "not-found");
}
if (testingServiceProviderService.deleteTestingEntityById(serviceEntity.getTestingEntityId())) {
log.info(
"Removed testing entity with id {}, for service {}",
serviceEntity.getTestingEntityId(),
serviceEntity.getEntityId()
log.info("Removed testing entity with id {}, for service {}",
serviceEntity.getTestingEntityId(),
serviceEntity.getEntityId()
);
}
}
Expand All @@ -246,10 +256,29 @@ public void cleanupUserProfile(CleanupEntity profileEntity) {
return;
}
if (testingUserProfileService.deleteTestingEntityById(profileEntity.getTestingEntityId())) {
log.info(
"Removed testing entity with id {}, for user-profile {}",
profileEntity.getTestingEntityId(),
profileEntity.getEntityId()
log.info("Removed testing entity with id {}, for user-profile {}",
profileEntity.getTestingEntityId(),
profileEntity.getEntityId()
);
}
}

public void cleanupCaseWorkerProfile(CleanupEntity profileEntity) {
try {
if (testingCaseWorkerProfileService.delete(profileEntity.getEntityId())) {
Span.current().setAttribute(TraceAttribute.OUTCOME, "deleted");
} else {
Span.current().setAttribute(TraceAttribute.OUTCOME, "not-found");
}
} catch (HttpStatusCodeException hsce) {
Span.current().setAttribute(TraceAttribute.OUTCOME, "detached");
testingCaseWorkerProfileService.detachEntity(profileEntity.getTestingEntityId());
return;
}
if (testingCaseWorkerProfileService.deleteTestingEntityById(profileEntity.getTestingEntityId())) {
log.info("Removed testing entity with id {}, for caseworker-profile {}",
profileEntity.getTestingEntityId(),
profileEntity.getEntityId()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.gov.hmcts.cft.idam.testingsupportapi.service;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.cft.idam.api.v2.common.model.User;
import uk.gov.hmcts.cft.idam.testingsupportapi.repo.TestingEntityRepo;
import uk.gov.hmcts.cft.idam.testingsupportapi.repo.model.TestingEntityType;
import uk.gov.hmcts.cft.rd.api.RefDataCaseWorkerApi;
import uk.gov.hmcts.cft.rd.model.CaseWorkerLocation;
import uk.gov.hmcts.cft.rd.model.CaseWorkerProfile;
import uk.gov.hmcts.cft.rd.model.CaseWorkerRole;
import uk.gov.hmcts.cft.rd.model.CaseWorkerService;
import uk.gov.hmcts.cft.rd.model.CreateCaseWorkerProfileRequest;

import java.util.Collections;
import java.util.List;

@Service
public class TestingCaseWorkerProfileService extends TestingEntityService<CaseWorkerProfile> {

private final RefDataCaseWorkerApi refDataCaseWorkerApi;

public TestingCaseWorkerProfileService(TestingEntityRepo testingEntityRepo,
JmsTemplate jmsTemplate,
RefDataCaseWorkerApi refDataCaseWorkerApi) {
super(testingEntityRepo, jmsTemplate);
this.refDataCaseWorkerApi = refDataCaseWorkerApi;
}

public User createCaseWorkerProfile(String sessionId, User user) {
CreateCaseWorkerProfileRequest createRequest = convertToCreateRequest(user);
refDataCaseWorkerApi.createCaseWorkerProfile(createRequest);

createTestingEntity(sessionId, createRequest);

return user;
}

private CreateCaseWorkerProfileRequest convertToCreateRequest(User user) {
CreateCaseWorkerProfileRequest createRequest = new CreateCaseWorkerProfileRequest();
createRequest.setCaseWorkerId(user.getId());
createRequest.setFirstName(user.getForename());
createRequest.setLastName(user.getSurname());
createRequest.setEmail(user.getEmail());

createRequest.setSkills(Collections.emptyList());
createRequest.setUserType("CTSC");
createRequest.setUserProfileIdamStatus("ACTIVE");
createRequest.setStaffAdmin(user.getRoleNames().contains("staff-admin"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we ever want testing-support-api clients to send us things like roles, user types, regions, locations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yes, but we don't have a "V2" model for that (yet). I think it's fine to leave these as defaults for now since IDAM is the only user. When we want to expand the usage of this we can discuss a newer input model

createRequest.setRegionId("4");
createRequest.setRegion("North East");

CaseWorkerRole role = new CaseWorkerRole();
role.setRoleId("2");
role.setRoleDescription("Legal Caseworker");
role.setPrimary(true);
createRequest.setRoles(List.of(role));

CaseWorkerLocation location = new CaseWorkerLocation();
location.setLocationId("206150");
location.setPrimary(true);
createRequest.setBaseLocations(List.of(location));

CaseWorkerService service = new CaseWorkerService();
service.setServiceCode("BAB2");
createRequest.setServices(List.of(service));

return createRequest;
}

public CaseWorkerProfile getCaseWorkerProfileById(String userId) {
return refDataCaseWorkerApi.findCaseWorkerProfileByUserId(userId);
}

@Override
protected void deleteEntity(String key) {
refDataCaseWorkerApi.deleteCaseWorkerProfileByUserId(key);
}

@Override
protected String getEntityKey(CaseWorkerProfile entity) {
return entity.getCaseWorkerId();
}

@Override
protected TestingEntityType getTestingEntityType() {
return TestingEntityType.PROFILE_CASEWORKER;
}

}
Loading