Skip to content

Commit

Permalink
Merge pull request #114 from mlibrary/history-settings
Browse files Browse the repository at this point in the history
History Settings
  • Loading branch information
niquerio authored May 25, 2021
2 parents 93d7136 + efc776c commit 31251b2
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 75 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ npm install
docker-compose run --rm web npm install
```

You may need to log into github to pull the circulation-history image. To do that, get a [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to "read:packages". On the command line log in with your github username and then Personal Access Token as the password.
You may need to log into github to pull the circulation-history image. To do that, get a [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token) with permission to "read:packages". On the command line:
```
docker login docker.pkg.github.com
```

Log in with your github username and then Personal Access Token as the password.

start containers

Expand Down
12 changes: 12 additions & 0 deletions css/_banner.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.banner {
background: var(--color-teal-200);
padding: var(--space-x-small) 0;
* {
color: var(--color-neutral-400);
}
m-icon {
position: relative;
margin-right: var(--space-xx-small);
top: var(--space-xxx-small);
}
}
34 changes: 34 additions & 0 deletions css/_patron.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
form[method="post"] {
m-icon {
position: relative;
margin-right: var(--space-xx-small);
top: var(--space-xxx-small);
}

#text-notifications:not(:checked) ~ .sms-number-container {
display: none;
}

#sms-number:invalid + #sms-number-description {
color: var(--color-pink-500);
}

.retain-history-option {
input[type="radio"] {
&:checked + label m-icon[name="radio-button-unchecked"] {
display: none;
}
&:not(:checked) + label m-icon[name="radio-button-checked"] {
display: none;
}
&:focus + label m-icon {
outline: 0;
box-shadow: 0 0 0 2px var(--color-maize-400),0 0 0 3px var(--color-neutral-400);
border-radius: 2px;
}
}
m-icon {
color: var(--color-teal-500);
}
}
}
4 changes: 2 additions & 2 deletions css/index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import "defaults", "base", "utilities", "focus", "site-navigation",
"horizontal-navigation", "site-footer", "overview-cards", "loans",
"empty-state-container", "buttons", "pagination", "messages",
"text-notifications", "tags", "renew", "dropdown",
"maybe-design-system/controls";
"text-notifications", "tags", "banner", "renew", "dropdown",
"patron", "maybe-design-system/controls";
58 changes: 56 additions & 2 deletions css/maybe-design-system/_controls.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
input[type="text"] {
input[type="text"],
input[type="tel"] {
font-size: 1rem;
font-family: var(--font-base-family);
border: solid 1px rgba(0, 0, 0, 0.3);
box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1);
border-radius: 4px;
padding: var(--space-small);
margin: 0;
width: 100%;
&:invalid {
border-color: var(--color-pink-500);
}
&:not(:focus) {
box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1);
}
}

select {
Expand Down Expand Up @@ -49,3 +55,51 @@ option {
select::-ms-expand {
display: none;
}

.toggle-switch {
& + .toggle-switch-label {
.toggle {
background-color: var(--color-neutral-200);
border: 1px solid var(--color-neutral-200);
border-radius: var(--space-medium);
display: inline-block;
height: 1.5em;
margin-right: var(--space-x-small);
position: relative;
top: var(--space-x-small);
transition: none 0.2s ease-in-out;
transition-property: background-color, border-color;
width: 3em;
&:before {
content: '';
background-color: white;
border-radius: var(--space-medium);
display: block;
height: 100%;
left: 0;
position: absolute;
transition: left 0.2s ease-in-out;
width: 50%;
}
}
}
&:checked + .toggle-switch-label {
.toggle {
background-color: var(--color-teal-400);
border-color: var(--color-teal-400);
&:before {
left: 50%;
}
}
}
&:focus + .toggle-switch-label {
.toggle {
outline: 0;
box-shadow: 0 0 0 2px var(--color-maize-400),0 0 0 3px var(--color-neutral-400);
}
}
}

.input-description {
font-size: var(--space-medium);
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ services:

circulation-history-db:
image: niquerio/circulation_history_dev_db:latest

sass:
build: .
volumes:
Expand All @@ -64,3 +63,4 @@ services:

volumes:
gem_cache:
circ_history_db:
2 changes: 1 addition & 1 deletion lib/circ_history_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def download_csv
end

def set_retain_history(retain_history)
self.class.put("users/#{@uniqname}", query: {retain_history: retain_history})
self.class.put("/users/#{@uniqname}", query: {retain_history: retain_history})
end

end
41 changes: 27 additions & 14 deletions models/patron.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
require 'telephone_number'
class Patron
def initialize(uniqname:, parsed_response:)
@uniqname = uniqname
@parsed_response = parsed_response
def initialize(alma_data:, circ_history_data: nil)
@alma_data = alma_data
@circ_history_data = circ_history_data
end

def self.for(uniqname:, client: AlmaRestClient.client)
def self.for(uniqname:, alma_client: AlmaRestClient.client, circ_history_client: CircHistoryClient.new(uniqname))
url = "/users/#{uniqname}?user_id_type=all_unique&view=full&expand=none"
response = client.get(url)
if response.code == 200
Patron.new(uniqname: uniqname, parsed_response: response.parsed_response)
alma_response = alma_client.get(url)
circ_history_response = circ_history_client.user_info
if alma_response.code == 200 && circ_history_response.code == 200
Patron.new(alma_data: alma_response.parsed_response, circ_history_data: circ_history_response.parsed_response)
else
#should be something else
AlmaError.new(response)
AlmaError.new(alma_response)
end
end
def self.set_retain_history(uniqname:, retain_history:, circ_history_client: CircHistoryClient.new(uniqname))
circ_history_client.set_retain_history(retain_history)
end
def confirmed_history_setting?
@circ_history_data["confirmed"]
end
def retain_history?
@circ_history_data["retain_history"]
end
def get_record_count(history=CircHistoryClient.new(uniqname))
history.loans["total_record_count"]
end

def update_sms(sms, client=AlmaRestClient.client, phone=TelephoneNumber.parse(sms, :US))
return Error.new(message: "Phone number #{sms} is invalid") unless phone.valid? || sms.empty?
Expand All @@ -24,30 +37,30 @@ def update_sms(sms, client=AlmaRestClient.client, phone=TelephoneNumber.parse(sm
end

def uniqname
@parsed_response["primary_id"]&.downcase
@alma_data["primary_id"]&.downcase
end
def sms_number
@parsed_response.dig("contact_info","phone")&.find(-> {{}}){|x| x["preferred_sms"]}&.dig("phone_number")
@alma_data.dig("contact_info","phone")&.find(-> {{}}){|x| x["preferred_sms"]}&.dig("phone_number")
end
def sms_number?
!!sms_number
end
def full_name
@parsed_response["full_name"]
@alma_data["full_name"]
end
def user_group
@parsed_response.dig("user_group","desc")
@alma_data.dig("user_group","desc")
end
def can_book?
['Faculty','Graduate','Staff'].any?{|x| user_group.match(x)}
end
def addresses
@parsed_response.dig("contact_info","address")&.map{|x| Address.new(x)}
@alma_data.dig("contact_info","address")&.map{|x| Address.new(x)}
end

private
def patron_with_internal_sms(sms_number)
updated_patron = JSON.parse(@parsed_response.to_json)
updated_patron = JSON.parse(@alma_data.to_json)

phones = updated_patron["contact_info"].delete("phone")
my_phones = phones.map{ |x| Phone.new(x)}
Expand Down
11 changes: 5 additions & 6 deletions models/session_patron.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
class SessionPatron
extend Forwardable
def_delegators :@alma_patron, :uniqname, :full_name, :can_book?

def initialize(uniqname)
@alma_patron = Patron.for(uniqname: uniqname)
@patron = Patron.for(uniqname: uniqname)
end
def to_h
{
uniqname: @alma_patron.uniqname,
full_name: @alma_patron.full_name,
can_book: @alma_patron.can_book?
uniqname: @patron.uniqname,
full_name: @patron.full_name,
can_book: @patron.can_book?,
confirmed_history_setting: @patron.confirmed_history_setting?
}
end
end
34 changes: 22 additions & 12 deletions my_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@
# :nocov:

get '/' do
session[:uniqname] = '[email protected]' if !session[:uniqname]
session[:full_name] = 'Julian Tutor' if session[:uniqname] == '[email protected]'
session[:can_book] = true if session[:uniqname] == '[email protected]'

if !session[:uniqname]
patron = SessionPatron.new('[email protected]')
patron.to_h.each{|k,v| session[k] = v}
end
test_users = [
{
label: 'Graduate student (few)',
Expand Down Expand Up @@ -241,10 +241,20 @@
redirect 'https://apps.lib.umich.edu/my-account/favorites'
end

get '/settings' do
#session[:uniqname] = 'tutor' #need to get this from cosign?
patron = Patron.for(uniqname: session[:uniqname])
erb :patron, :locals => {patron: patron}
namespace '/settings' do
get '' do
patron = Patron.for(uniqname: session[:uniqname])
erb :patron, :locals => {patron: patron}
end
post '/history' do
response = Patron.set_retain_history(uniqname: session[:uniqname], retain_history: params[:retain_history])
if response.code == 200
flash[:success] = "<strong>Success:</strong> History Setting Successfully Changed"
else
flash[:error] = "<strong>Error:</strong> #{response.message}"
end
redirect "/settings"
end
end
#TODO set up renew loan to handle renew in place with top part message???
post '/renew-loan' do
Expand All @@ -262,12 +272,12 @@

post '/sms' do
patron = Patron.for(uniqname: session[:uniqname])
response = patron.update_sms(params["phone-number"])
response = patron.update_sms(params["text-notifications"] == "on" ? params["sms-number"] : "")
if response.code == 200
if params["phone-number"] == ''
flash[:success] = "<strong>Success:</strong> SMS Successfully Removed"
else
if params["text-notifications"] == "on"
flash[:success] = "<strong>Success:</strong> SMS Successfully Updated"
else
flash[:success] = "<strong>Success:</strong> SMS Successfully Removed"
end
else
flash[:error] = "<strong>Error:</strong> #{response.message}"
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/circ_history_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"uniqname": "tutor",
"retain_history": true,
"confirmed": true,
"updated_at": "2021-05-20T11:47:48.199-04:00"
}
34 changes: 33 additions & 1 deletion spec/models/patron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
context "found uniqname" do
before(:each) do
@alma_response = JSON.parse(File.read('./spec/fixtures/mrio_user_alma.json'))
@circ_history_response = JSON.parse(File.read('./spec/fixtures/circ_history_user.json'))
@patron_url = "users/mrio?user_id_type=all_unique&view=full&expand=none"
end
subject do
stub_alma_get_request(
url: @patron_url,
body: @alma_response.to_json
)
stub_circ_history_get_request(
url: 'users/mrio',
output: @circ_history_response.to_json
)
Patron.for(uniqname: 'mrio')
end
context "#update_sms(sms_number)" do
Expand Down Expand Up @@ -96,6 +101,24 @@
expect(subject.can_book?).to eq(false)
end
end
context "#retain_history?" do
it "returns correct true boolean" do
expect(subject.retain_history?).to eq(true)
end
it "returns correct false boolean" do
@circ_history_response["retain_history"] = false
expect(subject.retain_history?).to eq(false)
end
end
context "#confiremd_history_setting?" do
it "returns correct true boolean" do
expect(subject.confirmed_history_setting?).to eq(true)
end
it "returns correct false boolean" do
@circ_history_response["confirmed"] = false
expect(subject.confirmed_history_setting?).to eq(false)
end
end
context "#sms_number" do
it "returns sms number if preferred_sms is set" do
expect(subject.sms_number).to eq('734-123-4567')
Expand Down Expand Up @@ -146,6 +169,9 @@
url: @patron_url,
body: @alma_response
)
stub_circ_history_get_request(
url: 'users/mrioaaa',
)
end
subject do
Patron.for(uniqname: 'mrioaaa')
Expand All @@ -157,5 +183,11 @@
end
end
end

end
describe Patron, ".set_retain_history" do
it "updates the circ history setting" do
stub = stub_circ_history_put_request(url: 'users/tutor', query: {retain_history: false})
Patron.set_retain_history(uniqname: 'tutor', retain_history: 'false')
expect(stub).to have_been_requested
end
end
Loading

0 comments on commit 31251b2

Please sign in to comment.