Skip to content

Commit

Permalink
Feature/pbrh+ 45 confirm returned item (#203)
Browse files Browse the repository at this point in the history
* write tests and hate ruby on rails

* fix factory bots

* fucking errors

* fix last tests and add more locale translations

* rubocop

* fix crash when another user is on waitlist

* fix waitlist notifications not being shown correctly

Co-authored-by: Julian <julian@DESKTOP-I3NQ0TP>
  • Loading branch information
2 people authored and antonneubauer committed Jan 6, 2023
1 parent 64ee699 commit d42253f
Show file tree
Hide file tree
Showing 38 changed files with 647 additions and 86 deletions.
35 changes: 19 additions & 16 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def request_lend
@item = Item.find(params[:id])
@user = current_user
@owner = User.find(@item.owner)
@notification = LendRequestNotification.new(item: @item, borrower: @user, user: @owner, date: Time.zone.now,
@notification = LendRequestNotification.new(item: @item, borrower: @user, receiver: @owner, date: Time.zone.now,
unread: true, active: true)
@notification.save
@item.set_status_pending_lend_request
Expand All @@ -106,34 +106,37 @@ def request_return
@item.save
@user = current_user
unless ReturnRequestNotification.find_by(item: @item)
@notification = ReturnRequestNotification.new(user: User.find(@item.owner), date: Time.zone.now, item: @item,
borrower: @user, active: true, unread: true)
@notification = ReturnRequestNotification.new(receiver: User.find(@item.owner), date: Time.zone.now,
item: @item, borrower: @user, active: true, unread: true)
@notification.save
end
redirect_to item_url(@item)
end

def accept_return
@item = Item.find(params[:id])
@notification = ReturnRequestNotification.find_by(item: @item)
@notification.destroy
# TODO: Send return accepted notification to borrower
@item.rental_start = nil
@item.rental_duration_sec = nil
@item.holder = nil
@item.set_status_available
@user = current_user
@request_notification = ReturnRequestNotification.find_by(item: @item)
@request_notification.destroy
@accepted_notif = ReturnAcceptedNotification.new(active: true, unread: true, date: Time.zone.now,
item: @item, receiver: User.find(@item.holder), owner: @user)
@accepted_notif.save
@item.reset_status
@item.save
redirect_to item_url(@item)
end

def deny_return
@item = Item.find(params[:id])
@notification = ReturnRequestNotification.find_by(item: @item)
@notification.destroy
# TODO: Send return declined notification to borrower and handle decline return
@item.deny_return
@item.save
redirect_to item_url(@item)
@user = current_user
@request_notification = ReturnRequestNotification.find_by(item: @item)
@request_notification.destroy
@declined_notification = ReturnDeclinedNotification.new(item_name: @item.name, owner: @user,
receiver: User.find(@item.holder),
date: Time.zone.now, active: true, unread: true)
@declined_notification.save
@item.destroy
redirect_to notifications_path
end

def generate_qrcode
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def index
end

def fetch_active_notification
@active_notifications = Notification.where(active: true, user_id: current_user.id)
@active_notifications = Notification.where(active: true, receiver_id: current_user.id)
@active_dates = @active_notifications.group_by do |notification|
notification.date.strftime('%y%m%d')
end
@active_dates = @active_dates.sort_by { |date, _| date }.reverse
end

def fetch_inactive_notifications
@inactive_notifications = Notification.where(active: false, user_id: current_user.id)
@inactive_notifications = Notification.where(active: false, receiver_id: current_user.id)
@inactive_dates = @inactive_notifications.group_by do |notification|
notification.date.strftime('%y%m%d')
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/added_to_waitlist_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def title
end

def description
I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(user) + 1,
I18n.t("views.notifications.added_to_waitlist.description", position: item.waitlist.position(receiver) + 1,
item: item.name)
end
end
15 changes: 11 additions & 4 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class Item < ApplicationRecord
has_one_attached :image
has_one :waitlist, dependent: :destroy
has_many :lend_request_notifications, dependent: :destroy
has_many :return_request_notifications, dependent: :destroy
has_many :return_accepted_notifications, dependent: :destroy
has_many :move_up_on_waitlist_notification, dependent: :destroy
has_many :added_to_waitlist_notification, dependent: :destroy
has_and_belongs_to_many :users, join_table: "wishlist"

validates :name, presence: true
Expand Down Expand Up @@ -48,14 +52,17 @@ def set_status_unavailable
self.lend_status = :unavailable
end

def deny_return
self.lend_status = :unavailable
end

def price_in_euro=(euros)
self.price_ct = euros * 100
end

def reset_status
self.rental_start = nil
self.rental_duration_sec = nil
self.holder = nil
set_status_available
end

def add_to_waitlist(user)
waitlist.add_user(user)
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/lend_request_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def description
user_name = borrower.name
item_name = item.name
if active
I18n.t "views.notifications.lend_request.description", user: user_name, item: item_name
I18n.t "views.notifications.lend_request.description", receiver: user_name, item: item_name
elsif accepted
I18n.t "views.notifications.lend_request.description_accepted", user: user_name, item: item_name
I18n.t "views.notifications.lend_request.description_accepted", receiver: user_name, item: item_name
else
I18n.t "views.notifications.lend_request.description_declined", user: user_name, item: item_name
I18n.t "views.notifications.lend_request.description_declined", receiver: user_name, item: item_name
end
end
end
2 changes: 1 addition & 1 deletion app/models/move_up_on_waitlist_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def title
end

def description
I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(user) + 1,
I18n.t("views.notifications.move_up_on_waitlist.description", position: item.waitlist.position(receiver) + 1,
item: item.name)
end
end
2 changes: 1 addition & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Notification < ApplicationRecord
actable

belongs_to :user
belongs_to :receiver, class_name: "User"

def custom_partial
specific.class.name.underscore
Expand Down
18 changes: 18 additions & 0 deletions app/models/return_accepted_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# class of a basic return accepted notification.
class ReturnAcceptedNotification < ApplicationRecord
acts_as :notification

belongs_to :owner, class_name: "User"
belongs_to :item

validates :receiver, presence: true
validates :date, presence: true

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

def description
I18n.t "views.notifications.return_accepted.description", owner: owner.name, item: item.name
end
end
18 changes: 18 additions & 0 deletions app/models/return_declined_notification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# class of a basic return declined notification.
class ReturnDeclinedNotification < ApplicationRecord
acts_as :notification

belongs_to :owner, class_name: "User"

validates :receiver, presence: true
validates :date, presence: true
validates :item_name, presence: true

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

def description
I18n.t "views.notifications.return_declined.description", owner: owner.name, item: item_name
end
end
4 changes: 2 additions & 2 deletions app/models/return_request_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class ReturnRequestNotification < ApplicationRecord
belongs_to :borrower, class_name: "User"
belongs_to :item

validates :user, presence: true
validates :receiver, presence: true
validates :date, presence: true

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

def description
I18n.t "views.notifications.return_request.description", user: borrower.name, item: item.name
I18n.t "views.notifications.return_request.description", receiver: borrower.name, item: item.name
end
end
8 changes: 4 additions & 4 deletions app/models/waitlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ def delete_waitlist_notifications(user)
end

def add_added_to_waitlist_notification(user)
@notification = AddedToWaitlistNotification.new(user: user, date: Time.zone.now, item: item, active: false,
@notification = AddedToWaitlistNotification.new(receiver: user, date: Time.zone.now, item: item, active: false,
unread: true)
@notification.save
end

def add_move_up_on_waitlist_notification(user)
@notification = MoveUpOnWaitlistNotification.new(user: user, date: Time.zone.now, item: item, active: false,
@notification = MoveUpOnWaitlistNotification.new(receiver: user, date: Time.zone.now, item: item, active: false,
unread: true)
@notification.save
end

def delete_added_to_waitlist_notification(user)
@notification = AddedToWaitlistNotification.find_by(item: item, user: user)
@notification = AddedToWaitlistNotification.find_by(item: item, receiver: user)
return if @notification.nil?

@notification.destroy
end

def delete_moved_up_on_waitlist_notification(user)
@notification = MoveUpOnWaitlistNotification.find_by(item: item, user: user)
@notification = MoveUpOnWaitlistNotification.find_by(item: item, receiver: user)
return if @notification.nil?

@notification.destroy
Expand Down
2 changes: 1 addition & 1 deletion app/views/items/_adaptive_lend_button.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% elsif item.lend_status == "available" %>
<%= button_to t('views.show_item.lend'), { action: "request_lend" }, class: "btn-primary" %>
<% elsif !item.waitlist.users.exists?(current_user.id) %>
<% if item.lend_status == "pending_lend_request" && !LendRequestNotification.find_by(user: item.owner, item: item, borrower: current_user).nil? %>
<% if item.lend_status == "pending_lend_request" && !LendRequestNotification.find_by(receiver: item.owner, item: item, borrower: current_user).nil? %>
<%= button_to t('views.show_item.pending_lend_request'), { }, class: "btn-secondary", disabled: true %>
<% else %>
<%= button_to t('views.show_item.enter_waitlist'), { action: "add_to_waitlist" }, class: "btn-primary" %>
Expand Down
72 changes: 72 additions & 0 deletions app/views/notifications/_added_to_waitlist_notification.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<style>
.button {
background-color: grey;
color: black;
margin-top: 1em;
font-family: Roboto, sans-serif;
float: bottom;
}
.button:hover {
background-color: #B1063A;
color: #efcdd8;
border-color: #efcdd8;
}
.subtitle {
font-family: Roboto, sans-serif;
font-weight: bold;
font-size: 20px;
spacing: auto spacing;
color: #b8043c;
margin-bottom: 7px;
margin-top: 0.5em;
}
.description {
font-family: Roboto, sans-serif;
}
.btn-primary{
background-color: #dddfe0 !important;
border: #dddfe0 !important;
color: black !important;
margin: 5px;
}
</style>

<button type="button" style= "background-color: <%= notification.unread == true ? "#cce4ec" : "#dddfe0c" %> !important"
class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#<%="notificationModal#{notification.id}"%>">
<div class="notification">
<h2 class="subtitle">
<%= notification.title %>
</h2>
<p class="description">
<%= notification.description %>
</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">
<%if notification.active == true %>
<%end%>
</div>
</div>
</div>
</div>

<script>
$('<%="notificationModal#{notification.id}" %>').on('shown.bs.modal', function (e) {
"<% notification.mark_as_read%>";
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<%= notification.description %>
<% if notification.item.image.attached? %>
<div class="col-md-4">
<%= image_tag notification.item.image, class: "img-responsive rounded mx-auto d-block", style: "max-width: 13em" %>
<%= image_tag notification.item.image, class: "img-responsive rounded mx-auto d-block", style: "max-width: 13em" if notification.item.image.attached? %>
</div>
<% end %>
</p>
Expand Down
Loading

0 comments on commit d42253f

Please sign in to comment.