api/src/Application/School/Query/GetSchoolByIdQueryHandler.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 47
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 41
mnd 2
bc 2
fnc 1
dl 0
loc 47
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetSchoolByIdQueryHandler.execute 0 27 3
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { GetSchoolByIdQuery } from './GetSchoolByIdQuery';
4
import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository';
5
import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException';
6
import { SchoolDetailView } from '../View/SchoolDetailView';
7
import { CanUserAccessToSchool } from 'src/Domain/User/Specification/CanUserAccessToSchool';
8
import { UserCantAccessToSchoolException } from 'src/Domain/User/Exception/UserCantAccessToSchoolException';
9
10
@QueryHandler(GetSchoolByIdQuery)
11
export class GetSchoolByIdQueryHandler {
12
  constructor(
13
    @Inject('ISchoolRepository')
14
    private readonly schoolRepository: ISchoolRepository,
15
    private readonly canUserAccessToSchool: CanUserAccessToSchool
16
  ) {}
17
18
  public async execute(query: GetSchoolByIdQuery): Promise<SchoolDetailView> {
19
    const { id, userId } = query;
20
    const school = await this.schoolRepository.findOneById(id);
21
22
    if (!school) {
23
      throw new SchoolNotFoundException();
24
    }
25
26
    if (false === await this.canUserAccessToSchool.isSatisfiedBy(school, userId)) {
27
      throw new UserCantAccessToSchoolException();
28
    }
29
30
    return new SchoolDetailView(
31
      school.getId(),
32
      school.getName(),
33
      school.getReference(),
34
      school.getAddress(),
35
      school.getCity(),
36
      school.getZipCode(),
37
      school.getStatus(),
38
      school.getType(),
39
      school.getEmail(),
40
      school.getPhoneNumber(),
41
      school.getNumberOfClasses(),
42
      school.getNumberOfStudents(),
43
      school.getObservation()
44
    );
45
  }
46
}
47