diff --git a/models/enums.py b/models/enums.py index 8928b830..35cda129 100644 --- a/models/enums.py +++ b/models/enums.py @@ -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" diff --git a/routes/reports/general.py b/routes/reports/general.py index ec818ddc..2fdb4dd2 100644 --- a/routes/reports/general.py +++ b/routes/reports/general.py @@ -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) diff --git a/services/reports/general_report_service.py b/services/reports/general_report_service.py index 57fa9bf5..24624da8 100644 --- a/services/reports/general_report_service.py +++ b/services/reports/general_report_service.py @@ -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 @@ -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