Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 66 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
2 | import { Inject } from '@nestjs/common'; |
||
3 | import { UpdateSchoolCommand } from './UpdateSchoolCommand'; |
||
4 | import { ISchoolRepository } from 'src/Domain/School/Repository/ISchoolRepository'; |
||
5 | import { IsSchoolAlreadyExist } from 'src/Domain/School/Specification/IsSchoolAlreadyExist'; |
||
6 | import { SchoolAlreadyExistException } from 'src/Domain/School/Exception/SchoolAlreadyExistException'; |
||
7 | import { SchoolNotFoundException } from 'src/Domain/School/Exception/SchoolNotFoundException'; |
||
8 | |||
9 | @CommandHandler(UpdateSchoolCommand) |
||
10 | export class UpdateSchoolCommandHandler { |
||
11 | constructor( |
||
12 | @Inject('ISchoolRepository') |
||
13 | private readonly schoolRepository: ISchoolRepository, |
||
14 | private readonly isSchoolAlreadyExist: IsSchoolAlreadyExist |
||
15 | ) {} |
||
16 | |||
17 | public async execute(command: UpdateSchoolCommand): Promise<string> { |
||
18 | const { |
||
19 | id, |
||
20 | reference, |
||
21 | address, |
||
22 | city, |
||
23 | name, |
||
24 | email, |
||
25 | zipCode, |
||
26 | numberOfClasses, |
||
27 | numberOfStudents, |
||
28 | status, |
||
29 | type, |
||
30 | observation, |
||
31 | phoneNumber |
||
32 | } = command; |
||
33 | |||
34 | const school = await this.schoolRepository.findOneById(id); |
||
35 | if (!school) { |
||
36 | throw new SchoolNotFoundException(); |
||
37 | } |
||
38 | |||
39 | if ( |
||
40 | reference !== school.getReference() && |
||
41 | true === (await this.isSchoolAlreadyExist.isSatisfiedBy(reference)) |
||
42 | ) { |
||
43 | throw new SchoolAlreadyExistException(); |
||
44 | } |
||
45 | |||
46 | school.update( |
||
47 | reference, |
||
48 | name, |
||
49 | address, |
||
50 | zipCode, |
||
51 | city, |
||
52 | status, |
||
53 | type, |
||
54 | email, |
||
55 | phoneNumber, |
||
56 | numberOfStudents, |
||
57 | numberOfClasses, |
||
58 | observation |
||
59 | ); |
||
60 | |||
61 | await this.schoolRepository.save(school); |
||
62 | |||
63 | return school.getId(); |
||
64 | } |
||
65 | } |
||
66 |