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

SPSH-911 add sorting to find-organizations endpoint #697

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 20 additions & 1 deletion src/modules/organisation/api/find-organisation-query.param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { AutoMap } from '@automapper/classes';
import { PagedQueryParams } from '../../../shared/paging/index.js';
import { ArrayUnique, IsEnum, IsOptional, IsString, IsUUID } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { OrganisationsTyp, OrganisationsTypName } from '../domain/organisation.enums.js';
import { OrganisationSortField, OrganisationsTyp, OrganisationsTypName } from '../domain/organisation.enums.js';
import { RollenSystemRecht, RollenSystemRechtTypName } from '../../rolle/domain/rolle.enums.js';
import { TransformToArray } from '../../../shared/util/array-transform.validator.js';
import { SortingOrder } from '../../../shared/domain/order.enums.js';

export class FindOrganisationQueryParams extends PagedQueryParams {
@AutoMap()
Expand Down Expand Up @@ -99,4 +100,22 @@ export class FindOrganisationQueryParams extends PagedQueryParams {
'Liefert Organisationen mit den angegebenen IDs, selbst wenn andere Filterkriterien nicht zutreffen (ODER-verknüpft mit anderen Kriterien).',
})
public readonly organisationIds?: string[];

@IsOptional()
@ApiProperty({
required: false,
nullable: true,
isArray: true,
description: 'Sortierung der Organisationen.',
})
public readonly sortField?: OrganisationSortField;

@IsOptional()
@ApiProperty({
required: false,
nullable: true,
isArray: true,
description: 'Sortierung der Organisationen.',
})
public readonly sortOrder?: SortingOrder;
}
78 changes: 77 additions & 1 deletion src/modules/organisation/api/organisation.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Test, TestingModule } from '@nestjs/testing';

import { DoFactory, ConfigTestModule } from '../../../../test/utils/index.js';
import { Paged } from '../../../shared/paging/paged.js';
import { OrganisationsTyp, Traegerschaft } from '../domain/organisation.enums.js';
import { OrganisationSortField, OrganisationsTyp, Traegerschaft } from '../domain/organisation.enums.js';
import { CreateOrganisationBodyParams } from './create-organisation.body.params.js';
import { FindOrganisationQueryParams } from './find-organisation-query.param.js';
import { OrganisationByIdParams } from './organisation-by-id.params.js';
Expand All @@ -30,6 +30,7 @@ import { OrganisationByNameQueryParams } from './organisation-by-name.query.js';
import { DBiamPersonenkontextRepo } from '../../personenkontext/persistence/dbiam-personenkontext.repo.js';
import { ParentOrganisationenResponse } from './organisation.parents.response.js';
import { ParentOrganisationsByIdsBodyParams } from './parent-organisations-by-ids.body.params.js';
import { SortingOrder } from '../../../shared/domain/order.enums.js';

function getFakeParamsAndBody(): [OrganisationByIdParams, OrganisationByIdBodyParams] {
const params: OrganisationByIdParams = {
Expand Down Expand Up @@ -317,6 +318,81 @@ describe('OrganisationController', () => {

expect(result.items.length).toEqual(3);
});

it('should find all organizations and include sorting params', async () => {
const organisationIds: string[] = [faker.string.uuid(), faker.string.uuid()];

const queryParams: FindOrganisationQueryParams = {
typ: OrganisationsTyp.SONSTIGE,
searchString: faker.lorem.word(),
systemrechte: [],
administriertVon: [faker.string.uuid(), faker.string.uuid()],
// Assuming you have a field for organisationIds in your query params
organisationIds: organisationIds,
sortField: OrganisationSortField.NAME,
sortOrder: SortingOrder.ASC,
};

const selectedOrganisationMap: Map<string, Organisation<true>> = new Map(
organisationIds.map((id: string) => [
id,
DoFactory.createOrganisationAggregate(true, {
id: id,
createdAt: faker.date.recent(),
updatedAt: faker.date.recent(),
administriertVon: faker.string.uuid(),
zugehoerigZu: faker.string.uuid(),
kennung: faker.lorem.word(),
name: faker.lorem.word(),
namensergaenzung: faker.lorem.word(),
kuerzel: faker.lorem.word(),
typ: OrganisationsTyp.SCHULE,
traegerschaft: Traegerschaft.LAND,
}),
]),
);

const mockedRepoResponse: Counted<Organisation<true>> = [
[
DoFactory.createOrganisationAggregate(true, {
id: faker.string.uuid(),
createdAt: faker.date.recent(),
updatedAt: faker.date.recent(),
administriertVon: faker.string.uuid(),
zugehoerigZu: faker.string.uuid(),
kennung: faker.lorem.word(),
name: faker.lorem.word(),
namensergaenzung: faker.lorem.word(),
kuerzel: faker.lorem.word(),
typ: OrganisationsTyp.SCHULE,
traegerschaft: Traegerschaft.LAND,
}),
...selectedOrganisationMap.values(),
],
selectedOrganisationMap.size + 1,
];

const permissionsMock: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();

organisationRepositoryMock.findAuthorized.mockResolvedValue(mockedRepoResponse);

const result: Paged<OrganisationResponse> = await organisationController.findOrganizations(
queryParams,
permissionsMock,
);

expect(organisationRepositoryMock.findAuthorized).toHaveBeenCalledTimes(1);
expect(organisationRepositoryMock.findAuthorized).toHaveBeenCalledWith(
permissionsMock,
queryParams.systemrechte,
{
...queryParams,
sort: [{ field: OrganisationSortField.NAME, order: SortingOrder.ASC }],
},
);

expect(result.items.length).toEqual(3);
});
});
});

Expand Down
11 changes: 9 additions & 2 deletions src/modules/organisation/api/organisation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { FindOrganisationQueryParams } from './find-organisation-query.param.js'
import { OrganisationByIdParams } from './organisation-by-id.params.js';
import { UpdateOrganisationBodyParams } from './update-organisation.body.params.js';
import { OrganisationByIdBodyParams } from './organisation-by-id.body.params.js';
import { OrganisationRepository } from '../persistence/organisation.repository.js';
import { OrganisationRepository, OrganisationSeachOptions } from '../persistence/organisation.repository.js';
import { Organisation } from '../domain/organisation.js';
import { OrganisationResponse } from './organisation.response.js';
import { Permissions } from '../../authentication/api/permissions.decorator.js';
Expand Down Expand Up @@ -261,10 +261,17 @@ export class OrganisationController {
@Query() queryParams: FindOrganisationQueryParams,
@Permissions() permissions: PersonPermissions,
): Promise<PagedResponse<OrganisationResponse>> {
let organisationSeachOptions: OrganisationSeachOptions = queryParams;
if (queryParams.sortField && queryParams.sortOrder) {
organisationSeachOptions = {
...queryParams,
sort: [{ field: queryParams.sortField, order: queryParams.sortOrder }],
};
}
const [organisations, total]: Counted<Organisation<true>> = await this.organisationRepository.findAuthorized(
permissions,
queryParams.systemrechte,
queryParams,
organisationSeachOptions,
);

const organisationResponses: OrganisationResponse[] = organisations.map((organisation: Organisation<true>) => {
Expand Down
5 changes: 5 additions & 0 deletions src/modules/organisation/domain/organisation.enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ export enum RootDirectChildrenType {
ERSATZ = 'ERSATZ',
OEFFENTLICH = 'OEFFENTLICH',
}

export enum OrganisationSortField {
KENNUNG = 'kennung',
NAME = 'name',
}
Loading
Loading