From 75e5a8c505271c705d78642a2624437197fb9ee4 Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Mon, 9 Dec 2024 12:43:03 +0530 Subject: [PATCH 1/2] Fix dont create review for already approved visits --- commcare_connect/opportunity/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commcare_connect/opportunity/views.py b/commcare_connect/opportunity/views.py index 645ba4ef..c8036c37 100644 --- a/commcare_connect/opportunity/views.py +++ b/commcare_connect/opportunity/views.py @@ -866,8 +866,9 @@ def visit_verification(request, org_slug=None, pk=None): @org_member_required def approve_visit(request, org_slug=None, pk=None): user_visit = UserVisit.objects.get(pk=pk) + old_status = user_visit.status user_visit.status = VisitValidationStatus.approved - if user_visit.opportunity.managed: + if user_visit.opportunity.managed and old_status != VisitValidationStatus.approved: user_visit.review_created_on = now() user_visit.save() opp_id = user_visit.opportunity_id From d04163e48806424ee913c0a1a3bed3581d289fc2 Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Tue, 10 Dec 2024 12:45:58 +0530 Subject: [PATCH 2/2] Refactor save only when status changed --- commcare_connect/opportunity/views.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/commcare_connect/opportunity/views.py b/commcare_connect/opportunity/views.py index c8036c37..a9503f6d 100644 --- a/commcare_connect/opportunity/views.py +++ b/commcare_connect/opportunity/views.py @@ -866,17 +866,18 @@ def visit_verification(request, org_slug=None, pk=None): @org_member_required def approve_visit(request, org_slug=None, pk=None): user_visit = UserVisit.objects.get(pk=pk) - old_status = user_visit.status - user_visit.status = VisitValidationStatus.approved - if user_visit.opportunity.managed and old_status != VisitValidationStatus.approved: - user_visit.review_created_on = now() - user_visit.save() opp_id = user_visit.opportunity_id - access = OpportunityAccess.objects.get(user_id=user_visit.user_id, opportunity_id=opp_id) - update_payment_accrued(opportunity=access.opportunity, users=[access.user]) + if user_visit.status != VisitValidationStatus.approved: + user_visit.status = VisitValidationStatus.approved + if user_visit.opportunity.managed: + user_visit.review_created_on = now() + user_visit.save() + update_payment_accrued(opportunity=user_visit.opportunity, users=[user_visit.user]) if user_visit.opportunity.managed: return redirect("opportunity:user_visit_review", org_slug, opp_id) - return redirect("opportunity:user_visits_list", org_slug=org_slug, opp_id=user_visit.opportunity.id, pk=access.id) + return redirect( + "opportunity:user_visits_list", org_slug=org_slug, opp_id=opp_id, pk=user_visit.opportunity_access_id + ) @org_member_required