Skip to content

Commit

Permalink
feat(gap-analysis): search over features (#589)
Browse files Browse the repository at this point in the history
* feat(gap-analysis): search over features

* feat(gap-analysis): search over features on post-gap
  • Loading branch information
kgajowy authored Oct 21, 2021
1 parent 185f132 commit 8472605
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { In, Repository, SelectQueryBuilder } from 'typeorm';
import { Brackets, In, Repository, SelectQueryBuilder } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { FiltersSpecification } from 'nestjs-base-service';
Expand Down Expand Up @@ -45,6 +45,48 @@ export class ScenarioFeaturesGapDataService extends AppBaseService<
return query;
}

async extendFindAllQuery(
query: SelectQueryBuilder<ScenarioFeaturesGapData>,
filters?: FiltersSpecification,
info?: UserSearchCriteria,
): Promise<SelectQueryBuilder<ScenarioFeaturesGapData>> {
// DEBT same issue as
// `api/apps/api/src/modules/geo-features/geo-features.service.ts`
// https://github.com/Vizzuality/marxan-cloud/pull/572/files#diff-2b4e531cc05bd4686fb0fc6e5cbf9fd3e2684e40a818179750dc8095bc93d49dR138-R149

if (!info?.params?.searchPhrase) {
return query;
}

const featuresIds = (
await this.features
.createQueryBuilder('features')
.select('features.id')
.where(
new Brackets((orBuilder) =>
orBuilder
.where('feature_class_name like :phrase', {
phrase: `%${info?.params?.searchPhrase}%`,
})
.orWhere('alias like :phrase', {
phrase: `%${info?.params?.searchPhrase}%`,
}),
),
)
.getMany()
).map((feature) => feature.id);

if (featuresIds.length > 0) {
query.andWhere(`feature_id IN (:...featuresIds)`, {
featuresIds,
});
} else {
query.andWhere(`false`);
}

return query;
}

async extendFindAllResults(
entitiesAndCount: [any[], number],
): Promise<[any[], number]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { In, Repository, SelectQueryBuilder } from 'typeorm';
import { Brackets, In, Repository, SelectQueryBuilder } from 'typeorm';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
AppBaseService,
JSONAPISerializerConfig,
} from '../../utils/app-base.service';

import { ScenarioFeaturesOutputGapData } from '@marxan/features';
import {
ScenarioFeaturesGapData,
ScenarioFeaturesOutputGapData,
} from '@marxan/features';
import { UserSearchCriteria } from './search-criteria';
import { AppConfig } from '../../utils/config.utils';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { GeoFeature } from '../geo-features/geo-feature.api.entity';
import { isInt, isPositive } from 'class-validator';
import { FiltersSpecification } from 'nestjs-base-service';

const scenarioFeaturesOutputGapDataFilterKeyNames = ['runId'] as const;
type ScenarioFeaturesOutputGapDataFilterKeys = keyof Pick<
Expand Down Expand Up @@ -69,6 +73,48 @@ export class ScenarioFeaturesOutputGapDataService extends AppBaseService<
return query;
}

async extendFindAllQuery(
query: SelectQueryBuilder<ScenarioFeaturesOutputGapData>,
filters?: FiltersSpecification,
info?: UserSearchCriteria,
): Promise<SelectQueryBuilder<ScenarioFeaturesOutputGapData>> {
// DEBT same issue as
// `api/apps/api/src/modules/geo-features/geo-features.service.ts`
// https://github.com/Vizzuality/marxan-cloud/pull/572/files#diff-2b4e531cc05bd4686fb0fc6e5cbf9fd3e2684e40a818179750dc8095bc93d49dR138-R149

if (!info?.params?.searchPhrase) {
return query;
}

const featuresIds = (
await this.features
.createQueryBuilder('features')
.select('features.id')
.where(
new Brackets((orBuilder) =>
orBuilder
.where('feature_class_name like :phrase', {
phrase: `%${info?.params?.searchPhrase}%`,
})
.orWhere('alias like :phrase', {
phrase: `%${info?.params?.searchPhrase}%`,
}),
),
)
.getMany()
).map((feature) => feature.id);

if (featuresIds.length > 0) {
query.andWhere(`feature_id IN (:...featuresIds)`, {
featuresIds,
});
} else {
query.andWhere(`false`);
}

return query;
}

async extendFindAllResults(
entitiesAndCount: [any[], number],
): Promise<[any[], number]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { User } from '../users/user.api.entity';
export interface SearchCriteria<T> extends InfoDTO<T> {
params?: {
scenarioId?: string;
searchPhrase?: string;
};
}

Expand Down
4 changes: 4 additions & 0 deletions api/apps/api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,14 @@ export class ScenariosController {
async getScenarioFeaturesGapData(
@Param('id', ParseUUIDPipe) id: string,
@ProcessFetchSpecification() fetchSpecification: FetchSpecification,
@Query('q') featureClassAndAliasFilter?: string,
): Promise<Partial<ScenarioFeaturesGapData>[]> {
const result = await this.scenarioFeaturesGapDataService.findAllPaginated(
fetchSpecification,
{
params: {
scenarioId: id,
searchPhrase: featureClassAndAliasFilter,
},
},
);
Expand Down Expand Up @@ -591,12 +593,14 @@ export class ScenariosController {
async getScenarioFeaturesOutputGapData(
@Param('id', ParseUUIDPipe) id: string,
@ProcessFetchSpecification() fetchSpecification: FetchSpecification,
@Query('q') featureClassAndAliasFilter?: string,
): Promise<Partial<ScenarioFeaturesOutputGapData>[]> {
const result = await this.scenarioFeaturesOutputGapDataService.findAllPaginated(
fetchSpecification,
{
params: {
scenarioId: id,
searchPhrase: featureClassAndAliasFilter,
},
},
);
Expand Down

0 comments on commit 8472605

Please sign in to comment.