Skip to content

Command to approve over limit visits - #1856

Draft
mkangia wants to merge 2 commits into
mainfrom
mk/claude/ci/1044-approve-over-limit-visits
Draft

mkangia wants to merge 2 commits into
mainfrom
mk/claude/ci/1044-approve-over-limit-visits

Conversation

@mkangia

@mkangia mkangia commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Product Description

Technical Summary

Documentation

Safety Assurance

Safety story

Automated test coverage

QA Plan

Deployment

  • This PR can be deployed: any required configuration, commands, or other manual steps required before this PR can be deployed are done.

Labels & Review

  • The set of people pinged as reviewers is appropriate for the level of risk of the change

mkangia and others added 2 commits November 27, 2026 18:14
…view

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@codescene-access codescene-access Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Health Improved (1 files improve in Code Health)

Gates Failed
New code is healthy (2 new files with code health below 10.00)

Our agent can fix these. Install it.

Gates Passed
2 Quality Gates Passed

Reason for failure
New code is healthy Violations Code Health Impact
visit_approval.py 1 rule 9.69 Suppress
approve_over_limit_visits.py 1 rule 9.69 Suppress

See analysis details in CodeScene

View Improvements
File Code Health Impact Categories Improved
init.py 4.56 → 4.73 Lines of Code in a Single File, Complex Method, Bumpy Road Ahead

Quality Gate Profile: The Bare Minimum
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

Comment on lines +21 to +59
def approve_user_visits(
opportunity: Opportunity, visits, justification: str | None = None, agree: bool = False
) -> int:
"""Approve the visits, then recalculate the workers' accrual and the visits' work areas.

The visits go to PM review, and one the PM disagreed with goes back to pending review. With agree,
they are marked agreed instead, as form processing does for an auto-approved visit. Flagged visits
get the justification, and MissingJustificationError is raised before anything is written if one of
them needs it and none is given. Returns the number of visits updated.
"""
visits = list(visits)
if not justification and any(visit.flagged for visit in visits):
raise MissingJustificationError("Justification is mandatory for flagged visits.")

approved_on = now()
for visit in visits:
_approve(visit, approved_on, justification)
if agree:
visit.review_status = VisitReviewStatus.agree
visit.review_status_modified_date = approved_on

approved_count = UserVisit.objects.bulk_update(
visits,
[
"status",
"status_modified_date",
"review_created_on",
"review_status",
"review_status_modified_date",
"justification",
],
batch_size=100,
)
user_ids = list({visit.user_id for visit in visits})
if user_ids:
update_payment_accrued(opportunity=opportunity, users=user_ids, incremental=True)
for work_area in {visit.work_area for visit in visits if visit.work_area}:
work_area.update_status()
return approved_count

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ New issue: Complex Method
approve_user_visits has a cyclomatic complexity of 11, threshold = 9

Suppress

Comment on lines +35 to +53
def handle(self, *args, opportunity, visit_ids, agree, commit, **options):
_check_justification()
opportunity = _get_opportunity(opportunity)
_check_agree(opportunity, agree)

candidates = list(approvable_visits(opportunity, visit_ids))
over_limit = [visit for visit in candidates if visit.status == VisitValidationStatus.over_limit]
flagged_skipped = [visit for visit in over_limit if visit.flagged] if agree else []
to_approve = [visit for visit in over_limit if visit not in flagged_skipped]

self._report(opportunity, visit_ids, candidates, over_limit, flagged_skipped, to_approve)
self._warn_about_payment(opportunity, agree)
if not commit:
self.stdout.write(self.style.WARNING("Dry run: nothing was saved. Rerun with --commit to approve."))
return

with transaction.atomic():
approved_count = approve_user_visits(opportunity, to_approve, JUSTIFICATION, agree=agree)
self.stdout.write(self.style.SUCCESS(f"Approved {approved_count} visits."))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ New issue: Excess Number of Function Arguments
Command.handle has 6 arguments, max arguments = 4

Suppress

Comment on lines +55 to +68
def _report(self, opportunity, visit_ids, candidates, over_limit, flagged_skipped, to_approve):
not_over_limit = [visit for visit in candidates if visit not in over_limit]
candidate_ids = {visit.id for visit in candidates}
not_approvable_ids = [visit_id for visit_id in visit_ids if visit_id not in candidate_ids]

self.stdout.write(f"Opportunity {opportunity.pk} ({opportunity.name})")
self._write_ids("Will approve", to_approve)
self._write_ids(" of which flagged, saved with the justification", [v for v in to_approve if v.flagged])
self.stdout.write(f"Justification: {JUSTIFICATION}")
self._write_ids("Skipped, flagged (--agree leaves them for review)", flagged_skipped)
self._write_ids("Skipped, not over limit", not_over_limit)
self.stdout.write(
f"Skipped, not in this opportunity or already approved: {len(not_approvable_ids)} {not_approvable_ids}"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ New issue: Excess Number of Function Arguments
Command._report has 6 arguments, max arguments = 4

Suppress

try:
approved_count = approve_user_visits(request.opportunity, visits, request.POST.get("justification"))
except MissingJustificationError as error:
return HttpResponse(str(error), status=400, headers={"HX-Trigger": "form_error"})

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants