Skip to content

Commit

Permalink
report: audit
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Jan 9, 2024
1 parent 0ffa0ae commit 167fdc3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ class ReportEnum(Enum):
RPT_PATIENT_DAY = "rpt_patient_day"
RPT_PRESCRIPTION = "rpt_prescription"
RPT_INTERVENTION = "rpt_intervention"
RPT_PRESCRIPTION_AUDIT = "rpt_prescription_audit"
15 changes: 15 additions & 0 deletions routes/reports/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,18 @@ def intervention():
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, report_data)


@app_rpt_general.route("/reports/general/audit", methods=["GET"])
@jwt_required()
def audit():
user = User.find(get_jwt_identity())

try:
report_data = general_report_service.get_audit_report(
user=user, clearCache=request.args.get("clearCache", False)
)
except ValidationError as e:
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, report_data)
75 changes: 75 additions & 0 deletions services/reports/general_report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ def get_intervention_report(user, clearCache=False):
}


def get_audit_report(user, clearCache=False):
report = ReportEnum.RPT_PRESCRIPTION_AUDIT.value

if not clearCache:
cached_link = cache_service.generate_link_from_cache(
report=report, schema=user.schema
)

if cached_link != None:
return cached_link

list = _get_audit_list(user)

cache_service.save_cache(
report=report,
schema=user.schema,
data=list,
)

return {
"url": cache_service.generate_link(report, user.schema),
"updatedAt": datetime.today().isoformat(),
}


def _get_patient_day_list(user):
sql = f"""
select
Expand Down Expand Up @@ -341,3 +366,53 @@ def _get_intervention_list(user):
)

return itens


def _get_audit_list(user):
sql = f"""
select
case
when tp_audit = 1 then 'checagem'
when tp_audit = 2 then 'deschecagem'
end as tipo,
pa.created_at as dtevento,
u.nome,
pa.nratendimento,
pa.fkprescricao,
s.nome,
seg.nome,
pa.total_itens,
pa.agregada
from
{user.schema}.prescricao_audit pa
left join {user.schema}.setor s on s.fksetor = pa.fksetor
left join {user.schema}.segmento seg on seg.idsegmento = pa.idsegmento
left join public.usuario u on pa.created_by = u.idusuario
where
pa.created_at > now() - interval '2 months'
order by
pa.created_at
"""

db_session = db.create_scoped_session(
options={"bind": db.get_engine(db.get_app(), ReportEnum.RPT_BIND.value)}
)

results = db_session.execute(sql).fetchall()
itens = []
for i in results:
itens.append(
{
"type": i[0],
"date": i[1].isoformat(),
"responsible": i[2],
"admissionNumber": i[3],
"idPrescription": i[4],
"department": i[5],
"segment": i[6],
"itens": i[7],
"agg": i[8],
}
)

return itens

0 comments on commit 167fdc3

Please sign in to comment.