-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projects): allow projects to be public
- Loading branch information
Showing
10 changed files
with
239 additions
and
55 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
api/apps/api/src/migrations/api/1629877107141-PublicProjects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class PublicProjects1629877107141 implements MigrationInterface { | ||
name = 'PublicProjects1629877107141'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "projects" | ||
ADD "is_public" boolean NOT NULL DEFAULT false`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "projects" | ||
DROP COLUMN "is_public"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
api/apps/api/src/modules/projects/projects-listing.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
applyDecorators, | ||
Controller, | ||
Get, | ||
Query, | ||
Req, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiBearerAuth, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiQuery, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { | ||
FetchSpecification, | ||
ProcessFetchSpecification, | ||
} from 'nestjs-base-service'; | ||
|
||
import { JSONAPIQueryParams } from '@marxan-api/decorators/json-api-parameters.decorator'; | ||
import { RequestWithAuthenticatedUser } from '@marxan-api/app.controller'; | ||
import { apiGlobalPrefixes } from '@marxan-api/api.config'; | ||
import { JwtAuthGuard } from '@marxan-api/guards/jwt-auth.guard'; | ||
|
||
import { projectResource, ProjectResultPlural } from './project.api.entity'; | ||
import { ProjectsService } from './projects.service'; | ||
import { ProjectSerializer } from './dto/project.serializer'; | ||
|
||
@ApiTags(projectResource.className) | ||
@Controller(`${apiGlobalPrefixes.v1}/projects`) | ||
export class ProjectsListingController { | ||
constructor( | ||
private readonly projectsService: ProjectsService, | ||
private readonly projectSerializer: ProjectSerializer, | ||
) {} | ||
|
||
@ApiBearerAuth() | ||
@UseGuards(JwtAuthGuard) | ||
@ProjectsListing() | ||
@Get() | ||
async findAll( | ||
@ProcessFetchSpecification() fetchSpecification: FetchSpecification, | ||
@Req() req: RequestWithAuthenticatedUser, | ||
@Query('q') namesSearch?: string, | ||
): Promise<ProjectResultPlural> { | ||
const results = await this.projectsService.findAll(fetchSpecification, { | ||
params: { | ||
namesSearch, | ||
}, | ||
authenticatedUser: req.user, | ||
}); | ||
return this.projectSerializer.serialize(results.data, results.metadata); | ||
} | ||
|
||
@ProjectsListing() | ||
@Get(`published`) | ||
async findAllPublic( | ||
@ProcessFetchSpecification() fetchSpecification: FetchSpecification, | ||
@Query('q') namesSearch?: string, | ||
): Promise<ProjectResultPlural> { | ||
const results = await this.projectsService.findAll(fetchSpecification, { | ||
params: { | ||
namesSearch, | ||
}, | ||
}); | ||
return this.projectSerializer.serialize(results.data, results.metadata); | ||
} | ||
} | ||
|
||
function ProjectsListing() { | ||
return applyDecorators( | ||
...[ | ||
ApiOperation({ | ||
description: 'Find all projects', | ||
}), | ||
ApiOkResponse({ type: ProjectResultPlural }), | ||
JSONAPIQueryParams({ | ||
entitiesAllowedAsIncludes: projectResource.entitiesAllowedAsIncludes, | ||
availableFilters: [ | ||
{ name: 'name' }, | ||
{ name: 'organizationId' }, | ||
{ name: 'countryId' }, | ||
{ name: 'adminAreaLevel1Id' }, | ||
{ name: 'adminAreaLevel21Id' }, | ||
], | ||
}), | ||
ApiQuery({ | ||
name: 'q', | ||
required: false, | ||
description: `A free search over names`, | ||
}), | ||
], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { FixtureType } from '@marxan/utils/tests/fixture-type'; | ||
import { getFixtures } from './public-projects.fixtures'; | ||
|
||
let fixtures: FixtureType<typeof getFixtures>; | ||
|
||
beforeEach(async () => { | ||
fixtures = await getFixtures(); | ||
}); | ||
afterEach(async () => { | ||
await fixtures?.cleanup(); | ||
}); | ||
|
||
test(`getting public projects while none is available`, async () => { | ||
await fixtures.GivenPrivateProjectWasCreated(); | ||
const response = await fixtures.WhenGettingPublicProjects(); | ||
fixtures.ThenNoProjectIsAvailable(response); | ||
}); | ||
|
||
test(`getting public projects`, async () => { | ||
const publicProjectId = await fixtures.GivenPublicProjectWasCreated(); | ||
await fixtures.GivenPrivateProjectWasCreated(); | ||
const response = await fixtures.WhenGettingPublicProjects(); | ||
fixtures.ThenPublicProjectIsAvailable(publicProjectId, response); | ||
}); |
Oops, something went wrong.