Skip to content

Commit

Permalink
Merge pull request #401 from noharm-ai/develop
Browse files Browse the repository at this point in the history
v4.03-beta
  • Loading branch information
marceloarocha authored Oct 28, 2024
2 parents 60c3cec + 081f771 commit f3821b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class Config:
VERSION = "v4.02-beta"
FRONTEND_VERSION = "4.0.3"
VERSION = "v4.03-beta"
FRONTEND_VERSION = "4.0.4"
ENV = getenv("ENV") or NoHarmENV.DEVELOPMENT.value
SECRET_KEY = getenv("SECRET_KEY") or "secret_key"
API_KEY = getenv("API_KEY") or ""
Expand Down
88 changes: 42 additions & 46 deletions services/reports/reports_antimicrobial_service.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
from sqlalchemy import select, and_, desc
from sqlalchemy import select, desc, text

from models.main import db
from models.main import db, User
from models.prescription import (
Patient,
Prescription,
PrescriptionDrug,
Drug,
DrugAttributes,
Frequency,
MeasureUnit,
Substance,
)
from exception.validation_error import ValidationError
from utils import dateutils, status
from decorators.has_permission_decorator import has_permission, Permission


@has_permission(Permission.READ_REPORTS)
def get_history(admission_number: int):
def get_history(admission_number: int, user_context: User):
if admission_number == None:
raise ValidationError(
"admissionNUmber inválido",
"admissionNmber inválido",
"errors.invalidParams",
status.HTTP_400_BAD_REQUEST,
)

admission_list = [admission_number]
admission_list = [int(admission_number)]
admission = db.session.execute(
select(Patient.idPatient)
.select_from(Patient)
Expand All @@ -44,43 +37,46 @@ def get_history(admission_number: int):

a_list = db.session.execute(admissions_query).all()
for a in a_list:
admission_list.append(a.admissionNumber)
admission_list.append(int(a.admissionNumber))

query = (
select(
Prescription.id,
Prescription.date,
Prescription.expire,
Prescription.admissionNumber,
PrescriptionDrug.suspendedDate,
PrescriptionDrug.id.label("idPrescriptionDrug"),
PrescriptionDrug.dose,
PrescriptionDrug.route,
Drug.name,
Substance.name.label("substance"),
MeasureUnit.description.label("measureUnit"),
Frequency.description.label("frequency"),
)
.select_from(Prescription)
.join(PrescriptionDrug, PrescriptionDrug.idPrescription == Prescription.id)
.join(Drug, Drug.id == PrescriptionDrug.idDrug)
.join(
DrugAttributes,
and_(
DrugAttributes.idDrug == PrescriptionDrug.idDrug,
DrugAttributes.idSegment == PrescriptionDrug.idSegment,
),
)
.outerjoin(Substance, Drug.sctid == Substance.id)
.outerjoin(Frequency, Frequency.id == PrescriptionDrug.idFrequency)
.outerjoin(MeasureUnit, MeasureUnit.id == PrescriptionDrug.idMeasureUnit)
.where(Prescription.admissionNumber.in_(admission_list))
.where(DrugAttributes.antimicro == True)
.order_by(desc(Prescription.date))
.limit(2000)
query = text(
f"""
select
antimicro.*,
m.nome as name,
s.nome as substance
from (
SELECT
p.fkprescricao as id,
p.dtprescricao as date,
p.dtvigencia as expire,
p.nratendimento as "admissionNumber",
pm.dtsuspensao as "suspendedDate",
pm.fkpresmed AS "idPrescriptionDrug",
pm.dose as dose,
pm.via as route,
pm.fkmedicamento,
um.nome AS "measureUnit",
f.nome AS frequency
FROM
{user_context.schema}.prescricao p
JOIN {user_context.schema}.presmed pm ON pm.fkprescricao = p.fkprescricao
JOIN {user_context.schema}.medatributos ma ON ma.fkmedicamento = pm.fkmedicamento AND ma.idsegmento = pm.idsegmento
LEFT OUTER JOIN {user_context.schema}.frequencia f ON f.fkfrequencia = pm.fkfrequencia
LEFT OUTER JOIN {user_context.schema}.unidademedida um ON um.fkunidademedida = pm.fkunidademedida
WHERE
p.nratendimento = ANY(:admissionList)
AND ma.antimicro = TRUE
ORDER BY
p.dtprescricao desc
limit 1000
) antimicro
JOIN {user_context.schema}.medicamento m ON antimicro.fkmedicamento = m.fkmedicamento
LEFT OUTER JOIN public.substancia s ON m.sctid = s.sctid
"""
)

results = db.session.execute(query).all()
results = db.session.execute(query, {"admissionList": admission_list}).all()
items = []

for row in results:
Expand Down

0 comments on commit f3821b8

Please sign in to comment.