Passed
Push — master ( 1029c0...f58cd4 )
by Mathieu
01:51 queued 22s
created

DiscountRepository.countBySchool   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 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 findOneByAmountAndSchool(amount: number, school: School): Promise<Discount | undefined> {
20
    return this.repository
21
      .createQueryBuilder('discount')
22
      .select(['discount.id'])
23
      .innerJoin('discount.school', 'school', 'school.id = :school', {
24
        school: school.getId()
25
      })
26
      .where('discount.amount = :amount', { amount })
27
      .getOne();
28
  }
29
30
  public findBySchool(schoolId: string): Promise<Discount[]> {
31
    return this.repository
32
      .createQueryBuilder('discount')
33
      .select([
34
        'discount.id',
35
        'discount.type',
36
        'discount.amount',
37
        'discount.value',
38
      ])
39
      .innerJoin('discount.school', 'school', 'school.id = :schoolId', { schoolId })
40
      .orderBy('discount.amount', 'ASC')
41
      .getMany();
42
  }
43
44
  public countBySchool(id: string): Promise<number> {
45
    return this.repository
46
      .createQueryBuilder('discount')
47
      .select('discount.id')
48
      .innerJoin('discount.school', 'school', 'school.id = :id', { id })
49
      .getCount();
50
  }
51
}
52