|
18 | 18 | pageview_decorator,
|
19 | 19 | )
|
20 | 20 | from benefits.core.views import ROUTE_LOGGED_OUT
|
21 |
| -from . import analytics, forms |
| 21 | +from . import analytics, expiration, forms |
22 | 22 |
|
23 | 23 |
|
24 | 24 | ROUTE_INDEX = "enrollment:index"
|
@@ -84,25 +84,56 @@ def index(request):
|
84 | 84 | client.oauth.ensure_active_token(client.token)
|
85 | 85 |
|
86 | 86 | funding_source = client.get_funding_source_by_token(card_token)
|
| 87 | + group_id = eligibility.group_id |
87 | 88 |
|
88 | 89 | try:
|
89 |
| - client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=eligibility.group_id) |
90 |
| - except HTTPError as e: |
91 |
| - # 409 means that customer already belongs to a concession group. |
92 |
| - # the response JSON will look like: |
93 |
| - # {"errors":[{"detail":"Conflict (409) - Customer already belongs to a concession group."}]} |
94 |
| - if e.response.status_code == 409: |
| 90 | + group_funding_sources = client.get_concession_group_linked_funding_sources(group_id) |
| 91 | + matching_group_funding_source = None |
| 92 | + for group_funding_source in group_funding_sources: |
| 93 | + if group_funding_source.id == funding_source.id: |
| 94 | + matching_group_funding_source = group_funding_source |
| 95 | + break |
| 96 | + |
| 97 | + # funding source is already linked to the group |
| 98 | + if matching_group_funding_source: |
| 99 | + # if no expiration date, return success |
| 100 | + if matching_group_funding_source.concession_expiry is None: |
| 101 | + analytics.returned_success(request, group_id) |
| 102 | + return success(request) |
| 103 | + # else if expiration date has passed, then re-enroll |
| 104 | + elif expiration.is_expired(matching_group_funding_source): |
| 105 | + client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=group_id) |
| 106 | + analytics.returned_success(request, group_id) |
| 107 | + return success(request) |
| 108 | + # else expiration date hasn't passed, so calculate re-enrollment date and show user |
| 109 | + else: |
| 110 | + reenrollment_date = expiration.calculate_reenrollment_date( # noqa |
| 111 | + matching_group_funding_source, eligibility.expiration_reenrollment_days |
| 112 | + ) |
| 113 | + |
| 114 | + analytics.returned_error(request, "Funding source already enrolled and has not expired") |
| 115 | + # todo for #1921: show reenrollment_date to user |
| 116 | + # funding source has not been linked to the group yet |
| 117 | + else: |
| 118 | + # if eligibility_type does not supports_expiration, then just link it |
| 119 | + if not eligibility.supports_expiration: |
| 120 | + client.link_concession_group_funding_source(funding_source_id=funding_source.id, group_id=group_id) |
| 121 | + # else eligibility_type supports_expiration, so calculate expiration date from today and include in request |
| 122 | + else: |
| 123 | + expiry_date = expiration.calculate_expiry_date(matching_group_funding_source, eligibility.expiration_days) |
| 124 | + client.link_concession_group_funding_source( |
| 125 | + funding_source_id=funding_source.id, group_id=group_id, expiry_date=expiry_date |
| 126 | + ) |
| 127 | + |
95 | 128 | analytics.returned_success(request, eligibility.group_id)
|
96 | 129 | return success(request)
|
97 |
| - else: |
98 |
| - analytics.returned_error(request, str(e)) |
99 |
| - raise Exception(f"{e}: {e.response.json()}") |
| 130 | + |
| 131 | + except HTTPError as e: |
| 132 | + analytics.returned_error(request, str(e)) |
| 133 | + raise Exception(f"{e}: {e.response.json()}") |
100 | 134 | except Exception as e:
|
101 | 135 | analytics.returned_error(request, str(e))
|
102 | 136 | raise e
|
103 |
| - else: |
104 |
| - analytics.returned_success(request, eligibility.group_id) |
105 |
| - return success(request) |
106 | 137 |
|
107 | 138 | # GET enrollment index
|
108 | 139 | else:
|
|
0 commit comments