Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: include expiration date during enrollment #1973

Closed
wants to merge 2 commits into from
Closed
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
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
65 changes: 51 additions & 14 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,47 @@ 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:
analytics.returned_success(request, eligibility.group_id)
return success(request)
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):
return _link_concession_group_funding_source(
request, eligibility, client, matching_group_funding_source, group_id
)
# 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:
analytics.returned_error(request, str(e))
raise Exception(f"{e}: {e.response.json()}")
return _link_concession_group_funding_source(
request, eligibility, client, matching_group_funding_source, group_id
)

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 All @@ -125,6 +147,21 @@ def index(request):
return TemplateResponse(request, TEMPLATE_INDEX, context)


def _link_concession_group_funding_source(request, eligibility, client: Client, matching_group_funding_source, group_id):
# 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=matching_group_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=matching_group_funding_source.id, group_id=group_id, expiry_date=expiry_date
)

analytics.returned_success(request, eligibility.group_id)
return success(request)


@decorator_from_middleware(EligibleSessionRequired)
def retry(request):
"""View handler for a recoverable failure condition."""
Expand Down
Loading