Gethash para ImViewer4, configurable en .env

This commit is contained in:
2025-07-21 14:13:08 -03:00
parent 9938943989
commit 0a2d0a714c
2 changed files with 48 additions and 36 deletions

View File

@ -28,6 +28,7 @@ defmodule ApiWeb.IndexLive do
|> assign(logo: Envar.get("LOGO"))
|> assign(logo_sm: Envar.get("LOGO_SM"))
|> assign(idstudyfield: idstudyfield)
|> assign(idstudy: nil)
{:ok, socket}
end
@ -175,36 +176,6 @@ defmodule ApiWeb.IndexLive do
token
end
def generate_token_imv4(studyidentifier_type, studyidentifier, username) do
idstudy = if studyidentifier_type == "IDSTUDY" do
# El tipo es IDSTUDY
studyidentifier
else
# El tipo es ACCESSIONNUMBER
query = from s in "study",
where: s.accessionnumber == ^studyidentifier,
select: s.idstudy
case Repo.one(query) do
nil -> {:error, "No se encontró idstudy para el acccessionnumer: #{studyidentifier}"}
idstudy -> Logger.info("idstudy encontrado: #{idstudy}")
end
end
json = %{
idstudy: Integer.to_string(idstudy),
username: username,
vencimiento: DateTime.add(DateTime.utc_now(), 2 * 24 * 60 * 60, :second)
}
json_string_64 = Jason.encode!(json) |> Base.encode64()
query_json = "select encrypt('#{json_string_64}'::bytea, '1nf0rm3', 'aes')::text"
token = Repo.query!(query_json).rows |> hd() |> hd()
token = String.replace_prefix(token, "\\x", "")
token
end
def open_viewer(idstudy, accessionnumber, patientid, assigns) do
Envar.load(".env")
Envar.require_env_file(".env")
@ -212,6 +183,7 @@ defmodule ApiWeb.IndexLive do
viewer = Envar.get("imViewer4")
viewer_host = Envar.get("imViewer4_host")
viewer_uid = Envar.get("imViewer4_uid")
gethash = Envar.get("imViewer4_gethash")
studyidentifier = if studyidentifier_ed == "IDSTUDY" do
idstudy
@ -226,14 +198,51 @@ defmodule ApiWeb.IndexLive do
},
limit: 1
username = Repo.one(query)
viewer_config = %{
client_uuid: viewer_uid,
gethash: gethash,
imviewer4_domain: viewer_host
}
case viewer do
"1" -> "https://#{viewer_host}/imviewer4/viewer/#{viewer_uid}/#{generate_token_imv4(studyidentifier_ed, studyidentifier, username)}"
"1" ->
case open_imviewer4(viewer_config, studyidentifier, 172800, assigns) do
{:ok, hash} ->
"https://#{viewer_host}/imviewer4/viewer/#{viewer_uid}/#{hash}"
{:error, reason} ->
Logger.error("No se pudo obtener el hash para ImViewer4: #{inspect(reason)}")
end
"0" -> "https://estudio.informemedico.com.ar/#/#{assigns.idsite}/#{generate_token(studyidentifier_ed, studyidentifier, patientid)}"
end
end
def open_imviewer4(v, access_study, expiration, assigns) do
url = v.gethash <> v.client_uuid
if access_study == assigns.idstudy do
case Req.post(url, json: %{"idstudy" => access_study, "expiration" => expiration}) do
{:ok, %Req.Response{status: 200, body: %{"hash" => hash}}} ->
{:ok, hash}
{:ok, %Req.Response{status: status, body: body}} ->
{:error, {:unexpected_response, status, body}}
{:error, reason} ->
{:error, {:request_failed, reason}}
end
else
case Req.post(url, json: %{"accessionnumber" => access_study, "expiration" => expiration}) do
{:ok, %Req.Response{status: 200, body: %{"hash" => hash}}} ->
{:ok, hash}
{:ok, %Req.Response{status: status, body: body}} ->
{:error, {:unexpected_response, status, body}}
{:error, reason} ->
{:error, {:request_failed, reason}}
end
end
end
def render(assigns) do
~H"""
<body class="m-0 p-0 bg-[rgb(228, 228, 228)]">