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

feat(api): add q-search in projects #336

Merged
merged 1 commit into from
Jul 19, 2021
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
38 changes: 38 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 @@ -16,6 +16,8 @@ import {
import { AdminAreasService } from '@marxan-api/modules/admin-areas/admin-areas.service';
import { CountriesService } from '@marxan-api/modules/countries/countries.service';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity';
import { User } from '@marxan-api/modules/users/user.api.entity';
import { FetchSpecification } from 'nestjs-base-service';
import {
MultiplePlanningAreaIds,
Expand All @@ -35,6 +37,12 @@ type ProjectFilterKeys = keyof Pick<
>;
type ProjectFilters = Record<ProjectFilterKeys, string[]>;

export type ProjectsInfoDTO = AppInfoDTO & {
params?: {
nameSearch?: string;
};
};

@Injectable()
export class ProjectsCrudService extends AppBaseService<
Project,
Expand Down Expand Up @@ -233,6 +241,36 @@ export class ProjectsCrudService extends AppBaseService<
return entity;
}

async extendFindAllQuery(
query: SelectQueryBuilder<Project>,
fetchSpecification: FetchSpecification,
info?: ProjectsInfoDTO,
): Promise<SelectQueryBuilder<Project>> {
const { namesSearch } = info?.params ?? {};
if (namesSearch) {
const nameSearchFilterField = 'nameSearchFilter' as const;
query.leftJoin(
GeoFeature,
'geofeature',
`${this.alias}.id = geofeature.project_id`,
);
query.leftJoin(User, 'user', `${this.alias}.createdBy = user.id`);
query.andWhere(
`(
${this.alias}.name
||' '|| COALESCE(geofeature.description, '')
||' '|| COALESCE(geofeature.feature_class_name, '')
||' '|| COALESCE(user.fname, '')
||' '|| COALESCE(user.lname, '')
||' '|| COALESCE(user.display_name, '')
) ILIKE :${nameSearchFilterField}`,
{ [nameSearchFilterField]: `%${namesSearch}%` },
);
}

return query;
}

async extendFindAllResults(
entitiesAndCount: [Project[], number],
_fetchSpecification?: FetchSpecification,
Expand Down
13 changes: 12 additions & 1 deletion api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ApiNoContentResponse,
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
Expand Down Expand Up @@ -128,11 +129,21 @@ export class ProjectsController {
{ name: 'adminAreaLevel21Id' },
],
})
@ApiQuery({
name: 'q',
required: false,
description: `A free search over names`,
})
@Get()
async findAll(
@ProcessFetchSpecification() fetchSpecification: FetchSpecification,
@Query('q') namesSearch?: string,
): Promise<ProjectResultPlural> {
const results = await this.projectsService.findAll(fetchSpecification);
const results = await this.projectsService.findAll(fetchSpecification, {
params: {
namesSearch,
},
});
return this.projectSerializer.serialize(results.data, results.metadata);
}

Expand Down
6 changes: 3 additions & 3 deletions api/apps/api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppInfoDTO } from '@marxan-api/dto/info.dto';

import { GeoFeaturesService } from '@marxan-api/modules/geo-features/geo-features.service';

import { ProjectsCrudService } from './projects-crud.service';
import { ProjectsCrudService, ProjectsInfoDTO } from './projects-crud.service';
import { JobStatusService } from './job-status';
import { ProtectedAreasFacade } from './protected-areas/protected-areas.facade';
import { Project } from './project.api.entity';
Expand Down Expand Up @@ -32,9 +32,9 @@ export class ProjectsService {
return this.geoCrud.findAllPaginated(fetchSpec, appInfo);
}

async findAll(fetchSpec: FetchSpecification, _?: AppInfoDTO) {
async findAll(fetchSpec: FetchSpecification, info?: ProjectsInfoDTO) {
// /ACL slot/
return this.projectsCrud.findAllPaginated(fetchSpec);
return this.projectsCrud.findAllPaginated(fetchSpec, info);
}

async findOne(id: string) {
Expand Down
11 changes: 11 additions & 0 deletions api/apps/api/test/projects.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ describe('ProjectsModule (e2e)', () => {
expect(jsonAPIResponse.data[0].type).toBe('projects');
});

test('A user should be able to get a list of projects with q param', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/projects?q=User')
.set('Authorization', `Bearer ${jwtToken}`)
.expect(200);

const jsonAPIResponse: ProjectResultPlural = response.body;

expect(jsonAPIResponse.data[0].type).toBe('projects');
});

test('A user should be get a list of projects without any included relationships if these have not been requested', async () => {
const response = await request(app.getHttpServer())
.get('/api/v1/projects')
Expand Down