Skip to content

Commit

Permalink
Merge pull request #1768 from bcgov/bugfix/ALCS-2064
Browse files Browse the repository at this point in the history
Fix Application Decision Component PATCH not working for null values
  • Loading branch information
Abradat authored Jun 19, 2024
2 parents e1f2c7e + 6c6234b commit 650db03
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,46 @@ describe('ApplicationDecisionComponentService', () => {
expect(result[0].applicationDecisionComponentTypeCode).toEqual('fake_code');
});

it('should update only changed fields in DTO for a given UID', async() => {
mockApplicationDecisionComponentRepository.findOneOrFail.mockResolvedValue({
uuid: 'fake',
applicationDecisionComponentTypeCode: 'fake_code',
alrArea: 1,
agCap: '2',
agCapSource: '3',
agCapMap: '4',
agCapConsultant: '5',
endDate: new Date(0),
} as ApplicationDecisionComponent);

const mockDto = new CreateApplicationDecisionComponentDto();
mockDto.uuid = 'fake';
mockDto.alrArea = 10;
mockDto.applicationDecisionComponentTypeCode = 'should_not_beUpdated';

const result = await service.createOrUpdate([mockDto], false);
expect(result).toBeDefined();
expect(result.length).toBe(1);
expect(mockApplicationDecisionComponentRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockApplicationDecisionComponentRepository.findOneOrFail)
.toHaveBeenCalledWith({
where: { uuid: 'fake' },
relations: {
lots: true,
},
});

expect(result[0].uuid).toEqual(mockDto.uuid);
expect(result[0].alrArea).toEqual(mockDto.alrArea);
expect(result[0].agCap).toEqual('2');
expect(result[0].agCapSource).toEqual('3');
expect(result[0].agCapMap).toEqual('4');
expect(result[0].agCapConsultant).toEqual('5');
expect(result[0].endDate).toEqual(new Date(0));
expect(result[0].applicationDecisionComponentTypeCode).toEqual('fake_code');

});

