-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.rb
More file actions
121 lines (95 loc) · 3.19 KB
/
user.rb
File metadata and controls
121 lines (95 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'json'
module Alma
class User
include ActiveModel::Model
# Won't be needed.. temp for working out model specs that depend on these values....
# Any two-digit year over this, and Ruby's Date.parse() will wrap back to 1969.
MILLENNIUM_MAX_DATE = Date.new(2068, 12, 31)
# Any two-digit year below this, and Ruby's Date.parse() will wrap ahead to 2068.
MILLENNIUM_MIN_DATE = Date.new(1969, 1, 1)
attr_accessor :user_obj
attr_accessor :id
attr_accessor :email
attr_accessor :blocks
attr_accessor :name
attr_accessor :type
attr_accessor :expiration_date
class << self
def find(id)
raw_user = AlmaServices::Patron.get_user(id)
# If we get no user back return nothing
return unless raw_user
# If Alma sends back and error return nothing
return if raw_user.body['errorsExist']
Alma::User.new raw_user
end
def find_if_exists(id)
return unless id
find(id)
end
def find_if_active(id)
find_if_exists(id).tap do |rec|
return nil unless rec && rec.active?
end
rescue Error::AlmaRecordNotFoundError => e
return if %w[400 404].include? e.message
raise
end
end
# rubocop:disable Metrics/AbcSize
def initialize(raw_user = nil)
return unless raw_user
@user_obj = JSON.parse raw_user.body
@id = @user_obj['primary_id']
@name = @user_obj['full_name']
@email = @user_obj['contact_info']['email'][0]['email_address']
# Alma codes have a history of being mixed case so upcase user_group:
@type = @user_obj['user_group']['value'].upcase if @user_obj['user_group']['value']
@expiration_date = Date.strptime(@user_obj['expiry_date'], '%Y-%m-%d') if @user_obj['expiry_date']
end
# rubocop:enable Metrics/AbcSize
def to_json(*_args)
user_obj.to_json
end
def save
AlmaServices::Patron.save(@id, self)
end
def active?
!expired?
end
def expired?
return true unless expiration_date
expiration_date < Date.current
end
def fees
AlmaServices::Fees.fetch_all(@id) || nil
end
# TODO: Clean up use of notes_array vs. find_note
def notes_array
user_obj['user_note'].pluck('note_text')
end
# TODO: Clean up use of notes_array vs. find_note
def find_note(note)
@user_obj['user_note'].find { |p| p['note_text'].include? note }
end
def delete_note(note)
@user_obj['user_note'].reject! { |p| p['note_text'].include? note }
end
# Types: Library, Address, Barcode, Circulation, ERP, General, Other, Registrar
# rubocop:disable Metrics/MethodLength
def add_note(text)
Rails.logger.debug("Setting note #{text} for patron #{id}")
new_note = {
'note_type' => { 'value' => 'LIBRARY', 'desc' => 'Library' },
'note_text' => text,
'user_viewable' => false,
'popup_note' => false,
'created_by' => 'Framework',
'created_date' => Time.now.utc.strftime('%Y-%m-%dT%H:%M:00Z'),
'segment_type' => 'Internal'
}
user_obj['user_note'].push(new_note)
end
# rubocop:enable Metrics/MethodLength
end
end