Skip to content

Commit

Permalink
recalculate prescription
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed May 22, 2024
1 parent 05c76b4 commit 148f594
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 77 deletions.
82 changes: 5 additions & 77 deletions routes/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,84 +818,12 @@ def getPrescriptionUpdate(idPrescription):
user = User.find(get_jwt_identity())
dbSession.setSchema(user.schema)

p = Prescription.query.get(idPrescription)
if p is None:
return {
"status": "error",
"message": "Prescrição Inexistente!",
}, status.HTTP_400_BAD_REQUEST

if p.agg:
if user.cpoe():
query = text(
"INSERT INTO "
+ user.schema
+ ".presmed \
SELECT pm.*\
FROM "
+ user.schema
+ ".presmed pm\
WHERE fkprescricao IN (\
SELECT fkprescricao\
FROM "
+ user.schema
+ ".prescricao p\
WHERE p.nratendimento = :admissionNumber"
+ "\
AND p.idsegmento IS NOT NULL \
AND "
+ "date(:prescDate)\
BETWEEN p.dtprescricao::date AND COALESCE(p.dtvigencia::date, "
+ "date(:prescDate))\
);"
)

db.session.execute(
query, {"admissionNumber": p.admissionNumber, "prescDate": p.date}
)
else:
query = text(
"INSERT INTO "
+ user.schema
+ ".presmed \
SELECT pm.*\
FROM "
+ user.schema
+ ".presmed pm\
WHERE fkprescricao IN (\
SELECT fkprescricao\
FROM "
+ user.schema
+ ".prescricao p\
WHERE p.nratendimento = :admissionNumber"
+ "\
AND p.idsegmento IS NOT NULL \
AND (\
p.dtprescricao::date = "
+ "date(:prescDate) OR\
p.dtvigencia::date = "
+ "date(:prescDate)\
)\
);"
)

db.session.execute(
query, {"admissionNumber": p.admissionNumber, "prescDate": p.date}
)
else:
query = text(
"INSERT INTO "
+ user.schema
+ ".presmed \
SELECT *\
FROM "
+ user.schema
+ ".presmed\
WHERE fkprescricao = :idPrescription"
+ ";"
try:
prescription_service.recalculate_prescription(
id_prescription=idPrescription, user=user
)

db.session.execute(query, {"idPrescription": idPrescription})
except ValidationError as e:
return {"status": "error", "message": str(e), "code": e.code}, e.httpStatus

return tryCommit(db, escape_html(str(idPrescription)))

Expand Down
89 changes: 89 additions & 0 deletions services/prescription_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,92 @@ def review_prescription(idPrescription, user, review_type, evaluation_time):
"reviewedAt": datetime.today().isoformat(),
"reviewedBy": db_user.name,
}


def recalculate_prescription(id_prescription: int, user: User):
p = Prescription.query.get(id_prescription)
if p is None:
raise ValidationError(
"Prescrição inexistente.",
"errors.invalidRegister",
status.HTTP_400_BAD_REQUEST,
)

if p.agg:
if user.cpoe():
prescription_results = get_query_prescriptions_by_agg(
agg_prescription=p, is_cpoe=user.cpoe(), only_id=True
).all()

prescription_ids = []
for item in prescription_results:
prescription_ids.append(item.id)

db.session.query(Prescription).filter(
Prescription.id.in_(prescription_ids)
).filter(Prescription.idSegment == None).update(
{
"idHospital": p.idHospital,
"idDepartment": p.idDepartment,
"idSegment": p.idSegment,
},
synchronize_session="fetch",
)

db.session.flush()

query = text(
f"""INSERT INTO {user.schema}.presmed
SELECT
pm.*
FROM
{user.schema}.presmed pm
WHERE
fkprescricao = ANY(:prescriptionIds)
"""
)

db.session.execute(query, {"prescriptionIds": prescription_ids})
else:
query = text(
"INSERT INTO "
+ user.schema
+ ".presmed \
SELECT pm.*\
FROM "
+ user.schema
+ ".presmed pm\
WHERE fkprescricao IN (\
SELECT fkprescricao\
FROM "
+ user.schema
+ ".prescricao p\
WHERE p.nratendimento = :admissionNumber"
+ "\
AND p.idsegmento IS NOT NULL \
AND (\
p.dtprescricao::date = "
+ "date(:prescDate) OR\
p.dtvigencia::date = "
+ "date(:prescDate)\
)\
);"
)

db.session.execute(
query, {"admissionNumber": p.admissionNumber, "prescDate": p.date}
)
else:
query = text(
"INSERT INTO "
+ user.schema
+ ".presmed \
SELECT *\
FROM "
+ user.schema
+ ".presmed\
WHERE fkprescricao = :idPrescription"
+ ";"
)

db.session.execute(query, {"idPrescription": p.id})

0 comments on commit 148f594

Please sign in to comment.