Passed
Pull Request — master (#103)
by Mathieu
01:32
created

PayStubRepository.findAll   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 17
c 0
b 0
f 0
rs 9.6
cc 1
1
import {InjectRepository} from '@nestjs/typeorm';
2
import {Repository} from 'typeorm';
3
import {IPayStubRepository} from 'src/Domain/HumanResource/PayStub/Repository/IPayStubRepository';
4
import {PayStub} from 'src/Domain/HumanResource/PayStub/PayStub.entity';
5
import {User} from 'src/Domain/HumanResource/User/User.entity';
6
7
export class PayStubRepository implements IPayStubRepository {
8
  constructor(
9
    @InjectRepository(PayStub)
10
    private readonly repository: Repository<PayStub>
11
  ) {}
12
13
  public save(payStub: PayStub): Promise<PayStub> {
14
    return this.repository.save(payStub);
15
  }
16
17
  public findOneByUserAndDate(
18
    user: User,
19
    date: Date
20
  ): Promise<PayStub | undefined> {
21
    const month = new Date(date).getMonth() + 1;
22
    const year = new Date(date).getFullYear();
23
24
    return this.repository
25
      .createQueryBuilder('payStub')
26
      .select(['payStub.id'])
27
      .where('payStub.user = :userId', {userId: user.getId()})
28
      .andWhere('extract(month FROM payStub.date) = :month', {month})
29
      .andWhere('extract(year FROM payStub.date) = :year', {year})
30
      .getOne();
31
  }
32
33
  public findAll(): Promise<PayStub[]> {
34
    return this.repository
35
      .createQueryBuilder('payStub')
36
      .select([
37
        'payStub.id',
38
        'payStub.date',
39
        'user.id',
40
        'user.firstName',
41
        'user.lastName',
42
        'file.id',
43
        'file.name',
44
        'file.size'
45
      ])
46
      .innerJoin('payStub.file', 'file')
47
      .innerJoin('payStub.user', 'user')
48
      .orderBy('payStub.date', 'DESC')
49
      .getMany();
50
  }
51
}
52