it('should persist entity if persist flag is true', async () => {
mockApplicationDecisionComponentRepository.findOneOrFail.mockResolvedValue(
{} as ApplicationDecisionComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
CreateApplicationDecisionComponentDto,
} from './application-decision-component.dto';
import { ApplicationDecisionComponent } from './application-decision-component.entity';
import { filterUndefined } from '../../../../../utils/undefined';
import { formatIncomingDate } from '../../../../../utils/incoming-date.formatter';

@Injectable()
export class ApplicationDecisionComponentService {
Expand Down Expand Up @@ -44,15 +46,30 @@ export class ApplicationDecisionComponentService {
updateDto.applicationDecisionComponentTypeCode;
}

component.alrArea = updateDto.alrArea ?? component.alrArea;
component.agCap = updateDto.agCap ?? component.agCap;
component.agCapSource = updateDto.agCapSource ?? component.agCapSource;
component.agCapMap = updateDto.agCapMap ?? component.agCapMap;
component.agCapConsultant =
updateDto.agCapConsultant ?? component.agCapConsultant;
component.endDate2 = updateDto.endDate2
? new Date(updateDto.endDate2)
: component.endDate2;
component.alrArea = filterUndefined(
updateDto.alrArea,
component.alrArea
);
component.agCap = filterUndefined(
updateDto.agCap,
component.agCap
);
component.agCapSource = filterUndefined(
updateDto.agCapSource,
component.agCapSource
);
component.agCapMap = filterUndefined(
updateDto.agCapMap,
component.agCapMap
);
component.agCapConsultant = filterUndefined(
updateDto.agCapConsultant,
component.agCapConsultant
);
component.endDate2 = filterUndefined(
formatIncomingDate(updateDto.endDate2),
component.endDate2
);

this.patchNfuFields(component, updateDto);
this.patchTurpFields(component, updateDto);
Expand All @@ -66,9 +83,10 @@ export class ApplicationDecisionComponentService {
//INCL / EXCL
if (updateDto.inclExclApplicantType !== undefined) {
component.inclExclApplicantType = updateDto.inclExclApplicantType;
component.expiryDate = updateDto.expiryDate
? new Date(updateDto.expiryDate)
: component.expiryDate;
component.expiryDate = filterUndefined(
formatIncomingDate(updateDto.expiryDate),
component.expiryDate
);
}

updatedComponents.push(component);
Expand Down Expand Up @@ -139,70 +157,102 @@ export class ApplicationDecisionComponentService {
component: ApplicationDecisionComponent,
updateDto: CreateApplicationDecisionComponentDto,
) {
component.endDate = updateDto.endDate
? new Date(updateDto.endDate)
: component.endDate;
component.nfuSubType = updateDto.nfuSubType ?? component.nfuSubType;
component.nfuType = updateDto.nfuType ?? component.nfuType;
component.endDate = filterUndefined(
formatIncomingDate(updateDto.endDate),
component.endDate
);
component.nfuSubType = filterUndefined(
updateDto.nfuSubType,
component.nfuSubType
);
component.nfuType = filterUndefined(
updateDto.nfuType,
component.nfuType
);
}

private patchTurpFields(
component: ApplicationDecisionComponent,
updateDto: CreateApplicationDecisionComponentDto,
) {
component.expiryDate = updateDto.expiryDate
? new Date(updateDto.expiryDate)
: component.expiryDate;
component.expiryDate = filterUndefined(
formatIncomingDate(updateDto.expiryDate),
component.expiryDate
);
}

private patchPofoFields(
component: ApplicationDecisionComponent,
updateDto: CreateApplicationDecisionComponentDto,
) {
component.endDate = updateDto.endDate
? new Date(updateDto.endDate)
: component.endDate;
component.soilFillTypeToPlace =
updateDto.soilFillTypeToPlace ?? component.soilFillTypeToPlace;
component.soilToPlaceArea =
updateDto.soilToPlaceArea ?? component.soilToPlaceArea;
component.soilToPlaceVolume =
updateDto.soilToPlaceVolume ?? component.soilToPlaceVolume;
component.soilToPlaceMaximumDepth =
updateDto.soilToPlaceMaximumDepth ?? component.soilToPlaceMaximumDepth;
component.soilToPlaceAverageDepth =
updateDto.soilToPlaceAverageDepth ?? component.soilToPlaceAverageDepth;
component.endDate = filterUndefined(
formatIncomingDate(updateDto.endDate),
component.endDate
);
component.soilFillTypeToPlace = filterUndefined(
updateDto.soilFillTypeToPlace,
component.soilFillTypeToPlace
);
component.soilToPlaceArea = filterUndefined(
updateDto.soilToPlaceArea,
component.soilToPlaceArea
);
component.soilToPlaceVolume = filterUndefined(
updateDto.soilToPlaceVolume,
component.soilToPlaceVolume
);
component.soilToPlaceMaximumDepth = filterUndefined(
updateDto.soilToPlaceMaximumDepth,
component.soilToPlaceMaximumDepth
);
component.soilToPlaceAverageDepth = filterUndefined(
updateDto.soilToPlaceAverageDepth,
component.soilToPlaceAverageDepth
);
}

private patchRosoFields(
component: ApplicationDecisionComponent,
updateDto: CreateApplicationDecisionComponentDto,
) {
component.endDate = updateDto.endDate
? new Date(updateDto.endDate)
: component.endDate;
component.soilTypeRemoved =
updateDto.soilTypeRemoved ?? component.soilTypeRemoved;
component.soilToRemoveVolume =
updateDto.soilToRemoveVolume ?? component.soilToRemoveVolume;
component.soilToRemoveArea =
updateDto.soilToRemoveArea ?? component.soilToRemoveArea;
component.soilToRemoveMaximumDepth =
updateDto.soilToRemoveMaximumDepth ?? component.soilToRemoveMaximumDepth;
component.soilToRemoveAverageDepth =
updateDto.soilToRemoveAverageDepth ?? component.soilToRemoveAverageDepth;
component.endDate = filterUndefined(
formatIncomingDate(updateDto.endDate),
component.endDate
);
component.soilTypeRemoved = filterUndefined(
updateDto.soilTypeRemoved,
component.soilTypeRemoved
);
component.soilToRemoveVolume = filterUndefined(
updateDto.soilToRemoveVolume,
component.soilToRemoveVolume
);
component.soilToRemoveArea = filterUndefined(
updateDto.soilToRemoveArea,
component.soilToRemoveArea
);
component.soilToRemoveMaximumDepth = filterUndefined(
updateDto.soilToRemoveMaximumDepth,
component.soilToRemoveMaximumDepth
);
component.soilToRemoveAverageDepth = filterUndefined(
updateDto.soilToRemoveAverageDepth,
component.soilToRemoveAverageDepth
);
}

private patchNaruFields(
component: ApplicationDecisionComponent,
updateDto: CreateApplicationDecisionComponentDto,
) {
component.endDate = updateDto.endDate
? new Date(updateDto.endDate)
: component.endDate;
component.expiryDate = updateDto.expiryDate
? new Date(updateDto.expiryDate)
: component.expiryDate;
component.endDate = filterUndefined(
formatIncomingDate(updateDto.endDate),
component.endDate
);
component.expiryDate = filterUndefined(
formatIncomingDate(updateDto.expiryDate),
component.expiryDate
);
component.naruSubtypeCode = updateDto.naruSubtypeCode;
}

Expand Down

0 comments on commit 650db03

Please sign in to comment.