-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a command for opening cases in ahjo (#2724)
* feat: add a command for opening cases in ahjo * fix: refactored delete request and tests * feat: add applications with ahjo status in seeder * fix: failing test, line length, error handling
- Loading branch information
Showing
7 changed files
with
319 additions
and
47 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
backend/benefit/applications/management/commands/open_cases_in_ahjo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import logging | ||
import time | ||
from typing import List | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from django.core.management.base import BaseCommand | ||
|
||
from applications.enums import AhjoStatus as AhjoStatusEnum | ||
from applications.models import Application | ||
from applications.services.ahjo_authentication import AhjoToken | ||
from applications.services.ahjo_integration import ( | ||
create_status_for_application, | ||
get_applications_for_open_case, | ||
get_token, | ||
send_open_case_request_to_ahjo, | ||
) | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Send a request to Ahjo to open cases for applications" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--number", | ||
type=int, | ||
default=50, | ||
help="Number of applications to send open case requests for", | ||
) | ||
|
||
parser.add_argument( | ||
"--dry-run", | ||
action="store_true", | ||
help="Run the command without making actual changes", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
ahjo_auth_token = get_token() | ||
except ImproperlyConfigured as e: | ||
LOGGER.error(f"Failed to get auth token from Ahjo: {e}") | ||
return | ||
|
||
number_to_process = options["number"] | ||
dry_run = options["dry_run"] | ||
|
||
applications = get_applications_for_open_case() | ||
|
||
if not applications: | ||
self.stdout.write("No applications to process") | ||
return | ||
|
||
applications = applications[:number_to_process] | ||
|
||
if dry_run: | ||
self.stdout.write( | ||
f"Would send open case requests for {len(applications)} applications to Ahjo" | ||
) | ||
return | ||
|
||
self.run_requests(applications[:number_to_process], ahjo_auth_token) | ||
|
||
def run_requests(self, applications: List[Application], ahjo_auth_token: AhjoToken): | ||
start_time = time.time() | ||
successful_applications = [] | ||
|
||
self.stdout.write( | ||
f"Sending request to Ahjo to open cases for {len(applications)} applications" | ||
) | ||
|
||
for application in applications: | ||
if not application.calculation.handler.ad_username: | ||
raise ImproperlyConfigured( | ||
f"No ad_username set for the handler for Ahjo open case request for application {application.id}." | ||
) | ||
sent_application, response_text = send_open_case_request_to_ahjo( | ||
application, ahjo_auth_token.access_token | ||
) | ||
if sent_application: | ||
successful_applications.append(sent_application) | ||
self._handle_succesfully_opened_application( | ||
sent_application, response_text | ||
) | ||
|
||
self.stdout.write( | ||
f"Sent open case requests for {len(successful_applications)} applications to Ahjo" | ||
) | ||
end_time = time.time() | ||
elapsed_time = end_time - start_time | ||
self.stdout.write( | ||
f"Submitting {len(successful_applications)} open case requests took {elapsed_time} seconds to run." | ||
) | ||
|
||
def _handle_succesfully_opened_application( | ||
self, application: Application, response_text: str | ||
): | ||
"""Create Ahjo status for application and set Ahjo case guid""" | ||
create_status_for_application( | ||
application, AhjoStatusEnum.REQUEST_TO_OPEN_CASE_SENT | ||
) | ||
# The guid is returned in the response text in text format {guid}, so remove brackets here | ||
response_text = response_text.replace("{", "").replace("}", "") | ||
application.ahjo_case_guid = response_text | ||
application.save() | ||
|
||
self.stdout.write( | ||
f"Successfully submitted open case request for application {application.id} to Ahjo, \ | ||
received GUID: {response_text}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.