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: Remove inbound sms from service #4312

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

joybytes
Copy link
Contributor

@joybytes joybytes commented Dec 15, 2024

  1. Created endpoint to remove inbound sms for service(remove_inbound_sms_for_service)
    This process is currently done manually with this query https://github.com/alphagov/notifications-manuals/wiki/Support-Runbook#query-to-remove-the-ability-to-receive-text-messages
    This endpoint:
  • removes inbound_sms permission for the service
  • removes service_sms_senders when inbound number id is present
  • in inbound_number table disassociates service id from the number and based on the payload (archive: bool) we archive the number by setting the active field to false or release by leaving the active field as true
  1. Created endpoint to get the most recent usage date for the service inbound number (get_most_recent_inbound_usage_date):
  • This checks the following tables: notification, sms inbound, sms inbound history
  • This was created to provide a date of last usage on admin, when platform admins are looking to archive or release the number, they have this information promptly which can help with the decision

Ticket:

https://trello.com/c/jC9B8l27/1056-build-platform-admin-endpoint-to-remove-inbound-sms-from-service

@joybytes joybytes changed the title feat: remove inbound sms from service feat: Remove inbound sms from service Dec 16, 2024
@joybytes joybytes force-pushed the feat/remove-inbound-sms-from-service branch 3 times, most recently from ffede18 to c271b16 Compare December 16, 2024 11:03
@joybytes joybytes marked this pull request as ready for review December 16, 2024 11:04
Comment on lines 84 to 86
dao_remove_service_permission(service_id, INBOUND_SMS_TYPE)
dao_remove_inbound_sms_senders(service_id)
archive_or_release_inbound_number_for_service(service_id, archive)
Copy link
Contributor

@spatel033 spatel033 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are three database operations. If the second or last transaction fails, data inconsistency may arise. For instance, if a user attempts to archive a number and dao_remove_inbound_sms_senders encounters an exception, archive_or_release_inbound_number_for_service will not be executed. Consequently, only dao_remove_service_permission will be successful, leading to a partial update and potential data corruption.

Transaction allows series of database operations are either all committed to the database or all rolled back if an error occurs. We can follow similar approach https://github.com/alphagov/notifications-api/blob/81af15c03ca0ec7557101752291d8305e658217f/app/dao/services_dao.py#L310C1-L331C1. May be @klssmith or @leohemsted can advise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good point actually! well spotted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joybytes joybytes force-pushed the feat/remove-inbound-sms-from-service branch 2 times, most recently from 621dd4d to 022a969 Compare December 19, 2024 16:01
@joybytes joybytes requested a review from spatel033 December 19, 2024 16:10
We are automating the following query

```
select * from service_sms_senders where service_id= 'service_id';
select * from inbound_numbers where service_id = 'service_id';

select * from service_permissions where service_id= 'service_id';

delete from service_permissions where service_id = 'service_id' and permission = 'inbound_sms';

delete from service_sms_senders where service_id = 'service_id' and inbound_number_id is not null;

-- run one of the two following queries
-- if we wish to release the number (if it has never been used)
update inbound_numbers set service_id = null, updated_at = now()
where service_id = 'service_id' and active;

-- if we wish to archive the number instead (if it has been used before so we dont want new services receiving the prev service's messages)
update inbound_numbers set service_id = null, updated_at = now(), active = false
where service_id = 'service_id' and active;
```

- Remove inbound_sms permission from service
- Remove service_sms_sender with inbound_number_id
- Disassociate inbound_number with service
- If the number is being release, leave 'active' status as True, if being archived, update 'active' to False
…vice

- Look through the following tables to get most recent date for inbound sms use: notification, inbound sms and inbound sms history
- Endpoint created to showcase information on the front-end in regards to number activity to prevent us from going into the DB to check the latest use for the number when deciding to archive or release this number
@joybytes joybytes force-pushed the feat/remove-inbound-sms-from-service branch from d5cb226 to 76e4666 Compare December 19, 2024 16:22
@@ -106,3 +108,15 @@ def _raise_when_no_default(old_default):
# check that the update is not updating the only default to false
if not old_default:
raise Exception("You must have at least one SMS sender as the default.", 400)


def dao_remove_inbound_sms_senders(service_id: UUID, commit=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def dao_remove_inbound_sms_senders(service_id: UUID, commit=True):
@autocommit
def dao_remove_inbound_sms_senders(service_id: UUID):
result = (
ServiceSmsSender.query.filter_by(service_id=service_id)
.filter(ServiceSmsSender.inbound_number_id.isnot(None))
.delete(synchronize_session="fetch")
)
return result

I am not sure but is commit=True and flag necessary? transaction is a generator function and it handles the sessions and commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants