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

7102 map msp payee numbers to a user account #126

Merged
merged 11 commits into from
Oct 3, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package ca.bc.gov.hlth.hnweb.controller;

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import ca.bc.gov.hlth.hnweb.exception.BcscPayeeMappingException;
import ca.bc.gov.hlth.hnweb.model.rest.pbf.BcscPayeeMappingRequest;
import ca.bc.gov.hlth.hnweb.model.rest.pbf.BcscPayeeMappingResponse;
import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.BcscPayeeMapping;
import ca.bc.gov.hlth.hnweb.service.BcscPayeeMappingService;

/**
* Controller to handle CRUD requests for maintaining BC Services Card (BCSC) Users to their PBF MSP Payee Number mappings.
*
*/
@RequestMapping("/payee-mapping")
@RestController
public class BcscPayeeMappingController {

private static final Logger logger = LoggerFactory.getLogger(BcscPayeeMappingController.class);

@Autowired
private BcscPayeeMappingService bcscPayeeMappingService;

/**
* Create a BCSC User to MSP Payee Number mapping
*
* @param bcscPayeeMappingRequest
* @return the response containing the newly created mapping if successful otherwise an error status
*/
@PostMapping("")
public ResponseEntity<BcscPayeeMappingResponse> addBcscPayeeMapping(@RequestBody BcscPayeeMappingRequest bcscPayeeMappingRequest) {
logger.info("Adding a new BCSC User to Payee Mapping:\n{}", bcscPayeeMappingRequest.toString());

if (StringUtils.isBlank(bcscPayeeMappingRequest.getBcscGuid())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Missing value in required request field bcscGuid.");
}
if (StringUtils.isBlank(bcscPayeeMappingRequest.getPayeeNumber())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Missing value in required request field payeeNumber.");
weskubo-cgi marked this conversation as resolved.
Show resolved Hide resolved
}

try {
BcscPayeeMapping bcscPayeeMapping = mapRequestToEntity(bcscPayeeMappingRequest);
BcscPayeeMapping newBcscPayeeMapping = bcscPayeeMappingService.add(bcscPayeeMapping);
BcscPayeeMappingResponse bcscPayeeMappingResponse = mapEntityToRepsonse(newBcscPayeeMapping);
return ResponseEntity.ok(bcscPayeeMappingResponse);
} catch(Exception exception) {
HttpStatus status = handleException(exception);
throw new ResponseStatusException(status, exception.getMessage(), exception);
}
}

@PutMapping("/{id}")
public ResponseEntity<BcscPayeeMappingResponse> updateBcscPayeeMapping(@RequestBody BcscPayeeMappingRequest bcscPayeeMappingRequest, @PathVariable String id) {
logger.info("Updating a BCSC User to Payee Mapping for ID: {}; Updated entity: \n{}", id, bcscPayeeMappingRequest.toString());

if (StringUtils.isBlank(bcscPayeeMappingRequest.getPayeeNumber())) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Missing value in required request field payeeNumber");
}
weskubo-cgi marked this conversation as resolved.
Show resolved Hide resolved

try {
BcscPayeeMapping bcscPayeeMapping = mapRequestToEntity(bcscPayeeMappingRequest);
BcscPayeeMapping updatedBcscPayeeMapping = bcscPayeeMappingService.update(bcscPayeeMapping, id);
BcscPayeeMappingResponse bcscPayeeMappingResponse = mapEntityToRepsonse(updatedBcscPayeeMapping);
return ResponseEntity.ok(bcscPayeeMappingResponse);
} catch(Exception exception) {
HttpStatus status = handleException(exception);
throw new ResponseStatusException(status, exception.getMessage(), exception);
}
}

