Skip to content

Commit

Permalink
Feature/tf+ 157 notification event lending denied (#258)
Browse files Browse the repository at this point in the history
* created new model for lending denied notifications
Co-authored-by: kohlros <[email protected]>

* denying a lend request now creates an according notification
Co-authored-by: kohlros <[email protected]>

* added tests and fixed linter
Co-authored-by: kohlros <[email protected]>

* fixed test after merge
Co-authored-by: kohlros <[email protected]>
  • Loading branch information
JakobTimm authored Jan 13, 2023
1 parent ec88ff3 commit 3391dea
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 6 deletions.
19 changes: 18 additions & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
class ItemsController < ApplicationController
before_action :set_item, only: %i[ show edit update destroy request_return accept_return request_lend accept_lend]
before_action :set_item,
only: %i[ show edit update destroy request_return accept_return request_lend accept_lend deny_lend]

# GET /items or /items.json
def index
Expand Down Expand Up @@ -109,6 +110,22 @@ def accept_lend
redirect_to item_url(@item)
end

def deny_lend
@notification = LendRequestNotification.find_by(item: @item)
@item.set_status_available
@job = Job.create
@job.item = @item
@job.save
ReminderNotificationJob.set(wait: 4.days).perform_later(@job)
@notification.mark_as_inactive
@lendrequest = LendRequestNotification.find(@notification.actable_id)
@lendrequest.update(active: false)
@item.save
LendingDeniedNotification.create(item: @item, receiver: @notification.borrower, date: Time.zone.now,
active: false, unread: true)
redirect_to notifications_path
end

def start_lend
@item = Item.find(params[:id])
@job = Job.find_by(item: @item)
Expand Down
16 changes: 16 additions & 0 deletions app/models/lending_denied_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This class defines the specific lending denied notification, a notification
# that additionally belongs to a borrower and an item.
class LendingDeniedNotification < ApplicationRecord
acts_as :notification

belongs_to :item

def title
I18n.t "views.notifications.lending_denied.title"
end

def description
owner = item.owning_user
I18n.t "views.notifications.lending_denied.description", owner: owner.name, item: item.name
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<div class="modal-footer">
<% if notification.active == true %>
<%= button_to t("defaults.decline"), { action: "decline", id: notification.id }, method: :post, class: "btn btn-danger" %>
<%= button_to t("defaults.decline"), deny_lend_path(id: notification.item.id), class: "btn btn-danger" %>
<%= button_to t("defaults.accept"), accept_lend_path(id: notification.item.id), class: "btn btn-success" %>
<% end %>
</div>
Expand Down
39 changes: 39 additions & 0 deletions app/views/notifications/_lending_denied_notification.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- Button trigger modal -->
<button type="button" class="btn btn-notification" style= "background-color: <%= notification.unread == true ? "#cce4ec" : "#dddfe0c" %> !important" data-bs-toggle="modal" data-bs-target="#<%= "notificationModal#{notification.id}" %>">
<div class="notification">
<h2 class="subtitle">
<%= notification.title %>
</h2>
<p class="body-1">
<%= notification.description %>
</p>
<p class="timestamp">
<%= notification.parse_time %>
</p>
</div>
</button>

<!-- Modal -->
<div class="modal fade" id="<%= "notificationModal#{notification.id}" %>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"><%= notification.title %></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>
<%= notification.description %>
</p>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>

<script>
$('<%="notificationModal#{notification.id}" %>').on('shown.bs.modal', function (e) {
"<% notification.mark_as_read%>";
})
</script>
3 changes: 3 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ de:
lending_accepted:
title: "Ausleihanfrage angenommen"
description: "%{owner} hat deine Anfrage zu %{item} angenommen. Du kannst es jetzt abholen"
lending_denied:
title: "Ausleihanfrage abgelehnt"
description: "%{owner} hat deine Anfrage zu %{item} abgelehnt."
lend_request:
title: "Ausleihanfrage"
description: "%{receiver} möchte %{item} von dir ausleihen."
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ en:
lending_accepted:
title: "Lending Accepted"
description: "%{owner} has accepted your request to borrow %{item}. You can pick it up now"
lending_denied:
title: "Lending Denied"
description: "%{owner} has denied your request to borrow %{item}."
lend_request:
title: "Lend Request"
description: "%{receiver} wants to borrow your %{item}."
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
post 'deny_return/:id', to: 'items#deny_return', as: 'deny_return'
post 'request_lend/:id', to: 'items#request_lend', as: 'request_lend'
post 'accept_lend/:id', to: 'items#accept_lend', as: 'accept_lend'
post 'deny_lend/:id', to: 'items#deny_lend', as: 'deny_lend'
get 'generate_qrcode/:id', to: 'items#generate_qrcode', as: 'generate_qrcode'
post 'start_lend/:id', to: 'items#start_lend', as: 'start_lend'

Expand Down
16 changes: 12 additions & 4 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions spec/features/notifications/lend_request_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@
expect(page).to have_text(item.name)
expect(page).to have_text('Lending Accepted')
end

it "borrower gets notified when the request is denied" do
visit notifications_path
click_on('Lend Request')
click_button('Decline')
sign_in borrower
visit notifications_path
expect(page).to have_text(user.name)
expect(page).to have_text(item.name)
expect(page).to have_text('Lending Denied')
end
end

0 comments on commit 3391dea

Please sign in to comment.