Skip to content

Commit

Permalink
chore: fixing discrepancies
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Dec 18, 2024
1 parent 6a6ae7b commit bbbf47e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,59 +133,61 @@ public Mono<ClientDetailsDto> getClientDetailsByIncorporationNumber(
})

// If document type is SP and party contains only one entry that is not a person, fail
.filter(document -> provider.equalsIgnoreCase("idir")
|| !("SP".equalsIgnoreCase(document.business().legalType())
&& document.parties().size() == 1
&& !document.parties().get(0).isPerson())
.filter(document -> provider.equalsIgnoreCase("idir")
|| !("SP".equalsIgnoreCase(document.business().legalType())
&& document.parties().size() == 1
&& !document.parties().get(0).isPerson())
)
.flatMap(buildDetails())
.switchIfEmpty(Mono.error(new UnableToProcessRequestException(
"Unable to process request. This sole proprietor is not owned by a person"
)));
}

public Mono<ForestClientDetailsDto> getClientDetailsByClientNumber(String clientNumber) {
return legacyService
.searchByClientNumber(clientNumber)
.flatMap(forestClientDetailsDto -> Mono
.just(forestClientDetailsDto)
.filter(dto ->(StringUtils.isNotBlank(dto.corpRegnNmbr())))
.doOnNext(dto -> log.info("Retrieved corporation registration number: {}", forestClientDetailsDto.corpRegnNmbr()))
.flatMap(dto ->
bcRegistryService
.requestDocumentData( dto.corpRegnNmbr())
.next()
)
.flatMap(documentMono -> populateGoodStandingInd(forestClientDetailsDto, documentMono) )
.onErrorContinue(NoClientDataFound.class, (ex, obj) ->
log.error("No data found on BC Registry for client number: {}", clientNumber)
)
.switchIfEmpty(
Mono.just(forestClientDetailsDto)
.doOnNext(dto -> log.info("Corporation registration number not provided. Returning legacy details."))
)
);
return legacyService
.searchByClientNumber(clientNumber)
.flatMap(forestClientDetailsDto -> Mono
.just(forestClientDetailsDto)
.filter(dto -> (StringUtils.isNotBlank(dto.corpRegnNmbr())))
.doOnNext(dto -> log.info("Retrieved corporation registration number: {}",
forestClientDetailsDto.corpRegnNmbr()))
.flatMap(dto ->
bcRegistryService
.requestDocumentData(dto.corpRegnNmbr())
.next()
)
.flatMap(documentMono -> populateGoodStandingInd(forestClientDetailsDto, documentMono))
.onErrorContinue(NoClientDataFound.class, (ex, obj) ->
log.error("No data found on BC Registry for client number: {}", clientNumber)
)
.switchIfEmpty(
Mono.just(forestClientDetailsDto)
.doOnNext(dto -> log.info(
"Corporation registration number not provided. Returning legacy details."))
)
);
}

private Mono<ForestClientDetailsDto> populateGoodStandingInd(
ForestClientDetailsDto forestClientDetailsDto,
BcRegistryDocumentDto document
) {
Boolean goodStandingInd = document.business().goodStanding();
String goodStanding = BooleanUtils.toString(
goodStandingInd,
"Y",
"N",
StringUtils.EMPTY
);
Boolean goodStandingInd = document.business().goodStanding();
String goodStanding = BooleanUtils.toString(
goodStandingInd,
"Y",
"N",
StringUtils.EMPTY
);

log.info("Setting goodStandingInd for client: {} to {}",
forestClientDetailsDto.clientNumber(), goodStanding);
log.info("Setting goodStandingInd for client: {} to {}",
forestClientDetailsDto.clientNumber(), goodStanding);

ForestClientDetailsDto updatedDetails =
forestClientDetailsDto.withGoodStandingInd(goodStanding);
ForestClientDetailsDto updatedDetails =
forestClientDetailsDto.withGoodStandingInd(goodStanding);

return Mono.just(updatedDetails);
return Mono.just(updatedDetails);
}

