-
Notifications
You must be signed in to change notification settings - Fork 2
feat: #1469 Enable email notification for granting delegated admin #1529
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5326e0
feat(1469: added gc notify integration to admin management, refs: #1469
MCatherine1994 fb54588
fix(1469): simplify gc notify api key, refs: #1469
MCatherine1994 3379bff
Merge branch 'main' of https://github.com/bcgov/nr-forests-access-man…
MCatherine1994 ccae93a
feat(1469): enable sending email notification for delegated admin, re…
MCatherine1994 ecd075c
fix(1469): fix gc notify parameters and add logs, refs: #1469
MCatherine1994 f2e2229
fix(1469): only send email for successful grants, refs: #1469
MCatherine1994 9f4a86c
fix(1469): add gc notify key to admin management terraform config, re…
MCatherine1994 0ed321e
Merge branch 'main' of https://github.com/bcgov/nr-forests-access-man…
MCatherine1994 bff7e41
fix(1469): update email message, refs: #1469
MCatherine1994 172ae33
fix(1469): handle the situation when all roles failed to grant, no em…
MCatherine1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,64 @@ | ||
import logging | ||
|
||
import requests | ||
from api.app.schemas import GCNotifyGrantDelegatedAdminEmailParam | ||
from api.config import config | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
GC_NOTIFY_EMAIL_BASE_URL = "https://api.notification.canada.ca" | ||
GC_NOTIFY_GRANT_DELEGATED_ADMIN_EMAIL_TEMPLATE_ID = "9abff613-e507-4562-aae0-008317dfe3b9" | ||
GC_NOTIFY_GRANT_APP_ADMIN_EMAIL_TEMPLATE_ID = "230bca59-4906-40b2-8f2b-2f6186a98663" | ||
|
||
|
||
class GCNotifyEmailService: | ||
""" | ||
The class is used for sending email | ||
""" | ||
|
||
TIMEOUT = (5, 10) # Timeout (connect, read) in seconds. | ||
|
||
def __init__(self): | ||
self.API_KEY = config.get_gc_notify_email_api_key() | ||
self.email_base_url = GC_NOTIFY_EMAIL_BASE_URL | ||
self.headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": "ApiKey-v1 " + self.API_KEY, | ||
} | ||
|
||
self.session = requests.Session() | ||
self.session.headers.update(self.headers) | ||
|
||
def send_delegated_admin_granted_email(self, params: GCNotifyGrantDelegatedAdminEmailParam): | ||
""" | ||
Send email notification for new delegated admin | ||
""" | ||
# GC Notify does not have sufficient conditional rendering, cannot send None to variable, and does not support | ||
# 'variable' within coditional text. Easier to do this in code. | ||
contact_message = ( | ||
f"Please contact your administrator {params.application_team_contact_email} if you have any questions." | ||
if params.application_team_contact_email is not None | ||
else "Please contact your administrator if you have any questions." | ||
) | ||
|
||
email_params = { | ||
"email_address": params.send_to_email_address, | ||
"template_id": GC_NOTIFY_GRANT_DELEGATED_ADMIN_EMAIL_TEMPLATE_ID, | ||
"personalisation": { | ||
"first_name": params.first_name, | ||
"last_name": params.last_name, | ||
"application_name": params.application_name, | ||
"role_list_string": params.role_list_string, | ||
"contact_message": contact_message | ||
}, | ||
} | ||
gc_notify_email_send_url = f"{self.email_base_url}/v2/notifications/email" | ||
|
||
r = self.session.post( | ||
gc_notify_email_send_url, timeout=self.TIMEOUT, json=email_params | ||
) | ||
r.raise_for_status() # There is a general error handler, see: requests_http_error_handler | ||
send_email_result = r.json() | ||
|
||
LOGGER.debug(f"Send Email result: {send_email_result}") | ||
return send_email_result |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.