|
1
|
|
|
import {InjectRepository} from '@nestjs/typeorm'; |
|
2
|
|
|
import {Repository} from 'typeorm'; |
|
3
|
|
|
import {IHolidayRepository} from 'src/Domain/HumanResource/Holiday/Repository/IHolidayRepository'; |
|
4
|
|
|
import { |
|
5
|
|
|
Holiday, |
|
6
|
|
|
HolidayStatus |
|
7
|
|
|
} from 'src/Domain/HumanResource/Holiday/Holiday.entity'; |
|
8
|
|
|
import {User} from 'src/Domain/HumanResource/User/User.entity'; |
|
9
|
|
|
|
|
10
|
|
|
export class HolidayRepository implements IHolidayRepository { |
|
11
|
|
|
constructor( |
|
12
|
|
|
@InjectRepository(Holiday) |
|
13
|
|
|
private readonly repository: Repository<Holiday> |
|
14
|
|
|
) {} |
|
15
|
|
|
|
|
16
|
|
|
public save(holiday: Holiday): Promise<Holiday> { |
|
17
|
|
|
return this.repository.save(holiday); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public findOneById(id: string): Promise<Holiday | undefined> { |
|
21
|
|
|
return this.repository |
|
22
|
|
|
.createQueryBuilder('holiday') |
|
23
|
|
|
.select(['holiday.id', 'holiday.status', 'user.id']) |
|
24
|
|
|
.where('holiday.id = :id', {id}) |
|
25
|
|
|
.innerJoin('holiday.user', 'user') |
|
26
|
|
|
.getOne(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public findExistingHolidaysByUserAndPeriod( |
|
30
|
|
|
user: User, |
|
31
|
|
|
startDate: string, |
|
32
|
|
|
endDate: string |
|
33
|
|
|
): Promise<Holiday | undefined> { |
|
34
|
|
|
return this.repository |
|
35
|
|
|
.createQueryBuilder('holiday') |
|
36
|
|
|
.select('holiday.id') |
|
37
|
|
|
.where('holiday.user = :id', {id: user.getId()}) |
|
38
|
|
|
.andWhere( |
|
39
|
|
|
'(holiday.startDate BETWEEN :startDate AND :endDate OR holiday.endDate BETWEEN :startDate AND :endDate)', |
|
40
|
|
|
{ |
|
41
|
|
|
startDate, |
|
42
|
|
|
endDate |
|
43
|
|
|
} |
|
44
|
|
|
) |
|
45
|
|
|
.andWhere('(holiday.status = :accepted OR holiday.status = :pending)', { |
|
46
|
|
|
accepted: HolidayStatus.ACCEPTED, |
|
47
|
|
|
pending: HolidayStatus.PENDING |
|
48
|
|
|
}) |
|
49
|
|
|
.getOne(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|