Skip to content

Commit

Permalink
SPSH-698: Changed the endpoint for the request CreatePersonWithPerson…
Browse files Browse the repository at this point in the history
…enkontext
  • Loading branch information
phaelcg committed Jul 5, 2024
1 parent 7a6e0f6 commit 7d71adf
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,122 @@ describe('DbiamPersonenkontextWorkflowController Integration Test', () => {
expect(response.body).toBeInstanceOf(Object);
});
});

describe('/POST create person with personenkontext', () => {
it('should return created person and personenkontext', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.LEHR,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);
personpermissions.getOrgIdsWithSystemrecht.mockResolvedValueOnce([organisation.id]);
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);

const response: Response = await request(app.getHttpServer() as App)
.post('/personenkontext-workflow')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});

expect(response.status).toBe(201);
});

it('should return error with status-code=404 if organisation does NOT exist', async () => {
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: faker.string.uuid(),
rollenart: RollenArt.LEHR,
}),
);
const permissions: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValueOnce(permissions);
permissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);
permissions.canModifyPerson.mockResolvedValueOnce(true);

const response: Response = await request(app.getHttpServer() as App)
.post('/personenkontext-workflow')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: faker.string.uuid(),
rolleId: rolle.id,
});

expect(response.status).toBe(404);
});

it('should return error with status-code 400 if specification ROLLE_NUR_AN_PASSENDE_ORGANISATION is NOT satisfied', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.SYSADMIN,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);
personpermissions.getOrgIdsWithSystemrecht.mockResolvedValueOnce([organisation.id]);

const response: Response = await request(app.getHttpServer() as App)
.post('/personenkontext-workflow')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});

expect(response.status).toBe(400);
expect(response.body).toEqual({
code: 400,
i18nKey: 'ROLLE_NUR_AN_PASSENDE_ORGANISATION',
});
});

it('should return error with status-code 404 if user does NOT have permissions', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.LEHR,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(false);
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);

const response: Response = await request(app.getHttpServer() as App)
.post('/personenkontext-workflow')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});
expect(response.status).toBe(404);
expect(response.body).toEqual({
code: 404,
subcode: '01',
titel: 'Angefragte Entität existiert nicht',
beschreibung: 'Die angeforderte Entität existiert nicht',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HttpStatus,
Inject,
Param,
Post,
Put,
Query,
UseFilters,
Expand All @@ -15,6 +16,7 @@ import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiConflictResponse,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiInternalServerErrorResponse,
ApiOAuth2,
Expand Down Expand Up @@ -45,21 +47,30 @@ import { DbiamUpdatePersonenkontexteBodyParams } from './param/dbiam-update-pers
import { PersonenkontexteUpdateResponse } from './response/personenkontexte-update.response.js';
import { DbiamPersonenkontexteUpdateError } from './dbiam-personenkontexte-update.error.js';
import { DomainError } from '../../../shared/error/domain.error.js';
import { DBiamPersonResponse } from './response/dbiam-person.response.js';
import { DbiamPersonenkontextError } from './dbiam-personenkontext.error.js';
import { DbiamCreatePersonWithContextBodyParams } from './param/dbiam-create-person-with-context.body.params.js';
import { PersonPersonenkontext, PersonenkontextCreationService } from '../domain/personenkontext-creation.service.js';
import { PersonenkontextCommitError } from '../domain/error/personenkontext-commit.error.js';
import { PersonenkontextSpecificationError } from '../specification/error/personenkontext-specification.error.js';
import { SchulConnexErrorMapper } from '../../../shared/error/schul-connex-error.mapper.js';
import { PersonenkontextExceptionFilter } from './personenkontext-exception-filter.js';

@UseFilters(SchulConnexValidationErrorFilter)
@UseFilters(SchulConnexValidationErrorFilter, new PersonenkontextExceptionFilter())
@ApiTags('personenkontext')
@ApiBearerAuth()
@ApiOAuth2(['openid'])
@Controller({ path: 'personenkontext-workflow' })
export class DbiamPersonenkontextWorkflowController {
public constructor(
private readonly personenkontextWorkflowFactory: PersonenkontextWorkflowFactory,
private readonly personenkontextCreationService: PersonenkontextCreationService,
@Inject(getMapperToken()) private readonly mapper: Mapper,
) {}

@Get('step')
@ApiOkResponse({
description: `Initialize or process data from the person creation form.
description: `Initialize or process data from the person creation form.
Valid combinations:
- Both organisationId and rolleId are undefined: Fetch all possible organisations.
- organisationId is provided, but rolleId is undefined: Fetch Rollen for the given organisation.
Expand Down Expand Up @@ -203,4 +214,51 @@ export class DbiamPersonenkontextWorkflowController {

return response;
}

@Post()
@HttpCode(HttpStatus.CREATED)
@ApiCreatedResponse({
description: 'Person with Personenkontext was successfully created.',
type: DBiamPersonResponse,
})
@ApiBadRequestResponse({
description: 'The person and the personenkontext could not be created, may due to unsatisfied specifications.',
type: DbiamPersonenkontextError,
})
@ApiUnauthorizedResponse({ description: 'Not authorized to create person with personenkontext.' })
@ApiForbiddenResponse({ description: 'Insufficient permission to create person with personenkontext.' })
@ApiForbiddenResponse({ description: 'Insufficient permissions to create the person with personenkontext.' })
@ApiBadRequestResponse({ description: 'Request has wrong format.', type: DbiamPersonenkontextError })
@ApiInternalServerErrorResponse({
description: 'Internal server error while creating person with personenkontext.',
})
public async createPersonWithKontext(
@Body() params: DbiamCreatePersonWithContextBodyParams,
@Permissions() permissions: PersonPermissions,
): Promise<DBiamPersonResponse> {
//Check all references & permissions then save person
const savedPersonWithPersonenkontext: PersonPersonenkontext | DomainError | PersonenkontextCommitError =
await this.personenkontextCreationService.createPersonWithPersonenkontext(
permissions,
params.vorname,
params.familienname,
params.organisationId,
params.rolleId,
);

if (savedPersonWithPersonenkontext instanceof PersonenkontextSpecificationError) {
throw savedPersonWithPersonenkontext;
}

if (savedPersonWithPersonenkontext instanceof DomainError) {
throw SchulConnexErrorMapper.mapSchulConnexErrorToHttpException(
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(savedPersonWithPersonenkontext),
);
}

return new DBiamPersonResponse(
savedPersonWithPersonenkontext.person,
savedPersonWithPersonenkontext.personenkontext,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,122 +495,4 @@ describe('dbiam Personenkontext API', () => {
});
});
});

describe('/POST create person with personenkontext', () => {
it('should return created person and personenkontext', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.LEHR,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);
personpermissions.getOrgIdsWithSystemrecht.mockResolvedValueOnce([organisation.id]);
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);

const response: Response = await request(app.getHttpServer() as App)
.post('/dbiam/personenkontext/person')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});

expect(response.status).toBe(201);
});

