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

feat: local office/DBC TSS requests #253

Merged
merged 4 commits into from
Jul 19, 2024
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>
<groupId>uk.nhs.tis</groupId>
<artifactId>sync</artifactId>
<version>1.23.0</version>
<version>1.24.0</version>
<packaging>jar</packaging>
<name>sync</name>
<description>Separate Microservice for synchronisation</description>
Expand Down Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>com.transformuk.hee</groupId>
<artifactId>reference-client</artifactId>
<version>4.10.0</version>
<version>4.16.0</version>
</dependency>
<dependency>
<groupId>com.transformuk.hee</groupId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/uk/nhs/tis/sync/dto/DmsDtoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.transformuk.hee.tis.profile.service.dto.HeeUserDTO;
import com.transformuk.hee.tis.reference.api.dto.DBCDTO;
import com.transformuk.hee.tis.reference.api.dto.GradeDTO;
import com.transformuk.hee.tis.reference.api.dto.LocalOfficeDTO;
import com.transformuk.hee.tis.reference.api.dto.SiteDTO;
import com.transformuk.hee.tis.reference.api.dto.TrustDTO;
import com.transformuk.hee.tis.tcs.api.dto.ContactDetailsDTO;
Expand All @@ -25,6 +26,7 @@
import uk.nhs.tis.sync.mapper.DmsMapper;
import uk.nhs.tis.sync.mapper.GradeMapper;
import uk.nhs.tis.sync.mapper.HeeUserMapper;
import uk.nhs.tis.sync.mapper.LocalOfficeMapper;
import uk.nhs.tis.sync.mapper.PersonMapper;
import uk.nhs.tis.sync.mapper.PlacementSpecialtyMapper;
import uk.nhs.tis.sync.mapper.PlacementSummaryMapper;
Expand Down Expand Up @@ -53,6 +55,7 @@ public enum DmsDtoType {
GMC_DETAILS(GmcDetailsDTO.class, Schemas.TCS, "GmcDetails", null),
GRADE(GradeDTO.class, Schemas.REFERENCE, "Grade", GradeMapper.class),
HEE_USER(HeeUserDTO.class, Schemas.AUTH, "HeeUser", HeeUserMapper.class),
LOCAL_OFFICE(LocalOfficeDTO.class, Schemas.REFERENCE, "LocalOffice", LocalOfficeMapper.class),
PERSON(PersonDTO.class, Schemas.TCS, "Person", PersonMapper.class),
PERSONAL_DETAILS(PersonalDetailsDTO.class, Schemas.TCS, "PersonalDetails", null),
PLACEMENT_DETAILS(PlacementSummaryDTO.class, Schemas.TCS, "Placement",
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/uk/nhs/tis/sync/dto/LocalOfficeDmsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uk.nhs.tis.sync.dto;

import java.util.UUID;
import lombok.Data;

/**
* A DTO for transferring LocalOffice data to the DMS.
*/
@Data
public class LocalOfficeDmsDto {
private String id;
private UUID uuid;
private String abbreviation;
private String name;
private String postAbbreviation;
private String status;
}
25 changes: 25 additions & 0 deletions src/main/java/uk/nhs/tis/sync/mapper/LocalOfficeMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.nhs.tis.sync.mapper;

import com.transformuk.hee.tis.reference.api.dto.LocalOfficeDTO;
import com.transformuk.hee.tis.reference.api.enums.Status;
import org.mapstruct.Mapper;
import uk.nhs.tis.sync.dto.LocalOfficeDmsDto;

/**
* A mapper to map between Reference and DMS DTOs for the LocalOffice data type.
*/
@Mapper(componentModel = "spring")
public interface LocalOfficeMapper extends DmsMapper<LocalOfficeDTO, LocalOfficeDmsDto> {

LocalOfficeDmsDto toDmsDto(LocalOfficeDTO localOfficeDto);

/**
* Maps a Status to string.
*
* @param source The status to map.
* @return The string value of the status.
*/
default String map(Status source) {
return source == null ? null : source.toString();
}
}
151 changes: 92 additions & 59 deletions src/main/java/uk/nhs/tis/sync/service/DataRequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class DataRequestService {
private static final String TABLE_DBC = "DBC";
private static final String TABLE_GRADE = "Grade";
private static final String TABLE_HEE_USER = "HeeUser";
private static final String TABLE_LOCAL_OFFICE = "LocalOffice";
private static final String TABLE_PERSON = "Person";
private static final String TABLE_PLACEMENT = "Placement";
private static final String TABLE_PLACEMENT_SPECIALTY = "PlacementSpecialty";
Expand Down Expand Up @@ -60,74 +61,106 @@ public class DataRequestService {
*/
public List<Object> retrieveDtos(Map<String, String> message) {
try {
String table = message.get("table");

if (table.equals(TABLE_PLACEMENT_SPECIALTY) && message.containsKey("placementId")) {
long placementId = Long.parseLong(message.get("placementId"));
return createNonNullList(retrievePlacementSpecialty(placementId));
if (message.containsKey("id")) {
return retrieveDtosById(message);
} else {
return retrieveDtosByOtherKey(message);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return Collections.emptyList();
}

if (table.equals(TABLE_PROGRAMME_MEMBERSHIP) && message.containsKey("uuid")) {
UUID uuid = UUID.fromString(message.get("uuid"));
return createNonNullList(tcsServiceImpl.getProgrammeMembershipByUuid(uuid));
}
/**
* Return the list of DTOs for tables with an id primary key.
*
* @param message The message containing the request details.
* @return The non-null list of DTOs, or an empty list if the table and/or id was not found.
*/
private List<Object> retrieveDtosById(Map<String, String> message) {
String table = message.get("table");
long id = Long.parseLong(message.get("id"));

switch (table) {
case TABLE_PERSON:
return retrieveFullSyncData(id);
case TABLE_CURRICULUM:
return createNonNullList(tcsServiceImpl.getCurriculumById(id));
case TABLE_PLACEMENT:
return createNonNullList(tcsServiceImpl.getPlacementById(id));
case TABLE_POST:
return createNonNullList(tcsServiceImpl.getPostById(id));
case TABLE_PROGRAMME:
return createNonNullList(
tcsServiceImpl.findProgrammesIn(Collections.singletonList(String.valueOf(id)))
.get(0));
case TABLE_SITE:
return createNonNullList(
referenceServiceImpl.findSitesIdIn(Collections.singleton(id)).get(0));
case TABLE_SPECIALTY:
return createNonNullList(tcsServiceImpl.getSpecialtyById(id));
case TABLE_TRUST:
return createNonNullList(referenceServiceImpl.findTrustById(id));
case TABLE_GRADE:
return createNonNullList(
referenceServiceImpl.findGradesIdIn(Collections.singleton(id)).get(0));
default:
return Collections.emptyList();
}
}

if (table.equals(TABLE_CURRICULUM_MEMBERSHIP) && message.containsKey(
"programmeMembershipUuid")) {
UUID uuid = UUID.fromString(message.get("programmeMembershipUuid"));
ProgrammeMembershipDTO programmeMembership = tcsServiceImpl.getProgrammeMembershipByUuid(
uuid);
/**
* Return the list of DTOs for tables using another (non-id) key.
*
* @param message The message containing the request details.
* @return The non-null list of DTOs, or an empty list if the table and/or key was not found.
*/
private List<Object> retrieveDtosByOtherKey(Map<String, String> message) {
String table = message.get("table");

return programmeMembership.getCurriculumMemberships().stream()
.map(cm -> new CurriculumMembershipWrapperDto(uuid, cm))
.collect(Collectors.toList());
}
if (table.equals(TABLE_PLACEMENT_SPECIALTY) && message.containsKey("placementId")) {
long placementId = Long.parseLong(message.get("placementId"));
return createNonNullList(retrievePlacementSpecialty(placementId));
}

if (table.equals(TABLE_HEE_USER) && message.containsKey("name")) {
String name = message.get("name");
return createNonNullList(profileServiceImpl.getSingleAdminUser(name));
}
if (table.equals(TABLE_PROGRAMME_MEMBERSHIP) && message.containsKey("uuid")) {
UUID uuid = UUID.fromString(message.get("uuid"));
return createNonNullList(tcsServiceImpl.getProgrammeMembershipByUuid(uuid));
}

if (table.equals(TABLE_DBC) && message.containsKey("dbc")) {
String dbc = message.get("dbc");
ResponseEntity<DBCDTO> responseEntity = referenceServiceImpl.getDBCByCode(dbc);
return createNonNullList(responseEntity.getBody());
}
if (table.equals(TABLE_CURRICULUM_MEMBERSHIP) && message.containsKey(
"programmeMembershipUuid")) {
UUID uuid = UUID.fromString(message.get("programmeMembershipUuid"));
ProgrammeMembershipDTO programmeMembership = tcsServiceImpl.getProgrammeMembershipByUuid(
uuid);

if (message.containsKey("id")) {
long id = Long.parseLong(message.get("id"));

switch (table) {
case TABLE_PERSON:
return retrieveFullSyncData(id);
case TABLE_CURRICULUM:
return createNonNullList(tcsServiceImpl.getCurriculumById(id));
case TABLE_PLACEMENT:
return createNonNullList(tcsServiceImpl.getPlacementById(id));
case TABLE_POST:
return createNonNullList(tcsServiceImpl.getPostById(id));
case TABLE_PROGRAMME:
return createNonNullList(
tcsServiceImpl.findProgrammesIn(Collections.singletonList(String.valueOf(id)))
.get(0));
case TABLE_SITE:
return createNonNullList(
referenceServiceImpl.findSitesIdIn(Collections.singleton(id)).get(0));
case TABLE_SPECIALTY:
return createNonNullList(tcsServiceImpl.getSpecialtyById(id));
case TABLE_TRUST:
return createNonNullList(referenceServiceImpl.findTrustById(id));
case TABLE_GRADE:
return createNonNullList(
referenceServiceImpl.findGradesIdIn(Collections.singleton(id)).get(0));
default:
break;
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return programmeMembership.getCurriculumMemberships().stream()
.map(cm -> new CurriculumMembershipWrapperDto(uuid, cm))
.collect(Collectors.toList());
}

if (table.equals(TABLE_HEE_USER) && message.containsKey("name")) {
String name = message.get("name");
return createNonNullList(profileServiceImpl.getSingleAdminUser(name));
}

if (table.equals(TABLE_DBC) && message.containsKey("dbc")) {
String dbc = message.get("dbc");
ResponseEntity<DBCDTO> responseEntity = referenceServiceImpl.getDBCByCode(dbc);
return createNonNullList(responseEntity.getBody());
}
if (table.equals(TABLE_DBC) && message.containsKey("abbr")) {
String abbr = message.get("abbr");
ResponseEntity<DBCDTO> responseEntity = referenceServiceImpl.getDBCByAbbr(abbr);
return createNonNullList(responseEntity.getBody());
}

if (table.equals(TABLE_LOCAL_OFFICE) && message.containsKey("abbreviation")) {
String abbreviation = message.get("abbreviation");
return createNonNullList(
referenceServiceImpl.findLocalOfficesByAbbrev(abbreviation).get(0));
}
return Collections.emptyList();
}

Expand Down
51 changes: 51 additions & 0 deletions src/test/java/uk/nhs/tis/sync/mapper/LocalOfficeMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.nhs.tis.sync.mapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.transformuk.hee.tis.reference.api.dto.LocalOfficeDTO;
import com.transformuk.hee.tis.reference.api.enums.Status;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uk.nhs.tis.sync.dto.LocalOfficeDmsDto;

class LocalOfficeMapperTest {

private LocalOfficeMapper mapper;

private LocalOfficeDTO localOfficeDto;

private final UUID uuid = UUID.randomUUID();

@BeforeEach
public void setUp() {
mapper = new LocalOfficeMapperImpl();

localOfficeDto = new LocalOfficeDTO();
localOfficeDto.setName("name");
localOfficeDto.setAbbreviation("abbr");
localOfficeDto.setId(1L);
localOfficeDto.setUuid(uuid);
localOfficeDto.setStatus(Status.CURRENT);
}

@Test
void shouldMapALocalOfficeDtoToADataDmsDto() {
LocalOfficeDmsDto localOfficeDmsDto = mapper.toDmsDto(localOfficeDto);

assertEquals("1", localOfficeDmsDto.getId());
assertEquals("name", localOfficeDmsDto.getName());
assertEquals("abbr", localOfficeDmsDto.getAbbreviation());
assertEquals(uuid, localOfficeDmsDto.getUuid());
assertEquals("CURRENT", localOfficeDmsDto.getStatus());
}

@Test
void shouldMapNullStatusToNull() {
localOfficeDto.setStatus(null);
LocalOfficeDmsDto localOfficeDmsDto = mapper.toDmsDto(localOfficeDto);

assertNull(localOfficeDmsDto.getStatus());
}
}
Loading
Loading