Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion strr-api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "strr-api"
version = "0.3.49"
version = "0.3.50"
description = ""
authors = ["thorwolpert <thor@wolpert.ca>"]
license = "BSD 3-Clause"
Expand Down
25 changes: 14 additions & 11 deletions strr-api/src/strr_api/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,24 @@ def find_non_terminal_renewal_for_payment_account(
cls,
payment_account: str | int | None,
registration_id: int,
terminal_status_values: tuple[str, ...],
finished_status_values: tuple[str, ...],
exclude_application_id: int | None = None,
) -> Application | None:
"""Return a renewal for this account+registration that is not in a terminal status, if any."""
"""Return a renewal for this account+registration that is not in one of the finished statuses, if any.

exclude_application_id leaves out the application being submitted, so a draft isn't its own conflict.
"""
if payment_account is None:
return None
return (
cls.query.filter(
cls.type == ApplicationType.RENEWAL.value,
cls.registration_id == registration_id,
cls.payment_account == str(payment_account),
~cls.status.in_(terminal_status_values),
)
.order_by(cls.id.desc())
.first()
query = cls.query.filter(
cls.type == ApplicationType.RENEWAL.value,
cls.registration_id == registration_id,
cls.payment_account == str(payment_account),
~cls.status.in_(finished_status_values),
)
if exclude_application_id is not None:
query = query.filter(cls.id != exclude_application_id)
return query.order_by(cls.id.desc()).first()

@classmethod
def _filter_by_application_registration_number(cls, search_term: str, query: Query) -> Query:
Expand Down
105 changes: 62 additions & 43 deletions strr-api/src/strr_api/models/rental.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ def _collect_sub_status_conditions(cls, filter_criteria: RegistrationSearch, que
from strr_api.models.application import Application

sub_status_conditions = []
needs_latest_app = bool(filter_criteria.approval_methods) or filter_criteria.review_renew is True
needs_renewal_regs = filter_criteria.review_renew is True or (
bool(filter_criteria.approval_methods) and filter_criteria.examiner_reviewed is False
)
needs_latest_app = bool(filter_criteria.approval_methods)
needs_renewal_regs = bool(filter_criteria.approval_methods) and filter_criteria.examiner_reviewed is not True

latest_app = None
if needs_latest_app:
Expand Down Expand Up @@ -335,21 +333,27 @@ def _collect_sub_status_conditions(cls, filter_criteria: RegistrationSearch, que
)
query = query.outerjoin(renewal_regs, renewal_regs.c.registration_id == Registration.id)

# Built once and shared: the Approved filter excludes the rows that the Review Renew filter selects.
review_renew_condition = None
if filter_criteria.approval_methods or filter_criteria.review_renew is True:
query, review_renew_condition = cls._review_renew_condition(query)

if filter_criteria.approval_methods:
sub_status_conditions.append(
cls._approval_method_condition(
filter_criteria.approval_methods,
filter_criteria.examiner_reviewed,
latest_app=latest_app,
renewal_regs=renewal_regs,
review_renew_condition=review_renew_condition,
)
)
if filter_criteria.noc_statuses:
sub_status_conditions.append(Registration.noc_status.in_(filter_criteria.noc_statuses))
if filter_criteria.is_set_aside is True:
sub_status_conditions.append(Registration.is_set_aside == db.true())
if filter_criteria.review_renew is True:
sub_status_conditions.append(cls._review_renew_condition(latest_app=latest_app, renewal_regs=renewal_regs))
sub_status_conditions.append(review_renew_condition)
return query, sub_status_conditions

@classmethod
Expand Down Expand Up @@ -638,25 +642,31 @@ def _filter_by_registration_requirement(cls, requirement: list[str], query):
return query

@classmethod
def _approval_method_condition(
def _approval_method_condition( # pylint: disable=too-many-arguments
cls,
approval_methods: list[str],
examiner_reviewed: bool | None = None,
latest_app=None,
renewal_regs=None,
review_renew_condition=None,
):
"""Build SQL condition for filtering by latest application status and review state."""
"""Build SQL condition for filtering by latest application status and review state.

The conditions match the examiner dashboard's row labels, so a row is only returned by the filter that has
its label:
- Approved (examiner_reviewed=True): AUTO_APPROVED/FULL_REVIEW_APPROVED, or provisional with a decision
- Review (examiner_reviewed=False): provisional with no decision and no filed renewal
- both (examiner_reviewed=None): either of the above
Review Renew takes precedence over Approved, so registrations matching review_renew_condition are left out of
the approved conditions. Review needs no such exclusion: a registration with a filed renewal never matches it.
"""
from strr_api.models.application import Application

latest_app_status = (
latest_app.c.status
if latest_app is not None
else cls._latest_application_field_subquery(Application, Application.status)
)
approval_status_condition = latest_app_status.in_(approval_methods)
if examiner_reviewed is None:
return approval_status_condition

latest_app_decider = (
latest_app.c.decider_id
if latest_app is not None
Expand All @@ -679,6 +689,9 @@ def _approval_method_condition(
if renewal_regs is not None
else db.not_(cls._has_renewal_filed_condition())
)
not_review_renew_condition = (
db.not_(review_renew_condition) if review_renew_condition is not None else db.true()
)

# examinerReviewed only applies to provisional workflow statuses.
provisional_statuses = {
Expand All @@ -691,54 +704,60 @@ def _approval_method_condition(
filter_conditions = []
if terminal_approval_methods:
# AUTO_APPROVED/FULL_REVIEW_APPROVED are approved regardless of decider.
filter_conditions.append(latest_app_status.in_(terminal_approval_methods))
filter_conditions.append(
db.and_(latest_app_status.in_(terminal_approval_methods), not_review_renew_condition)
)

if provisional_approval_methods:
provisional_status_condition = latest_app_status.in_(provisional_approval_methods)
filter_conditions.append(
db.and_(provisional_status_condition, reviewed_condition)
if examiner_reviewed
else db.and_(
provisional_status_condition,
not_reviewed_condition,
no_renewal_condition,
if examiner_reviewed is not False:
filter_conditions.append(
db.and_(provisional_status_condition, reviewed_condition, not_review_renew_condition)
)
if examiner_reviewed is not True:
filter_conditions.append(
db.and_(provisional_status_condition, not_reviewed_condition, no_renewal_condition)
)
)

if not filter_conditions:
return db.false()
return db.or_(*filter_conditions)

@classmethod
def _review_renew_condition(cls, latest_app=None, renewal_regs=None):
"""Build SQL condition for renewals requiring human review.
def _review_renew_condition(cls, query):
"""Join the renewals awaiting review and return the query with the SQL condition for them.

A registration matches when both are true:
- no registration level decision exists
- one of its renewals is in the provisional review queue with no decision

A registration matches when all are true:
- a renewal is filed
- no registration level or latest application decision exists yet
- latest application is still in provisional review queue
The renewal is looked for among all of the registration's renewals, not only its latest application, so a
newer renewal (even a draft) doesn't hide an older one that still awaits review.
"""
# pylint: disable=import-outside-toplevel
from strr_api.enums.enum import ApplicationType
from strr_api.models.application import Application

provisional_statuses = [
Application.Status.PROVISIONALLY_APPROVED.value,
Application.Status.PROVISIONAL_REVIEW.value,
]
latest_app_decider = (
latest_app.c.decider_id
if latest_app is not None
else cls._latest_application_field_subquery(Application, Application.decider_id)
)
has_renewal = (
renewal_regs.c.registration_id.isnot(None)
if renewal_regs is not None
else cls._has_renewal_filed_condition()
pending_renewal_regs = (
select(Application.registration_id.label("registration_id"))
.where(
Application.registration_id.isnot(None),
Application.type == ApplicationType.RENEWAL.value,
Application.status.in_(
[
Application.Status.PROVISIONALLY_APPROVED.value,
Application.Status.PROVISIONAL_REVIEW.value,
]
),
Application.decider_id.is_(None),
)
.distinct()
.subquery("pending_renewal_regs")
)
return db.and_(
has_renewal,
query = query.outerjoin(pending_renewal_regs, pending_renewal_regs.c.registration_id == Registration.id)
return query, db.and_(
Registration.decider_id.is_(None),
latest_app_decider.is_(None),
cls._approval_method_condition(provisional_statuses, latest_app=latest_app, renewal_regs=renewal_regs),
pending_renewal_regs.c.registration_id.isnot(None),
)

@classmethod
Expand Down
20 changes: 14 additions & 6 deletions strr-api/src/strr_api/resources/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
APPLICATION_STATES_STAFF_ACTION,
APPLICATION_TERMINAL_STATES,
APPLICATION_UNPAID_STATES,
RENEWAL_FINISHED_STATES,
)
from strr_api.services.examiner_note_service import ExaminerNoteNotAllowedException, ExaminerNoteService
from strr_api.utils.renewal_window import RenewalWindow
Expand Down Expand Up @@ -144,8 +145,9 @@ def create_application(application_number: Optional[str] = None):
description: Application not found (when path includes application_number).
409:
description: >
Renewal conflict — pathless POST /applications for renewal when a non-terminal renewal
already exists for the same registration and Account-Id (errorCode RENEWAL_ALREADY_IN_PROGRESS).
Renewal conflict — a renewal is started (pathless POST) or a renewal draft is submitted when another
renewal that is neither approved (provisionally or in full) nor declined already exists for the same
registration and Account-Id (errorCode RENEWAL_ALREADY_IN_PROGRESS). Saving a draft isn't checked.
402:
description: Payment required (e.g. invoice creation failed on final submit).
502:
Expand Down Expand Up @@ -226,14 +228,20 @@ def create_application(application_number: Optional[str] = None):
error_code="RENEWAL_WINDOW_CLOSED",
)

if application is None:
terminal_status_values = tuple(s.value for s in APPLICATION_TERMINAL_STATES)
# Check when a renewal is started, and again when a draft is submitted, because submitting creates an
# invoice and the draft may predate the renewal now in progress. Saving a draft isn't checked.
if application is None or not is_draft:
# A provisionally approved renewal has extended the registration, so it doesn't block the next one
finished_status_values = tuple(s.value for s in RENEWAL_FINISHED_STATES)
conflicting = ApplicationModel.find_non_terminal_renewal_for_payment_account(
account_id, registration_id, terminal_status_values
account_id,
registration_id,
finished_status_values,
exclude_application_id=application.id if application else None,
)
if conflicting:
logger.info(
"STRR renewal create returned 409: a non-terminal renewal already exists for this "
"STRR renewal create or submit returned 409: a non-terminal renewal already exists for this "
"registration and payment account. "
"registration_id=%s payment_account=%s existing_renewal_id=%s "
"existing_application_number=%s existing_renewal_status=%s error_code=RENEWAL_ALREADY_IN_PROGRESS",
Expand Down
37 changes: 23 additions & 14 deletions strr-api/src/strr_api/resources/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from flasgger import swag_from
from flask import Blueprint, current_app, g, jsonify, request, send_file
from flask_cors import cross_origin
from sqlalchemy import and_, or_
from werkzeug.utils import secure_filename

from strr_api.common.auth import jwt
Expand Down Expand Up @@ -76,6 +77,7 @@
SnapshotService,
UserService,
)
from strr_api.services.application_service import RENEWAL_APPROVED_STATES, RENEWAL_FINISHED_STATES
from strr_api.services.examiner_note_service import ExaminerNoteNotAllowedException, ExaminerNoteService
from strr_api.services.registration_service import (
REGISTRATION_STATES_STAFF_ACTION,
Expand Down Expand Up @@ -726,22 +728,29 @@ def get_todos(registration_id):
)
todos.append({"task": {"type": todo_type, "detail": renewal_application.application_number}})
else:
# Renew is hidden once a renewal has been submitted in this window. A declined renewal doesn't
# count, so the host can renew again; create_application's 409 check also accepts a new renewal
# after a declined one, because APPLICATION_TERMINAL_STATES includes both declined statuses.
# Renew is hidden while a submitted renewal is still in progress, whenever it was submitted, as
# create_application's 409 check refuses a new renewal then. It's also hidden once a renewal
# submitted in this window is approved, provisionally or in full; an approval from an earlier
# window doesn't count. A declined renewal never counts, so the host can renew again.
finished_statuses = [status.value for status in RENEWAL_FINISHED_STATES]
approved_statuses = [status.value for status in RENEWAL_APPROVED_STATES]
submitted_renewal_application = (
Application.query.filter(
Application.registration_id == registration.id,
Application.type == ApplicationType.RENEWAL.value,
Application.status.notin_(
[
Application.Status.DRAFT.value,
Application.Status.PAYMENT_DUE.value,
Application.Status.DECLINED.value,
Application.Status.PROVISIONALLY_DECLINED.value,
]
or_(
Application.status.notin_(
[
Application.Status.DRAFT.value,
Application.Status.PAYMENT_DUE.value,
*finished_statuses,
]
),
and_(
Application.status.in_(approved_statuses),
Application.application_date >= renewal_window.start_utc,
),
),
Application.application_date >= renewal_window.start_utc,
)
.order_by(Application.application_date.desc())
.first()
Expand Down Expand Up @@ -1209,11 +1218,11 @@ def search_registrations():
items:
type: string
enum: [FULL_REVIEW_APPROVED, AUTO_APPROVED, PROVISIONALLY_APPROVED]
description: Approval method filter. Can provide multiple values.
description: Approval method filter, on the latest application's status. Can provide multiple values. Registrations that reviewRenew would match are left out of the approved results (FULL_REVIEW_APPROVED, AUTO_APPROVED, and provisional with an examiner decision), and provisional records with no examiner decision are only returned when no renewal has been filed.
- in: query
name: examinerReviewed
type: boolean
description: When provided with approvalMethod, filter provisional records by whether an examiner decision exists at either registration level or latest application level.
description: When provided with approvalMethod, filter provisional records by whether an examiner decision exists at either registration level or latest application level. When omitted, provisional records of either kind are returned.
- in: query
name: nocStatus
type: array
Expand All @@ -1232,7 +1241,7 @@ def search_registrations():
- in: query
name: reviewRenew
type: boolean
description: When true, only return registrations with a filed renewal, no registration-level or latest-application decider, and latest application status in PROVISIONALLY_APPROVED/PROVISIONAL_REVIEW.
description: When true, only return registrations with no registration-level decider and at least one renewal in PROVISIONALLY_APPROVED/PROVISIONAL_REVIEW with no decider, whatever applications came after it.
responses:
200:
description:
Expand Down
13 changes: 13 additions & 0 deletions strr-api/src/strr_api/services/application_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@
Application.Status.ADDITIONAL_INFO_REQUESTED,
]
APPLICATION_UNPAID_STATES = [Application.Status.DRAFT, Application.Status.PAYMENT_DUE]
# A renewal in one of these statuses has extended the registration. Provisional approval (PROVISIONAL_REVIEW, and its
# NOC statuses) extends it at once, even though an examiner may still review it, so it counts as approved too.
RENEWAL_APPROVED_STATES = [
Application.Status.FULL_REVIEW_APPROVED,
Application.Status.PROVISIONALLY_APPROVED,
Application.Status.AUTO_APPROVED,
Application.Status.PROVISIONAL_REVIEW,
Application.Status.PROVISIONAL_REVIEW_NOC_PENDING,
Application.Status.PROVISIONAL_REVIEW_NOC_EXPIRED,
]
RENEWAL_DECLINED_STATES = [Application.Status.DECLINED, Application.Status.PROVISIONALLY_DECLINED]
# A renewal in any other status is still in progress, and blocks a new one
RENEWAL_FINISHED_STATES = RENEWAL_APPROVED_STATES + RENEWAL_DECLINED_STATES
APPLICATION_ASSIGN_STATES = [
Application.Status.FULL_REVIEW,
Application.Status.PROVISIONAL_REVIEW,
Expand Down
Loading
Loading