/**
Expand Down Expand Up @@ -402,13 +404,13 @@ private Predicate<ForestClientDto> isMatchWith(BcRegistryDocumentDto document) {
return legacy ->
StringUtils.equals(
StringUtils.defaultString(legacy.registryCompanyTypeCode()) +
StringUtils.defaultString(legacy.corpRegnNmbr()),
StringUtils.defaultString(legacy.corpRegnNmbr()),
document.business().identifier()
) &&
StringUtils.equals(
document.business().legalName(),
legacy.legalName()
);
StringUtils.equals(
document.business().legalName(),
legacy.legalName()
);
}

private Function<ForestClientDto, Mono<ForestClientDto>> triggerEmailDuplicatedClient(
Expand Down
73 changes: 40 additions & 33 deletions backend/src/main/java/ca/bc/gov/app/util/JwtPrincipalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
public class JwtPrincipalUtil {

public static final String LAST_NAME = "lastName";

/**
* Retrieves the provider of the JWT token from the given JwtAuthenticationToken principal. The
* provider is extracted from the token attributes under the key "custom:idp_name". If the
Expand All @@ -29,7 +31,7 @@ public class JwtPrincipalUtil {
*
* @param principal JwtAuthenticationToken object from which the provider is to be extracted.
* @return The provider of the JWT token in uppercase, or an empty string if the provider is
* blank.
* blank.
*/
public static String getProvider(JwtAuthenticationToken principal) {
return getProviderValue(principal.getTokenAttributes());
Expand All @@ -43,7 +45,7 @@ public static String getProvider(JwtAuthenticationToken principal) {
*
* @param principal Jwt object from which the provider is to be extracted.
* @return The provider of the JWT token in uppercase, or an empty string if the provider is
* blank.
* blank.
*/
public static String getProvider(Jwt principal) {
return getProviderValue(principal.getClaims());
Expand All @@ -58,7 +60,7 @@ public static String getProvider(Jwt principal) {
*
* @param principal JwtAuthenticationToken object from which the user ID is to be extracted.
* @return The user ID prefixed with the provider in uppercase and a backslash, or an empty string
* if the user ID is blank.
* if the user ID is blank.
*/
public static String getUserId(JwtAuthenticationToken principal) {
return getUserIdValue(principal.getTokenAttributes());
Expand All @@ -72,7 +74,7 @@ public static String getUserId(JwtAuthenticationToken principal) {
*
* @param principal Jwt object from which the user ID is to be extracted.
* @return The user ID prefixed with the provider in uppercase and a backslash, or an empty string
* if the user ID is blank.
* if the user ID is blank.
*/
public static String getUserId(Jwt principal) {
return getUserIdValue(principal.getClaims());
Expand Down Expand Up @@ -174,7 +176,7 @@ public static String getName(JwtAuthenticationToken principal) {
*
* @param principal Jwt object from which the display name is to be extracted.
* @return The display name, or the concatenated first and last names, or an empty string if both
* the display name and the first and last names are blank.
* the display name and the first and last names are blank.
*/
public static String getName(Jwt principal) {
return getNameValue(principal.getClaims());
Expand Down Expand Up @@ -205,14 +207,17 @@ public static String getLastName(Jwt principal) {
}

/**
* Retrieves a list of groups from the given JwtPrincipal.
* This method extracts the token attributes from the provided {@link JwtAuthenticationToken}, then looks for the key "cognito:groups"
* in the token attributes. If the value associated with this key is a {@link List}, the method filters the elements to only
* include non-null values of type {@link String}. The resulting list of strings is returned.
* Retrieves a list of groups from the given JwtPrincipal. This method extracts the token
* attributes from the provided {@link JwtAuthenticationToken}, then looks for the key
* "cognito:groups" in the token attributes. If the value associated with this key is a
* {@link List}, the method filters the elements to only include non-null values of type
* {@link String}. The resulting list of strings is returned.
*
* @param jwtPrincipal The {@link JwtAuthenticationToken} containing the token attributes. It must have the "cognito:groups" key.
* If the key does not exist or the value is not a list of strings, an empty list is returned.
* @return A list of group names, or an empty list if the key is missing or the value is not a list of strings.
* @param jwtPrincipal The {@link JwtAuthenticationToken} containing the token attributes. It must
* have the "cognito:groups" key. If the key does not exist or the value is
* not a list of strings, an empty list is returned.
* @return A list of group names, or an empty list if the key is missing or the value is not a
* list of strings.
*/
public static Set<String> getGroups(JwtAuthenticationToken jwtPrincipal) {
if (jwtPrincipal == null || jwtPrincipal.getTokenAttributes() == null) {
Expand All @@ -222,14 +227,17 @@ public static Set<String> getGroups(JwtAuthenticationToken jwtPrincipal) {
}

/**
* Retrieves a list of groups from the given JwtPrincipal.
* This method extracts the token attributes from the provided {@link Jwt}, then looks for the key "cognito:groups"
* in the token attributes. If the value associated with this key is a {@link List}, the method filters the elements to only
* include non-null values of type {@link String}. The resulting list of strings is returned.
* Retrieves a list of groups from the given JwtPrincipal. This method extracts the token
* attributes from the provided {@link Jwt}, then looks for the key "cognito:groups" in the token
* attributes. If the value associated with this key is a {@link List}, the method filters the
* elements to only include non-null values of type {@link String}. The resulting list of strings
* is returned.
*
* @param jwtPrincipal The {@link Jwt} containing the token attributes. It must have the "cognito:groups" key.
* If the key does not exist or the value is not a list of strings, an empty list is returned.
* @return A list of group names, or an empty list if the key is missing or the value is not a list of strings.
* @param jwtPrincipal The {@link Jwt} containing the token attributes. It must have the
* "cognito:groups" key. If the key does not exist or the value is not a list
* of strings, an empty list is returned.
* @return A list of group names, or an empty list if the key is missing or the value is not a
* list of strings.
*/
public static Set<String> getGroups(Jwt jwtPrincipal) {
if (jwtPrincipal == null || jwtPrincipal.getClaims() == null) {
Expand All @@ -238,7 +246,7 @@ public static Set<String> getGroups(Jwt jwtPrincipal) {
return getClaimGroups(jwtPrincipal.getClaims());
}

private static Set<String> getClaimGroups(Map<String,Object> tokenAttributes) {
private static Set<String> getClaimGroups(Map<String, Object> tokenAttributes) {
Object groups = tokenAttributes.get("cognito:groups");

if (groups instanceof List) {
Expand All @@ -259,7 +267,7 @@ private static Set<String> getClaimGroups(Map<String,Object> tokenAttributes) {
* @param claims The map containing the JWT claims.
* @param claimName The name of the claim to retrieve.
* @return The value of the specified claim as a String, or an empty string if the claim is not
* present.
* present.
*/
private static String getClaimValue(Map<String, Object> claims, String claimName) {
return claims
Expand All @@ -275,7 +283,7 @@ private static String getClaimValue(Map<String, Object> claims, String claimName
*
* @param claims The map containing the JWT claims.
* @return The provider's name in uppercase or "BCSC" if it starts with "ca.bc.gov.flnr.fam.", or
* an empty string if the provider is not specified.
* an empty string if the provider is not specified.
*/
private static String getProviderValue(Map<String, Object> claims) {
String provider = getClaimValue(claims, "custom:idp_name");
Expand Down Expand Up @@ -309,7 +317,7 @@ private static String getBusinessNameValue(Map<String, Object> claims) {
*
* @param claims The map containing the JWT claims.
* @return The constructed user ID in the format "Provider\Username" or "Provider\UserID", or an
* empty string if neither the username nor the user ID is present in the claims.
* empty string if neither the username nor the user ID is present in the claims.
*/
private static String getUserIdValue(Map<String, Object> claims) {
return
Expand Down Expand Up @@ -359,7 +367,7 @@ private static String getEmailValue(Map<String, Object> claims) {
*
* @param claims The map containing the JWT claims.
* @return The display name value as a String, or an empty string if the "custom:idp_display_name"
* claim is not present.
* claim is not present.
*/
private static String getDisplayNameValue(Map<String, Object> claims) {
return getClaimValue(claims, "custom:idp_display_name");
Expand All @@ -374,10 +382,9 @@ private static String getDisplayNameValue(Map<String, Object> claims) {
*
* @param claims The map containing the JWT claims from which the name information is to be
* extracted.
* @return A map with keys "businessName", "firstName", "lastName", and "fullName",
* containing the extracted and/or computed name information.
* If specific name components are not found, their values in the map
* will be empty strings.
* @return A map with keys "businessName", "firstName", "lastName", and "fullName", containing the
* extracted and/or computed name information. If specific name components are not found, their
* values in the map will be empty strings.
*/
private static Map<String, String> processName(Map<String, Object> claims) {
Map<String, String> additionalInfo = new HashMap<>();
Expand All @@ -392,17 +399,17 @@ private static Map<String, String> processName(Map<String, Object> claims) {
// Determine if special handling for names is required
boolean useDisplayName =
"bceidbusiness".equals(getProviderValue(claims)) || (firstName.isEmpty()
&& lastName.isEmpty());
&& lastName.isEmpty());
if (useDisplayName) {
Map<String, String> names = ClientMapper.parseName(getDisplayNameValue(claims),
getProviderValue(claims));
firstName = names.get("firstName");
lastName = names.get("lastName");
lastName = names.get(LAST_NAME);
}

// Put extracted or computed first and last names into the map
additionalInfo.put("firstName", firstName.trim());
additionalInfo.put("lastName", lastName.trim());
additionalInfo.put(LAST_NAME, lastName.trim());
additionalInfo.put("fullName", String.join(" ", firstName, lastName).trim());

return additionalInfo;
Expand All @@ -417,7 +424,7 @@ private static Map<String, String> processName(Map<String, Object> claims) {
* @return The last name extracted from the JWT claims, or an empty string if not specified.
*/
private static String getLastNameValue(Map<String, Object> claims) {
return processName(claims).get("lastName");
return processName(claims).get(LAST_NAME);
}

/**
Expand All @@ -428,7 +435,7 @@ private static String getLastNameValue(Map<String, Object> claims) {
*
* @param claims The map containing the JWT claims.
* @return The full name (concatenation of first and last names) extracted from the JWT claims, or
* an empty string if not specified.
* an empty string if not specified.
*/
private static String getNameValue(Map<String, Object> claims) {
return processName(claims).get("fullName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class ClientServiceIntegrationTest extends AbstractTestContainerIntegrationTest
void testGetClientDetailsWithGoodStandingIndicator() {
String clientNumber = "123456";
String corpRegnNmbr = "9607514";
List<String> groups = List.of("CLIENT_ADMIN");

ForestClientDetailsDto initialDto = new ForestClientDetailsDto(
clientNumber,
Expand Down

0 comments on commit bbbf47e

Please sign in to comment.