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:
@ -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(
|
||||
|
@ -9,7 +9,6 @@ defmodule Api.Study do
|
||||
|
||||
idsite = Envar.get("IDSITE") |> String.to_integer
|
||||
domain = Envar.get("CHECK_ORIGIN")
|
||||
escaneados = Envar.get("ESCANEADOS")
|
||||
|
||||
id_study = if Envar.get("IDENTIFIERFIELD") == "ACCESSIONNUMBER" do
|
||||
query =
|
||||
@ -25,15 +24,6 @@ defmodule Api.Study do
|
||||
|
||||
id_study = if is_integer(id_study), do: id_study, else: String.to_integer(id_study)
|
||||
|
||||
|
||||
# accession_fragment =
|
||||
# if Envar.get("IDENTIFIERFIELD") == "ACCESSIONNUMBER" do
|
||||
# fragment("?::text", ^accession)
|
||||
# else
|
||||
# fragment("?::integer", ^id_study)
|
||||
# end
|
||||
|
||||
|
||||
Logger.info("id_study -------> #{inspect(id_study)}")
|
||||
|
||||
# Las siguientes consultas obtienen la información completa
|
||||
@ -70,56 +60,109 @@ defmodule Api.Study do
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# escaneados
|
||||
subquery2 = subquery(
|
||||
from s in "study",
|
||||
join: ss in "studyscans", on: ss.idstudy == s.idstudy,
|
||||
join: sc in "scanclasses", on: sc.idscanclass == ss.idscanclass,
|
||||
join: p in "patient", on: s.idpatient == p.idpatient,
|
||||
where: s.idstudy == ^id_study,
|
||||
select: %{
|
||||
idsite: type(^idsite, :integer),
|
||||
escaneados_clave = Envar.get("ESCANEADOS_CLAVE")
|
||||
base_escaneados = Envar.get("ESCANEADOS")
|
||||
|
||||
escaneados_expr =
|
||||
if escaneados_clave do
|
||||
dynamic([_, ss, _, _],
|
||||
fragment(
|
||||
"concat(?::text, substring(encrypt(?::text::bytea, ?, 'aes')::text from 3))",
|
||||
^base_escaneados,
|
||||
ss.idstudyscan,
|
||||
^escaneados_clave
|
||||
)
|
||||
)
|
||||
else
|
||||
dynamic([_, ss, _, _],
|
||||
fragment(
|
||||
"concat(?::text, ?::text)",
|
||||
^base_escaneados,
|
||||
ss.idstudyscan
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
select_expr_es =
|
||||
dynamic([s, ss, sc, p], %{
|
||||
idsite: type(^idsite, :integer),
|
||||
iddocument: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", ss.idstudyscan),
|
||||
document_name: sc.scanclass,
|
||||
document_type: "url",
|
||||
url: fragment(
|
||||
"concat(?::text, substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3))",
|
||||
^escaneados,
|
||||
ss.idstudyscan
|
||||
),
|
||||
url: ^escaneados_expr,
|
||||
patientname: p.patientname,
|
||||
proceduredescription: s.studydescription,
|
||||
studydate: fragment("TO_CHAR(?, 'YYYY-MM-DD')", s.studydate),
|
||||
studytime: fragment("TO_CHAR(?, 'HH24:MI:SS')", s.studytime),
|
||||
accessionnumber: s.idstudy,
|
||||
patientid: p.patientid
|
||||
}
|
||||
})
|
||||
|
||||
subquery2 = subquery(
|
||||
from s in "study",
|
||||
join: ss in "studyscans", on: ss.idstudy == s.idstudy,
|
||||
join: sc in "scanclasses", on: sc.idscanclass == ss.idscanclass,
|
||||
join: p in "patient", on: s.idpatient == p.idpatient,
|
||||
where: s.idstudy == ^id_study,
|
||||
select: ^select_expr_es
|
||||
)
|
||||
|
||||
# adjuntos
|
||||
subquery3 =
|
||||
adjuntos_clave = Envar.get("ADJUNTOS_CLAVE")
|
||||
base_adjuntos = Envar.get("ADJUNTOS")
|
||||
|
||||
adjuntos_expr =
|
||||
if adjuntos_clave do
|
||||
dynamic([_, sa, _],
|
||||
fragment(
|
||||
"concat(?::text, substring(encrypt(?::text::bytea, ?, 'aes')::text from 3))",
|
||||
^base_adjuntos,
|
||||
sa.idstudyattachment,
|
||||
^adjuntos_clave
|
||||
)
|
||||
)
|
||||
else
|
||||
dynamic([_, sa, _],
|
||||
fragment(
|
||||
"concat(?::text, ?::text)",
|
||||
^base_adjuntos,
|
||||
sa.idstudyattachment
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
select_expr_ad =
|
||||
dynamic([s, sa, p], %{
|
||||
idsite: type(^idsite, :integer),
|
||||
iddocument:
|
||||
fragment(
|
||||
"substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)",
|
||||
sa.idstudyattachment
|
||||
),
|
||||
document_name: sa.name,
|
||||
document_type:
|
||||
fragment(
|
||||
"CASE WHEN ? NOT IN ('jpg', 'jpeg', 'png') THEN 'attachment' ELSE 'url' END",
|
||||
sa.format
|
||||
),
|
||||
url: ^adjuntos_expr,
|
||||
patientname: p.patientname,
|
||||
proceduredescription: s.studydescription,
|
||||
studydate: fragment("TO_CHAR(?, 'YYYY-MM-DD')", s.studydate),
|
||||
studytime: fragment("TO_CHAR(?, 'HH24:MI:SS')", s.studytime),
|
||||
accessionnumber: s.idstudy,
|
||||
patientid: ""
|
||||
})
|
||||
|
||||
subquery3 = subquery(
|
||||
from s in "study",
|
||||
join: sa in "studyattachments", on: sa.idstudy == s.idstudy,
|
||||
join: p in "patient", on: p.idpatient == s.idpatient,
|
||||
where: s.idstudy == ^id_study,
|
||||
select: %{
|
||||
idsite: type(^idsite, :integer),
|
||||
iddocument: fragment("substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3)", sa.idstudyattachment),
|
||||
document_name: sa.name,
|
||||
document_type: fragment("CASE WHEN ? NOT IN ('jpg', 'jpeg', 'png') THEN 'attachment' ELSE 'url' END", sa.format),
|
||||
url: fragment(
|
||||
"concat(?::text, ?::text, substring(encrypt(?::text::bytea, '1nf0rm3', 'aes')::text from 3))",
|
||||
^domain,
|
||||
"/api/attachment/",
|
||||
sa.idstudyattachment
|
||||
),
|
||||
patientname: p.patientname,
|
||||
proceduredescription: s.studydescription,
|
||||
studydate: fragment("TO_CHAR(?, 'YYYY-MM-DD')", s.studydate),
|
||||
studytime: fragment("TO_CHAR(?, 'HH24:MI:SS')", s.studytime),
|
||||
accessionnumber: s.idstudy,
|
||||
patientid: ""
|
||||
}
|
||||
select: ^select_expr_ad
|
||||
)
|
||||
|
||||
# Si no encontró informes, ni adjuntos, ni nada
|
||||
# se traen sólo los datos básicos del estudio
|
||||
|
Reference in New Issue
Block a user