Skip to content

Commit

Permalink
fix(FeatureAmountsExporter): Workaround for exporting Feature Amounts…
Browse files Browse the repository at this point in the history
… when a feature has been previously deleted
  • Loading branch information
KevSanchez committed Dec 19, 2023
1 parent fa16ff4 commit 3279901
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,28 @@ export class ProjectFeatureAmountsPerPlanningUnitPieceExporter
featureAmountPerPlanningUnit: FeatureAmountPerProjectPlanningUnit[],
projectId: string,
) {
const featureIds = featureAmountPerPlanningUnit.map(
({ featureId }) => featureId,
const featureIds = new Set(
featureAmountPerPlanningUnit.map(({ featureId }) => featureId),
);

const featuresById = await this.getFeaturesById(featureIds);
const featuresById = await this.getFeaturesById([...featureIds]);

const projectPusById = await this.getProjectPusById(projectId);

return featureAmountPerPlanningUnit.map(
({ featureId, amount, projectPuId }) => {
// @TODO: Because the feature_amounts_per_planning_unit table is not accounted for in the clean up task, it can happen
// that when exporting after having deleted a feature, in the mapping below the feature info would not be found and
// cause an error. Because it's not clear whether deleting the feature_amount_per_pu records of the dangling feature
// in the clean up task has any implications, to avoid this error, the missing features are discarded.
return featureAmountPerPlanningUnit
.filter(({ featureId }) => featuresById[featureId])
.map(({ featureId, amount, projectPuId }) => {
const feature = featuresById[featureId];
return {
amount,
puid: projectPusById[projectPuId],
featureName: feature.featureName,
isCustom: feature.isCustom,
};
},
);
});
}

private async getFeaturesById(featureIds: string[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ let fixtures: FixtureType<typeof getFixtures>;
describe(ProjectFeatureAmountsPerPlanningUnitPieceExporter, () => {
beforeEach(async () => {
fixtures = await getFixtures();
}, 10_000);
}, 100000);

afterEach(async () => {
await fixtures?.cleanUp();
});
}, 100000);

it("saves an empty file when project doesn't have neither derived features and puvspr calculations", async () => {
const input =
Expand All @@ -69,6 +69,7 @@ describe(ProjectFeatureAmountsPerPlanningUnitPieceExporter, () => {
await fixtures.GivenProjectExist();
const { customFeature } =
await fixtures.GivenACustomAndAPlatformFeatureForProject();
await fixtures.GivenProjectHasPlanningUnits();
await fixtures.GivenProjectHasFeatureAmountsPerPlanningUnit(
customFeature.id,
);
Expand All @@ -85,6 +86,7 @@ describe(ProjectFeatureAmountsPerPlanningUnitPieceExporter, () => {
await fixtures.GivenProjectExist();
const { platformFeature } =
await fixtures.GivenACustomAndAPlatformFeatureForProject();
await fixtures.GivenProjectHasPlanningUnits();
await fixtures.GivenProjectHasFeatureAmountsPerPlanningUnit(
platformFeature.id,
);
Expand Down Expand Up @@ -128,6 +130,24 @@ describe(ProjectFeatureAmountsPerPlanningUnitPieceExporter, () => {
customFeature,
);
});

it('saves successfully discarding feature amount with no corresponding feature found on the api', async () => {
const input =
fixtures.GivenAProjectFeatureAmountsPerPlanningUnitExportJob();
await fixtures.GivenProjectExist();
const { customFeature } =
await fixtures.GivenACustomAndAPlatformFeatureForProject();
await fixtures.GivenProjectHasPlanningUnits();
await fixtures.GivenProjectHasFeatureAmountsPerPlanningUnit(
customFeature.id,
);
await fixtures.GivenProjectHasFeatureAmountsPerPlanningUnit(v4());
await fixtures
.WhenPieceExporterIsInvoked(input)
.ThenFeatureAmountsPerPlanningUnitFileHasFeatureAmountsPerPlanningUnitForFeature(
customFeature,
);
});
});

const getFixtures = async () => {
Expand Down Expand Up @@ -251,21 +271,25 @@ const getFixtures = async () => {
return derivedFeature;
},
GivenNoDerivedFeaturesForProject: () => {},
GivenProjectHasFeatureAmountsPerPlanningUnit: async (featureId: string) => {
GivenProjectHasPlanningUnits: async () => {
projectPus = await GivenProjectPus(
geoEntityManager,
projectId,
amountOfPuvsrCalculationsPerFeature,
);
},
GivenProjectHasFeatureAmountsPerPlanningUnit: async (featureId: string) => {
const featureAmountsPerPlanningUnit = Array(
amountOfPuvsrCalculationsPerFeature,
)
.fill(0)
.map((_, index) => ({
amount: expectedAmountPerPu,
featureId,
projectPuId: projectPus[index].id,
}));
.map((_, index) => {
return {
amount: expectedAmountPerPu,
featureId,
projectPuId: projectPus[index].id,
};
});
return featureAmountsPerPlanningUnitRepo.saveAmountPerPlanningUnitAndFeature(
projectId,
featureAmountsPerPlanningUnit,
Expand Down

0 comments on commit 3279901

Please sign in to comment.