• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tulibraries / tul_cob / 26c6ba82-1697-40d7-b09e-22a50f06ea69

26 Jun 2024 03:14PM UTC coverage: 85.848% (+0.1%) from 85.739%
26c6ba82-1697-40d7-b09e-22a50f06ea69

push

circleci

web-flow
Merge pull request #4466 from tulibraries/request-time-estimate

BL-1865 Add time estimates to request confirmation message

31 of 38 new or added lines in 2 files covered. (81.58%)

3 existing lines in 1 file now uncovered.

3033 of 3533 relevant lines covered (85.85%)

63.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.8
/app/controllers/almaws_controller.rb
1
# frozen_string_literal: true
2

3
class AlmawsController < CatalogController
1✔
4
  layout proc { |controller| false if request.xhr? }
7✔
5

6
  before_action :authenticate_user!, except: [:item]
1✔
7
  before_action :xhr!, only: [:item, :request_options]
1✔
8

9
  rescue_from Alma::BibItemSet::ResponseError,
1✔
10
    with: :offset_too_large
11

12
  def item
1✔
13
    @mms_id = params[:mms_id]
6✔
14
    _, @document = begin search_service.fetch(params[:doc_id]) rescue [ nil, SolrDocument.new({}) ] end
6✔
15

16
    @items = get_bib_items(@mms_id)
6✔
17
    availability = @items.group_by { |item| item["item_data"]["pid"] }
9✔
18
        .transform_values { |item| { availability: helpers.availability_status(item.first) } }
4✔
19
    @document.merge_item_data!(availability)
5✔
20
    @document_availability = helpers.document_availability_info(@document)
5✔
21

22
    @request_data = RequestData.new(@items)
5✔
23
    @pickup_locations = @request_data.pickup_locations.join(",")
5✔
24
    @request_level = @request_data.get_request_level
5✔
25
    @redirect_to = params[:redirect_to]
5✔
26
    render layout: false
5✔
27
  end
28

29
  def request_options
1✔
30
    @mms_id = params[:mms_id]
5✔
31
    @user_id = current_user.uid
5✔
32

33
    _, @document = begin search_service.fetch(@mms_id) rescue [ nil, SolrDocument.new({}) ] end
5✔
34
    @items = get_bib_items(@mms_id)
5✔
35
    @request_data = RequestData.new(@items, params)
5✔
36

37
    # Information about document and bib items for request modal
38
    @books = @document.fetch("format") if @document["format"]&.include?("Book")
5✔
39
    @author = @document.fetch("creator_display", []).first || ""
5✔
40
    @description = @request_data.material_types_and_descriptions
5✔
41
    @asrs_description = @request_data.asrs_material_types_and_descriptions
5✔
42
    @material_types = @request_data.material_types
5✔
43

44
    # Pickup locations
45
    @pickup_locations = @request_data.pickup_locations&.collect { |lib| { lib => helpers.library_name_from_short_code(lib) } }
9✔
46
    @asrs_pickup_locations = @request_data.asrs_pickup_locations&.collect { |lib| { lib => helpers.library_name_from_short_code(lib) } }
30✔
47
    @item_level_locations = @request_data.item_level_locations
5✔
48
    @equipment = @request_data.equipment_locations
5✔
49
    @booking_location = @request_data.booking_locations
5✔
50

51
    # Request levels and options
52
    @request_level = @request_data.request_level
5✔
53
    @asrs_request_level = @request_data.asrs_request_level
5✔
54

55
    if @request_level == "item" || @asrs_request_level == "item"
5✔
56
      @item_level_holdings = CobAlma::Requests.item_holding_ids(@items)
×
57
      @request_options = get_item_request_options(@mms_id, @user_id, @item_level_holdings)
×
58

59
      if @request_options&.request_options.nil?
×
60
        @second_attempt_holdings = CobAlma::Requests.second_attempt_item_holding_ids(@items)
×
61
        @request_options = get_item_request_options(@mms_id, @user_id, @second_attempt_holdings)
×
62
      end
63
    else
64
      @request_options = get_bib_request_options(@mms_id, @user_id)
5✔
65
    end
66

