api/src/Infrastructure/School/Repository/SchoolUserRepository.ts   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1

Size

Lines of Code 61
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

5 Functions

Rating   Name   Duplication   Size   Complexity  
A SchoolUserRepository.remove 0 3 1
A SchoolUserRepository.findUsersBySchool 0 8 1
A SchoolUserRepository.findOneByUserAndSchool 0 16 1
A SchoolUserRepository.findOneById 0 6 1
A SchoolUserRepository.save 0 3 1
1
import { Injectable } from '@nestjs/common';
2
import { Repository } from 'typeorm';
3
import { InjectRepository } from '@nestjs/typeorm';
4
import { SchoolUser } from 'src/Domain/School/SchoolUser.entity';
5
import { ISchoolUserRepository } from 'src/Domain/School/Repository/ISchoolUserRepository';
6
import { School } from 'src/Domain/School/School.entity';
7
import { User } from 'src/Domain/User/User.entity';
8
9
@Injectable()
10
export class SchoolUserRepository implements ISchoolUserRepository {
11
  constructor(
12
    @InjectRepository(SchoolUser)
13
    private readonly repository: Repository<SchoolUser>
14
  ) {}
15
16
  public save(schoolUser: SchoolUser): Promise<SchoolUser> {
17
    return this.repository.save(schoolUser);
18
  }
19
20
  public remove(schoolUser: SchoolUser): void {
21
    this.repository.delete(schoolUser.getId());
22
  }
23
24
  public findOneById(id: string): Promise<SchoolUser | undefined> {
25
    return this.repository
26
      .createQueryBuilder('schoolUser')
27
      .select('schoolUser.id')
28
      .where('schoolUser.id = :id', { id })
29
      .getOne();
30
  }
31
32
  public findOneByUserAndSchool(user: User, school: School): Promise<SchoolUser | undefined> {
33
    return this.repository
34
      .createQueryBuilder('schoolUser')
35
      .select('schoolUser.id')
36
      .innerJoin(
37
        'schoolUser.user',
38
        'user',
39
        'user.id = :userId',
40
        { userId: user.getId() }
41
      )
42
      .innerJoin(
43
        'schoolUser.school',
44
        'school',
45
        'school.id = :schoolId',
46
        { schoolId: school.getId() }
47
      )
48
      .getOne();
49
  }
50
51
  public findUsersBySchool(schoolId: string): Promise<SchoolUser[]> {
52
    return this.repository
53
      .createQueryBuilder('schoolUser')
54
      .select([ 'schoolUser.id', 'user.email' ])
55
      .innerJoin('schoolUser.user', 'user')
56
      .innerJoin('schoolUser.school', 'school', 'school.id = :schoolId', { schoolId })
57
      .orderBy('user.lastName', 'ASC')
58
      .getMany();
59
  }
60
}
61