Skip to content

Commit

Permalink
feat(FSADT1-1012): updating DoB insert and data insertion
Browse files Browse the repository at this point in the history
- updating data to be inserted as close as the green interface without relying too much on the procedures.
  • Loading branch information
paulushcgcj committed Nov 27, 2023
1 parent a0cc90a commit c5963ea
Show file tree
Hide file tree
Showing 8 changed files with 3,887 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class ApplicationConstant {
public static final String LOCATION_ID = "locationId";
public static final String TOTAL = "total";
public static final String INDEX = "index";
public static final String PROCESSOR_USER_NAME = "Otto Mated";
public static final String PROCESSOR_USER_NAME = "IDIR\\OTTOMATED";
public static final long ORG_UNIT = 70L;
public static final String LOCATION_CODE = "locationCode";
public static final String SUBMISSION_MAIL_BUILD_CHANNEL = "submissionMailBuildChannel";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.bc.gov.app.entity.legacy;


import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class ForestClientEntity {
private String clientStatusCode;
@Column("CLIENT_TYPE_CODE")
private String clientTypeCode;
@Column("BIRTHDATE")
private LocalDate birthDate;
@Column("CLIENT_ID_TYPE_CODE")
private String clientIdTypeCode;
@Column("CLIENT_IDENTIFICATION ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,6 @@ public Mono<Message<Integer>> createContact(Message<Integer> message) {
)
);

// Load the next contact id
IntFunction<Mono<String>> nextContactId = increment -> legacyR2dbcEntityTemplate
.selectOne(
Query
.empty()
.sort(Sort.by(Direction.DESC, "CLIENT_CONTACT_ID"))
.limit(1),
ForestClientContactEntity.class
)
.map(ForestClientContactEntity::getClientContactId)
.map(lastForestClientContactId -> String.valueOf(
Integer.parseInt(lastForestClientContactId) + increment));

// Load the contact and converts it into a forest client contact entity
IntFunction<Mono<ForestClientContactEntity>> toContact = contactId ->
contactRepository
Expand All @@ -428,13 +415,12 @@ public Mono<Message<Integer>> createContact(Message<Integer> message) {
);

// Convert the contact into a forest client contact entity and save it
BiFunction<SubmissionLocationContactEntity, Integer, Mono<ForestClientContactEntity>> createContact =
(locationContact, increment) ->
Function<SubmissionLocationContactEntity, Mono<ForestClientContactEntity>> createContact =
locationContact ->
toContact
.apply(locationContact.getSubmissionContactId())
.flatMap(forestClientContact ->
nextContactId
.apply(increment)
getNextContactId()
.doOnNext(forestClientContact::setClientContactId)
.thenReturn(forestClientContact)
)
Expand All @@ -456,22 +442,16 @@ public Mono<Message<Integer>> createContact(Message<Integer> message) {
.findBySubmissionLocationId(
message.getHeaders().get(ApplicationConstant.LOCATION_ID, Integer.class)
)
.index()
.flatMap(locationContactTuple ->
.flatMap(locationContactEntity ->
forestContact
.apply(locationContactTuple.getT2().getSubmissionContactId())
.switchIfEmpty(
createContact
.apply(
locationContactTuple.getT2(),
locationContactTuple.getT1().intValue() + 1
)
)
.apply(locationContactEntity.getSubmissionContactId())
.switchIfEmpty(createContact.apply(locationContactEntity))
)
.collectList()
.thenReturn(message);
}


/**
* Sends a notification to the user that the submission has been processed
*/
Expand Down Expand Up @@ -525,31 +505,46 @@ protected String getUser(Message<?> message, String headerName) {
}

private Mono<String> getNextClientNumber() {
return legacyR2dbcEntityTemplate
.selectOne(
Query
.empty()
.sort(Sort.by(Direction.DESC, ApplicationConstant.CLIENT_NUMBER))
.limit(1),
ForestClientEntity.class
return
legacyR2dbcEntityTemplate
.getDatabaseClient()
.sql("""
UPDATE
max_client_nmbr
SET
client_number = (SELECT LPAD(TO_NUMBER(NVL(max(CLIENT_NUMBER),'0'))+1,8,'0') FROM FOREST_CLIENT)"""
)
.map(ForestClientEntity::getClientNumber)
.map(lastForestClientNumber -> String.format("%08d",
Integer.parseInt(lastForestClientNumber) + 1)
.fetch()
.rowsUpdated()
.then(
legacyR2dbcEntityTemplate
.getDatabaseClient()
.sql("SELECT client_number FROM max_client_nmbr")
.map((row, rowMetadata) -> row.get("client_number", String.class))
.first()
);
}

private Mono<Integer> getNextDoingBusinessAs() {
return
legacyR2dbcEntityTemplate
.getDatabaseClient()
.sql("select THE.client_dba_seq.NEXTVAL from dual")
.fetch()
.first()
.map(row -> row.get("NEXTVAL"))
.map(String::valueOf)
.map(Integer::parseInt);
}

private Mono<String> getNextContactId() {
return legacyR2dbcEntityTemplate
.selectOne(
Query
.empty()
.sort(Sort.by(Direction.DESC, "CLIENT_DBA_ID"))
.limit(1),
ClientDoingBusinessAsEntity.class
)
.map(ClientDoingBusinessAsEntity::getId)
.map(lastId -> lastId + 1);
.getDatabaseClient()
.sql("SELECT THE.client_contact_seq.NEXTVAL FROM dual")
.fetch()
.first()
.map(row -> row.get("NEXTVAL"))
.map(String::valueOf);
}

private Mono<ClientDoingBusinessAsEntity> createClientDoingBusinessAs(
Expand Down Expand Up @@ -607,6 +602,9 @@ private Mono<ForestClientLocationEntity> toForestClientLocationEntity(
.addressOne(submissionLocation.getStreetAddress())
.city(submissionLocation.getCityName())
.province(submissionLocation.getProvinceCode())
//So as not to impact HBS, set HDBS Company Code to a space if not provided
//as it would have been in CLI
.hdbsCompanyCode(" ")
.country(
countryList
.getOrDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public Mono<Message<ForestClientEntity>> generateForestClient(Message<String> me
getUser(message, ApplicationConstant.CREATED_BY),
getUser(message, ApplicationConstant.UPDATED_BY)
)
.withBirthDate(detailEntity.getBirthdate())
.withLegalFirstName(ProcessorUtil.splitName(detailEntity.getOrganizationName())[1].toUpperCase())
.withClientName(ProcessorUtil.splitName(detailEntity.getOrganizationName())[0].toUpperCase())
.withClientComment("Individual with data acquired from BC Services Card")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public Mono<Message<ForestClientEntity>> generateForestClient(Message<String> me
getUser(message, ApplicationConstant.CREATED_BY),
getUser(message, ApplicationConstant.UPDATED_BY)
)
.withBirthDate(submissionDetail.getBirthdate())
.withClientIdentification(submissionDetail.getIncorporationNumber())
.withClientComment(
String.join(" ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public Mono<Message<ForestClientEntity>> generateForestClient(Message<String> me
getUser(message, ApplicationConstant.CREATED_BY),
getUser(message, ApplicationConstant.UPDATED_BY)
)
.withBirthDate(detail.getBirthdate())
.withLegalFirstName(ProcessorUtil.splitName(detail.getOrganizationName())[1].toUpperCase())
.withClientName(ProcessorUtil.splitName(detail.getOrganizationName())[0].toUpperCase())
.withLegalMiddleName(ProcessorUtil.splitName(detail.getOrganizationName())[2].toUpperCase())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import ca.bc.gov.app.entity.client.SubmissionDetailEntity;
import ca.bc.gov.app.entity.client.SubmissionLocationContactEntity;
import ca.bc.gov.app.entity.client.SubmissionLocationEntity;
import ca.bc.gov.app.entity.legacy.ClientDoingBusinessAsEntity;
import ca.bc.gov.app.entity.legacy.ForestClientContactEntity;
import ca.bc.gov.app.entity.legacy.ForestClientEntity;
import ca.bc.gov.app.entity.legacy.ForestClientLocationEntity;
Expand All @@ -25,7 +24,10 @@
import ca.bc.gov.app.repository.client.SubmissionLocationRepository;
import ca.bc.gov.app.repository.client.SubmissionRepository;
import ca.bc.gov.app.repository.legacy.ClientDoingBusinessAsRepository;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Stream;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -36,6 +38,10 @@
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.r2dbc.core.ReactiveInsertOperation.ReactiveInsert;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.r2dbc.core.DatabaseClient.GenericExecuteSpec;
import org.springframework.r2dbc.core.FetchSpec;
import org.springframework.r2dbc.core.RowsFetchSpec;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
Expand Down Expand Up @@ -181,8 +187,11 @@ void shouldCreateLocations() {
@DisplayName("create contacts")
void shouldCreateContacts() {
ReactiveInsert<ForestClientContactEntity> insert = mock(ReactiveInsert.class);
DatabaseClient dbCLient = mock(DatabaseClient.class);
GenericExecuteSpec execSpec = mock(GenericExecuteSpec.class);
FetchSpec<Map<String, Object>> fetchSpec = mock(FetchSpec.class);

when(locationContactRepository.findBySubmissionLocationId(eq(1)))
when(locationContactRepository.findBySubmissionLocationId(any()))
.thenReturn(Flux.just(
SubmissionLocationContactEntity
.builder()
Expand All @@ -191,7 +200,7 @@ void shouldCreateContacts() {
.build()
)
);
when(contactRepository.findById(eq(1)))
when(contactRepository.findById(any(Integer.class)))
.thenReturn(Mono.just(
SubmissionContactEntity
.builder()
Expand All @@ -211,6 +220,12 @@ void shouldCreateContacts() {
.thenReturn(Mono.just(ForestClientContactEntity.builder().contactName("Text").build()));
when(legacyR2dbcEntityTemplate.selectOne(any(), any()))
.thenReturn(Mono.empty());
when(legacyR2dbcEntityTemplate.getDatabaseClient())
.thenReturn(dbCLient);
when(dbCLient.sql(any(String.class)))
.thenReturn(execSpec);
when(execSpec.fetch()).thenReturn(fetchSpec);
when(fetchSpec.first()).thenReturn(Mono.just(Map.of("NEXTVAL", 1)));

service.createContact(
MessageBuilder
Expand Down Expand Up @@ -322,7 +337,31 @@ void shouldSendEmail() {
@DisplayName("check client data")
void shouldCheckClientData(
String clientTypeCode,
String clientNumber) {
String clientNumber
) {

ReactiveInsert<ForestClientContactEntity> insert = mock(ReactiveInsert.class);
DatabaseClient dbCLient = mock(DatabaseClient.class);
GenericExecuteSpec execSpec = mock(GenericExecuteSpec.class);
FetchSpec<Map<String, Object>> fetchSpec = mock(FetchSpec.class);
RowsFetchSpec<String> fetchString = mock(RowsFetchSpec.class);

when(legacyR2dbcEntityTemplate.getDatabaseClient())
.thenReturn(dbCLient);
when(dbCLient.sql(any(String.class)))
.thenReturn(execSpec);
when(execSpec.fetch()).thenReturn(fetchSpec);
when(fetchSpec.rowsUpdated()).thenReturn(Mono.just(1L));
when(execSpec.map(any(BiFunction.class)))
.thenReturn(fetchString);
when(fetchString.first()).thenReturn(Mono.just(

BooleanUtils.toString(
StringUtils.isNotBlank(clientNumber),
"00000000",
"00000001"
)
));

when(submissionDetailRepository.findBySubmissionId(any()))
.thenReturn(Mono.just(
Expand Down Expand Up @@ -438,8 +477,7 @@ void shouldCreateClient() {
assertThat(message)
.as("message")
.isNotNull()
.hasFieldOrPropertyWithValue("payload",2);

.hasFieldOrPropertyWithValue("payload", 2);

assertThat(message.getHeaders().get(ApplicationConstant.FOREST_CLIENT_NUMBER))
.as("forest client number")
Expand Down
Loading

0 comments on commit c5963ea

Please sign in to comment.