Skip to content

Commit

Permalink
add ability to fetch public project metadata and to know if a project…
Browse files Browse the repository at this point in the history
… is public
  • Loading branch information
hotzevzl committed Mar 2, 2022
1 parent 24f1bdd commit 32a057e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
32 changes: 31 additions & 1 deletion api/apps/api/src/modules/projects/project.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { User } from '../users/user.api.entity';
import { Scenario } from '../scenarios/scenario.api.entity';
import {
AfterLoad,
Column,
Entity,
JoinColumn,
Expand All @@ -19,14 +20,15 @@ import { ProtectedAreaDto } from '@marxan-api/modules/projects/dto/protected-are
import { JsonApiAsyncJobMeta } from '@marxan-api/dto/async-job.dto';
import { ProjectBlm } from '@marxan-api/modules/blm/values/repositories/project-blm/project-blm.api.entity';
import { PlanningUnitGridShape } from '@marxan/scenarios-planning-unit';
import { PublishedProject } from '../published-project/entities/published-project.api.entity';

export const projectResource: BaseServiceResource = {
className: 'Project',
name: {
singular: 'project',
plural: 'projects',
},
entitiesAllowedAsIncludes: ['scenarios', 'users'],
entitiesAllowedAsIncludes: ['scenarios', 'users', 'publicMetadata'],
};

@Entity('projects')
Expand Down Expand Up @@ -166,6 +168,34 @@ export class Project extends TimeUserEntityMetadata {
type: ProtectedAreaDto,
})
customProtectedAreas?: ProtectedAreaDto[];

@ApiPropertyOptional({
description: 'Metadata of the project as published (if it was made public)',
})
@OneToOne(() => PublishedProject)
@JoinColumn({ name: 'id' })
publicMetadata?: PublishedProject;

@ApiPropertyOptional({
description:
'Whether this project is publicly available through the Community section',
})
isPublic?: boolean;

@AfterLoad()
setIsPublicProperty() {
/**
* 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.isPublic = this.publicMetadata ? true : undefined;
}
}

export class JSONAPIProjectData {
Expand Down
9 changes: 9 additions & 0 deletions api/apps/api/src/modules/projects/projects-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ProjectId, SetProjectGridFromShapefile } from './planning-unit-grid';
import { ProjectRoles } from '@marxan-api/modules/access-control/projects-acl/dto/user-role-project.dto';
import { Roles } from '@marxan-api/modules/access-control/role.api.entity';
import { PlanningUnitGridShape } from '@marxan/scenarios-planning-unit';
import { PublishedProject } from '../published-project/entities/published-project.api.entity';

const projectFilterKeyNames = [
'name',
Expand Down Expand Up @@ -70,6 +71,8 @@ export class ProjectsCrudService extends AppBaseService<
private readonly userProjects: Repository<UsersProjectsApiEntity>,
@InjectRepository(ProtectedArea, DbConnections.geoprocessingDB)
private readonly protectedAreas: Repository<ProtectedArea>,
@InjectRepository(PublishedProject)
private readonly publishedProjects: Repository<PublishedProject>,
private readonly commandBus: CommandBus,
) {
super(repository, 'project', 'projects', {
Expand All @@ -94,8 +97,14 @@ export class ProjectsCrudService extends AppBaseService<
'planningAreaName',
'bbox',
'customProtectedAreas',
'isPublic',
'publicMetadata',
],
keyForAttribute: 'camelCase',
publicMetadata: {
ref: 'id',
attributes: ['name', 'description'],
},
scenarios: {
ref: 'id',
attributes: [
Expand Down
12 changes: 4 additions & 8 deletions api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ export class ProjectsController {
@ImplementsAcl()
@TilesOpenApi()
@ApiOperation({
description:
'Get planning area grid tiles for user uploaded grid.',
description: 'Get planning area grid tiles for user uploaded grid.',
})
@ApiParam({
name: 'id',
Expand Down Expand Up @@ -351,8 +350,7 @@ export class ProjectsController {
@ImplementsAcl()
@TilesOpenApi()
@ApiOperation({
description:
'Get planning area tiles for uploaded planning area.',
description: 'Get planning area tiles for uploaded planning area.',
})
@ApiParam({
name: 'id',
Expand Down Expand Up @@ -381,8 +379,7 @@ export class ProjectsController {
@ImplementsAcl()
@TilesOpenApi()
@ApiOperation({
description:
'Get planning area tiles for project planning area.',
description: 'Get planning area tiles for project planning area.',
})
@ApiParam({
name: 'projectId',
Expand Down Expand Up @@ -427,8 +424,7 @@ export class ProjectsController {
@ImplementsAcl()
@TilesOpenApi()
@ApiOperation({
description:
'Get planning area grid tiles for project grid.',
description: 'Get planning area grid tiles for project grid.',
})
@ApiParam({
name: 'projectId',
Expand Down

0 comments on commit 32a057e

Please sign in to comment.