Skip to content

Commit

Permalink
feat: generic function for revision count (#1367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos authored Jul 9, 2024
1 parent b7baa87 commit 2b424af
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ crown land reforestation use (`true` yes) or not (`false` no).
description = "Indicates whether the source of the Seedlot is within British Columbia",
example = "true")
@NotNull
Boolean bcSourceInd) {}
Boolean bcSourceInd,
@Schema(
description = "Number representing the revision version of the seedlot record",
example = "42")
@NotNull
Integer revisionCount) {}
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public SeedlotAclassFormDto getFullSeedlotInfo(
/**
* PATCH an entry on the Seedlot table.
*
* @param patchDto A {@link SeedlotApplicationPatchDto} containig all required field to get a new
* @param patchDto A {@link SeedlotApplicationPatchDto} containing all required field to get a new
* registration started.
* @return A {@link Seedlot} with all updated values.
*/
Expand Down Expand Up @@ -401,7 +401,7 @@ public Seedlot patchApplicantAndSeedlotInfo(
@Valid
SeedlotApplicationPatchDto patchDto) {

return seedlotService.patchApplicantionInfo(seedlotNumber, patchDto);
return seedlotService.patchApplicantInfo(seedlotNumber, patchDto);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import ca.bc.gov.backendstartapi.exception.InvalidSeedlotRequestException;
import ca.bc.gov.backendstartapi.exception.NoSpuForOrchardException;
import ca.bc.gov.backendstartapi.exception.OracleApiProviderException;
import ca.bc.gov.backendstartapi.exception.SeedlotConflictDataException;
import ca.bc.gov.backendstartapi.exception.SeedlotFormValidationException;
import ca.bc.gov.backendstartapi.exception.SeedlotNotFoundException;
import ca.bc.gov.backendstartapi.exception.SeedlotSourceNotFoundException;
Expand Down Expand Up @@ -651,13 +652,18 @@ public SeedlotAclassFormDto getAclassSeedlotFormInfo(@NonNull String seedlotNumb
* @throws SeedlotNotFoundException in case of seedlot not found error.
* @throws SeedlotSourceNotFoundException in case of seedlot source not found error.
*/
public Seedlot patchApplicantionInfo(
public Seedlot patchApplicantInfo(
@NonNull String seedlotNumber, SeedlotApplicationPatchDto patchDto) {
SparLog.info("Patching seedlot entry for seedlot number {}", seedlotNumber);

Seedlot seedlotInfo =
seedlotRepository.findById(seedlotNumber).orElseThrow(SeedlotNotFoundException::new);

if (!patchDto.revisionCount().equals(seedlotInfo.getRevisionCount())) {
SparLog.info("Seedlot number {} updated by another user", seedlotNumber);
throw new SeedlotConflictDataException(seedlotNumber);
}

SparLog.info("Seedlot number {} found", seedlotNumber);

updateApplicantAndSeedlot(seedlotInfo, patchDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class SeedlotEndpointTest {
"applicantEmailAddress": "groot@wood.com",
"seedlotSourceCode": "CUS",
"toBeRegistrdInd": true,
"bcSourceInd": false
"bcSourceInd": false,
"revisionCount": 1
}
""";

Expand Down Expand Up @@ -520,7 +521,7 @@ void getSingleSeedlotAclassFullInfoNotFoundTest() throws Exception {
@Test
@DisplayName("patchSeedlotApplicationBadSourceTest")
void patchSeedlotApplicationBadSourceTest() throws Exception {
when(seedlotService.patchApplicantionInfo(any(), any()))
when(seedlotService.patchApplicantInfo(any(), any()))
.thenThrow(new SeedlotSourceNotFoundException());

mockMvc
Expand All @@ -537,7 +538,7 @@ void patchSeedlotApplicationBadSourceTest() throws Exception {
@Test
@DisplayName("patchSeedlotApplicationBadIdTest")
void patchSeedlotApplicationBadIdTest() throws Exception {
when(seedlotService.patchApplicantionInfo(any(), any()))
when(seedlotService.patchApplicantInfo(any(), any()))
.thenThrow(new SeedlotNotFoundException());

mockMvc
Expand All @@ -557,7 +558,7 @@ void patchSeedlotApplicationSuccessTest() throws Exception {
String seedlotNumber = "555888";
Seedlot testSeedlot = new Seedlot(seedlotNumber);

when(seedlotService.patchApplicantionInfo(any(), any())).thenReturn(testSeedlot);
when(seedlotService.patchApplicantInfo(any(), any())).thenReturn(testSeedlot);

String path = String.format("/api/seedlots/%s/application-info", seedlotNumber);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import ca.bc.gov.backendstartapi.entity.SeedlotParentTree;
import ca.bc.gov.backendstartapi.entity.SeedlotParentTreeGeneticQuality;
import ca.bc.gov.backendstartapi.entity.SeedlotParentTreeSmpMix;
import ca.bc.gov.backendstartapi.entity.SeedlotSeedPlanZoneEntity;
import ca.bc.gov.backendstartapi.entity.SeedlotSourceEntity;
import ca.bc.gov.backendstartapi.entity.SeedlotStatusEntity;
import ca.bc.gov.backendstartapi.entity.SmpMix;
Expand All @@ -48,6 +49,7 @@
import ca.bc.gov.backendstartapi.entity.seedlot.SeedlotOwnerQuantity;
import ca.bc.gov.backendstartapi.exception.ClientIdForbiddenException;
import ca.bc.gov.backendstartapi.exception.InvalidSeedlotRequestException;
import ca.bc.gov.backendstartapi.exception.SeedlotConflictDataException;
import ca.bc.gov.backendstartapi.exception.SeedlotNotFoundException;
import ca.bc.gov.backendstartapi.exception.SeedlotSourceNotFoundException;
import ca.bc.gov.backendstartapi.provider.Provider;
Expand Down Expand Up @@ -361,7 +363,12 @@ void findSingleSeedlotSuccessTest() {
Seedlot seedlotEntity = new Seedlot(seedlotId);

when(seedlotRepository.findById(seedlotId)).thenReturn(Optional.of(seedlotEntity));
when(seedlotSeedPlanZoneRepository.findAllBySeedlot_id(seedlotId)).thenReturn(List.of());

GeneticClassEntity classEntity = new GeneticClassEntity("A", "A class seedlot", DATE_RANGE);
SeedlotSeedPlanZoneEntity spzEntity =
new SeedlotSeedPlanZoneEntity(seedlotEntity, "AA", classEntity, true, "Description");
when(seedlotSeedPlanZoneRepository.findAllBySeedlot_id(seedlotId))
.thenReturn(List.of(spzEntity));
when(seedlotOrchardService.getPrimarySeedlotOrchard(seedlotId)).thenReturn(Optional.empty());

SeedlotGeneticWorth seedlotGenWor =
Expand Down Expand Up @@ -464,7 +471,7 @@ void getSeedlotByIdWithErrorFromOracleSpu() {
@DisplayName("A-Class Seedlot Form success test")
void findAclassSeedlotFormFullDataSuccessTest() {
String seedlotNumber = "0000000";
String onwerNumber = "1234";
String ownerNumber = "1234";
String ownerLoc = "01";
String methodOfPayment = "TEST";
Seedlot seedlotEntity = new Seedlot(seedlotNumber);
Expand Down Expand Up @@ -528,9 +535,9 @@ void findAclassSeedlotFormFullDataSuccessTest() {
List.of(new SeedlotCollectionMethod(seedlotEntity, new ConeCollectionMethodEntity()));

SeedlotOwnerQuantity seedlotOwners =
new SeedlotOwnerQuantity(seedlotEntity, onwerNumber, ownerLoc);
BigDecimal orginalPercOwned = new BigDecimal(100);
seedlotOwners.setOriginalPercentageOwned(orginalPercOwned);
new SeedlotOwnerQuantity(seedlotEntity, ownerNumber, ownerLoc);
BigDecimal originalPercentOwned = new BigDecimal(100);
seedlotOwners.setOriginalPercentageOwned(originalPercentOwned);
seedlotOwners.setMethodOfPayment(new MethodOfPaymentEntity(methodOfPayment, "", null));

String orchardId = "100";
Expand Down Expand Up @@ -592,7 +599,13 @@ void findAclassSeedlotFormFullDataSuccessTest() {
null, null, null, null, null, null, null, null, List.of(0)),
List.of(
new SeedlotFormOwnershipDto(
onwerNumber, ownerLoc, orginalPercOwned, null, null, methodOfPayment, null)),
ownerNumber,
ownerLoc,
originalPercentOwned,
null,
null,
methodOfPayment,
null)),
new SeedlotFormInterimDto(null, null, null, null, null, null),
new SeedlotFormOrchardDto(
orchardId, null, null, null, null, null, null, null, null, null),
Expand Down Expand Up @@ -642,12 +655,12 @@ void patchSeedlotFailByIdTest() {
when(seedlotRepository.findById(seedlotNumber)).thenReturn(Optional.empty());

SeedlotApplicationPatchDto testDto =
new SeedlotApplicationPatchDto("groot@wood.com", "CUS", false, false);
new SeedlotApplicationPatchDto("groot@wood.com", "CUS", false, false, 0);

Assertions.assertThrows(
SeedlotNotFoundException.class,
() -> {
seedlotService.patchApplicantionInfo(seedlotNumber, testDto);
seedlotService.patchApplicantInfo(seedlotNumber, testDto);
});
}

Expand All @@ -657,7 +670,7 @@ void patchSeedlotFailBySourceTest() {
String seedlotNumber = "123456";

SeedlotApplicationPatchDto testDto =
new SeedlotApplicationPatchDto("groot@wood.com", "PlanetX", false, false);
new SeedlotApplicationPatchDto("groot@wood.com", "PlanetX", false, false, 0);

when(seedlotRepository.findById(seedlotNumber))
.thenReturn(Optional.of(new Seedlot(seedlotNumber)));
Expand All @@ -668,7 +681,25 @@ void patchSeedlotFailBySourceTest() {
Assertions.assertThrows(
SeedlotSourceNotFoundException.class,
() -> {
seedlotService.patchApplicantionInfo(seedlotNumber, testDto);
seedlotService.patchApplicantInfo(seedlotNumber, testDto);
});
}

@Test
@DisplayName("Patch applicant info conflict should fail")
void patchApplicantInfo_conflict_shouldFail() {
String seedlotNumber = "123456";

when(seedlotRepository.findById(seedlotNumber))
.thenReturn(Optional.of(new Seedlot(seedlotNumber)));

SeedlotApplicationPatchDto testDto =
new SeedlotApplicationPatchDto("groot@wood.com", "PlanetX", false, false, 46);

Assertions.assertThrows(
SeedlotConflictDataException.class,
() -> {
seedlotService.patchApplicantInfo(seedlotNumber, testDto);
});
}

Expand All @@ -678,7 +709,7 @@ void patchSeedlotSuccessTest() {
String seedlotNumber = "123456";

SeedlotApplicationPatchDto testDto =
new SeedlotApplicationPatchDto("groot@wood.com", "CUS", true, false);
new SeedlotApplicationPatchDto("groot@wood.com", "CUS", true, false, 0);

Seedlot testSeedlot = new Seedlot(seedlotNumber);

Expand All @@ -690,7 +721,7 @@ void patchSeedlotSuccessTest() {
// Returns the seedlot that's about to be saved os we can compare the object.
when(seedlotRepository.save(any())).thenAnswer(i -> i.getArguments()[0]);

Seedlot patchedSeedlot = seedlotService.patchApplicantionInfo(seedlotNumber, testDto);
Seedlot patchedSeedlot = seedlotService.patchApplicantInfo(seedlotNumber, testDto);

assertEquals(testDto.applicantEmailAddress(), patchedSeedlot.getApplicantEmailAddress());
assertEquals(
Expand Down Expand Up @@ -772,7 +803,7 @@ void updateSeedlotWithForm_tscAdmin_happyPath_shouldSucceed() {
SeedlotFormInterimDto formInterimDto = mock(SeedlotFormInterimDto.class);

SeedlotApplicationPatchDto applicationPatchDto =
new SeedlotApplicationPatchDto("email@bcgov.ca", "A", true, true);
new SeedlotApplicationPatchDto("email@bcgov.ca", "A", true, true, 0);

SeedlotFormSubmissionDto form =
new SeedlotFormSubmissionDto(
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/types/SeedlotRegistrationTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BooleanInputType, OptionsInputType, StringInputType } from './FormInputType';
import {
BooleanInputType,
OptionsInputType,
StringInputType
} from './FormInputType';

/**
* The form data obj used in seedlot creation.
Expand Down Expand Up @@ -28,8 +32,9 @@ export type SeedlotRegPayloadType = {
}

export type SeedlotPatchPayloadType = {
applicantEmailAddress: string,
seedlotSourceCode: string,
toBeRegistrdInd: boolean,
bcSourceInd: boolean
applicantEmailAddress: string;
seedlotSourceCode: string;
toBeRegistrdInd: boolean;
bcSourceInd: boolean;
revisionCount: number | undefined;
}
27 changes: 19 additions & 8 deletions frontend/src/views/Seedlot/EditAClassApplication/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,23 @@ const EditAClassApplicationForm = ({ isReview, applicantData, setApplicantData }
) => patchSeedlotApplicationInfo(seedlotNumber ?? '', payload),
onSuccess: () => navigate(addParamToPath(ROUTES.SEEDLOT_DETAILS, seedlotNumber ?? '')),
onError: (err: AxiosError) => {
toast.error(
<ErrorToast
title="Save failure"
subtitle={`An unexpected error occurred while saving your edits. Please try again, and if the issue persists, contact support. ${err.code}: ${err.message}`}
/>,
ErrToastOption
);
if (err.response?.status === 409) {
toast.error(
<ErrorToast
title="Save failure"
subtitle="Unable to update Seedlot applicant information! The Seedlot record was updated by another user. Please refresh your page and try again!"
/>,
ErrToastOption
);
} else {
toast.error(
<ErrorToast
title="Save failure"
subtitle={`An unexpected error occurred while saving your edits. Please try again, and if the issue persists, contact support. ${err.code}: ${err.message}`}
/>,
ErrToastOption
);
}
}
});

Expand Down Expand Up @@ -153,7 +163,8 @@ const EditAClassApplicationForm = ({ isReview, applicantData, setApplicantData }
applicantEmailAddress: applicantData.email.value,
seedlotSourceCode: applicantData.sourceCode.value,
toBeRegistrdInd: applicantData.willBeRegistered.value,
bcSourceInd: applicantData.isBcSource.value
bcSourceInd: applicantData.isBcSource.value,
revisionCount: seedlotQuery.data?.revisionCount
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ const SeedlotReviewContent = () => {
applicantEmailAddress: applicantData.email.value,
seedlotSourceCode: applicantData.sourceCode.value,
toBeRegistrdInd: applicantData.willBeRegistered.value,
bcSourceInd: applicantData.isBcSource.value
bcSourceInd: applicantData.isBcSource.value,
revisionCount: seedlotData?.revisionCount
};

const seedlotReviewSeedPlanZones: SeedPlanZoneDto[] = [{
Expand Down

0 comments on commit 2b424af

Please sign in to comment.