Skip to content

Commit

Permalink
Merge pull request #526 from akvo/develop
Browse files Browse the repository at this point in the history
[#525] Delete answers from db where not listed in answer payload (data PUT endpoint)
  • Loading branch information
wayangalihpratama authored Mar 8, 2024
2 parents a439826 + c54e34b commit b6feabf
Show file tree
Hide file tree
Showing 3 changed files with 439 additions and 355 deletions.
4 changes: 4 additions & 0 deletions backend/db/crud_answer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def get_answer_by_data_and_question(
)


def get_answer_by_data(session: Session, data: int) -> List[AnswerBase]:
return session.query(Answer).filter(Answer.data == data).all()


def delete_answer_by_id(session: Session, id: int) -> None:
session.query(Answer).filter(Answer.id == id).delete()
session.commit()
20 changes: 20 additions & 0 deletions backend/routes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,11 @@ def update_by_id(
for qid in qg["question"]:
repeat_qids.append(qid)
# questions = form.list_of_questions

checked = {}
checked_payload = {}
answer_payload_ids_with_repeat_index = [] # for DELETE answer

# if data_cleaning, delete old answer and save payload
answer_ids = []
if data_cleaning:
Expand All @@ -665,6 +668,7 @@ def update_by_id(

for a in answers:
key = f"{a['question']}_{a['repeat_index']}"
answer_payload_ids_with_repeat_index.append(key) # for DELETE answer
checked_payload.update({key: a})
execute = "update"
if a["question"] not in list(questions):
Expand Down Expand Up @@ -731,6 +735,22 @@ def update_by_id(
synchronize_session="fetch"
)
session.commit()

# HANDLE DELETE
# need to check if current answers in DB not available
# in answers payload (that mean DELETE)
if not data_cleaning:
all_answers = crud_answer.get_answer_by_data(session=session, data=id)
all_answers = [a.format_with_answer_id for a in all_answers]
for a in all_answers:
key = f"{a['question']}_{a['repeat_index']}"
if key in answer_payload_ids_with_repeat_index:
# ignore
continue
# delete answer
crud_answer.delete_answer_by_id(session=session, id=a["id"])
# EOL handle DELETE

# if submitted send and not
# data_cleaning notification email to secretariat admin
if submitted and not data_cleaning:
Expand Down
Loading

0 comments on commit b6feabf

Please # to comment.