it('should return error with status-code=404 if organisation does NOT exist', async () => {
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: faker.string.uuid(),
rollenart: RollenArt.LEHR,
}),
);
const permissions: DeepMocked<PersonPermissions> = createMock<PersonPermissions>();
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValueOnce(permissions);
permissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);
permissions.canModifyPerson.mockResolvedValueOnce(true);

const response: Response = await request(app.getHttpServer() as App)
.post('/dbiam/personenkontext/person')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: faker.string.uuid(),
rolleId: rolle.id,
});

expect(response.status).toBe(404);
});

it('should return error with status-code 400 if specification ROLLE_NUR_AN_PASSENDE_ORGANISATION is NOT satisfied', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.SYSADMIN,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(true);
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);
personpermissions.getOrgIdsWithSystemrecht.mockResolvedValueOnce([organisation.id]);

const response: Response = await request(app.getHttpServer() as App)
.post('/dbiam/personenkontext/person')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});

expect(response.status).toBe(400);
expect(response.body).toEqual({
code: 400,
i18nKey: 'ROLLE_NUR_AN_PASSENDE_ORGANISATION',
});
});

it('should return error with status-code 404 if user does NOT have permissions', async () => {
const organisation: OrganisationDo<true> = await organisationRepo.save(
DoFactory.createOrganisation(false, { typ: OrganisationsTyp.SCHULE }),
);
const rolle: Rolle<true> = await rolleRepo.save(
DoFactory.createRolle(false, {
administeredBySchulstrukturknoten: organisation.id,
rollenart: RollenArt.LEHR,
}),
);

const personpermissions: DeepMocked<PersonPermissions> = createMock();
personpermissions.hasSystemrechtAtOrganisation.mockResolvedValueOnce(false);
personpermissionsRepoMock.loadPersonPermissions.mockResolvedValue(personpermissions);

const response: Response = await request(app.getHttpServer() as App)
.post('/dbiam/personenkontext/person')
.send({
familienname: faker.person.lastName(),
vorname: faker.person.firstName(),
organisationId: organisation.id,
rolleId: rolle.id,
});
expect(response.status).toBe(404);
expect(response.body).toEqual({
code: 404,
subcode: '01',
titel: 'Angefragte Entität existiert nicht',
beschreibung: 'Die angeforderte Entität existiert nicht',
});
});
});
});
Loading

0 comments on commit 7d71adf

Please sign in to comment.