From b9de84905a58bf45dad4fc149f0530d8114a88ad Mon Sep 17 00:00:00 2001 From: andrea rota Date: Thu, 3 Mar 2022 10:23:24 +0000 Subject: [PATCH] simplify, with less magic: explicitly load relation --- .../api/src/modules/projects/project.api.entity.ts | 14 +++++++------- .../src/modules/projects/projects-crud.service.ts | 10 +++++----- .../input-parameter-file.provider.spec.ts | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/api/apps/api/src/modules/projects/project.api.entity.ts b/api/apps/api/src/modules/projects/project.api.entity.ts index 163fbad0e1..52a3aece9a 100644 --- a/api/apps/api/src/modules/projects/project.api.entity.ts +++ b/api/apps/api/src/modules/projects/project.api.entity.ts @@ -28,7 +28,7 @@ export const projectResource: BaseServiceResource = { singular: 'project', plural: 'projects', }, - entitiesAllowedAsIncludes: ['scenarios', 'users', 'publicMetadata'], + entitiesAllowedAsIncludes: ['scenarios', 'users'], }; @Entity('projects') @@ -187,12 +187,12 @@ export class Project extends TimeUserEntityMetadata { /** * This event listener relies on the `publicMetadata` relation to have been * loaded, which - when using QueryBuilder via BaseService - may mean that - * this will only happen if FetchSpecification's includes `publicMetadata` - * as a relation to include. Therefore, if we have no publicMetadata here, - * we cannot definitely say that the project is not public, but only that we - * don't know, as the project may well not be public, or it may be but - * publicMetadata has not been loaded -- hence the undefined here rather - * than an outright false. + * this will only happen if the relation has been explicitly loaded via + * `leftJoinAndSelect()` (or similar, as applicable). Therefore, if we have + * no publicMetadata here, we cannot definitely say that the project is not + * public, but only that we don't know, as the project may well not be + * public, or it may be but publicMetadata has not been loaded -- hence the + * undefined here rather than an outright false. */ this.isPublic = this.publicMetadata ? true : undefined; } diff --git a/api/apps/api/src/modules/projects/projects-crud.service.ts b/api/apps/api/src/modules/projects/projects-crud.service.ts index d34a8c380b..84fa067ced 100644 --- a/api/apps/api/src/modules/projects/projects-crud.service.ts +++ b/api/apps/api/src/modules/projects/projects-crud.service.ts @@ -98,13 +98,8 @@ export class ProjectsCrudService extends AppBaseService< 'bbox', 'customProtectedAreas', 'isPublic', - 'publicMetadata', ], keyForAttribute: 'camelCase', - publicMetadata: { - ref: 'id', - attributes: ['name', 'description'], - }, scenarios: { ref: 'id', attributes: [ @@ -330,6 +325,11 @@ export class ProjectsCrudService extends AppBaseService< `${this.alias}.id = acl.project_id`, ); + query.leftJoinAndSelect( + 'project.publicMetadata', + 'publicMetadata' + ); + if (namesSearch) { const nameSearchFilterField = 'nameSearchFilter' as const; query.leftJoin( diff --git a/api/apps/api/src/modules/scenarios/input-files/input-params/input-parameter-file.provider.spec.ts b/api/apps/api/src/modules/scenarios/input-files/input-params/input-parameter-file.provider.spec.ts index e38797992b..ae4d57c348 100644 --- a/api/apps/api/src/modules/scenarios/input-files/input-params/input-parameter-file.provider.spec.ts +++ b/api/apps/api/src/modules/scenarios/input-files/input-params/input-parameter-file.provider.spec.ts @@ -237,6 +237,7 @@ async function getFixtures() { values: [], defaults: [], }, + setIsPublicProperty: jest.fn(), }, status: JobStatus.done, type: ScenarioType.marxanWithZones,