Se crea el parser para customfilter. Agregamos variables de entorno para manejar url de adjuntos con o sin clave de encriptacion.

This commit is contained in:
2025-05-28 14:40:15 -03:00
parent 6b9c66c385
commit 4a9cb34f9c
8 changed files with 318 additions and 78 deletions

View File

@ -3,14 +3,23 @@ defmodule Api.Studies do
alias Api.Repo
def studies_sql_query(filters) do
customfilter =
case Map.get(filters, "customfilter") do
nil -> dynamic([_], true)
"" -> dynamic([_], true)
expr ->
case ExpressionParser.parse(expr) do
{:ok, ast} -> ExpressionToEcto.to_dynamic(ast)
{:error, _reason} -> raise "Error al parsear customfilter"
end
end
page = filters["page"] || 1
size = filters["size"] || 24
filter = filters["filter"] || []
sort = filters["sort"] || [%{"dir" => "desc", "field" => "studydate"}]
# Construcción de condiciones de filtro dinámicas
filter_conditions =
Enum.reduce(filter, dynamic(true), fn f, acc ->
@ -23,7 +32,7 @@ defmodule Api.Studies do
dynamic([s], ilike(s.modality, ^"%#{value}%"))
"idstudy" ->
dynamic([s], s.idstudy == ^String.to_integer(value))
dynamic([s], like(fragment("CAST(? AS TEXT)", s.idstudy), ^"#{value}%"))
"studydate" ->
dynamic([s], fragment("CAST(? AS DATE)::text LIKE ?", s.studydate, ^"%#{value}%"))
@ -82,35 +91,37 @@ defmodule Api.Studies do
{direction, field}
end)
combined_filter =
dynamic([q], ^filter_conditions and ^customfilter)
query =
from s in "study",
join: p in "patient",
on: p.idpatient == s.idpatient,
left_join: sr in "studyreport",
on: sr.idstudy == s.idstudy,
left_join: st in "statuses",
on: st.idstatus == sr.idstudyreport,
where: ^filter_conditions,
select: %{
recordstotal: fragment("count(*) over()"),
idstudy: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", s.idstudy),
#idstudy: s.idstudy,
accessionnumber: s.accessionnumber,
studydate: s.studydate,
studytime: s.studytime,
patientname: fragment("select replace(?, '^', ' ')", p.patientname),
proceduredescription: s.studydescription,
modality: s.modality,
sitename: s.institutionname,
insurer: s.insurer,
nrodocumento: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", p.patientid),
#nrodocumento: p.patientid,
hasaudio: fragment("CASE WHEN ? IS NOT NULL THEN true ELSE false END", s.audiofile)
},
order_by: ^sort_conditions,
limit: ^size,
offset: ^((page - 1) * size)
from s in "study",
join: p in "patient",
on: p.idpatient == s.idpatient,
left_join: sr in "studyreport",
on: sr.idstudy == s.idstudy,
left_join: st in "statuses",
on: st.idstatus == sr.idstudyreport,
where: ^combined_filter,
select: %{
recordstotal: fragment("count(*) over()"),
idstudy: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", s.idstudy),
#idstudy: s.idstudy,
accessionnumber: s.accessionnumber,
studydate: s.studydate,
studytime: s.studytime,
patientname: fragment("select replace(?, '^', ' ')", p.patientname),
proceduredescription: s.studydescription,
modality: s.modality,
sitename: s.institutionname,
insurer: s.insurer,
nrodocumento: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", p.patientid),
#nrodocumento: p.patientid,
hasaudio: fragment("CASE WHEN ? IS NOT NULL THEN true ELSE false END", s.audiofile)
},
order_by: ^sort_conditions,
limit: ^size,
offset: ^((page - 1) * size)
result = Repo.one(