Skip to content

Commit

Permalink
feat: outline new business logic for enrollment that includes expiry
Browse files Browse the repository at this point in the history
stub out a module for handling the calculations related to expiration /
re-enrollment
  • Loading branch information
angela-tran committed Mar 21, 2024
1 parent 31b486c commit 4de91de
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
18 changes: 18 additions & 0 deletions benefits/enrollment/expiration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
The enrollment application: helper functions for calculations related to expiration / re-enrollment.
"""


def is_expired(group_funding_source):
"""Returns whether the funding source has expired or not."""
pass


def calculate_reenrollment_date(group_funding_source, expiration_reenrollment_days):
"""Returns the date on which the funding source can be re-enrolled."""
pass


def calculate_expiry_date(group_funding_source, expiration_days):
"""Returns the date on which the funding source expires."""
pass
57 changes: 44 additions & 13 deletions benefits/enrollment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
pageview_decorator,
)
from benefits.core.views import ROUTE_LOGGED_OUT
from . import analytics, forms
from . import analytics, expiration, forms


ROUTE_INDEX = "enrollment:index"
Expand Down Expand Up @@ -84,25 +84,56 @@ def index(request):
client.oauth.ensure_active_token(client.token)

funding_source = client.get_funding_source_by_token(card_token)
group_id = eligibility.group_id

try:
client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=eligibility.group_id)
except HTTPError as e:
# 409 means that customer already belongs to a concession group.
# the response JSON will look like:
# {"errors":[{"detail":"Conflict (409) - Customer already belongs to a concession group."}]}
if e.response.status_code == 409:
group_funding_sources = client.get_concession_group_linked_funding_sources(group_id)
matching_group_funding_source = None
for group_funding_source in group_funding_sources:
if group_funding_source.id == funding_source.id:
matching_group_funding_source = group_funding_source
break

# funding source is already linked to the group
if matching_group_funding_source:
# if no expiration date, return success
if matching_group_funding_source.concession_expiry is None:
analytics.returned_success(request, group_id)
return success(request)
# else if expiration date has passed, then re-enroll
elif expiration.is_expired(matching_group_funding_source):
client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=group_id)
analytics.returned_success(request, group_id)
return success(request)
# else expiration date hasn't passed, so calculate re-enrollment date and show user
else:
reenrollment_date = expiration.calculate_reenrollment_date( # noqa
matching_group_funding_source, eligibility.expiration_reenrollment_days
)

analytics.returned_error(request, "Funding source already enrolled and has not expired")
# todo for #1921: show reenrollment_date to user
# funding source has not been linked to the group yet
else:
# if eligibility_type does not supports_expiration, then just link it
if not eligibility.supports_expiration:
client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=group_id)
# else eligibility_type supports_expiration, so calculate expiration date from today and include in request
else:
expiry_date = expiration.calculate_expiry_date(matching_group_funding_source, eligibility.expiration_days)
client.link_concession_group_funding_source(
funding_source_id=funding_source.id, group_id=group_id, expiry_date=expiry_date
)

analytics.returned_success(request, eligibility.group_id)
return success(request)
else:
analytics.returned_error(request, str(e))
raise Exception(f"{e}: {e.response.json()}")

except HTTPError as e:
analytics.returned_error(request, str(e))
raise Exception(f"{e}: {e.response.json()}")
except Exception as e:
analytics.returned_error(request, str(e))
raise e
else:
analytics.returned_success(request, eligibility.group_id)
return success(request)

# GET enrollment index
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"django-admin-sortable2==2.1.5",
"django-google-sso==6.0.2",
"eligibility-api==2023.9.1",
"calitp-littlepay==2024.3.1",
"calitp-littlepay@git+https://github.com/cal-itp/littlepay",
"requests==2.31.0",
"sentry-sdk==1.41.0",
"six==1.16.0",
Expand Down

0 comments on commit 4de91de

Please sign in to comment.