1
|
|
|
import {InjectRepository} from '@nestjs/typeorm'; |
2
|
|
|
import {Repository} from 'typeorm'; |
3
|
|
|
import {IPaySlipRepository} from 'src/Domain/HumanResource/PaySlip/Repository/IPaySlipRepository'; |
4
|
|
|
import {PaySlip} from 'src/Domain/HumanResource/PaySlip/PaySlip.entity'; |
5
|
|
|
import {User} from 'src/Domain/HumanResource/User/User.entity'; |
6
|
|
|
|
7
|
|
|
export class PaySlipRepository implements IPaySlipRepository { |
8
|
|
|
constructor( |
9
|
|
|
@InjectRepository(PaySlip) |
10
|
|
|
private readonly repository: Repository<PaySlip> |
11
|
|
|
) {} |
12
|
|
|
|
13
|
|
|
public save(paySlip: PaySlip): Promise<PaySlip> { |
14
|
|
|
return this.repository.save(paySlip); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public findOneByUserAndDate( |
18
|
|
|
user: User, |
19
|
|
|
date: Date |
20
|
|
|
): Promise<PaySlip | undefined> { |
21
|
|
|
const month = new Date(date).getMonth() + 1; |
22
|
|
|
const year = new Date(date).getFullYear(); |
23
|
|
|
|
24
|
|
|
return this.repository |
25
|
|
|
.createQueryBuilder('paySlip') |
26
|
|
|
.select(['paySlip.id']) |
27
|
|
|
.where('paySlip.user = :userId', {userId: user.getId()}) |
28
|
|
|
.andWhere('extract(month FROM paySlip.date) = :month', {month}) |
29
|
|
|
.andWhere('extract(year FROM paySlip.date) = :year', {year}) |
30
|
|
|
.getOne(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public findOneById(id: string): Promise<PaySlip | undefined> { |
34
|
|
|
return this.repository |
35
|
|
|
.createQueryBuilder('paySlip') |
36
|
|
|
.select([ |
37
|
|
|
'paySlip.id', |
38
|
|
|
'paySlip.date', |
39
|
|
|
'user.id', |
40
|
|
|
'user.firstName', |
41
|
|
|
'user.lastName', |
42
|
|
|
'file.id', |
43
|
|
|
'file.name', |
44
|
|
|
'file.size' |
45
|
|
|
]) |
46
|
|
|
.innerJoin('paySlip.file', 'file') |
47
|
|
|
.innerJoin('paySlip.user', 'user') |
48
|
|
|
.where('paySlip.id = :id', {id}) |
49
|
|
|
.getOne(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public findAll(): Promise<PaySlip[]> { |
53
|
|
|
return this.repository |
54
|
|
|
.createQueryBuilder('paySlip') |
55
|
|
|
.select([ |
56
|
|
|
'paySlip.id', |
57
|
|
|
'paySlip.date', |
58
|
|
|
'user.id', |
59
|
|
|
'user.firstName', |
60
|
|
|
'user.lastName', |
61
|
|
|
'file.id', |
62
|
|
|
'file.name', |
63
|
|
|
'file.size' |
64
|
|
|
]) |
65
|
|
|
.innerJoin('paySlip.file', 'file') |
66
|
|
|
.innerJoin('paySlip.user', 'user') |
67
|
|
|
.orderBy('paySlip.date', 'DESC') |
68
|
|
|
.getMany(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|