diff --git a/app/commands.py b/app/commands.py index b120838cd..766bc9f2d 100644 --- a/app/commands.py +++ b/app/commands.py @@ -38,6 +38,7 @@ from app.dao.service_callback_api_dao import get_service_delivery_status_callbac from app.dao.services_dao import ( delete_service_and_all_associated_db_objects, dao_fetch_all_services_by_user, + dao_fetch_all_services_created_by_user, dao_fetch_service_by_id, dao_update_service ) @@ -119,9 +120,20 @@ def purge_functional_test_data(user_email_prefix): else: services = dao_fetch_all_services_by_user(usr.id) if services: + print(f"Deleting user {usr.id} which is part of services") for service in services: delete_service_and_all_associated_db_objects(service) else: + services_created_by_this_user = dao_fetch_all_services_created_by_user(usr.id) + if services_created_by_this_user: + # user is not part of any services but may still have been the one to create the service + # sometimes things get in this state if the tests fail half way through + # Remove the service they created (but are not a part of) so we can then remove the user + print(f"Deleting services created by {usr.id}") + for service in services_created_by_this_user: + delete_service_and_all_associated_db_objects(service) + + print(f"Deleting user {usr.id} which is not part of any services") delete_user_verify_codes(usr) delete_model_user(usr) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index cf2541f60..8a5e35101 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -234,6 +234,16 @@ def dao_fetch_all_services_by_user(user_id, only_active=False): return query.all() +def dao_fetch_all_services_created_by_user(user_id): + query = Service.query.filter_by( + created_by_id=user_id + ).order_by( + asc(Service.created_at) + ) + + return query.all() + + @transactional @version_class( VersionOptions(ApiKey, must_write_history=False),