|
1
|
|
|
import {Injectable} from '@nestjs/common'; |
|
2
|
|
|
import {InjectRepository} from '@nestjs/typeorm'; |
|
3
|
|
|
import {Repository} from 'typeorm'; |
|
4
|
|
|
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
|
5
|
|
|
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity'; |
|
6
|
|
|
import {User} from 'src/Domain/HumanResource/User/User.entity'; |
|
7
|
|
|
import { Customer } from 'src/Domain/Customer/Customer.entity'; |
|
8
|
|
|
import { DailyRate } from 'src/Domain/Accounting/DailyRate.entity'; |
|
9
|
|
|
|
|
10
|
|
|
@Injectable() |
|
11
|
|
|
export class EventRepository implements IEventRepository { |
|
12
|
|
|
constructor( |
|
13
|
|
|
@InjectRepository(Event) |
|
14
|
|
|
private readonly repository: Repository<Event> |
|
15
|
|
|
) {} |
|
16
|
|
|
|
|
17
|
|
|
public save(event: Event): Promise<Event> { |
|
18
|
|
|
return this.repository.save(event); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public delete(event: Event): void { |
|
22
|
|
|
this.repository.delete(event.getId()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public async sumOfTimeSpentByUserAndDate( |
|
26
|
|
|
user: User, |
|
27
|
|
|
date: string |
|
28
|
|
|
): Promise<number> { |
|
29
|
|
|
const result = await this.repository |
|
30
|
|
|
.createQueryBuilder('event') |
|
31
|
|
|
.select('SUM(event.time) as time') |
|
32
|
|
|
.where('event.date = :date', {date}) |
|
33
|
|
|
.andWhere('user.id = :user', {user: user.getId()}) |
|
34
|
|
|
.innerJoin('event.user', 'user') |
|
35
|
|
|
.getRawOne(); |
|
36
|
|
|
|
|
37
|
|
|
return Number(result.time) || 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public findOneById(id: string): Promise<Event> { |
|
41
|
|
|
return this.repository |
|
42
|
|
|
.createQueryBuilder('event') |
|
43
|
|
|
.select([ |
|
44
|
|
|
'event.id', |
|
45
|
|
|
'event.time', |
|
46
|
|
|
'event.summary', |
|
47
|
|
|
'event.billable', |
|
48
|
|
|
'event.date', |
|
49
|
|
|
'event.type', |
|
50
|
|
|
'user.id', |
|
51
|
|
|
'project.id', |
|
52
|
|
|
'project.name', |
|
53
|
|
|
'task.id', |
|
54
|
|
|
'task.name' |
|
55
|
|
|
]) |
|
56
|
|
|
.where('event.id = :id', {id}) |
|
57
|
|
|
.innerJoin('event.user', 'user') |
|
58
|
|
|
.leftJoin('event.project', 'project') |
|
59
|
|
|
.leftJoin('event.task', 'task') |
|
60
|
|
|
.getOne(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public findMonthlyEvents(date: string, userId: string): Promise<Event[]> { |
|
64
|
|
|
const month = new Date(date).getMonth() + 1; |
|
65
|
|
|
const year = new Date(date).getFullYear(); |
|
66
|
|
|
|
|
67
|
|
|
return this.repository |
|
68
|
|
|
.createQueryBuilder('event') |
|
69
|
|
|
.select([ |
|
70
|
|
|
'event.id', |
|
71
|
|
|
'event.time', |
|
72
|
|
|
'event.date', |
|
73
|
|
|
'event.billable', |
|
74
|
|
|
'event.type', |
|
75
|
|
|
'project.id', |
|
76
|
|
|
'project.name', |
|
77
|
|
|
'project.dayDuration', |
|
78
|
|
|
'task.id', |
|
79
|
|
|
'task.name' |
|
80
|
|
|
]) |
|
81
|
|
|
.where('user.id = :userId', {userId}) |
|
82
|
|
|
.andWhere('extract(month FROM event.date) = :month', {month}) |
|
83
|
|
|
.andWhere('extract(year FROM event.date) = :year', {year}) |
|
84
|
|
|
.innerJoin('event.user', 'user') |
|
85
|
|
|
.leftJoin('event.project', 'project') |
|
86
|
|
|
.leftJoin('event.task', 'task') |
|
87
|
|
|
.orderBy('event.date', 'ASC') |
|
88
|
|
|
.getMany(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public findBillableEventsByMonthAndCustomer(date: Date, customer: Customer): Promise<any[]> { |
|
92
|
|
|
const month = date.getMonth() + 1; |
|
93
|
|
|
const year = date.getFullYear(); |
|
94
|
|
|
|
|
95
|
|
|
return this.repository |
|
96
|
|
|
.createQueryBuilder('event') |
|
97
|
|
|
.select([ |
|
98
|
|
|
'event.time as time', |
|
99
|
|
|
'event.billable as billable', |
|
100
|
|
|
'project.dayDuration as day_duration', |
|
101
|
|
|
'project.name as project_name', |
|
102
|
|
|
'task.name as task_name', |
|
103
|
|
|
'user.firstName as first_name', |
|
104
|
|
|
'user.lastName as last_name' |
|
105
|
|
|
]) |
|
106
|
|
|
.where('customer.id = :id', { id: customer.getId() }) |
|
107
|
|
|
.andWhere('extract(month FROM event.date) = :month', {month}) |
|
108
|
|
|
.andWhere('extract(year FROM event.date) = :year', {year}) |
|
109
|
|
|
.andWhere('event.type = :type', { type: EventType.MISSION }) |
|
110
|
|
|
.addSelect(query => { |
|
111
|
|
|
return query |
|
112
|
|
|
.select('dailyRate.amount') |
|
113
|
|
|
.from(DailyRate, 'dailyRate') |
|
114
|
|
|
.where('d_user.id = user.id') |
|
115
|
|
|
.andWhere('d_task.id = task.id') |
|
116
|
|
|
.andWhere('d_customer.id = :id', { id: customer.getId() }) |
|
117
|
|
|
.innerJoin('dailyRate.user', 'd_user') |
|
118
|
|
|
.innerJoin('dailyRate.task', 'd_task') |
|
119
|
|
|
.innerJoin('dailyRate.customer', 'd_customer') |
|
120
|
|
|
}, 'amount') |
|
121
|
|
|
.innerJoin('event.project', 'project') |
|
122
|
|
|
.innerJoin('project.customer', 'customer') |
|
123
|
|
|
.innerJoin('event.user', 'user') |
|
124
|
|
|
.innerJoin('event.task', 'task') |
|
125
|
|
|
.groupBy('user.id, event.time, project.id, task.id, event.billable') |
|
126
|
|
|
.getRawMany(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public async countEventsByUserAndPeriod( |
|
130
|
|
|
user: User, |
|
131
|
|
|
startDate: string, |
|
132
|
|
|
endDate: string |
|
133
|
|
|
): Promise<number> { |
|
134
|
|
|
const result = await this.repository |
|
135
|
|
|
.createQueryBuilder('event') |
|
136
|
|
|
.select('count(event.id) as id') |
|
137
|
|
|
.where('user.id = :id', {id: user.getId()}) |
|
138
|
|
|
.andWhere('event.date BETWEEN :startDate AND :endDate', { |
|
139
|
|
|
startDate, |
|
140
|
|
|
endDate |
|
141
|
|
|
}) |
|
142
|
|
|
.innerJoin('event.user', 'user') |
|
143
|
|
|
.getRawOne(); |
|
144
|
|
|
|
|
145
|
|
|
return Number(result.id) || 0; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|