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

Updates to PBF messaging logic #187

Merged
merged 1 commit into from
Mar 17, 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 @@ -47,6 +47,7 @@
import ca.bc.gov.hlth.hnweb.service.EnrollmentService;
import ca.bc.gov.hlth.hnweb.service.PBFClinicPayeeService;
import ca.bc.gov.hlth.hnweb.service.PatientRegistrationService;
import ca.bc.gov.hlth.hnweb.service.RegistrationResult;

/**
* Handles request related to R70 Patient Registration.
Expand Down Expand Up @@ -106,30 +107,19 @@ public ResponseEntity<PatientRegistrationResponse> getPatientRegistration(
PatientRegistrationResponse response = new PatientRegistrationResponse();
response.setPersonDetail(personDetailsResponse);

// Retrieve patient registration history
boolean patientRegistrationExist = false;
// Retrieve patient registration history
RegistrationResult result = patientRegistrationService.getPatientRegistration(patientRegistrationRequest.getPayee(), patientRegistrationRequest.getPhn());

List<PatientRegister> registrationRecords = patientRegistrationService
.getPatientRegistration(patientRegistrationRequest.getPayee(), patientRegistrationRequest.getPhn());

String registrationMessage = patientRegistrationService.checkRegistrationDetails(registrationRecords,
patientRegistrationRequest.getPayee(), patientRegistrationRequest.getPhn());

if (!registrationRecords.isEmpty() || StringUtils.isNotBlank(registrationMessage)) {
patientRegistrationExist = true;

}

List<PatientRegisterModel> registrationHistory = convertPatientRegistration(registrationRecords);
List<PatientRegisterModel> registrationHistory = convertPatientRegistration(result.getPatientRegisters());
response.setPatientRegistrationHistory(registrationHistory);

handlePatientRegistrationResponse(response, patientRegistrationExist, registrationMessage);
handlePatientRegistrationResponse(response, result);

ResponseEntity<PatientRegistrationResponse> responseEntity = ResponseEntity.ok(response);

transactionComplete(transaction);
addAffectedParty(transaction, IdentifierType.PHN, personDetailsResponse.getPhn(), AffectedPartyDirection.OUTBOUND);
registrationRecords.forEach(record -> {
result.getPatientRegisters().forEach(record -> {
addAffectedParty(transaction, IdentifierType.PAYEE_NUMBER, record.getPayeeNumber(), AffectedPartyDirection.OUTBOUND);
addAffectedParty(transaction, IdentifierType.PRACTITIONER_NUMBER, record.getRegisteredPractitionerNumber(), AffectedPartyDirection.OUTBOUND);
});
Expand Down Expand Up @@ -168,17 +158,17 @@ private void validatePayeeNumberMapping(PatientRegistrationRequest patientRegist
}

private PatientRegistrationResponse handlePatientRegistrationResponse(
PatientRegistrationResponse patientRegistrationResponse, boolean registrationExist, String infoMessage) {
PatientRegistrationResponse patientRegistrationResponse, RegistrationResult registrationResult) {

Set<String> messages = new HashSet<>();
patientRegistrationResponse.setStatus(StatusEnum.SUCCESS);
patientRegistrationResponse.setMessage("Transaction completed successfully");

GetPersonDetailsResponse personDetailsResponse = patientRegistrationResponse.getPersonDetail();

if (registrationExist && !StringUtils.isEmpty(infoMessage)) {
messages.add(infoMessage);
} else if (!registrationExist) {
if (registrationResult.exists() && StringUtils.isNotEmpty(registrationResult.getRegistrationMessage())) {
messages.add(registrationResult.getRegistrationMessage());
} else if (!registrationResult.exists()) {
// Check if demographics record found
if (personDetailsResponse.getStatus() == StatusEnum.ERROR) {
// If no demographics record found, set status as WARNING
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package ca.bc.gov.hlth.hnweb.persistence.repository.pbf;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.PBFClinicPayee;

Expand All @@ -18,5 +21,10 @@ public interface PBFClinicPayeeRepository extends JpaRepository<PBFClinicPayee,
+ " and pcp.archived = false"
+ " and pcp.effectiveDate <= CURRENT_DATE"
+ " and (pcp.cancelDate IS NULL OR pcp.cancelDate >= CURRENT_DATE)")
public long countActivePayeeEntries(String payeeNumber);
long countActivePayeeEntries(String payeeNumber);

@Query("select p2.payeeNumber from PBFClinicPayee p1 inner join PBFClinicPayee p2 on p1.reportGroup = p2.reportGroup "
+ " where p1.payeeNumber = :payee and p2.archived = false")
List<String> findPayeeClinicViaReportGroup(@Param("payee") String payee);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.PatientRegister;

public interface PatientRegisterRepository extends JpaRepository<PatientRegister, Long> {

@Query("select p from PatientRegister p where archived = false and p.payeeNumber in ("
+ "select p2.payeeNumber from PBFClinicPayee p1 inner join PBFClinicPayee p2 on p1.reportGroup = "
+ "p2.reportGroup where p1.payeeNumber= :payee ) and "
+ "p.phn = :phn order by p.effectiveDate desc" )
List<PatientRegister> findPatientRegisterByPayeeClinic(@Param("payee") String payee, @Param("phn") String phn);

@Query("select p.payeeNumber from PatientRegister p where p.phn = :phn and p.archived = false")
List<String> findPayeeByphn(@Param("phn") String phn);
List<PatientRegister> findByPhnAndArchivedIsFalseOrderByEffectiveDateDesc(String phn);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ca.bc.gov.hlth.hnweb.service;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.PatientRegister;
import ca.bc.gov.hlth.hnweb.persistence.repository.pbf.PBFClinicPayeeRepository;
import ca.bc.gov.hlth.hnweb.persistence.repository.pbf.PatientRegisterRepository;

/**
Expand All @@ -17,47 +20,50 @@ public class PatientRegistrationService extends BaseService {

@Autowired
private PatientRegisterRepository patientRegisterRepository;

public List<PatientRegister> getPatientRegistration(String payee, String phn) {
return patientRegisterRepository.findPatientRegisterByPayeeClinic(payee, phn);
}

public List<String> getPayeeByPHN(String phn) {
return patientRegisterRepository.findPayeeByphn(phn);
}

@Autowired
private PBFClinicPayeeRepository pbfClinicPayeeRepository;

/**
* This method checks if patient is registered with same MSP Payee number or Payee within/outside reporting group
* @param registrationRecords
* @param payee
* @param phn
* @return message if not registerd with assigned user payee.
* Gets a history of Patient Registration.
* @param payee The user's payee
* @param phn The patient's PHN
* @return The results and message (if applicable)
*/
public String checkRegistrationDetails(List<PatientRegister> registrationRecords, String payee, String phn) {
String registrationMessage = "";

if (!registrationRecords.isEmpty()) {

boolean isSamePayeeWithinGroup = registrationRecords.stream()
.anyMatch(p -> payee.contentEquals(p.getPayeeNumber()));

// Check if patient registered with different payee within reporting group
if (!isSamePayeeWithinGroup) {
public RegistrationResult getPatientRegistration(String payee, String phn) {
RegistrationResult result = new RegistrationResult();
List<String> validPayees = pbfClinicPayeeRepository.findPayeeClinicViaReportGroup(payee);

// Find all registration records
List<PatientRegister> patientRegisters = patientRegisterRepository.findByPhnAndArchivedIsFalseOrderByEffectiveDateDesc(phn);

if (patientRegisters.isEmpty()) {
return result;
}

// Check the most recent record
PatientRegister currentPatientRegister = patientRegisters.get(0);

String registrationMessage = null;
// If the current payee matches the user's payee do nothing
if (!StringUtils.equals(currentPatientRegister.getPayeeNumber(), payee)) {

if (validPayees.contains(currentPatientRegister.getPayeeNumber())) {
// If the current payee belongs to the user's report group show a warning
registrationMessage = "Patient is registered with a different MSP Payee number within the reporting group";
}

} else {
// Check if patient registered with different payee outside of reporting group
List<String> payeeList = getPayeeByPHN(phn);

if (!payeeList.isEmpty()) {

} else {
// If the current payee is outisde the user's reporting group show a warning
registrationMessage = "Patient is registered with a different MSP Payee number outside of reporting group";

}

}
return registrationMessage;

// Filter out records from outside the reporting group
List<PatientRegister> filteredPatientRegisters = patientRegisters.stream().filter(pr -> validPayees.contains(pr.getPayeeNumber())).collect(Collectors.toList());

result.setPatientRegisters(filteredPatientRegisters);
result.setRegistrationMessage(registrationMessage);
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ca.bc.gov.hlth.hnweb.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.PatientRegister;

public class RegistrationResult {
private List<PatientRegister> patientRegisters = new ArrayList<>();

private String registrationMessage;

public List<PatientRegister> getPatientRegisters() {
return patientRegisters;
}

public void setPatientRegisters(List<PatientRegister> patientRegisters) {
this.patientRegisters = patientRegisters;
}

public String getRegistrationMessage() {
return registrationMessage;
}

public void setRegistrationMessage(String registrationMessage) {
this.registrationMessage = registrationMessage;
}

public Boolean exists() {
return !patientRegisters.isEmpty() || StringUtils.isNotEmpty(registrationMessage);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ public void testRegistrationHistory_success_diffPayeeWithinGroup() throws Except
//Override the base setup of the user to ensure we return the User with the User ID mapped to the this Payee Number
mockStatic.when(SecurityUtil::loadUserInfo).thenReturn(new UserInfo("unittest", "e4414a89-8974-4cff-9677-d9d2df6f9cfb", "00000010", "Ministry of Health", "hnweb-user", UUID.randomUUID().toString()));

PatientRegistrationRequest viewPatientRegisterRequest = new PatientRegistrationRequest();
viewPatientRegisterRequest.setPhn("9879869673");
viewPatientRegisterRequest.setPayee("T0053");
ResponseEntity<PatientRegistrationResponse> response = patientRegistrationController
.getPatientRegistration(viewPatientRegisterRequest, createHttpServletRequest());

assertEquals(HttpStatus.OK, response.getStatusCode());
PatientRegistrationResponse patientRegistrationResponse = response.getBody();
List<PatientRegisterModel> patientRegistrationHistory = patientRegistrationResponse
.getPatientRegistrationHistory();

// Check the additional message , status and number of valid records
String additionalMessage = "Patient is registered with a different MSP Payee number within the reporting group";
assertTrue(patientRegistrationResponse.getAdditionalInfoMessage().contains(additionalMessage));
assertEquals(StatusEnum.SUCCESS, patientRegistrationResponse.getStatus());
assertEquals(1, patientRegistrationHistory.size());
}

@Test
public void testRegistrationHistory_success_newerPayeeOutsideGroup() throws Exception {
createPBFClinicPayee();
createPatientRegister();

mockBackEnd.enqueue(new MockResponse()
.setBody(TestUtil.convertXMLFileToString("src/test/resources/GetDemographicsResponse_Error.xml"))
.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE.toString()));

//Override the base setup of the user to ensure we return the User with the User ID mapped to the this Payee Number
mockStatic.when(SecurityUtil::loadUserInfo).thenReturn(new UserInfo("unittest", "e4414a89-8974-4cff-9677-d9d2df6f9cfb", "00000010", "Ministry of Health", "hnweb-user", UUID.randomUUID().toString()));

PatientRegistrationRequest viewPatientRegisterRequest = new PatientRegistrationRequest();
viewPatientRegisterRequest.setPhn("9879869673");
viewPatientRegisterRequest.setPayee("T0053");
Expand All @@ -191,20 +221,26 @@ public void testRegistrationHistory_success_diffPayeeWithinGroup() throws Except
assertEquals(1, patientRegistrationHistory.size());
}

/**
* Tests that a warning message shows when the PHN exists within the reporting group BUT a newer record (via effective date)
* exists outside the reporting group.
*
* @throws Exception
*/
@Test
public void testRegistrationHistory_success_diffPayeeOutsideGroup() throws Exception {
createPBFClinicPayee();
createPatientRegister();

mockBackEnd.enqueue(new MockResponse()
.setBody(TestUtil.convertXMLFileToString("src/test/resources/GetDemographicsResponse_Error.xml"))
.setBody(TestUtil.convertXMLFileToString("src/test/resources/GetDemographicsResponse.xml"))
.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE.toString()));

//Override the base setup of the user to ensure we return the User with the User ID mapped to the this Payee Number
mockStatic.when(SecurityUtil::loadUserInfo).thenReturn(new UserInfo("unittest", "e4414a89-8974-4cff-9677-d9d2df6f9cfb", "00000010", "Ministry of Health", "hnweb-user", UUID.randomUUID().toString()));

PatientRegistrationRequest viewPatientRegisterRequest = new PatientRegistrationRequest();
viewPatientRegisterRequest.setPhn("7363117301");
viewPatientRegisterRequest.setPhn("7363117304");
viewPatientRegisterRequest.setPayee("T0053");
ResponseEntity<PatientRegistrationResponse> response = patientRegistrationController
.getPatientRegistration(viewPatientRegisterRequest, createHttpServletRequest());
Expand All @@ -215,10 +251,10 @@ public void testRegistrationHistory_success_diffPayeeOutsideGroup() throws Excep
.getPatientRegistrationHistory();

// Check the additional message , status and number of valid records
String additionalMessage = "Patient is registered with a different MSP Payee number outside of reporting group\nBCHCIM.GD.2.0006 The HL7 message is invalid. Please correct the HL7 message, and resubmit it.Results from Schematron validation";
String additionalMessage = "Patient is registered with a different MSP Payee number outside of reporting group";
assertTrue(patientRegistrationResponse.getAdditionalInfoMessage().contains(additionalMessage));
assertEquals(StatusEnum.SUCCESS, patientRegistrationResponse.getStatus());
assertEquals(0, patientRegistrationHistory.size());
assertEquals(1, patientRegistrationHistory.size());
}

@Test
Expand Down Expand Up @@ -426,13 +462,43 @@ private void createPatientRegister() throws ParseException {
patientRegister6.setRegisteredPractitionerSurname("Thomas");
patientRegister6.setArchived(Boolean.FALSE);
patientRegister6.setPhn("7363117303");

PatientRegister patientRegister7 = new PatientRegister();
patientRegister7.setAdministrativeCode("0");
Date effectiveDate7 = format.parse("20230101");
patientRegister7.setEffectiveDate(effectiveDate7);
patientRegister7.setCancelDate(null);
patientRegister7.setRegistrationReasonCode("SL");
patientRegister7.setPayeeNumber("T0053");
patientRegister7.setRegisteredPractitionerNumber("X2753");
patientRegister7.setRegisteredPractitionerFirstName("Sam");
patientRegister7.setRegisteredPractitionerMiddleName("E");
patientRegister7.setRegisteredPractitionerSurname("Thomas");
patientRegister7.setArchived(Boolean.FALSE);
patientRegister7.setPhn("7363117304");

PatientRegister patientRegister8 = new PatientRegister();
patientRegister8.setAdministrativeCode("0");
Date effectiveDate8 = format.parse("20240101");
patientRegister8.setEffectiveDate(effectiveDate8);
patientRegister8.setCancelDate(null);
patientRegister8.setRegistrationReasonCode("SL");
patientRegister8.setPayeeNumber("X0053");
patientRegister8.setRegisteredPractitionerNumber("X2753");
patientRegister8.setRegisteredPractitionerFirstName("Sam");
patientRegister8.setRegisteredPractitionerMiddleName("E");
patientRegister8.setRegisteredPractitionerSurname("Thomas");
patientRegister8.setArchived(Boolean.FALSE);
patientRegister8.setPhn("7363117304");

patientRegisterRepository.save(patientRegister1);
patientRegisterRepository.save(patientRegister2);
patientRegisterRepository.save(patientRegister3);
patientRegisterRepository.save(patientRegister4);
patientRegisterRepository.save(patientRegister5);
patientRegisterRepository.save(patientRegister6);
patientRegisterRepository.save(patientRegister7);
patientRegisterRepository.save(patientRegister8);
}

private void createPBFClinicPayee() {
Expand Down