Skip to content

Commit

Permalink
Merge pull request #398 from openstax/notifications
Browse files Browse the repository at this point in the history
Modified user preferences
  • Loading branch information
chrisbendel authored Jan 6, 2025
2 parents 5eff6c1 + 24d254c commit 8c399c7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 59 deletions.
26 changes: 8 additions & 18 deletions backend/app/bindings/api/v1/bindings/user_preferences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

module Api::V1::Bindings
class UserPreferences
# User wishes to receive email about cycle deadlines
attr_accessor :cycle_deadlines_email

# User wishes to receive email about price deadlines
attr_accessor :prize_cycle_email
# User wishes to receive email when new digital badge becomes available
attr_accessor :digital_badge_available_email

# User wishes to receive email about study availibility
attr_accessor :study_available_email
Expand All @@ -36,8 +33,7 @@ class UserPreferences
# Attribute mapping from ruby-style variable name to JSON key.
def self.attribute_map
{
:'cycle_deadlines_email' => :'cycle_deadlines_email',
:'prize_cycle_email' => :'prize_cycle_email',
:'digital_badge_available_email' => :'digital_badge_available_email',
:'study_available_email' => :'study_available_email',
:'session_available_email' => :'session_available_email',
:'has_viewed_analysis_tutorial' => :'has_viewed_analysis_tutorial',
Expand All @@ -53,8 +49,7 @@ def self.acceptable_attributes
# Attribute type mapping.
def self.openapi_types
{
:'cycle_deadlines_email' => :'Boolean',
:'prize_cycle_email' => :'Boolean',
:'digital_badge_available_email' => :'Boolean',
:'study_available_email' => :'Boolean',
:'session_available_email' => :'Boolean',
:'has_viewed_analysis_tutorial' => :'Boolean',
Expand Down Expand Up @@ -83,12 +78,8 @@ def initialize(attributes = {})
h[k.to_sym] = v
}

if attributes.key?(:'cycle_deadlines_email')
self.cycle_deadlines_email = attributes[:'cycle_deadlines_email']
end

if attributes.key?(:'prize_cycle_email')
self.prize_cycle_email = attributes[:'prize_cycle_email']
if attributes.key?(:'digital_badge_available_email')
self.digital_badge_available_email = attributes[:'digital_badge_available_email']
end

if attributes.key?(:'study_available_email')
Expand Down Expand Up @@ -126,8 +117,7 @@ def valid?
def ==(o)
return true if self.equal?(o)
self.class == o.class &&
cycle_deadlines_email == o.cycle_deadlines_email &&
prize_cycle_email == o.prize_cycle_email &&
digital_badge_available_email == o.digital_badge_available_email &&
study_available_email == o.study_available_email &&
session_available_email == o.session_available_email &&
has_viewed_analysis_tutorial == o.has_viewed_analysis_tutorial &&
Expand All @@ -143,7 +133,7 @@ def eql?(o)
# Calculates hash code according to all attributes.
# @return [Integer] Hash code
def hash
[cycle_deadlines_email, prize_cycle_email, study_available_email, session_available_email, has_viewed_analysis_tutorial, has_viewed_welcome_message].hash
[digital_badge_available_email, study_available_email, session_available_email, has_viewed_analysis_tutorial, has_viewed_welcome_message].hash
end

# Builds the object from hash
Expand Down
8 changes: 2 additions & 6 deletions backend/app/controllers/api/v1/preferences_open_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ class Api::V1::PreferencesOpenApi
openapi_component do

schema :UserPreferences do
property :cycle_deadlines_email do
property :digital_badge_available_email do
key :type, :boolean
key :description, 'User wishes to receive email about cycle deadlines'
end
property :prize_cycle_email do
key :type, :boolean
key :description, 'User wishes to receive email about price deadlines'
key :description, 'User wishes to receive email when new digital badge becomes available'
end
property :study_available_email do
key :type, :boolean
Expand Down
8 changes: 8 additions & 0 deletions backend/db/migrate/20241219132405_update_user_preferences.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UpdateUserPreferences < ActiveRecord::Migration[7.1]
def change
remove_column :user_preferences, :prize_cycle_email, :boolean
remove_column :user_preferences, :cycle_deadlines_email, :boolean

add_column :user_preferences, :digital_badge_available_email, :boolean, default: false, null: false
end
end
6 changes: 3 additions & 3 deletions backend/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -779,14 +779,13 @@ ALTER SEQUENCE public.study_researchers_id_seq OWNED BY public.study_researchers
CREATE TABLE public.user_preferences (
id bigint NOT NULL,
user_id uuid NOT NULL,
cycle_deadlines_email boolean DEFAULT false NOT NULL,
prize_cycle_email boolean DEFAULT false NOT NULL,
study_available_email boolean DEFAULT false NOT NULL,
session_available_email boolean DEFAULT true NOT NULL,
created_at timestamp(6) with time zone NOT NULL,
updated_at timestamp(6) with time zone NOT NULL,
has_viewed_analysis_tutorial boolean DEFAULT false,
has_viewed_welcome_message boolean DEFAULT false
has_viewed_welcome_message boolean DEFAULT false,
digital_badge_available_email boolean DEFAULT false NOT NULL
);


Expand Down Expand Up @@ -1477,6 +1476,7 @@ ALTER TABLE ONLY public.response_exports
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20241219132405'),
('20240718193802'),
('20240718103317'),
('20240712192733'),
Expand Down
18 changes: 8 additions & 10 deletions backend/spec/requests/api/v1/preferences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
expect(response).to have_http_status(:ok)
expect(response_hash).to match(
a_hash_including(
cycle_deadlines_email: false,
prize_cycle_email: false,
digital_badge_available_email: false,
session_available_email: true,
study_available_email: false
)
Expand All @@ -37,13 +36,12 @@
before { stub_current_user(user_id) }

it 'returns their saved preferences' do
UserPreferences.create!(user_id:, cycle_deadlines_email: true)
UserPreferences.create!(user_id:, digital_badge_available_email: true)
get '/api/v1/preferences'
expect(response).to have_http_status(:ok)
expect(response_hash).to match(
a_hash_including(
cycle_deadlines_email: true,
prize_cycle_email: false,
digital_badge_available_email: true,
session_available_email: true,
study_available_email: false
)
Expand All @@ -56,7 +54,7 @@
describe 'update' do
context 'when no user is logged in' do
it 'rejects the request' do
post '/api/v1/preferences', params: { preferences: { cycle_deadlines_email: false } }
post '/api/v1/preferences', params: { preferences: { digital_badge_available_email: true } }
expect(response).to have_http_status(:unauthorized)
end
end
Expand All @@ -66,13 +64,13 @@

it 'updates' do
expect {
post '/api/v1/preferences', params: { preferences: { cycle_deadlines_email: false } }
post '/api/v1/preferences', params: { preferences: { digital_badge_available_email: true } }
expect(response).to have_http_status(:accepted)
}.to change { UserPreferences.count }.by(1)
expect(UserPreferences.for_user_id(user_id).cycle_deadlines_email).to be false
expect(UserPreferences.for_user_id(user_id).digital_badge_available_email).to be true

post '/api/v1/preferences', params: { preferences: { cycle_deadlines_email: true } }
expect(UserPreferences.for_user_id(user_id).cycle_deadlines_email).to be true
post '/api/v1/preferences', params: { preferences: { digital_badge_available_email: false } }
expect(UserPreferences.for_user_id(user_id).digital_badge_available_email).to be false
end

it 'updates the settings' do
Expand Down
16 changes: 4 additions & 12 deletions frontend/src/api/models/UserPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ import { exists, mapValues } from '../runtime';
*/
export interface UserPreferences {
/**
* User wishes to receive email about cycle deadlines
* User wishes to receive email when new digital badge becomes available
* @type {boolean}
* @memberof UserPreferences
*/
cycleDeadlinesEmail?: boolean;
/**
* User wishes to receive email about price deadlines
* @type {boolean}
* @memberof UserPreferences
*/
prizeCycleEmail?: boolean;
digitalBadgeAvailableEmail?: boolean;
/**
* User wishes to receive email about study availibility
* @type {boolean}
Expand Down Expand Up @@ -76,8 +70,7 @@ export function UserPreferencesFromJSONTyped(json: any, ignoreDiscriminator: boo
}
return {

'cycleDeadlinesEmail': !exists(json, 'cycle_deadlines_email') ? undefined : json['cycle_deadlines_email'],
'prizeCycleEmail': !exists(json, 'prize_cycle_email') ? undefined : json['prize_cycle_email'],
'digitalBadgeAvailableEmail': !exists(json, 'digital_badge_available_email') ? undefined : json['digital_badge_available_email'],
'studyAvailableEmail': !exists(json, 'study_available_email') ? undefined : json['study_available_email'],
'sessionAvailableEmail': !exists(json, 'session_available_email') ? undefined : json['session_available_email'],
'hasViewedAnalysisTutorial': !exists(json, 'has_viewed_analysis_tutorial') ? undefined : json['has_viewed_analysis_tutorial'],
Expand All @@ -94,8 +87,7 @@ export function UserPreferencesToJSON(value?: UserPreferences | null): any {
}
return {

'cycle_deadlines_email': value.cycleDeadlinesEmail,
'prize_cycle_email': value.prizeCycleEmail,
'digital_badge_available_email': value.digitalBadgeAvailableEmail,
'study_available_email': value.studyAvailableEmail,
'session_available_email': value.sessionAvailableEmail,
'has_viewed_analysis_tutorial': value.hasViewedAnalysisTutorial,
Expand Down
13 changes: 3 additions & 10 deletions frontend/src/screens/account-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,9 @@ export default function AccountDetails() {

<InputField
type="checkbox"
name="cycleDeadlinesEmail"
className="prizeCycleDeadlineEmail"
label="Notify me of upcoming prize cycle deadlines"
/>

<InputField
type="checkbox"
name="prizeCycleEmail"
className="newPrizeCycleEmail"
label="Notify me of the start of a new prize cycle"
name="digitalBadgeAvailableEmail"
className="newDigitalBadgeAvailableEmail"
label="Notify me of when a new digital badge is available"
/>

<InputField
Expand Down

0 comments on commit 8c399c7

Please sign in to comment.