diff --git a/config.py b/config.py index feec59e4..cf46b97b 100644 --- a/config.py +++ b/config.py @@ -15,6 +15,9 @@ class Config: getenv("POTGRESQL_CONNECTION_STRING") or "postgresql://postgres@localhost/noharm" ) + REPORT_CONNECTION_STRING = ( + getenv("REPORT_CONNECTION_STRING") or "postgresql://postgres@localhost/noharm" + ) JWT_ACCESS_TOKEN_EXPIRES = timedelta( minutes=int(getenv("JWT_ACCESS_TOKEN_EXPIRES", 20)) ) diff --git a/mobile.py b/mobile.py index 507b2a1e..6e0168fc 100644 --- a/mobile.py +++ b/mobile.py @@ -30,6 +30,7 @@ from routes.admin.integration import app_admin_integration from routes.admin.segment import app_admin_segment from routes.admin.exam import app_admin_exam +from routes.reports.general import app_rpt_general import os import logging from models.enums import NoHarmENV @@ -38,6 +39,7 @@ app = FlaskAPI(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = Config.POTGRESQL_CONNECTION_STRING +app.config["SQLALCHEMY_BINDS"] = {"report": Config.REPORT_CONNECTION_STRING} app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_ENGINE_OPTIONS"] = { "pool_recycle": 500, @@ -88,6 +90,8 @@ app.register_blueprint(app_admin_segment) app.register_blueprint(app_admin_exam) +app.register_blueprint(app_rpt_general) + CORS(app, origins=[Config.MAIL_HOST], supports_credentials=True) if Config.ENV == NoHarmENV.STAGING.value: diff --git a/routes/reports/general.py b/routes/reports/general.py new file mode 100644 index 00000000..bb220a01 --- /dev/null +++ b/routes/reports/general.py @@ -0,0 +1,104 @@ +import os +from flask import Blueprint, request +from flask_jwt_extended import jwt_required, get_jwt_identity + +from flask_api import status +from models.main import * +from models.appendix import * +from models.segment import * +from models.prescription import * +from services import memory_service +from exception.validation_error import ValidationError + +app_rpt_general = Blueprint("app_rpt_general", __name__) + + +@app_rpt_general.route("/reports/general/prescription", methods=["GET"]) +@jwt_required() +def general_prescription(): + user = User.find(get_jwt_identity()) + dbSession.setSchema(user.schema) + + cache = memory_service.get_memory("rpt_general") + + if cache is not None: + return { + "status": "success", + "data": cache.value, + }, status.HTTP_200_OK + + sql = f""" + select + distinct on (p.fkprescricao) + p.fksetor, + p.fkprescricao, + p.convenio, + p.nratendimento, + s.nome as setorNome, + p.dtprescricao::date as data, + case + when p.status = 's' then 1 + when pa.fkprescricao is not null then 1 + else 0 + end as checada, + case + when pa.fkprescricao is null then coalesce(array_length(p.aggmedicamento,1), 1) + else pa.total_itens + end total_itens, + case + when p.status <> 's' and pa.fkprescricao is null then 0 + when pa.fkprescricao is not null then pa.total_itens + else coalesce(array_length(p.aggmedicamento,1), 1) + end total_itens_checados, + case + when p.status <> 's' and pa.fkprescricao is null then 'Não Checado' + when pa.fkprescricao is not null then user_pa.nome + else user_p.nome + end as userNome + from + {user.schema}.prescricao p + inner join {user.schema}.setor s on s.fksetor = p.fksetor + left join ( + select * from {user.schema}.prescricao_audit pa where pa.tp_audit = 1 + ) pa on pa.fkprescricao = p.fkprescricao + left join public.usuario user_p on (p.update_by = user_p.idusuario) + left join public.usuario user_pa on pa.created_by = user_pa.idusuario + where + p.agregada = true + and p.concilia IS null + and s.fksetor in (select s2.fksetor from {user.schema}.segmentosetor s2 where s2.idsegmento is not null) + and p.dtprescricao > now() - interval '2 months' + order by p.fkprescricao, pa.created_at desc + """ + + db_session = db.create_scoped_session( + options={"bind": db.get_engine(db.get_app(), "report")} + ) + + results = db_session.execute(sql).fetchall() + itens = [] + for i in results: + itens.append( + { + "idDepartment": i[0], + "idPrescription": i[1], + "insurance": i[2], + "admissionNumber": i[3], + "department": i[4], + "date": i[5].isoformat(), + "checked": i[6], + "itens": i[7], + "checkedItens": i[8], + "responsible": i[9], + } + ) + + memory_service.save_unique_memory("rpt_general", itens, user) + + return tryCommit( + db, + { + "status": "success", + "data": itens, + }, + )