Skip to content

Commit

Permalink
Update cost surfaces piece exporter + format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel authored and hotzevzl committed Oct 6, 2023
1 parent 91f78a7 commit 3abab91
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
12 changes: 5 additions & 7 deletions api/apps/api/src/modules/cost-surface/cost-surface.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { CostSurface } from '@marxan-api/modules/cost-surface/cost-surface.api.e
import { Repository } from 'typeorm';
import { ProjectAclService } from '@marxan-api/modules/access-control/projects-acl/project-acl.service';
import { Either, left, right } from 'fp-ts/lib/Either';
import { projectNotEditable, projectNotVisible } from "@marxan-api/modules/projects/projects.service";
import {
projectNotEditable,
projectNotVisible,
} from '@marxan-api/modules/projects/projects.service';
import { UploadCostSurfaceShapefileDto } from '@marxan-api/modules/cost-surface/dto/upload-cost-surface-shapefile.dto';
import { UpdateCostSurfaceDto } from '@marxan-api/modules/cost-surface/dto/update-cost-surface.dto';
import { CostSurfaceCalculationPort } from '@marxan-api/modules/cost-surface/ports/project/cost-surface-calculation.port';
Expand Down Expand Up @@ -149,12 +152,7 @@ export class CostSurfaceService {
CostRange
>
> {
if (
!(await this.projectAclService.canViewProject(
userId,
projectId,
))
) {
if (!(await this.projectAclService.canViewProject(userId, projectId))) {
return left(projectNotVisible);
}
const costRange = await this.costSurfaceRepository.findOne({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export class ProjectCostSurfaceController {
@UseGuards(JwtAuthGuard)
@ApiParam({
name: 'costSurfaceId',
description: 'The id of the Cost Surface for which to retrieve [min,max] cost range',
description:
'The id of the Cost Surface for which to retrieve [min,max] cost range',
})
@ApiParam({
name: 'projectId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,22 @@ export class ProjectCostSurfacesPieceExporter implements ExportPieceProcessor {
const costSurfacesIds = costSurfaces.map((costSurface) => costSurface.id);
let costSurfaceData: CostSurfaceDataSelectResult[] = [];
let projectPusMap: Record<string, number> = {};
if (costSurfacesIds.length > 0) {
costSurfaceData = await this.geoprocessingEntityManager
.createQueryBuilder()
.select(['cost_surface_id', 'cost', 'projects_pu_id'])
.from('cost_surface_pu_data', 'scpd')
.where('cost_surface_id IN (:...costSurfacesIds)', {
costSurfacesIds,
})
.execute();

projectPusMap = await this.getProjectPusMap(input.resourceId);

if (!costSurfacesIds) {
const errorMessage = `${ProjectCostSurfacesPieceExporter.name} - Project Cost Surfaces - couldn't find cost surfaces for project ${input.resourceId}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
costSurfaceData = await this.geoprocessingEntityManager
.createQueryBuilder()
.select(['cost_surface_id', 'cost', 'projects_pu_id'])
.from('cost_surface_pu_data', 'scpd')
.where('cost_surface_id IN (:...costSurfacesIds)', {
costSurfacesIds,
})
.execute();

projectPusMap = await this.getProjectPusMap(input.resourceId);

const fileContent: ProjectCostSurfacesContent = {
costSurfaces: costSurfaces.map(({ id, ...costSurface }) => ({
Expand Down
12 changes: 7 additions & 5 deletions api/apps/geoprocessing/test/integration/cloning/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Readable, Transform } from 'stream';
import { DeepPartial, EntityManager, In } from 'typeorm';
import { v4 } from 'uuid';
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { CostSurfacePuDataEntity } from "@marxan/cost-surfaces";
import { CostSurfacePuDataEntity } from '@marxan/cost-surfaces';

export type TestSpecification = {
id: string;
Expand Down Expand Up @@ -604,8 +604,12 @@ export async function GivenCostSurfaces(
projectId: string,
) {
const costSurface = Array({
id: v4(), min, max, name, project_id: projectId,
})
id: v4(),
min,
max,
name,
project_id: projectId,
});

await Promise.all(
costSurface.map((values) =>
Expand All @@ -626,14 +630,12 @@ export async function GivenCostSurfaceData(
projectId: string,
costSurfaceId: string,
): Promise<{ id: string; hash: string; feature_id: string }[]> {

const projectPus = await GivenProjectPus(em, projectId, 10);
const insertValues = projectPus.map((pu, index) => ({
id: v4(),
costSurfaceId: costSurfaceId,
projectsPuId: pu.id,
cost: index + 1,

}));

const result = await em
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import { Readable } from 'stream';
import { EntityManager, In } from 'typeorm';
import { v4 } from 'uuid';
import {
DeleteProjectAndOrganization, GivenCostSurfaceData, GivenCostSurfaces,
DeleteProjectAndOrganization,
GivenCostSurfaceData,
GivenCostSurfaces,
GivenFeatures,
GivenFeaturesData,
GivenProjectExists,
readSavedFile
} from "../fixtures";
readSavedFile,
} from '../fixtures';
import { GeoCloningFilesRepositoryModule } from '@marxan-geoprocessing/modules/cloning-files-repository';
import { FakeLogger } from '@marxan-geoprocessing/utils/__mocks__/fake-logger';
import {
ProjectCostSurfacesPieceExporter
} from "@marxan-geoprocessing/export/pieces-exporters/project-cost-surfaces.piece-exporter";
import { CostSurfacePuDataEntity } from "@marxan/cost-surfaces";
import { ProjectCostSurfacesContent } from "@marxan/cloning/infrastructure/clone-piece-data/project-cost-surfaces";
import { ProjectCostSurfacesPieceExporter } from '@marxan-geoprocessing/export/pieces-exporters/project-cost-surfaces.piece-exporter';
import { CostSurfacePuDataEntity } from '@marxan/cost-surfaces';
import { ProjectCostSurfacesContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-cost-surfaces';

let fixtures: FixtureType<typeof getFixtures>;

Expand Down Expand Up @@ -78,10 +78,11 @@ const getFixtures = async () => {
const geoEntityManager: EntityManager = sandbox.get(
getEntityManagerToken(geoprocessingConnections.default),
);
const costSurfacesDataRepo = geoEntityManager.getRepository(CostSurfacePuDataEntity);
const costSurfacesDataRepo = geoEntityManager.getRepository(
CostSurfacePuDataEntity,
);
const fileRepository = sandbox.get(CloningFilesRepository);


return {
cleanUp: async () => {
await DeleteProjectAndOrganization(
Expand All @@ -90,7 +91,6 @@ const getFixtures = async () => {
organizationId,
);
await costSurfacesDataRepo.delete({});

},
GivenAProjectCostSurfacesExportJob: (): ExportJobInput => {
return {
Expand All @@ -114,14 +114,13 @@ const getFixtures = async () => {
GivenCostSurfacesForProject: async () => {
const costSurface = await GivenCostSurfaces(
apiEntityManager,
1, 10, 'Cost Surface', projectId
);

await GivenCostSurfaceData(
geoEntityManager,
1,
10,
'Cost Surface',
projectId,
costSurface.id,
);

await GivenCostSurfaceData(geoEntityManager, projectId, costSurface.id);
return costSurface.id;
},
GivenTagOnFeature: async (featureId: string, tag: string) =>
Expand All @@ -144,7 +143,9 @@ const getFixtures = async () => {
expect(content.costSurfaces).toHaveLength(2);
const costSurfacesExported = content.costSurfaces;

const nonDefaultCostSurface = costSurfacesExported.find((costSurface) => costSurface.name === 'Cost Surface')
const nonDefaultCostSurface = costSurfacesExported.find(
(costSurface) => costSurface.name === 'Cost Surface',
);
expect(nonDefaultCostSurface).toBeDefined();
expect(nonDefaultCostSurface?.data).toHaveLength(10);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ import { v4 } from 'uuid';
import {
DeleteProjectAndOrganization,
GenerateRandomGeometries,
GivenProjectExists, GivenProjectPus
} from "../fixtures";
GivenProjectExists,
GivenProjectPus,
} from '../fixtures';
import { GeoCloningFilesRepositoryModule } from '@marxan-geoprocessing/modules/cloning-files-repository';
import { FakeLogger } from '@marxan-geoprocessing/utils/__mocks__/fake-logger';
import {
ProjectCostSurfacesPieceImporter
} from "@marxan-geoprocessing/import/pieces-importers/project-cost-surfaces.piece-importer";
import { ProjectCostSurfacesContent } from "@marxan/cloning/infrastructure/clone-piece-data/project-cost-surfaces";
import { CostSurfacePuDataEntity } from "@marxan/cost-surfaces";
import {
ProjectCostSurfacesPieceExporter
} from "@marxan-geoprocessing/export/pieces-exporters/project-cost-surfaces.piece-exporter";
import { ProjectCostSurfacesPieceImporter } from '@marxan-geoprocessing/import/pieces-importers/project-cost-surfaces.piece-importer';
import { ProjectCostSurfacesContent } from '@marxan/cloning/infrastructure/clone-piece-data/project-cost-surfaces';
import { CostSurfacePuDataEntity } from '@marxan/cost-surfaces';
import { ProjectCostSurfacesPieceExporter } from '@marxan-geoprocessing/export/pieces-exporters/project-cost-surfaces.piece-exporter';

let fixtures: FixtureType<typeof getFixtures>;

Expand Down Expand Up @@ -138,8 +135,7 @@ const getFixtures = async () => {
},
GivenProject: () =>
GivenProjectExists(apiEntityManager, projectId, organizationId),
GivenProjectPus: () =>
GivenProjectPus(geoEntityManager, projectId, 3),
GivenProjectPus: () => GivenProjectPus(geoEntityManager, projectId, 3),
GivenJobInput: (archiveLocation: ArchiveLocation): ImportJobInput => {
const relativePath = ClonePieceRelativePathResolver.resolveFor(
ClonePiece.ProjectCostSurfaces,
Expand Down Expand Up @@ -178,7 +174,8 @@ const getFixtures = async () => {
);

validProjectCostSurfacesFile = {
costSurfaces: [{
costSurfaces: [
{
name: 'Cost Surface',
min: 1,
max: 10,
Expand All @@ -188,7 +185,8 @@ const getFixtures = async () => {
cost: dataIndex * 2,
puid: dataIndex + 1,
})),
}],
},
],
};

const exportId = v4();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ export const getFixtures = async (app: INestApplication) => {
},

cleanup: async () => {
await geoEntityManager.query(`DELETE FROM cost_surface_pu_data WHERE cost_surface_id = $1`, [costSurfaceId]);
await geoEntityManager.query(
`DELETE FROM cost_surface_pu_data WHERE cost_surface_id = $1`,
[costSurfaceId],
);
await geoEntityManager.query(`DELETE FROM projects_pu`);
await apiEntityManager.query(`DELETE FROM cost_surfaces WHERE id = $1`, [costSurfaceId]);
await apiEntityManager.query(`DELETE FROM cost_surfaces WHERE id = $1`, [
costSurfaceId,
]);
await apiEntityManager.query(`DELETE FROM projects`);
await apiEntityManager.query(`DELETE FROM organizations`);
},
Expand Down

1 comment on commit 3abab91

@vercel
Copy link

@vercel vercel bot commented on 3abab91 Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

marxan – ./

marxan-git-develop-vizzuality1.vercel.app
marxan23.vercel.app
marxan-vizzuality1.vercel.app

Please sign in to comment.