-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportsDataBaseHandler.js
More file actions
94 lines (74 loc) · 3.89 KB
/
ReportsDataBaseHandler.js
File metadata and controls
94 lines (74 loc) · 3.89 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
const { Client } = require('pg')
class ReportsDataBaseHandler {
constructor(connection_data) {
this.client = new Client(connection_data)
this.client.connect()
}
async deleteReport(reportId) {
// Deletes the report with id 'reportId' from the database.
await this.client.query(` DELETE FROM public.assigned_time
WHERE id = ${reportId}
`)
return { status: "OK" }
}
async getReports() {
const res = await this.client.query(`SELECT assigned_time.id, name, last_name, task_id, date, minutes
FROM public.assigned_time
INNER JOIN public.resources ON resources.id = assigned_time.resource_id
`)
return res.rows
}
async getReportsByDate(init_date, end_date) {
// 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, task id, date, minutes dedicated}
const res = await this.client.query(`SELECT assigned_time.id, name, last_name, task_id, date, minutes
FROM public.assigned_time
INNER JOIN public.resources ON resources.id = assigned_time.resource_id
WHERE date BETWEEN '${init_date}':: date AND '${end_date}':: date
`)
return res.rows
}
async getReportsByTaskId(taskId) {
// 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, task id, date, minutes dedicated}
const res = await this.client.query(`SELECT assigned_time.id, name, last_name, task_id, date, minutes
FROM public.assigned_time
INNER JOIN public.resources ON resources.id = assigned_time.resource_id
WHERE task_id = ${taskId}
`)
return res.rows
}
async getTimeDestinedToTasks(tasksIds) {
// The function returns the time in minutes destined to all the reports where
// the task id is in 'tasksIds' array.
const res = await this.client.query(`SELECT SUM(minutes)
FROM public.assigned_time
WHERE task_id IN (${tasksIds})
`)
return res.rows[0].sum
}
async saveReport(employeeId, taskId, date,
duration, description) {
// Saves the report in the database.
// 'date' parameter as a string in the following format: 'yyyy/mm/dd'.
// 'duration' parameter indicates the duration of the task in minutes.
await this.client.query(` INSERT INTO public.assigned_time
(minutes, date, resource_id, task_id, description)
VALUES
(${duration}, '${date}'::date, ${employeeId}, ${taskId}, '${description}')
`)
return { status: 201, statusMsg: "OK" }
}
async updateReport(reportId, minutes) {
// Updates the time destined to report with id 'reportId' in the database.
await this.client.query(`UPDATE public.assigned_time
SET minutes = ${minutes}
WHERE id = ${reportId}
`)
return { status: "OK" }
}
}
module.exports = ReportsDataBaseHandler