1
|
|
|
import { InjectRepository } from '@nestjs/typeorm'; |
2
|
|
|
import { Repository } from 'typeorm'; |
3
|
|
|
import { IHolidayRequestRepository } from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRequestRepository'; |
4
|
|
|
import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination'; |
5
|
|
|
import { HolidayRequest, HolidayStatus } from 'src/Domain/HumanResource/Holiday/HolidayRequest.entity'; |
6
|
|
|
import { User } from 'src/Domain/HumanResource/User/User.entity'; |
7
|
|
|
|
8
|
|
|
export class HolidayRequestRepository implements IHolidayRequestRepository { |
9
|
|
|
constructor( |
10
|
|
|
@InjectRepository(HolidayRequest) |
11
|
|
|
private readonly repository: Repository<HolidayRequest> |
12
|
|
|
) {} |
13
|
|
|
|
14
|
|
|
public save(holidayRequest: HolidayRequest): Promise<HolidayRequest> { |
15
|
|
|
return this.repository.save(holidayRequest); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public findOneById(id: string): Promise<HolidayRequest | undefined> { |
19
|
|
|
return this.repository |
20
|
|
|
.createQueryBuilder('holidayRequest') |
21
|
|
|
.select(['holidayRequest.id', 'holidayRequest.status', 'user.id']) |
22
|
|
|
.where('holidayRequest.id = :id', {id}) |
23
|
|
|
.innerJoin('holidayRequest.user', 'user') |
24
|
|
|
.getOne(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public findExistingHolidayRequestsByUserAndPeriod( |
28
|
|
|
user: User, |
29
|
|
|
startDate: string, |
30
|
|
|
endDate: string |
31
|
|
|
): Promise<HolidayRequest | undefined> { |
32
|
|
|
return this.repository |
33
|
|
|
.createQueryBuilder('holidayRequest') |
34
|
|
|
.select('holidayRequest.id') |
35
|
|
|
.where('holidayRequest.user = :id', {id: user.getId()}) |
36
|
|
|
.andWhere( |
37
|
|
|
'(holidayRequest.startDate BETWEEN :startDate AND :endDate OR holidayRequest.endDate BETWEEN :startDate AND :endDate)', |
38
|
|
|
{ |
39
|
|
|
startDate, |
40
|
|
|
endDate |
41
|
|
|
} |
42
|
|
|
) |
43
|
|
|
.andWhere('(holidayRequest.status = :accepted OR holidayRequest.status = :pending)', { |
44
|
|
|
accepted: HolidayStatus.ACCEPTED, |
45
|
|
|
pending: HolidayStatus.PENDING |
46
|
|
|
}) |
47
|
|
|
.getOne(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public findHolidayRequests(page: number): Promise<[HolidayRequest[], number]> { |
51
|
|
|
return this.repository |
52
|
|
|
.createQueryBuilder('holidayRequest') |
53
|
|
|
.select([ |
54
|
|
|
'holidayRequest.id', |
55
|
|
|
'holidayRequest.type', |
56
|
|
|
'holidayRequest.status', |
57
|
|
|
'holidayRequest.startDate', |
58
|
|
|
'holidayRequest.startsAllDay', |
59
|
|
|
'holidayRequest.endDate', |
60
|
|
|
'holidayRequest.endsAllDay', |
61
|
|
|
'user.id', |
62
|
|
|
'user.firstName', |
63
|
|
|
'user.lastName' |
64
|
|
|
]) |
65
|
|
|
.innerJoin('holidayRequest.user', 'user') |
66
|
|
|
.orderBy('holidayRequest.startDate', 'ASC') |
67
|
|
|
.limit(MAX_ITEMS_PER_PAGE) |
68
|
|
|
.offset((page - 1) * MAX_ITEMS_PER_PAGE) |
69
|
|
|
.getManyAndCount(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|