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

Complexity

Total Complexity 6
Complexity/F 1

Size

Lines of Code 64
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 50
mnd 0
bc 0
fnc 6
dl 0
loc 64
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

6 Functions

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