Passed
Pull Request — master (#98)
by Mathieu
01:27
created

PayStubRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 22
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A findOneByUserAndDate 0 14 1
A save 0 3 1
1
import {InjectRepository} from '@nestjs/typeorm';
2
import {Repository} from 'typeorm';
3
import {IPayStubRepository} from 'src/Domain/HumanResource/Repository/IPayStubRepository';
4
import {PayStub} from 'src/Domain/HumanResource/PayStub.entity';
5
import {User} from 'src/Domain/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