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

Fix: Create or update notification settings to avoid error when settings doesn’t exist #658

Merged

Conversation

samsmithKruz
Copy link
Contributor

Description

​This PR ensures that notification settings are created automatically if they don’t already exist when a user attempts to update them. Previously, users without existing notification preferences faced issues updating settings.

Now, when a PATCH request is sent to /api/v1/settings/notification-settings, the system checks if a notification preference exists for the user. If none exists, a new record is created before applying the update.

Related Issue (Link to Github issue)

Closes #657

Steps to Reproduce:

Screenshot 2025-03-02 at 19 14 13
  1. Send a PATCH request to /api/v1/settings/notification-settings with a valid JSON body as shown below, for a user who has no existing notification settings:

    {
      "mobile_push_notifications": true,
      "email_notification_activity_in_workspace": true,
      "email_notification_always_send_email_notifications": true,
      "email_notification_email_digest": true,
      "email_notification_announcement_and_update_emails": true,
      "slack_notifications_activity_on_your_workspace": true,
      "slack_notifications_always_send_email_notifications": true,
      "slack_notifications_announcement_and_update_emails": true
    }
    
  2. Observe that the request fails because the application does not create notification settings for users who do not already have them.

  3. As a result, users without existing notification settings receive an error instead of having default settings created automatically.

  4. This bug prevents users from updating their notification preferences unless their settings already exist in the database.
    Fix: Bug in Updating Notification Settings #657

Motivation and Context

This change is required to ensure that users who have not previously set their notification preferences can still update them without encountering errors. Previously, the system expected existing settings, preventing new users from modifying their preferences. This fix allows the system to create default settings if none exist, ensuring a seamless user experience.

How Has This Been Tested?

  • Unit Test: Added a new test case it_creates_settings_if_none_exist in UpdateNotificationPreferenceTest.php to verify that notification settings are created when a user attempts to update settings for the first time.
  • Manual Testing:
    1. Sent a PATCH request to /api/v1/settings/notification-settings with valid notification settings for a user without existing preferences.
    2. Verified that the settings were successfully created in the database.
    3. Confirmed that subsequent updates modify the existing settings instead of creating new ones.
    4. User's email must be verified before this can work.
  • Thunder Testing: Used Thunder client to simulate different scenarios, including valid updates, unauthorized access, and invalid data submissions.
Screenshot 2025-03-02 at 19 12 55 Screenshot 2025-03-02 at 19 13 13


Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@samsmithKruz samsmithKruz changed the title Fix: Ensure Notification Settings are Created if None Exist Fix: Create or update notification settings to avoid error when settings doesn’t exist Mar 3, 2025
@ezeanyimhenry ezeanyimhenry self-requested a review March 3, 2025 03:31
Copy link
Contributor

@ezeanyimhenry ezeanyimhenry left a comment

Choose a reason for hiding this comment

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

LGTM

@ezeanyimhenry ezeanyimhenry merged commit 0ed14e6 into hngprojects:dev Mar 3, 2025
1 check passed
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.

Fix: Bug in Updating Notification Settings
2 participants