@GetMapping("/{id}")
public ResponseEntity<BcscPayeeMappingResponse> getBcscPayeeMapping(@PathVariable String id) {
logger.info("Getting a BCSC User to Payee Mapping for ID: {}", id);

Optional<BcscPayeeMapping> bcscPayeeMappingOptional = bcscPayeeMappingService.find(id);
if (bcscPayeeMappingOptional.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Entity not found for ID %s", id));
}

BcscPayeeMappingResponse bcscPayeeMappingResponse = mapEntityToRepsonse(bcscPayeeMappingOptional.get());
return ResponseEntity.ok(bcscPayeeMappingResponse);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBcscPayeeMapping(@PathVariable String id) {
logger.info("Deleting a BCSC User to Payee Mapping for ID: {}", id);

try {
bcscPayeeMappingService.delete(id);
return ResponseEntity.noContent().build();
} catch(Exception exception) {
HttpStatus status = handleException(exception);
throw new ResponseStatusException(status, exception.getMessage(), exception);
}
}

private BcscPayeeMappingResponse mapEntityToRepsonse(BcscPayeeMapping newBcscPayeeMapping) {
BcscPayeeMappingResponse bcscPayeeMappingResponse = new BcscPayeeMappingResponse();
bcscPayeeMappingResponse.setBcscGuid(newBcscPayeeMapping.getBcscGuid());
bcscPayeeMappingResponse.setPayeeNumber(newBcscPayeeMapping.getPayeeNumber());
return bcscPayeeMappingResponse;
}

private BcscPayeeMapping mapRequestToEntity(BcscPayeeMappingRequest bcscPayeeMappingRequest) {
BcscPayeeMapping bcscPayeeMapping = new BcscPayeeMapping();
bcscPayeeMapping.setBcscGuid(bcscPayeeMappingRequest.getBcscGuid());
bcscPayeeMapping.setPayeeNumber(bcscPayeeMappingRequest.getPayeeNumber());
return bcscPayeeMapping;
}

private HttpStatus handleException(Exception exception) {
HttpStatus status;

if (exception instanceof BcscPayeeMappingException) {
BcscPayeeMappingException bpme = (BcscPayeeMappingException)exception;
switch (bpme.getType()) {
case ENTITY_ALREADY_EXISTS:
status = HttpStatus.CONFLICT;
break;
case ENTITY_NOT_FOUND:
status = HttpStatus.NOT_FOUND;
break;
default:
status = HttpStatus.BAD_REQUEST;
}
} else {
status = HttpStatus.BAD_REQUEST;
}

return status;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -17,13 +18,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import ca.bc.gov.hlth.hnweb.converter.hl7v3.GetDemographicsConverter;
import ca.bc.gov.hlth.hnweb.exception.HNWebException;
import ca.bc.gov.hlth.hnweb.model.rest.StatusEnum;
import ca.bc.gov.hlth.hnweb.model.rest.enrollment.GetPersonDetailsResponse;
import ca.bc.gov.hlth.hnweb.model.rest.patientregistration.PatientRegisterModel;
Expand All @@ -34,8 +38,12 @@
import ca.bc.gov.hlth.hnweb.persistence.entity.AffectedPartyDirection;
import ca.bc.gov.hlth.hnweb.persistence.entity.IdentifierType;
import ca.bc.gov.hlth.hnweb.persistence.entity.Transaction;
import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.BcscPayeeMapping;
import ca.bc.gov.hlth.hnweb.persistence.entity.pbf.PatientRegister;
import ca.bc.gov.hlth.hnweb.security.SecurityUtil;
import ca.bc.gov.hlth.hnweb.security.TransactionType;
import ca.bc.gov.hlth.hnweb.security.UserInfo;
import ca.bc.gov.hlth.hnweb.service.BcscPayeeMappingService;
import ca.bc.gov.hlth.hnweb.service.EnrollmentService;
import ca.bc.gov.hlth.hnweb.service.PatientRegistrationService;

Expand Down Expand Up @@ -65,6 +73,9 @@ public class PatientRegistrationController extends BaseController {

@Autowired
private PatientRegistrationService patientRegistrationService;

@Autowired
private BcscPayeeMappingService bcscPayeeMappingService;

@PostMapping("/get-patient-registration")
public ResponseEntity<PatientRegistrationResponse> getPatientRegistration(
Expand All @@ -77,6 +88,8 @@ public ResponseEntity<PatientRegistrationResponse> getPatientRegistration(
AffectedPartyDirection.INBOUND);

try {
validatePayeeNumberMapping(patientRegistrationRequest);

// Retrieve demographic details
GetDemographicsConverter converter = new GetDemographicsConverter();
GetDemographicsRequest demographicsRequest = converter.convertRequest(patientRegistrationRequest.getPhn());
Expand Down Expand Up @@ -119,6 +132,25 @@ public ResponseEntity<PatientRegistrationResponse> getPatientRegistration(
}
}

/**
* The Payee number submitted in the request must match the Payee Number mapped to the current user in the BCSC to Payee Number mappings.
*
* @param patientRegistrationRequest
* @throws HNWebException
*/
private void validatePayeeNumberMapping(PatientRegistrationRequest patientRegistrationRequest) {
UserInfo userInfo = SecurityUtil.loadUserInfo();
Optional<BcscPayeeMapping> bcscPayeeMappingOptional = bcscPayeeMappingService.find(userInfo.getUserId());
if (bcscPayeeMappingOptional.isEmpty()) {
logger.error("No Payee Number mapping was found for the current user");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "No Payee Number mapping was found for the current user");
}
if (!StringUtils.equals(patientRegistrationRequest.getPayee(), bcscPayeeMappingOptional.get().getPayeeNumber())) {
logger.error("Payee field value {} does not match the Payee Number mapped to this user", patientRegistrationRequest.getPayee());
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, String.format("Payee field value %s does not match the Payee Number mapped to this user", patientRegistrationRequest.getPayee()));
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ca.bc.gov.hlth.hnweb.exception;

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

/**
* Exception class for errors related to the {@link BcscPayeeMapping} entity.
*
*/
public class BcscPayeeMappingException extends Exception {

private static final long serialVersionUID = 1L;

private BcscPayeeMappingExceptionType type;

public BcscPayeeMappingException(BcscPayeeMappingExceptionType type) {
super(type.getMessage());
this.type = type;
}

public BcscPayeeMappingExceptionType getType() {
return type;
}

public void setType(BcscPayeeMappingExceptionType type) {
this.type = type;
}

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

/**
* enum for possible types of {@link BcscPayeeMappingException}
*
*/
public enum BcscPayeeMappingExceptionType {

ENTITY_ALREADY_EXISTS("Entity already exists."),
ENTITY_NOT_FOUND("Entity not found.");

private final String message;

private BcscPayeeMappingExceptionType(String message) {
this.message = message;
}

public String getMessage() {
return message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public enum ExceptionType {
DOWNSTREAM_FAILURE("Could not connect to downstream service"),
GENERAL("An error has occured"),
SSL_FAILURE("SSL Context could not be built, application will not start");

ExceptionType(String message) {
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ca.bc.gov.hlth.hnweb.model.rest.pbf;

public class BcscPayeeMappingRequest {

private String bcscGuid;

private String payeeNumber;
weskubo-cgi marked this conversation as resolved.
Show resolved Hide resolved

public String getBcscGuid() {
return bcscGuid;
}

public void setBcscGuid(String bcscGuid) {
this.bcscGuid = bcscGuid;
}

public String getPayeeNumber() {
return payeeNumber;
}

public void setPayeeNumber(String payeeNumber) {
this.payeeNumber = payeeNumber;
}

@Override
public String toString() {
return "BcscPayeeMapping [bcscGuid=" + bcscGuid + ", payeeNumber=" + payeeNumber + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ca.bc.gov.hlth.hnweb.model.rest.pbf;

public class BcscPayeeMappingResponse {

private String bcscGuid;

private String payeeNumber;

public String getBcscGuid() {
return bcscGuid;
}

public void setBcscGuid(String bcscGuid) {
this.bcscGuid = bcscGuid;
}

public String getPayeeNumber() {
return payeeNumber;
}

public void setPayeeNumber(String payeeNumber) {
this.payeeNumber = payeeNumber;
}

@Override
public String toString() {
return "BcscPayeeMapping [bcscGuid=" + bcscGuid + ", payeeNumber=" + payeeNumber + "]";
}

}
Loading