-
Notifications
You must be signed in to change notification settings - Fork 6
feat: user subscriptions + subscription endpoints impl #1740
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
443f884
feat: user subscriptions + subscription endpoints impl
cka-y 85fcfed
fix: doc
cka-y b87950d
merge: main
cka-y c73dc06
fix: tf
cka-y 55de00d
fix: error unauth description
cka-y 31ecc7d
Merge branch 'main' into feat/1692
cka-y 4529c79
Merge branch 'main' into feat/1692
cka-y 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
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
24 changes: 24 additions & 0 deletions
24
api/src/shared/db_models/notification_subscription_impl.py
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,24 @@ | ||
| from shared.users_database_gen.sqlacodegen_models import NotificationSubscription as NotificationSubscriptionOrm | ||
| from user_service_gen.models.notification_subscription import NotificationSubscription | ||
|
|
||
|
|
||
| class NotificationSubscriptionImpl(NotificationSubscription): | ||
| """Implementation of the NotificationSubscription model. | ||
| Converts a SQLAlchemy NotificationSubscription ORM object to a Pydantic NotificationSubscription model. | ||
| """ | ||
|
|
||
| class Config: | ||
| from_attributes = True | ||
|
|
||
| @classmethod | ||
| def from_orm(cls, sub: NotificationSubscriptionOrm | None) -> NotificationSubscription | None: | ||
| if not sub: | ||
| return None | ||
| return cls( | ||
| id=sub.id, | ||
| user_id=sub.user_id, | ||
| notification_id=sub.notification_type_id, | ||
| active=sub.active, | ||
| last_notified_at=sub.last_notified_at, | ||
| created_at=sub.created_at, | ||
| ) |
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,39 @@ | ||
| # | ||
| # MobilityData 2026 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| """Helpers shared between the authenticated (users) and public (subscriptions) APIs.""" | ||
|
|
||
| import logging | ||
|
|
||
| from fastapi import HTTPException | ||
|
|
||
| import sib_api_v3_sdk | ||
| from shared.common.brevo import add_contact_to_list, get_announcements_list_id, remove_contact_from_list | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| ANNOUNCEMENTS_NOTIFICATION_TYPE_ID = "api.announcements" | ||
|
|
||
|
|
||
| def sync_announcements(email: str, subscribe: bool, subscription_id: str | None = None) -> None: | ||
| """Sync an api.announcements subscription with Brevo, mapping provider errors to 502.""" | ||
| try: | ||
| if subscribe: | ||
| add_contact_to_list(email, get_announcements_list_id(), subscription_id) | ||
| else: | ||
| remove_contact_from_list(email, get_announcements_list_id()) | ||
| except (RuntimeError, sib_api_v3_sdk.rest.ApiException) as exc: | ||
| logger.error("Brevo sync failed for %s: %s", email, exc) | ||
| raise HTTPException(status_code=502, detail="Failed to sync subscription with email provider.") |
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,55 @@ | ||
| # | ||
| # MobilityData 2026 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| from fastapi import HTTPException | ||
|
|
||
| from shared.database.users_database import with_users_db_session | ||
| from shared.db_models.notification_subscription_impl import NotificationSubscriptionImpl | ||
| from shared.users_database_gen.sqlacodegen_models import ( | ||
| AppUser, | ||
| NotificationSubscription as NotificationSubscriptionOrm, | ||
| ) | ||
| from user_service.impl.subscription_helpers import ANNOUNCEMENTS_NOTIFICATION_TYPE_ID, sync_announcements | ||
| from user_service_gen.apis.subscriptions_api_base import BaseSubscriptionsApi | ||
| from user_service_gen.models.notification_subscription import NotificationSubscription | ||
|
|
||
|
|
||
| class SubscriptionsApiImpl(BaseSubscriptionsApi): | ||
| """Public, unauthenticated subscription management. | ||
|
|
||
| The subscription UUID is the access capability | ||
| """ | ||
|
|
||
| @with_users_db_session | ||
| def get_subscription(self, id: str, db_session=None) -> NotificationSubscription: | ||
| sub = db_session.get(NotificationSubscriptionOrm, id) | ||
| if sub is None: | ||
| raise HTTPException(status_code=404, detail="Subscription not found.") | ||
| return NotificationSubscriptionImpl.from_orm(sub) | ||
|
|
||
| @with_users_db_session | ||
| def delete_subscription(self, id: str, db_session=None) -> None: | ||
| sub = db_session.get(NotificationSubscriptionOrm, id) | ||
| if sub is None: | ||
| raise HTTPException(status_code=404, detail="Subscription not found.") | ||
|
|
||
| if sub.notification_type_id == ANNOUNCEMENTS_NOTIFICATION_TYPE_ID: | ||
| user = db_session.get(AppUser, sub.user_id) | ||
| if user is not None: | ||
| sync_announcements(user.email, subscribe=False) | ||
|
|
||
| db_session.delete(sub) | ||
| db_session.flush() |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question]: Does the
update_enabledadd the list_id to the lists, or does itresetthe list to only one list?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the doc the field
list_idsis described asIds of the lists to add the contact toso I believe it only adds. Also according to my tests with my own account, i haven't been removed from any other list when calling this endpoint.