Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
clauyan committed Dec 18, 2024
1 parent 440a164 commit ce63436
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/modules/authentication/domain/person-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type PersonFields = Pick<
| 'geschlecht'
| 'geburtsdatum'
| 'updatedAt'
| 'referrer'
>;
type PersonKontextFields = Pick<Personenkontext<true>, 'rolleId' | 'organisationId'>;
type RolleFields = Pick<Rolle<true>, 'systemrechte' | 'serviceProviderIds'>;
Expand Down Expand Up @@ -53,6 +54,7 @@ export class PersonPermissions implements IPersonPermissions {
geschlecht: person.geschlecht,
geburtsdatum: person.geburtsdatum,
updatedAt: person.updatedAt,
referrer: person.referrer,
};
}

Expand Down
30 changes: 5 additions & 25 deletions src/modules/person/api/person.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ describe('PersonController', () => {

it('should throw HttpException', async () => {
personRepositoryMock.findBy.mockResolvedValue([[], 0]);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: false,
error: new EntityNotFoundError(),
});
Expand All @@ -993,7 +993,7 @@ describe('PersonController', () => {

it('should throw HttpNotFoundException', async () => {
personRepositoryMock.findById.mockResolvedValue(undefined);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: false,
error: new EntityNotFoundError(),
});
Expand All @@ -1013,7 +1013,7 @@ describe('PersonController', () => {

it('should throw HttpException', async () => {
personRepositoryMock.findBy.mockResolvedValue([[], 0]);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: true,
value: createMock<Person<true>>({ referrer: undefined }),
});
Expand All @@ -1034,7 +1034,7 @@ describe('PersonController', () => {

it('should reset UEM-password for person', async () => {
personRepositoryMock.findById.mockResolvedValue(person);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: true,
value: person,
});
Expand Down Expand Up @@ -1063,7 +1063,7 @@ describe('PersonController', () => {
ok: false,
error: new PersonDomainError('Person', 'entityId', undefined),
});
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
personRepositoryMock.getPersonIfAllowed.mockResolvedValueOnce({
ok: true,
value: person,
});
Expand All @@ -1086,10 +1086,6 @@ describe('PersonController', () => {

it('should throw HttpException', async () => {
personRepositoryMock.findBy.mockResolvedValue([[], 0]);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
ok: false,
error: new EntityNotFoundError(),
});

await expect(personController.resetUEMPassword(personPermissionsMock)).rejects.toThrow(HttpException);
expect(personRepositoryMock.update).toHaveBeenCalledTimes(0);
Expand All @@ -1101,10 +1097,6 @@ describe('PersonController', () => {

it('should throw HttpNotFoundException', async () => {
personRepositoryMock.findById.mockResolvedValue(undefined);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
ok: false,
error: new EntityNotFoundError(),
});

await expect(personController.resetUEMPassword(personPermissionsMock)).rejects.toThrow(HttpException);
expect(personRepositoryMock.update).toHaveBeenCalledTimes(0);
Expand All @@ -1116,10 +1108,6 @@ describe('PersonController', () => {

it('should throw HttpException', async () => {
personRepositoryMock.findBy.mockResolvedValue([[], 0]);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
ok: true,
value: createMock<Person<true>>({ referrer: undefined }),
});

await expect(personController.resetUEMPassword(personPermissionsMock)).rejects.toThrow(HttpException);
expect(personRepositoryMock.update).toHaveBeenCalledTimes(0);
Expand All @@ -1133,10 +1121,6 @@ describe('PersonController', () => {
it('should reset UEM-password for person', async () => {
personPermissionsMock.personFields.id = person.id;
personRepositoryMock.findById.mockResolvedValue(person);
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
ok: true,
value: person,
});
ldapClientServiceMock.changeUserPasswordByPersonId.mockResolvedValueOnce({
ok: true,
value: person.id,
Expand All @@ -1161,10 +1145,6 @@ describe('PersonController', () => {
ok: false,
error: new PersonDomainError('Person', 'entityId', undefined),
});
personRepositoryMock.getPersonIfAllowedOrRequesterIsPerson.mockResolvedValueOnce({
ok: true,
value: person,
});

await expect(personController.resetUEMPassword(personPermissionsMock)).rejects.toThrow(
PersonUserPasswordModificationError,
Expand Down
24 changes: 10 additions & 14 deletions src/modules/person/api/person.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { ResultInterceptor } from '../../../shared/util/result-interceptor.js';
import { AuthenticationExceptionFilter } from '../../authentication/api/authentication-exception-filter.js';
import { Permissions } from '../../authentication/api/permissions.decorator.js';
import { StepUpGuard } from '../../authentication/api/steup-up.guard.js';
import { PermittedOrgas, PersonPermissions } from '../../authentication/domain/person-permissions.js';
import { PermittedOrgas, PersonFields, PersonPermissions } from '../../authentication/domain/person-permissions.js';
import { EmailRepo } from '../../email/persistence/email.repo.js';
import { UserLock } from '../../keycloak-administration/domain/user-lock.js';
import { KeycloakUserService } from '../../keycloak-administration/index.js';
Expand Down Expand Up @@ -554,7 +554,7 @@ export class PersonController {
@Permissions() permissions: PersonPermissions,
): Promise<Result<string>> {
//check that logged-in user is allowed to update person
const personResult: Result<Person<true>> = await this.personRepository.getPersonIfAllowedOrRequesterIsPerson(
const personResult: Result<Person<true>> = await this.personRepository.getPersonIfAllowed(
params.personId,
permissions,
);
Expand Down Expand Up @@ -591,30 +591,26 @@ export class PersonController {
@ApiInternalServerErrorResponse({ description: 'Internal server error.' })
@UseInterceptors(ResultInterceptor)
public async resetUEMPassword(@Permissions() permissions: PersonPermissions): Promise<Result<string>> {
const personId: string = permissions.personFields.id;
const personResult: Result<Person<true>> = await this.personRepository.getPersonIfAllowedOrRequesterIsPerson(
personId,
permissions,
);
if (!personResult.ok) {
const { id, referrer }: PersonFields = permissions.personFields;
if (!id) {
throw SchulConnexErrorMapper.mapSchulConnexErrorToHttpException(
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(new EntityNotFoundError('Person', personId)),
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(new EntityNotFoundError('Person', id)),
);
}
if (!personResult.value.referrer) {
if (!referrer) {
throw SchulConnexErrorMapper.mapSchulConnexErrorToHttpException(
SchulConnexErrorMapper.mapDomainErrorToSchulConnexError(
new PersonDomainError('Person-Referrer NOT defined', personId),
new PersonDomainError('Person-Referrer NOT defined', id),
),
);
}
const changeUserPasswordResult: Result<PersonID> = await this.ldapClientService.changeUserPasswordByPersonId(
personResult.value.id,
personResult.value.referrer,
id,
referrer,
);

if (!changeUserPasswordResult.ok) {
throw new PersonUserPasswordModificationError(personResult.value.id);
throw new PersonUserPasswordModificationError(id);
}

return { ok: true, value: changeUserPasswordResult.value };
Expand Down

0 comments on commit ce63436

Please sign in to comment.