Skip to content

Commit

Permalink
Merge pull request #67 from companieshouse/feature/DSND-2144
Browse files Browse the repository at this point in the history
Feature/dsnd 2144
  • Loading branch information
lbedrosyanCH authored Dec 14, 2023
2 parents 34a4b9e + ad51679 commit 1ba19d0
Show file tree
Hide file tree
Showing 9 changed files with 486 additions and 366 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.companieshouse.pscdataapi.service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -176,8 +177,12 @@ public Individual getIndividualPsc(
.filter(document -> document.getData().getKind()
.equals("individual-person-with-significant-control"));
if (pscDocument.isPresent()) {
boolean showFullDateOfBirth = determineShowFullDob(
companyNumber, registerView, pscDocument.get());

Individual individual = transformer
.transformPscDocToIndividual(pscDocument.get(), registerView);
.transformPscDocToIndividual(pscDocument.get(), showFullDateOfBirth);

if (individual == null) {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"Failed to transform PSCDocument to Individual");
Expand All @@ -187,6 +192,8 @@ public Individual getIndividualPsc(
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"Individual PSC document not found in Mongo with id " + notificationId);
}
} catch (ResourceNotFoundException rnfe) {
throw rnfe;
} catch (Exception ex) {
logger.error(ex.getMessage(), DataMapHolder.getLogMap());
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
Expand All @@ -208,8 +215,13 @@ public IndividualBeneficialOwner getIndividualBeneficialOwnerPsc(
.equals("individual-beneficial-owner")
&& document.getCompanyNumber().equals(companyNumber));
if (pscDocument.isPresent()) {
boolean showFullDateOfBirth = determineShowFullDob(
companyNumber, registerView, pscDocument.get());

IndividualBeneficialOwner individualBeneficialOwner = transformer
.transformPscDocToIndividualBeneficialOwner(pscDocument.get(),registerView);
.transformPscDocToIndividualBeneficialOwner(pscDocument.get(),
showFullDateOfBirth);

if (individualBeneficialOwner == null) {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"Failed to transform PSCDocument to IndividualBeneficialOwner");
Expand All @@ -220,13 +232,57 @@ public IndividualBeneficialOwner getIndividualBeneficialOwnerPsc(
"Individual Beneficial Owner PSC document not found in Mongo with id"
+ notificationId);
}
} catch (ResourceNotFoundException rnfe) {
throw rnfe;
} catch (Exception ex) {
logger.error(ex.getMessage(), DataMapHolder.getLogMap());
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"Unexpected error occurred while fetching PSC document");
}
}

private boolean determineShowFullDob(String companyNumber, Boolean registerView,
PscDocument pscDocument) throws ResourceNotFoundException {
if (!registerView) {
return false;
} else {
Optional<MetricsApi> companyMetrics = companyMetricsApiService
.getCompanyMetrics(companyNumber);

if (companyMetrics.isPresent()) {
try {
String registerMovedTo = companyMetrics.get().getRegisters()
.getPersonsWithSignificantControl().getRegisterMovedTo();
if (registerMovedTo.equals("public-register")) {
Boolean isCeased = pscDocument.getData().getCeased();
boolean ceased = isCeased != null && isCeased;
LocalDate ceasedOn = pscDocument.getData().getCeasedOn();
LocalDate movedToPublicRegister = companyMetrics.get().getRegisters()
.getPersonsWithSignificantControl().getMovedOn().toLocalDate();

if (!ceased || movedToPublicRegister.isBefore(ceasedOn)) {
return true;
} else {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"not-on-public-register");
}
} else {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"not-on-public-register");
}
} catch (ResourceNotFoundException rnfe) {
throw rnfe;
} catch (Exception ignored) {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
"not-on-public-register");
}
} else {
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, String.format(
"No company metrics data found for company number: %s", companyNumber));
}
}
}

/** Get PSC record and transform it into a Corporate Entity PSC.
* @param companyNumber Company number.
* @param notificationId Mongo Id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class CompanyPscTransformer {
* @return PSC mongo Document.
*/
public Individual transformPscDocToIndividual(
PscDocument pscDocument, Boolean registerView) {
PscDocument pscDocument, boolean showFullDateOfBirth) {
logger.info("Attempting to transform pscDocument to Individual",
DataMapHolder.getLogMap());
Individual individual = new Individual();
Expand All @@ -67,7 +67,7 @@ public Individual transformPscDocToIndividual(
}
if (pscDocument.getSensitiveData() != null) {
individual.setDateOfBirth(mapDateOfBirth(
pscDocument.getSensitiveData().getDateOfBirth(), registerView));
pscDocument.getSensitiveData().getDateOfBirth(), showFullDateOfBirth));
}
if (pscDocument.getDeltaAt() != null) {
individual.setNotifiedOn(LocalDate.parse(pscDocument.getDeltaAt(), dateTimeFormatter));
Expand All @@ -81,7 +81,7 @@ public Individual transformPscDocToIndividual(
* @return PSC mongo Document.
*/
public IndividualBeneficialOwner transformPscDocToIndividualBeneficialOwner(
PscDocument pscDocument, Boolean registerView) {
PscDocument pscDocument, boolean showFullDateOfBirth) {
logger.info("Attempting to transform pscDocument to IndividualBeneficialOwner",
DataMapHolder.getLogMap());
IndividualBeneficialOwner individualBo = new IndividualBeneficialOwner();
Expand All @@ -99,7 +99,7 @@ public IndividualBeneficialOwner transformPscDocToIndividualBeneficialOwner(
}
if (pscDocument.getSensitiveData() != null) {
individualBo.setDateOfBirth(mapDateOfBirth(
pscDocument.getSensitiveData().getDateOfBirth(), registerView));
pscDocument.getSensitiveData().getDateOfBirth(), showFullDateOfBirth));
}
if (pscDocument.getDeltaAt() != null) {
individualBo.setNotifiedOn(LocalDate.parse(pscDocument.getDeltaAt(),
Expand Down Expand Up @@ -413,11 +413,11 @@ private void handleSecureFields(Data data, PscData pscData) {
}

private uk.gov.companieshouse.api.psc.DateOfBirth mapDateOfBirth(
DateOfBirth inputDateOfBirth, Boolean registerView) {
DateOfBirth inputDateOfBirth, boolean showFullDateOfBirth) {
if (inputDateOfBirth != null) {
uk.gov.companieshouse.api.psc.DateOfBirth dateOfBirth =
new uk.gov.companieshouse.api.psc.DateOfBirth();
if (registerView) {
if (showFullDateOfBirth) {
dateOfBirth.setDay(inputDateOfBirth.getDay());
} else {
dateOfBirth.setDay(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.api.client.http.HttpResponseException;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -40,19 +39,11 @@ public class ChsKafkaApiServiceTest {
private PrivateChangedResourcePost privateChangedResourcePost;
@Mock
private ApiResponse<Void> response;

private TestHelper testHelper;
@InjectMocks
private ChsKafkaApiService chsKafkaApiService;

@Captor
ArgumentCaptor<ChangedResource> changedResourceCaptor;

@BeforeEach
void setUp() {
testHelper = new TestHelper();
}

@Test
void invokeChsKafkaEndpoint() throws ApiErrorResponseException {
when(internalApiClient.privateChangedResourceHandler()).thenReturn(privateChangedResourceHandler);
Expand Down
Loading

0 comments on commit 1ba19d0

Please sign in to comment.