Skip to content

Commit

Permalink
EW-617: Add create Personenkontext route to person controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Oct 5, 2023
1 parent 088894b commit 64c88fa
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/modules/person/api/create-personenkontext.body.params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AutoMap } from '@automapper/classes';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsEnum, IsOptional } from 'class-validator';
import { Jahrgangsstufe, Personenstatus, Rolle } from '../domain/personenkontext.enums.js';

export class CreatePersonenkontextBodyParams {
@AutoMap()
@IsString()
@IsOptional()
@ApiProperty({ required: false })
public readonly referrer?: string;

@AutoMap()
@IsEnum(Rolle)
@ApiProperty({ enum: Rolle, required: true })
public readonly rolle!: Rolle;

@AutoMap()
@IsEnum(Personenstatus)
@IsOptional()
@ApiProperty({ enum: Personenstatus, required: false })
public readonly personenstatus?: Personenstatus;

@AutoMap()
@IsEnum(Jahrgangsstufe)
@IsOptional()
@ApiProperty({ enum: Jahrgangsstufe, required: false })
public readonly jahrgangsstufe?: Jahrgangsstufe;
}
19 changes: 19 additions & 0 deletions src/modules/person/api/create-personenkontext.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AutoMap } from '@automapper/classes';
import { Rolle, Personenstatus, Jahrgangsstufe } from '../domain/personenkontext.enums.js';

export class CreatePersonenkontextDto {
@AutoMap()
public personId!: string;

@AutoMap()
public readonly referrer?: string;

@AutoMap()
public readonly rolle!: Rolle;

@AutoMap()
public readonly personenstatus?: Personenstatus;

@AutoMap()
public readonly jahrgangsstufe?: Jahrgangsstufe;
}
3 changes: 3 additions & 0 deletions src/modules/person/api/created-personenkontext-org.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class CreatedPersonenkontextDtoOrganisation {
public readonly id!: string;
}
29 changes: 29 additions & 0 deletions src/modules/person/api/created-personenkontext.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AutoMap } from '@automapper/classes';
import { Jahrgangsstufe, Personenstatus, Rolle } from '../domain/personenkontext.enums.js';
import { CreatedPersonenkontextDtoOrganisation } from './created-personenkontext-org.dto.js';

export class CreatedPersonenkontextDto {
@AutoMap()
public readonly id!: string;

@AutoMap()
public readonly referrer?: string;

@AutoMap()
public readonly mandant!: string;

@AutoMap()
public readonly organisation!: CreatedPersonenkontextDtoOrganisation;

@AutoMap()
public readonly rolle!: Rolle;

@AutoMap()
public readonly personenstatus?: Personenstatus;

@AutoMap()
public readonly jahrgangsstufe?: Jahrgangsstufe;

@AutoMap()
public readonly revision!: string;
}
33 changes: 33 additions & 0 deletions src/modules/person/api/person-api.mapper.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { FindPersonDatensatzDTO } from './finde-persondatensatz-dto.js';
import { PersonGender, PersonTrustLevel } from './person.enums.js';
import { PersonenQueryParam, SichtfreigabeType } from './personen-query.param.js';
import { PersonenDatensatz } from './personendatensatz.js';
import { CreatePersonenkontextBodyParams } from './create-personenkontext.body.params.js';
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';
import { PersonenkontextResponse } from './personenkontext.response.js';

export const personGenderToGenderConverter: Converter<PersonGender, Gender> = {
convert(source: PersonGender): Gender {
Expand Down Expand Up @@ -267,6 +272,34 @@ export class PersonApiMapperProfile extends AutomapperProfile {
forMember((dest: PersonDo<false>) => dest.isInformationBlocked, ignore()),
);
createMap(mapper, CreatePersonDto, UserDo);

createMap(
mapper,
CreatePersonenkontextBodyParams,
CreatePersonenkontextDto,
forMember((dest: CreatePersonenkontextDto) => dest.personId, ignore()),
);
createMap(
mapper,
CreatePersonenkontextDto,
PersonenkontextDo<false>,
forMember((dest: PersonenkontextDo<false>) => dest.mandant, ignore()),
forMember((dest: PersonenkontextDo<false>) => dest.organisation, ignore()),
forMember((dest: PersonenkontextDo<false>) => dest.loeschungZeitpunkt, ignore()),
forMember((dest: PersonenkontextDo<false>) => dest.revision, ignore()),
);

createMap(
mapper,
PersonenkontextDo,
CreatedPersonenkontextDto,

forMember(
(dest: CreatedPersonenkontextDto) => dest.id,
mapFrom((src: PersonenkontextDo<true>) => src.id),
),
);
createMap(mapper, CreatedPersonenkontextDto, PersonenkontextResponse);
};
}
}
40 changes: 36 additions & 4 deletions src/modules/person/api/person.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ import { PersonByIdParams } from './person-by-id.param.js';
import { PersonenQueryParam } from './personen-query.param.js';
import { FindPersonDatensatzDTO } from './finde-persondatensatz-dto.js';
import { PersonenDatensatz } from './personendatensatz.js';
import { CreatePersonenkontextBodyParams } from './create-personenkontext.body.params.js';
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';
import { PersonenkontextResponse } from './personenkontext.response.js';
import { PersonenkontextUc } from './personenkontext.uc.js';

