-
Notifications
You must be signed in to change notification settings - Fork 0
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
Preferences #590
Merged
Merged
Preferences #590
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
781eaa0
Create preferences pages mockup
DeeTheDev 19af8a7
Add update API call and success and error handling
DeeTheDev 357adcf
Add preference api, model changes, and field masking
rgalanakis 3a9b69b
Melt the preferences UI and API
rgalanakis 482c822
Localize context and fix paragraph spacing
DeeTheDev 9979f3b
Add member detail preferences fields
DeeTheDev 590e6af
Refactor exposed details and expose public url
DeeTheDev 51037f3
Add Copyable component, fix naming
DeeTheDev 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 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 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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
Sequel.migration do | ||
up do | ||
alter_table(:message_preferences) do | ||
add_column :access_token, :text, null: true, unique: true | ||
add_column :account_updates_optout, :boolean, null: false, default: false | ||
end | ||
from(:message_preferences).each do |row| | ||
from(:message_preferences).where(id: row.fetch(:id)).update(access_token: SecureRandom.uuid) | ||
end | ||
alter_table(:message_preferences) do | ||
set_column_not_null :access_token | ||
end | ||
end | ||
down do | ||
alter_table(:message_preferences) do | ||
drop_column :access_token | ||
drop_column :account_updates_optout | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "grape" | ||
|
||
require "suma/api" | ||
|
||
class Suma::API::Preferences < Suma::API::V1 | ||
include Suma::API::Entities | ||
|
||
resource :preferences do | ||
helpers do | ||
def update_preferences(member) | ||
params[:subscriptions].each do |k, optin| | ||
k = k.to_sym | ||
invalid!("subscription value #{k} must be a bool") unless Suma.bool?(optin) | ||
subscr = member.preferences!.subscriptions.find { |g| g.key == k && g.editable? } | ||
invalid!("subscription #{k} is invalid") if subscr.nil? | ||
subscr.set_from_opted_in(optin) | ||
end | ||
member.preferences.save_changes | ||
end | ||
end | ||
|
||
resource :public do | ||
helpers do | ||
def member! | ||
prefs = Suma::Message::Preferences[access_token: params[:access_token]] | ||
unauthenticated! if prefs.nil? | ||
unauthenticated! if prefs.member.soft_deleted? | ||
return prefs.member | ||
end | ||
end | ||
params do | ||
requires :access_token, type: String | ||
end | ||
get do | ||
member = member! | ||
present member, with: PublicPrefsMemberEntity | ||
end | ||
|
||
params do | ||
requires :access_token, type: String | ||
requires :subscriptions, type: Hash | ||
end | ||
post do | ||
member = member! | ||
update_preferences(member) | ||
status 200 | ||
present member, with: PublicPrefsMemberEntity | ||
end | ||
end | ||
|
||
params do | ||
requires :subscriptions, type: Hash | ||
end | ||
post do | ||
member = current_member | ||
update_preferences(member) | ||
status 200 | ||
present member, with: CurrentMemberEntity | ||
end | ||
end | ||
|
||
class PublicPrefsEntity < BaseEntity | ||
expose :subscriptions, with: Suma::API::Entities::PreferencesSubscriptionEntity | ||
end | ||
|
||
class PublicPrefsMemberEntity < BaseEntity | ||
expose :masked_email, as: :email | ||
expose :masked_name, as: :name | ||
expose :masked_phone, as: :phone | ||
expose :preferences!, as: :preferences, with: PublicPrefsEntity | ||
end | ||
end |
This file contains 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 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 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 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,111 @@ | ||
# frozen_string_literal: true | ||
|
||
require "suma/api/preferences" | ||
|
||
RSpec.describe Suma::API::Preferences, :db do | ||
include Rack::Test::Methods | ||
|
||
let(:app) { described_class.build_app } | ||
let(:member) { Suma::Fixtures.member.create(name: "Pedro Pascal") } | ||
|
||
describe "GET /v1/preferences/public" do | ||
it "returns prefs if the prefs access token is given" do | ||
get "/v1/preferences/public", access_token: member.preferences!.access_token | ||
|
||
expect(last_response).to have_status(200) | ||
expect(last_response).to have_json_body.that_includes( | ||
name: "Pe***al", | ||
preferences: include( | ||
subscriptions: [ | ||
{key: "account_updates", opted_in: true, editable_state: "on"}, | ||
{key: "marketing", opted_in: false, editable_state: "hidden"}, | ||
{key: "security", opted_in: true, editable_state: "off"}, | ||
], | ||
), | ||
) | ||
end | ||
|
||
it "401s for an invalid access token" do | ||
get "/v1/preferences/public", access_token: "abcd" | ||
|
||
expect(last_response).to have_status(401) | ||
end | ||
|
||
it "401s for a deleted member" do | ||
member.soft_delete | ||
get "/v1/preferences/public", access_token: member.preferences!.access_token | ||
|
||
expect(last_response).to have_status(401) | ||
end | ||
end | ||
|
||
describe "POST /v1/preferences/public" do | ||
it "updates prefs of the user with the access token" do | ||
post "/v1/preferences/public", | ||
access_token: member.preferences!.access_token, | ||
subscriptions: {account_updates: false} | ||
|
||
expect(last_response).to have_status(200) | ||
expect(last_response).to have_json_body.that_includes( | ||
name: "Pe***al", | ||
preferences: include( | ||
subscriptions: include(hash_including({key: "account_updates", opted_in: false, editable_state: "on"})), | ||
), | ||
) | ||
expect(member.preferences.refresh).to have_attributes(account_updates_optout: true) | ||
end | ||
|
||
it "401s for an invalid access token" do | ||
post "/v1/preferences/public", access_token: "abcd", subscriptions: {} | ||
|
||
expect(last_response).to have_status(401) | ||
end | ||
|
||
it "errors for an invalid subscription key" do | ||
post "/v1/preferences/public", | ||
access_token: member.preferences!.access_token, | ||
subscriptions: {foo: false} | ||
|
||
expect(last_response).to have_status(400) | ||
expect(last_response).to have_json_body. | ||
that_includes(error: include(message: "Subscription foo is invalid")) | ||
end | ||
|
||
it "errors for an invalid subscription value" do | ||
post "/v1/preferences/public", | ||
access_token: member.preferences!.access_token, | ||
subscriptions: {account_updates: nil} | ||
|
||
expect(last_response).to have_status(400) | ||
expect(last_response).to have_json_body. | ||
that_includes(error: include(message: "Subscription value account_updates must be a bool")) | ||
end | ||
end | ||
|
||
describe "POST /v1/preferences" do | ||
before(:each) do | ||
login_as(member) | ||
end | ||
|
||
it "updates prefs of the authed user" do | ||
post "/v1/preferences", subscriptions: {account_updates: false} | ||
|
||
expect(last_response).to have_status(200) | ||
expect(last_response).to have_json_body.that_includes( | ||
name: "Pedro Pascal", | ||
preferences: include( | ||
subscriptions: include(hash_including({key: "account_updates", opted_in: false, editable_state: "on"})), | ||
), | ||
) | ||
expect(member.preferences.refresh).to have_attributes(account_updates_optout: true) | ||
end | ||
|
||
it "401s if the user cannot auth" do | ||
logout | ||
|
||
post "/v1/preferences", subscriptions: {account_updates: false} | ||
|
||
expect(last_response).to have_status(401) | ||
end | ||
end | ||
end |
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.
I don't think this is ideal (should be like
{preferences: {subscriptions: []}}
but it's admin so easy to change later.