-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_and_format.rb
140 lines (134 loc) · 3.85 KB
/
query_and_format.rb
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
class QueryAndFormat
def self.get_all_tests
query = self.query_all_tests
json = self.format_to_json(query)
final = self.group_tests(json)
JSON.pretty_generate(final)
end
def self.get_single_test(token)
query = self.query_single_test(token)
json = self.format_to_json(query)
final = self.group_tests(json)
JSON.pretty_generate(final[0])
end
private
def self.query_single_test(token)
if ENV['APP_ENV'] != 'test'
db = PG.connect(host: 'postgres-server', user: 'postgres')
else
db = PG.connect(host: 'postgres-server-test', user: 'postgres')
end
data = db.exec("SELECT
t.token AS result_token,
t.date AS result_date,
p.cpf,
p.name,
p.email,
p.birthday,
json_build_object(
'crm', d.crm,
'crm_state', d.crm_state,
'name', d.name
) AS doctor,
json_build_object(
'type', tt.name,
'limits', tt.limits,
'result', ti.result
) AS tests
FROM tests AS t
JOIN patients p ON t.patient_id = p.id
JOIN doctors d ON t.doctor_id = d.id
JOIN test_items ti ON ti.test_id = t.id
JOIN test_types tt ON ti.test_type_id = tt.id
WHERE t.token = $1
GROUP BY t.id, t.date, p.cpf, p.name, p.email, p.birthday, d.crm, d.crm_state, d.name, tt.name, tt.limits, ti.id
ORDER BY date DESC
", [token]).to_a
db.close
data
end
def self.query_all_tests
if ENV['APP_ENV'] != 'test'
db = PG.connect(host: 'postgres-server', user: 'postgres')
else
db = PG.connect(host: 'postgres-server-test', user: 'postgres')
end
data = db.exec("SELECT
t.token AS result_token,
t.date AS result_date,
p.cpf,
p.name,
p.email,
p.birthday,
json_build_object(
'crm', d.crm,
'crm_state', d.crm_state,
'name', d.name
) AS doctor,
json_build_object(
'type', tt.name,
'limits', tt.limits,
'result', ti.result
) AS tests
FROM tests AS t
JOIN patients p ON t.patient_id = p.id
JOIN doctors d ON t.doctor_id = d.id
JOIN test_items ti ON ti.test_id = t.id
JOIN test_types tt ON ti.test_type_id = tt.id
GROUP BY t.id, t.date, p.cpf, p.name, p.email, p.birthday, d.crm, d.crm_state, d.name, tt.name, tt.limits, ti.id
ORDER BY date DESC
").to_a
db.close
data
end
def self.format_to_json(data)
data.map do |row|
{
'result_token' => row['result_token'],
'result_date' => row['result_date'],
'cpf' => row['cpf'],
'name' => row['name'],
'email' => row['email'],
'birthday' => row['birthday'],
'doctor' => JSON.parse(row['doctor']),
'tests' => JSON.parse(row['tests'])
}
end
end
def self.group_tests(data)
grouped_data = []
data.each do |record|
token = record['result_token']
found_index = nil
grouped_data.each_with_index do |item, index|
if item['result_token'] == token
found_index = index
break
end
end
if found_index
grouped_data[found_index]['tests'] << {
'type' => record['tests']['type'],
'limits' => record['tests']['limits'],
'result' => record['tests']['result']
}
else
grouped_data << {
'result_token' => token,
'result_date' => record['result_date'],
'cpf' => record['cpf'],
'name' => record['name'],
'email' => record['email'],
'birthday' => record['birthday'],
'doctor' => record['doctor'],
'tests' => [{
'type' => record['tests']['type'],
'limits' => record['tests']['limits'],
'result' => record['tests']['result']
}]
}
end
end
grouped_data
end
end