@ApiTags('person')
@Controller({ path: 'person' })
export class PersonController {
public constructor(private readonly uc: PersonUc, @Inject(getMapperToken()) private readonly mapper: Mapper) {}
public constructor(
private readonly personUc: PersonUc,
private readonly personenkontextUc: PersonenkontextUc,
@Inject(getMapperToken()) private readonly mapper: Mapper,
) {}

@Post()
@ApiCreatedResponse({ description: 'The person was successfully created.' })
Expand All @@ -31,7 +40,7 @@ export class PersonController {
@ApiInternalServerErrorResponse({ description: 'Internal server error while creating the person.' })
public async createPerson(@Body() params: CreatePersonBodyParams): Promise<void> {
const dto: CreatePersonDto = this.mapper.map(params, CreatePersonBodyParams, CreatePersonDto);
await this.uc.createPerson(dto);
await this.personUc.createPerson(dto);
}

@Get(':personId')
Expand All @@ -43,13 +52,36 @@ export class PersonController {
@ApiInternalServerErrorResponse({ description: 'Internal server error while getting the person.' })
public async findPersonById(@Param() params: PersonByIdParams): Promise<PersonenDatensatz | HttpException> {
try {
const person: PersonenDatensatz = await this.uc.findPersonById(params.personId);
const person: PersonenDatensatz = await this.personUc.findPersonById(params.personId);
return person;
} catch (error) {
return new HttpException('Requested entity does not exist', HttpStatus.NOT_FOUND);
}
}

@Post(':personId/personenkontexte')
@ApiCreatedResponse({ description: 'The personenkontext was successfully created.' })
@ApiBadRequestResponse({ description: 'The personenkontext already exists.' })
@ApiUnauthorizedResponse({ description: 'Not authorized to create the personenkontext.' })
@ApiForbiddenResponse({ description: 'Not permitted to create the personenkontext.' })
@ApiInternalServerErrorResponse({ description: 'Internal server error while creating the personenkontext.' })
public async createPersonenkontext(
@Param() pathParams: PersonByIdParams,
@Body() bodyParams: CreatePersonenkontextBodyParams,
): Promise<PersonenkontextResponse> {
const personenkontextDto: CreatePersonenkontextDto = this.mapper.map(
bodyParams,
CreatePersonenkontextBodyParams,
CreatePersonenkontextDto,
);
personenkontextDto.personId = pathParams.personId;

const createdPersonenkontext: CreatedPersonenkontextDto = await this.personenkontextUc.createPersonenkontext(
personenkontextDto,
);
return this.mapper.map(createdPersonenkontext, CreatedPersonenkontextDto, PersonenkontextResponse);
}

@Get()
@ApiCreatedResponse({ description: 'The persons were successfully pulled.' })
@ApiUnauthorizedResponse({ description: 'Not authorized to get persons.' })
Expand All @@ -61,7 +93,7 @@ export class PersonController {
PersonenQueryParam,
FindPersonDatensatzDTO,
);
const persons: PersonenDatensatz[] = await this.uc.findAll(persondatensatzDTO);
const persons: PersonenDatensatz[] = await this.personUc.findAll(persondatensatzDTO);
return persons;
}
}
29 changes: 29 additions & 0 deletions src/modules/person/api/personenkontext.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AutoMap } from '@automapper/classes';
import { Rolle, Personenstatus, Jahrgangsstufe } from '../domain/personenkontext.enums.js';
import { CreatedPersonenkontextDtoOrganisation } from './created-personenkontext-org.dto.js';

