Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1310 from YaleSTC/1203_request_expired_email
Browse files Browse the repository at this point in the history
[1203] Add request expired notifications
  • Loading branch information
orenyk committed Oct 11, 2015
2 parents ed229c3 + 8c9207c commit d91e4e9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def reservation_status_update(reservation, force = '') # rubocop:disable all
if @status == 'reserved'
# we only send emails for reserved reservations if it was a request
@status = 'request approved'
elsif @status == 'denied' &&
@reservation.flagged?(:expired)
@status = 'request expired'
end

status_formatted = @status.sub('_', ' ').split.map(&:capitalize) * ' '
Expand Down
5 changes: 4 additions & 1 deletion app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Reservation < ActiveRecord::Base
# query by where('flags & ? > 0', FLAGS[:flag])
# or where('flags & ? = 0', FLAGS[:flag]) for not flagged
FLAGS = { request: (1 << 1), broken: (1 << 2), lost: (1 << 3),
fined: (1 << 4), missed_email_sent: (1 << 5) }
fined: (1 << 4), missed_email_sent: (1 << 5),
expired: (1 << 6) }

## Class methods ##

Expand Down Expand Up @@ -95,6 +96,8 @@ def approved?
end

def flagged?(flag)
# checks to see if the given flag is set
# you must pass the symbol for the flag
flags & FLAGS[flag] > 0
end

Expand Down
3 changes: 3 additions & 0 deletions app/views/user_mailer/_request_expired.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>Your <%= link_to 'reservation request', reservation_url(@reservation, only_path: false) %> has expired.</p>

<p> You can request the equipment again <%= link_to 'here', equipment_model_url(@reservation.equipment_model, only_path: false) %>.</p>
6 changes: 5 additions & 1 deletion lib/tasks/deny_missed_requests.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ desc 'Mark missed requests as denied'
task deny_missed_requests: :environment do
# get all requests that began yesterday and weren't approved / denied /
# acted upon
# also sends an email to the user
missed_requests = Reservation.missed_requests
Rails.logger.info "Found #{missed_requests.size} missed requests."

missed_requests.each do |request|
Rails.logger.info "Marking as denied:\n #{request.inspect}"
request.update_attributes(status: 'denied')
request.status = 'denied'
request.flag(:expired)
request.save!
UserMailer.reservation_status_update(request).deliver
end

Rails.logger.info 'Finished processing missed requests.'
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/tasks/flag_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@

before(:each) { @res = FactoryGirl.create(:valid_reservation) }

it 'flags missed requests as missed' do
it 'flags missed requests as denied and expired' do
@res.update_attributes(FactoryGirl.attributes_for(:request))
@res.update_attributes(start_date: Time.zone.yesterday,
due_date: Time.zone.today)
expect { subject.invoke }.to(
change { Reservation.find(@res.id).status }.from(
'requested').to('denied'))
expect(Reservation.find(@res.id).flagged?(:expired)).to be_truthy
end

it "doesn't flag missed non-requests" do
Expand All @@ -84,4 +85,13 @@
@res.update_attributes(FactoryGirl.attributes_for(:request))
expect { subject.invoke }.not_to change { Reservation.find(@res.id).status }
end

it 'sends appropriate emails' do
@res.update_attributes(FactoryGirl.attributes_for(:request))
@res.update_attributes(start_date: Time.zone.yesterday,
due_date: Time.zone.today)
@no_email = FactoryGirl.create(:missed_reservation)
expect { subject.invoke }.to(
change { ActionMailer::Base.deliveries.count }.by(1))
end
end
9 changes: 9 additions & 0 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
"[Reservations] #{@res.equipment_model.name} Request Approved")
end

it 'sends expired request notifications' do
@res.update_attributes(status: 'denied',
flags: (Reservation::FLAGS[:request] |
Reservation::FLAGS[:expired]))
@mail = UserMailer.reservation_status_update(@res).deliver
expect(@mail.subject).to eq(
"[Reservations] #{@res.equipment_model.name} Request Expired")
end

it 'sends reminders to check-out' do
@res.update_attributes(
FactoryGirl.attributes_for(:upcoming_reservation))
Expand Down

0 comments on commit d91e4e9

Please sign in to comment.