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

implements-search-users-PublishedProjects #993

Merged
merged 1 commit into from
Apr 13, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,26 @@ export class PublishedProjectCrudService extends AppBaseService<

get serializerConfig(): JSONAPISerializerConfig<PublishedProject> {
return {
attributes: ['name', 'description', 'underModeration'],
attributes: ['name', 'description', 'underModeration', 'originalProject'],
keyForAttribute: 'camelCase',
originalProject: {
ref: 'id',
attributes: [
'name',
'description',
'countryId',
'adminAreaLevel1Id',
'adminAreaLevel2Id',
'planningUnitGridShape',
'planningUnitAreakm2',
'createdAt',
'lastModifiedAt',
'planningAreaId',
'planningAreaName',
'bbox',
'customProtectedAreas',
],
},
};
}

Expand All @@ -50,6 +68,18 @@ export class PublishedProjectCrudService extends AppBaseService<
): Promise<SelectQueryBuilder<PublishedProject>> {
const userId = info?.authenticatedUser?.id;

const { namesSearch } = info?.params ?? {};

if (namesSearch) {
const namesSearchFilterField = 'namesSearchFilter' as const;
query.andWhere(
`(${this.alias}.name
||' '|| COALESCE(${this.alias}.description, '')
) ILIKE :${namesSearchFilterField}`,
{ [namesSearchFilterField]: `%${namesSearch}%` },
);
}

/*
If we are listing projects for non-authenticated requests or for
authenticated users who are not admin, projects under moderation
Expand Down
9 changes: 9 additions & 0 deletions api/apps/api/src/modules/users/user-requests-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AppInfoDTO } from '@marxan-api/dto/info.dto';

export interface UsersRequest extends AppInfoDTO {
params?: {
featureClassAndAliasFilter?: string;
nameSearch?: string;
} & AppInfoDTO['params'];
}

33 changes: 28 additions & 5 deletions api/apps/api/src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { User, userResource } from './user.api.entity';
import { omit } from 'lodash';
import { CreateUserDTO } from './dto/create.user.dto';
import { UpdateUserDTO } from './dto/update.user.dto';
import { AppInfoDTO } from '@marxan-api/dto/info.dto';

import * as faker from 'faker';
import {
Expand All @@ -27,16 +26,18 @@ import { AppConfig } from '@marxan-api/utils/config.utils';
import { PlatformAdminEntity } from './platform-admin/admin.api.entity';
import { Either, left, right } from 'fp-ts/lib/Either';
import { FetchSpecification } from 'nestjs-base-service';
import { UsersRequest } from './user-requests-info';

export const forbiddenError = Symbol(`unauthorized access`);
export const badRequestError = Symbol(`operation not allowed`);
export const userNotFoundError = Symbol(`user not found in database`);

@Injectable()
export class UsersService extends AppBaseService<
User,
CreateUserDTO,
UpdateUserDTO,
AppInfoDTO
UsersRequest
> {
constructor(
@InjectRepository(User)
Expand Down Expand Up @@ -102,6 +103,28 @@ export class UsersService extends AppBaseService<
};
}

async extendFindAllQuery(
query: SelectQueryBuilder<User>,
fetchSpecification: FetchSpecification,
info?: UsersRequest,
): Promise<SelectQueryBuilder<User>> {
const { namesSearch } = info?.params ?? {};

if (namesSearch) {
const namesSearchFilterField = 'namesSearchFilter' as const;
query.andWhere(
`(${this.alias}.display_name
||' '|| COALESCE(${this.alias}.fname, '')
||' '|| COALESCE(${this.alias}.lname, '')
||' '|| COALESCE(${this.alias}.email, '')
) ILIKE :${namesSearchFilterField}`,
{ [namesSearchFilterField]: `%${namesSearch}%` },
);
}

return query;
}

async fakeFindOne(_id: string): Promise<Partial<User>> {
return {
...new User(),
Expand Down Expand Up @@ -182,7 +205,7 @@ export class UsersService extends AppBaseService<
async updateOwnPassword(
userId: string,
currentAndNewPasswords: UpdateUserPasswordDTO,
_info: AppInfoDTO,
_info: UsersRequest,
): Promise<void> {
const user = await this.getById(userId);
if (
Expand Down Expand Up @@ -215,7 +238,7 @@ export class UsersService extends AppBaseService<
async validateBeforeUpdate(
id: string,
updateModel: UpdateUserDTO,
_info?: AppInfoDTO,
_info?: UsersRequest,
): Promise<void> {
if (updateModel.password) {
throw new ForbiddenException(
Expand Down Expand Up @@ -245,7 +268,7 @@ export class UsersService extends AppBaseService<
async extendFindAllResults(
entitiesAndCount: [User[], number],
_fetchSpecification?: FetchSpecification,
_info?: AppInfoDTO,
_info?: UsersRequest,
): Promise<[User[], number]> {
const extendedEntities: Promise<User>[] = entitiesAndCount[0].map(
(entity) => this.extendGetByIdResult(entity),
Expand Down