Completed
Push — master ( e9382c...759d87 )
by Mathieu
28s queued 11s
created

DailyRateRepository.findDailyRates   A

Complexity

Conditions 1

Size

Total Lines 22
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 22
c 0
b 0
f 0
rs 9.376
cc 1
1
import {InjectRepository} from '@nestjs/typeorm';
2
import {Repository} from 'typeorm';
3
import {IDailyRateRepository} from 'src/Domain/Accounting/Repository/IDailyRateRepository';
4
import {DailyRate} from 'src/Domain/Accounting/DailyRate.entity';
5
import {User} from 'src/Domain/HumanResource/User/User.entity';
6
import {Customer} from 'src/Domain/Customer/Customer.entity';
7
import {Task} from 'src/Domain/Task/Task.entity';
8
import {MAX_ITEMS_PER_PAGE} from 'src/Application/Common/Pagination';
9
10
export class DailyRateRepository implements IDailyRateRepository {
11
  constructor(
12
    @InjectRepository(DailyRate)
13
    private readonly repository: Repository<DailyRate>
14
  ) {}
15
16
  public save(dailyRate: DailyRate): Promise<DailyRate> {
17
    return this.repository.save(dailyRate);
18
  }
19
20
  public findDailyRates(page: number): Promise<[DailyRate[], number]> {
21
    return this.repository
22
      .createQueryBuilder('dailyRate')
23
      .select([
24
        'dailyRate.id',
25
        'dailyRate.amount',
26
        'user.id',
27
        'user.firstName',
28
        'user.lastName',
29
        'task.id',
30
        'task.name',
31
        'customer.id',
32
        'customer.name'
33
      ])
34
      .innerJoin('dailyRate.user', 'user')
35
      .innerJoin('dailyRate.task', 'task')
36
      .innerJoin('dailyRate.customer', 'customer')
37
      .orderBy('user.lastName', 'ASC')
38
      .addOrderBy('user.firstName', 'ASC')
39
      .limit(MAX_ITEMS_PER_PAGE)
40
      .offset((page - 1) * MAX_ITEMS_PER_PAGE)
41
      .getManyAndCount();
42
  }
43
44
  public findOneById(id: string): Promise<DailyRate | undefined> {
45
    return this.repository
46
      .createQueryBuilder('dailyRate')
47
      .select([
48
        'dailyRate.id',
49
        'dailyRate.amount',
50
        'user.id',
51
        'user.firstName',
52
        'user.lastName',
53
        'task.id',
54
        'task.name',
55
        'customer.id',
56
        'customer.name'
57
      ])
58
      .innerJoin('dailyRate.user', 'user')
59
      .innerJoin('dailyRate.task', 'task')
60
      .innerJoin('dailyRate.customer', 'customer')
61
      .where('dailyRate.id = :id', {id})
62
      .getOne();
63
  }
64
65
  public findOneByUserCustomerAndTask(
66
    user: User,
67
    customer: Customer,
68
    task: Task
69
  ): Promise<DailyRate | undefined> {
70
    return this.repository
71
      .createQueryBuilder('dailyRate')
72
      .select('dailyRate.id')
73
      .where('dailyRate.user = :user', {user: user.getId()})
74
      .andWhere('dailyRate.customer = :customer', {customer: customer.getId()})
75
      .andWhere('dailyRate.task = :task', {task: task.getId()})
76
      .getOne();
77
  }
78
}
79