Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref/project metadata piece importer #1008

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe('ExportConfigReader', () => {
scenarios: [],
version: '1.0.0',
description: 'description',
isCloning: false,
exportId: v4(),
};
const zipFile = await fixtures.GivenArchiveWithInvalidExportConfig(
Expand All @@ -65,7 +64,6 @@ describe('ExportConfigReader', () => {
scenarios: [],
version: '1.0.0',
description: 'description',
isCloning: false,
exportId: v4(),
};
const zipFile = await fixtures.GivenArchiveWithInvalidExportConfig(
Expand All @@ -85,7 +83,6 @@ describe('ExportConfigReader', () => {
scenarios: [],
version: '1.0.0',
description: 'description',
isCloning: false,
exportId: v4(),
};
const zipFile = await fixtures.GivenArchiveWithInvalidExportConfig(
Expand Down Expand Up @@ -131,7 +128,6 @@ const getFixtures = async () => {
resourceKind: ResourceKind.Project,
scenarios: [{ id: v4(), name: 'random scenario' }],
version: exportVersion,
isCloning: false,
};

const sut = sandbox.get(ExportConfigReader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const getFixtures = async () => {
piece: ClonePiece.ProjectMetadata,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
WhenJobFinishes: async (input: ExportJobInput) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class SchedulePieceExportHandler
resourceId,
resourceKind,
allPieces,
isCloning: Boolean(importResourceId),
});

if (!job) {
Expand Down
1 change: 0 additions & 1 deletion api/apps/api/test/project/clone-project.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export const getFixtures = async () => {
const exportId = exportInstance.id.value;
if (piece.piece === ClonePiece.ExportConfig) {
const exportConfigContent: ProjectExportConfigContent = {
isCloning: true,
version: exportVersion,
name: 'random name',
description: 'random desc',
Expand Down
2 changes: 0 additions & 2 deletions api/apps/api/test/project/import-project.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export const getFixtures = async () => {
},
GivenImportFile: async () => {
const exportConfigContent: ProjectExportConfigContent = {
isCloning: false,
version: exportVersion,
name: 'random name',
description: 'random desc',
Expand All @@ -119,7 +118,6 @@ export const getFixtures = async () => {
);
const projectMetadataContent: ProjectMetadataContent = {
name: 'test project',
projectAlreadyCreated: false,
description: 'description',
};

Expand Down
1 change: 0 additions & 1 deletion api/apps/api/test/scenarios/clone-scenario.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export const getFixtures = async () => {
const exportId = exportInstance.id.value;
if (piece.piece === ClonePiece.ExportConfig) {
const exportConfigContent: ScenarioExportConfigContent = {
isCloning: true,
version: exportVersion,
name: 'random name',
description: 'random desc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class ExportConfigProjectPieceExporter implements ExportPieceProcessor {
resourceKind: input.resourceKind,
resourceId: projectId,
exportId: input.exportId,
isCloning: input.isCloning,
pieces: {
project: projectPieces,
scenarios: scenarioPieces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class ExportConfigScenarioPieceExporter implements ExportPieceProcessor {
resourceId: scenarioId,
exportId: input.exportId,
pieces: input.allPieces.map((elem) => elem.piece),
isCloning: true,
};

const relativePath = ClonePieceRelativePathResolver.resolveFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningAreaGAdm,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenAPlanningAreaGadmScenarioExportJob: () => {
Expand All @@ -117,7 +116,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningAreaGAdm,
resourceId: scenarioId,
resourceKind: ResourceKind.Scenario,
isCloning: false,
};
},
WhenProjectGadmDataIsMissing: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class ProjectMetadataPieceExporter implements ExportPieceProcessor {
name: projectData.name,
description: projectData.description,
planningUnitGridShape: projectData.planning_unit_grid_shape,
projectAlreadyCreated: input.isCloning,
};

const relativePath = ClonePieceRelativePathResolver.resolveFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export class ProjectMetadataPieceImporter implements ImportPieceProcessor {
.execute();
}

private async checkIfProjectExists(em: EntityManager, projectId: string) {
const [project]: [{ id: string }] = await em
.createQueryBuilder()
.select('id')
.from('projects', 'p')
.where('id = :projectId', { projectId })
.execute();

return Boolean(project);
}

async run(input: ImportJobInput): Promise<ImportJobOutput> {
const { uris, pieceResourceId, projectId, piece, ownerId } = input;

Expand Down Expand Up @@ -107,7 +118,11 @@ export class ProjectMetadataPieceImporter implements ImportPieceProcessor {
);

await this.entityManager.transaction(async (em) => {
if (projectMetadata.projectAlreadyCreated) {
const projectAlreadyCreated = await this.checkIfProjectExists(
em,
projectId,
);
if (projectAlreadyCreated) {
await this.updateProject(em, projectId, projectMetadata);
} else {
await this.createProject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ const getFixtures = async () => {
project: [ClonePiece.ProjectMetadata, ClonePiece.ExportConfig],
scenarios,
},
isCloning: false,
};
};

Expand Down Expand Up @@ -143,7 +142,6 @@ const getFixtures = async () => {
piece: ClonePiece.ExportConfig,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async (options = defaultFixtureOptions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const getFixtures = async () => {
resourceId: scenarioId,
exportId,
pieces: [ClonePiece.ScenarioMetadata, ClonePiece.ExportConfig],
isCloning: true,
};

return {
Expand All @@ -106,7 +105,6 @@ const getFixtures = async () => {
piece: ClonePiece.ExportConfig,
resourceId: scenarioId,
resourceKind: ResourceKind.Scenario,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ const getFixtures = async () => {
piece: ClonePiece.MarxanExecutionMetadata,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenMarxanExecutionMetadata: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningAreaCustomGeojson,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningAreaCustom,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningAreaGAdm,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectWithGadmArea: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningUnitsGridGeojson,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const getFixtures = async () => {
piece: ClonePiece.PlanningUnitsGrid,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ const getFixtures = async () => {
piece: ClonePiece.ProjectCustomFeatures,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ const getFixtures = async () => {
piece: ClonePiece.ProjectMetadata,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const getFixtures = async () => {
const expectedContent: ProjectMetadataContent = {
name: `test project - ${projectId}`,
planningUnitGridShape: PlanningUnitGridShape.Square,
projectAlreadyCreated: false,
};

return {
Expand All @@ -96,7 +95,6 @@ const getFixtures = async () => {
piece: ClonePiece.ProjectMetadata,
resourceId: projectId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenProjectExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ const getFixtures = async () => {
piece: ClonePiece.ScenarioFeaturesData,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenScenarioExist: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ const getFixtures = async () => {
piece: ClonePiece.FeaturesSpecification,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const getFixtures = async () => {
piece: ClonePiece.ScenarioMetadata,
resourceId: scenarioId,
resourceKind: ResourceKind.Scenario,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ const getFixtures = async () => {
piece: ClonePiece.ScenarioPlanningUnitsData,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ const getFixtures = async () => {
piece: ClonePiece.ScenarioProtectedAreas,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const getFixtures = async () => {
piece: ClonePiece.ScenarioRunResults,
resourceId: scenarioId,
resourceKind: ResourceKind.Project,
isCloning: false,
};
},
GivenScenarioExist: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ describe(ProjectMetadataPieceImporter, () => {
await fixtures.GivenOrganization();
await fixtures.GivenUser();

const archiveLocation = await fixtures.GivenValidProjectMetadataFile({
existingProject: false,
});
const archiveLocation = await fixtures.GivenValidProjectMetadataFile();
const input = fixtures.GivenJobInput(archiveLocation);
await fixtures
.WhenPieceImporterIsInvoked(input)
Expand All @@ -78,9 +76,7 @@ describe(ProjectMetadataPieceImporter, () => {
await fixtures.GivenUser();
await fixtures.GivenProject();

const archiveLocation = await fixtures.GivenValidProjectMetadataFile({
existingProject: true,
});
const archiveLocation = await fixtures.GivenValidProjectMetadataFile();
const input = fixtures.GivenJobInput(archiveLocation);
await fixtures
.WhenPieceImporterIsInvoked(input)
Expand Down Expand Up @@ -120,7 +116,6 @@ const getFixtures = async () => {
name: `test project - ${projectId}`,
description: 'project description',
planningUnitGridShape: PlanningUnitGridShape.Hexagon,
projectAlreadyCreated: false,
};

return {
Expand Down Expand Up @@ -168,11 +163,7 @@ const getFixtures = async () => {
GivenNoProjectMetadataFileIsAvailable: () => {
return new ArchiveLocation('not found');
},
GivenValidProjectMetadataFile: async (
{ existingProject } = { existingProject: false },
) => {
validProjectMetadataFileContent.projectAlreadyCreated = existingProject;

GivenValidProjectMetadataFile: async () => {
const exportId = v4();
const relativePath = ClonePieceRelativePathResolver.resolveFor(
ClonePiece.ProjectMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ class CommonFields {
@IsString()
@IsOptional()
description?: string;

@IsBoolean()
@IsOptional()
isCloning!: boolean;
}

class ScenarioMetadata {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export type ProjectMetadataContent = {
name: string;
description?: string;
planningUnitGridShape?: PlanningUnitGridShape;
projectAlreadyCreated: boolean;
};

export const projectMetadataRelativePath = 'project-metadata.json';
1 change: 0 additions & 1 deletion api/libs/cloning/src/job-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface ExportJobInput {
readonly resourceId: string;
readonly resourceKind: ResourceKind;
readonly piece: ClonePiece;
readonly isCloning: boolean;
readonly allPieces: { piece: ClonePiece; resourceId: string }[];
}

Expand Down