Skip to content

Commit

Permalink
simplify, with less magic: explicitly load relation
Browse files Browse the repository at this point in the history
  • Loading branch information
hotzevzl committed Mar 3, 2022
1 parent 32a057e commit b9de849
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
14 changes: 7 additions & 7 deletions api/apps/api/src/modules/projects/project.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const projectResource: BaseServiceResource = {
singular: 'project',
plural: 'projects',
},
entitiesAllowedAsIncludes: ['scenarios', 'users', 'publicMetadata'],
entitiesAllowedAsIncludes: ['scenarios', 'users'],
};

@Entity('projects')
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions api/apps/api/src/modules/projects/projects-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ async function getFixtures() {
values: [],
defaults: [],
},
setIsPublicProperty: jest.fn(),
},
status: JobStatus.done,
type: ScenarioType.marxanWithZones,
Expand Down

0 comments on commit b9de849

Please sign in to comment.