67
    # Define when we want modal exit button to be a link.
68
    @make_modal_link = params[:pickup_locations].blank? &&
3✔
69
      params[:request_level].blank?
70
  end
71

72
  def send_hold_request
1✔
73
    bib_options = {
74
    mms_id: params[:mms_id],
6✔
75
    user_id: current_user.uid,
76
    description: params[:hold_description],
77
    pickup_location_library: params[:hold_pickup_location],
78
    pickup_location_type: "LIBRARY",
79
    material_type: { value: params[:material_type] },
80
    request_type: "HOLD",
81
    comment: params[:hold_comment]
82
    }
83
    @request_level = params[:request_level]
6✔
84
    log = { type: "submit_hold_request", user: current_user.id }.merge(bib_options)
6✔
85

86
    begin
87
      response = do_with_json_logger(log) { Alma::BibRequest.submit(bib_options) }
12✔
88
      confirmation = RequestConfirmation.new(response, params[:hold_pickup_location])
6✔
NEW
89
      flash["notice"] = confirmation.message
×
UNCOV
90
      redirect_back(fallback_location: root_path)
×
91
    rescue => e
92
      Honeybadger.notify(e.message + " " + log.to_s)
6✔
93
      flash["notice"] = "There was an error processing your request. Contact Temple University Libraries for help."
6✔
94
      redirect_back(fallback_location: root_path)
6✔
95
    end
96
  end
97

98
  def send_asrs_request
1✔
99
    options = {
100
    mms_id: params[:mms_id],
×
101
    user_id: current_user.uid,
102
    description: params[:asrs_description],
103
    pickup_location_library: params[:asrs_pickup_location],
104
    pickup_location_type: "LIBRARY",
105
    material_type: { value: params[:material_type] },
106
    request_type: "HOLD",
107
    comment: params[:asrs_comment]
108
    }
109

110
    @asrs_request_level = params[:asrs_request_level]
×
111
    log = { type: "submit_asrs_request", user: current_user.id }.merge(options)
×
112

113
    begin
114
      requests_made = 0
×
115
      if @asrs_request_level == "bib"
×
NEW
116
        response = do_with_json_logger(log) { Alma::BibRequest.submit(options) }
×
117
        requests_made += 1
×
118
      else
119
        params["available_asrs_items"]
×
120
          .select { |item| item["description"] == options[:description] }
×
121
          .each do |item|
122

123
          holding_id = item["holding_id"]
×
124
          item_pid = item["item_pid"]
×
125

126
          item_options = { holding_id:, item_pid: }
×
127

NEW
128
          response = do_with_json_logger(log.merge(item_options)) {
×
129
            Alma::ItemRequest.submit(options.merge(item_options))
×
130
          }
131

132
          requests_made += 1
×
133
          break
×
134
        end
135
      end
136

137
      if requests_made > 0
×
NEW
138
        confirmation = RequestConfirmation.new(response, params[:asrs_pickup_location])
×
NEW
139
        flash["notice"] = confirmation.message
×
140
      else
141
        flash["notice"] = "There was an error processing your request. Contact Temple University Libraries for help."
×
142
      end
143

144
      redirect_back(fallback_location: root_path)
×
145

146
    rescue => e
147
      Honeybadger.notify(e.message + " " + log.to_s)
×
148
      flash["notice"] = "There was an error processing your request. Contact Temple University Libraries for help."
×
149
      redirect_back(fallback_location: root_path)
×
150
    end
151
  end
152

153
  def send_booking_request
1✔
154
    start_date = date_or_nil(params[:booking_start_date])
4✔
155
    end_date = date_or_nil(params[:booking_end_date])
4✔
156
    bib_options = {
157
    mms_id: params[:mms_id],
4✔
158
    user_id: current_user.uid,
159
    pickup_location_library: params[:booking_pickup_location],
160
    pickup_location_type: "LIBRARY",
161
    material_type: { value: params[:material_type] },
162
    request_type: "BOOKING",
163
    booking_start_date: start_date,
164
    booking_end_date: end_date,
165
    description: params[:booking_description],
166
    comment: params[:booking_comment]
167
    }
168