export class PersonenkontextResponse {
@AutoMap()
public readonly id!: string;

@AutoMap()
public readonly referrer?: string;

@AutoMap()
public readonly mandant!: string;

@AutoMap()
public readonly organisation!: CreatedPersonenkontextDtoOrganisation;

@AutoMap()
public readonly rolle!: Rolle;

@AutoMap()
public readonly personenstatus?: Personenstatus;

@AutoMap()
public readonly jahrgangsstufe?: Jahrgangsstufe;

@AutoMap()
public readonly revision!: string;
}
32 changes: 32 additions & 0 deletions src/modules/person/api/personenkontext.uc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Mapper } from '@automapper/core';
import { getMapperToken } from '@automapper/nestjs';
import { Inject, Injectable } from '@nestjs/common';
import { PersonenkontextDo } from '../domain/personenkontext.do.js';
import { PersonenkontextService } from '../domain/personenkontext.service.js';
import { CreatePersonenkontextDto } from './create-personenkontext.dto.js';
import { CreatedPersonenkontextDto } from './created-personenkontext.dto.js';

@Injectable()
export class PersonenkontextUc {
public constructor(
private readonly personenkontextService: PersonenkontextService,
@Inject(getMapperToken()) private readonly mapper: Mapper,
) {}

public async createPersonenkontext(
personenkontextDto: CreatePersonenkontextDto,
): Promise<CreatedPersonenkontextDto> {
const personenkontextDo: PersonenkontextDo<false> = this.mapper.map(
personenkontextDto,
CreatePersonenkontextDto,
PersonenkontextDo,
);
const result: Result<PersonenkontextDo<true>> = await this.personenkontextService.createPersonenkontext(
personenkontextDo,
);
if (result.ok) {
return this.mapper.map(result.value, PersonenkontextDo, CreatedPersonenkontextDto);
}
throw result.error;
}
}
51 changes: 51 additions & 0 deletions src/modules/person/domain/personenkontext.do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { AutoMap } from '@automapper/classes';
import { DoBase } from '../../../shared/types/index.js';
import { OrganisationDo } from '../../organisation/domain/organisation.do.js';
import { Jahrgangsstufe, Personenstatus, Rolle } from './personenkontext.enums.js';

export class PersonenkontextDo<WasPersisted extends boolean> implements DoBase<WasPersisted> {
/**
* @deprecated This constructor is for automapper only.
*/
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-empty-function
public constructor() {}

@AutoMap()
public createdAt!: Persisted<Date, WasPersisted>;

@AutoMap()
public updatedAt!: Persisted<Date, WasPersisted>;

@AutoMap()
public id!: Persisted<string, WasPersisted>;

@AutoMap()
public personId!: string;

@AutoMap()
public referrer?: string;

@AutoMap()
public mandant!: string;

@AutoMap()
public organisation!: OrganisationDo<WasPersisted>;

@AutoMap()
public rolle!: Rolle;

@AutoMap()
public personenstatus?: Personenstatus;

@AutoMap()
public jahrgangsstufe?: Jahrgangsstufe;

@AutoMap()
public sichtfreigabe?: boolean = false;

@AutoMap()
public loeschungZeitpunkt?: Date;

@AutoMap()
public revision!: string;
}
25 changes: 25 additions & 0 deletions src/modules/person/domain/personenkontext.enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export enum Personenstatus {
Aktiv = 'AKTIV',
}

export enum Jahrgangsstufe {
Jahrgangsstufe1 = '01',
Jahrgangsstufe2 = '02',
Jahrgangsstufe3 = '03',
Jahrgangsstufe4 = '04',
Jahrgangsstufe5 = '05',
Jahrgangsstufe6 = '06',
Jahrgangsstufe7 = '07',
Jahrgangsstufe8 = '08',
Jahrgangsstufe9 = '09',
Jahrgangsstufe10 = '10',
}

export enum Rolle {
Lernender = 'LERN',
Lehrender = 'LEHR',
Externe_Person = 'EXTERN',
Organisationsadministrator = 'ORGADMIN',
Organisationsleitung = 'LEIT',
Systemadministrator = 'SYSADMIN',
}
20 changes: 20 additions & 0 deletions src/modules/person/domain/personenkontext.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { DomainError } from '../../../shared/error/index.js';
import { EntityCouldNotBeCreated } from '../../../shared/error/entity-could-not-be-created.error.js';
import { PersonenkontextRepo } from '../persistence/personenkontext.repo.js';
import { PersonenkontextDo } from './personenkontext.do.js';

@Injectable()
export class PersonenkontextService {
public constructor(private readonly personenkontextRepo: PersonenkontextRepo) {}

public async createPersonenkontext(
personenkontextDo: PersonenkontextDo<false>,
): Promise<Result<PersonenkontextDo<true>, DomainError>> {
const personenkontext: PersonenkontextDo<true> = await this.personenkontextRepo.save(personenkontextDo);
if (personenkontext) {
return { ok: true, value: personenkontext };
}
return { ok: false, error: new EntityCouldNotBeCreated(`Personenkontext could not be created`) };
}
}
Loading

0 comments on commit 64c88fa

Please sign in to comment.