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