-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportSearcher.js
More file actions
192 lines (154 loc) · 6.52 KB
/
ReportSearcher.js
File metadata and controls
192 lines (154 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const ReportsDataBaseHandler = require('./ReportsDataBaseHandler')
const EmployeesDataBaseHandler = require('./EmployeesDataBaseHandler')
const ProjectsApiHandler = require('./ProjectsApiHandler')
const db_password = require('./dbpswd')
class ReportSearcher {
constructor() {
this.reportsDbHandler = new ReportsDataBaseHandler({
user: 'fqhdvfjibpslvq',
host: 'ec2-52-86-177-34.compute-1.amazonaws.com',
database: 'd591fltfo2mqg4',
password: process.env.DATABASE_PASSWORD || db_password,
port: 5432,
ssl: {
rejectUnauthorized: false
}
})
this.employeesDbHanbler = new EmployeesDataBaseHandler({
user: 'fqhdvfjibpslvq',
host: 'ec2-52-86-177-34.compute-1.amazonaws.com',
database: 'd591fltfo2mqg4',
password: process.env.DATABASE_PASSWORD || db_password,
port: 5432,
ssl: {
rejectUnauthorized: false
}
})
this.projectsApiHandler = new ProjectsApiHandler()
}
async getReports() {
const projects = await this.projectsApiHandler.getAllProjects()
const projectDict = this.getProjectDict(projects)
const tasks = await this.projectsApiHandler.getAllTasks()
const taskDict = this.getTaskDict(tasks)
const reports = await this.reportsDbHandler.getReports()
let updatedReports = []
for (let report of reports) {
if (!taskDict[report.task_id]) {
report.project = 'Deleted project'
report.task = 'Deleted task'
} else {
const projectId = taskDict[report.task_id].project_id
const projectName = projectDict[projectId]
const taskName = taskDict[report.task_id].name
report.project = projectName
report.task = taskName
}
delete report.task_id
updatedReports.push(report)
}
return updatedReports
}
async getReportsByDate(init_date, end_date) {
// The function returns all the reports which were accomplished between 'init_date' and 'end_date'
// string parameters.
// The return value is an array of objects in the format:
// {id, employee name, employee last name, project name, task name, date, minutes dedicated}
const projects = await this.projectsApiHandler.getAllProjects()
const projectDict = this.getProjectDict(projects)
const tasks = await this.projectsApiHandler.getAllTasks()
const taskDict = this.getTaskDict(tasks)
const reports = await this.reportsDbHandler.getReportsByDate(init_date, end_date)
let updatedReports = []
for (let report of reports) {
if (!taskDict[report.task_id]) {
report.project = 'Deleted project'
report.task = 'Deleted task'
} else {
const projectId = taskDict[report.task_id].project_id
const projectName = projectDict[projectId]
const taskName = taskDict[report.task_id].name
report.project = projectName
report.task = taskName
}
delete report.task_id
updatedReports.push(report)
}
return updatedReports
}
async getReportsByProjectId(projectId) {
// The function returns all the reports where the project id is 'projectId'.
// The return value is an array of objects in the format:
// {id, employee name, employee last name, project name, task name, date, minutes dedicated}
const projects = await this.projectsApiHandler.getAllProjects()
const project = projects.filter(p => (p.id === projectId))[0]
if (project == undefined) return []
const tasks = await this.projectsApiHandler.getAllTasksFromProject(projectId)
let reports = []
for (let task of tasks) {
const newReports = await this.reportsDbHandler.getReportsByTaskId(task.id)
for (let report of newReports) {
const task = tasks.filter(w => (w.id === report.task_id))[0]
let taskName
if (task == undefined) {
taskName = 'Deleted task'
} else {
taskName = task.name
}
report.project = project.name
report.task = taskName
delete report.task_id
reports.push(report)
}
}
return reports
}
async getReportsByTaskId(taskId) {
// The function returns all the reports where the task id is 'taskId'.
// The return value is an array of objects in the format:
// {id, employee name, employee last name, project name, task name, date, minutes dedicated}
const projectId = await this.projectsApiHandler.getProjectIdAssociatedToTask(taskId)
if (projectId == undefined) return []
const projects = await this.projectsApiHandler.getAllProjects()
const projectName = projects.filter(p => (p.id === projectId))[0].name
const tasks = await this.projectsApiHandler.getAllTasksFromProject(projectId)
const task = tasks.filter(w => (w.id === taskId))[0]
if (task == undefined) return []
const taskName = task.name
const reports = await this.reportsDbHandler.getReportsByTaskId(taskId)
let updatedReports = []
for (let report of reports) {
report.project = projectName
report.task = taskName
delete report.task_id
updatedReports.push(report)
}
return updatedReports
}
async getTimeDestinedToProject(projectId) {
// The function returns the time in minutes destined to all the reports where
// the project id is 'projectId'.
const tasks = await this.projectsApiHandler.getAllTasksFromProject(projectId)
if (!tasks[0]) return 0
const tasksIds = tasks.map(t => t.id)
return await this.reportsDbHandler.getTimeDestinedToTasks(tasksIds)
}
getProjectDict(projects) {
let dict = {}
for (let p of projects) {
dict[p.id] = p.name
}
return dict
}
getTaskDict(tasks) {
let dict = {}
for (let t of tasks) {
dict[t.id] = {
name: t.name,
project_id: t.id_project
}
}
return dict
}
}
module.exports = ReportSearcher