169
    log = { type: "submit_booking_request", user: current_user.id }.merge(bib_options)
4✔
170
    begin
171
      response = do_with_json_logger(log) { Alma::BibRequest.submit(bib_options) }
8✔
172
      confirmation = RequestConfirmation.new(response)
4✔
NEW
173
      flash["notice"] = confirmation.message
×
UNCOV
174
      redirect_back(fallback_location: root_path)
×
175
    rescue Alma::BibRequest::ItemAlreadyExists
176
      flash["notice"] = "This item is already booked for those dates."
×
177
      redirect_back(fallback_location: root_path)
×
178
    rescue
179
      flash["notice"] = "There was an error processing your request. Contact Temple University Libraries for help."
4✔
180
      redirect_back(fallback_location: root_path)
4✔
181
    end
182
  end
183

184
  def send_digitization_request
1✔
185
    bib_options = {
186
      mms_id: params[:mms_id],
2✔
187
      user_id: current_user.uid,
188
      description: params[:digitization_description],
189
      chapter_or_article_title: params[:chapter_or_article_title],
190
      chapter_or_article_author: params[:chapter_or_article_author],
191
      request_type: "DIGITIZATION",
192
      target_destination: { value: "DIGI_DEPT_INST" },
193
      partial_digitization: true,
194
      comment: [params[:digitization_comment], "Title: #{params[:chapter_or_article_title]}", "Author: #{params[:chapter_or_article_author]}", "From page: #{params[:from_page]}", "To page: #{params[:to_page]}"].compact.join("\n"),
195
      required_pages_range: [{
196
        from_page: params[:from_page], to_page: params[:to_page]
197
      }]
198
    }
199
    @request_level = params[:request_level]
2✔
200

201
    log = { type: "submit_digitization_request", user: current_user.id }.merge(bib_options)
2✔
202
    begin
203
      response = do_with_json_logger(log) { Alma::BibRequest.submit(bib_options) }
4✔
204
      confirmation = RequestConfirmation.new(response)
2✔
NEW
205
      flash["notice"] = confirmation.message
×
UNCOV
206
      redirect_back(fallback_location: root_path)
×
207
    rescue
208
      flash["notice"] = "There was an error processing your request. Contact Temple University Libraries for help."
2✔
209
      redirect_back(fallback_location: root_path)
2✔
210
    end
211
  end
212

213
  def offset_too_large
1✔
214
    render html: "<p class='m-2'>Availability information can not be loaded. Contact a librarian for help.</p>".html_safe, status: :bad_gateway
1✔
215
  end
216

217
  private
1✔
218

219
    def get_bib_items(mms_id)
1✔
220
      Rails.cache.fetch("#{mms_id}/bib_items", expires_in: 30.seconds) do
11✔
221
        log = { type: "bib_items_availability" }
11✔
222
        response = do_with_json_logger(log) { Alma::BibItem.find(mms_id, limit: 100, offset: 0, expand: "due_date").all }.to_a.reject(&:missing_or_lost?)
22✔
223
      end
224
    end
225

226
    def get_bib_request_options(mms_id, user_id)
1✔
227
      log = { type: "bib_request_options", user: user_id }
5✔
228
      response = do_with_json_logger(log) { Alma::RequestOptions.get(mms_id, user_id:) }
10✔
229
    end
230

231
    def get_item_request_options(mms_id, user_id, holdings)
1✔
232
      holdings.map { |holding_id, item_pid|
×
233
        log = { type: "item_request_options", mms_id:, holding_id:, item_pid:, user: user_id }
×
234
        do_with_json_logger(log) { Alma::ItemRequestOptions.get(mms_id, holding_id, item_pid, user_id:) }
×
235
      }
236
        .reduce do |acc, request|
237
          options = acc.request_options || []
×
238
          next_options = request.request_options || []
×
239
          acc.request_options = options + next_options
×
240
          acc
×
241
        end
242
    end
243

244
    def date_or_nil(param)
1✔
245
      begin
246
        date = Date.strptime(param, "%Y-%m-%d")
12✔
247
      rescue
248
        date = nil
7✔
249
      end
250
      date
12✔
251
    end
252
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc