diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index fffb39a9..8daafcbf 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -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 @@ -106,8 +106,8 @@ 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) @@ -115,25 +115,28 @@ def request_return 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 diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index ef7381a2..953b02fd 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -6,7 +6,7 @@ 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 @@ -14,7 +14,7 @@ def fetch_active_notification 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 diff --git a/app/models/added_to_waitlist_notification.rb b/app/models/added_to_waitlist_notification.rb index bb8cc4b4..6304d0cd 100644 --- a/app/models/added_to_waitlist_notification.rb +++ b/app/models/added_to_waitlist_notification.rb @@ -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 diff --git a/app/models/item.rb b/app/models/item.rb index e1b9f174..4a045789 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 @@ -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 diff --git a/app/models/lend_request_notification.rb b/app/models/lend_request_notification.rb index fb536367..b1cff4eb 100644 --- a/app/models/lend_request_notification.rb +++ b/app/models/lend_request_notification.rb @@ -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 diff --git a/app/models/move_up_on_waitlist_notification.rb b/app/models/move_up_on_waitlist_notification.rb index a10fe41d..a16c9923 100644 --- a/app/models/move_up_on_waitlist_notification.rb +++ b/app/models/move_up_on_waitlist_notification.rb @@ -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 diff --git a/app/models/notification.rb b/app/models/notification.rb index a77e1368..8970e63e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -5,7 +5,7 @@ class Notification < ApplicationRecord actable - belongs_to :user + belongs_to :receiver, class_name: "User" def custom_partial specific.class.name.underscore diff --git a/app/models/return_accepted_notification.rb b/app/models/return_accepted_notification.rb new file mode 100644 index 00000000..ff7d71e6 --- /dev/null +++ b/app/models/return_accepted_notification.rb @@ -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 diff --git a/app/models/return_declined_notification.rb b/app/models/return_declined_notification.rb new file mode 100644 index 00000000..c5fbb297 --- /dev/null +++ b/app/models/return_declined_notification.rb @@ -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 diff --git a/app/models/return_request_notification.rb b/app/models/return_request_notification.rb index 637f73ee..f58c54cf 100644 --- a/app/models/return_request_notification.rb +++ b/app/models/return_request_notification.rb @@ -5,7 +5,7 @@ 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 @@ -13,6 +13,6 @@ def 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 diff --git a/app/models/waitlist.rb b/app/models/waitlist.rb index 9cc1b35f..576dd3e2 100644 --- a/app/models/waitlist.rb +++ b/app/models/waitlist.rb @@ -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 diff --git a/app/views/items/_adaptive_lend_button.html.erb b/app/views/items/_adaptive_lend_button.html.erb index 89f1415e..1eafd57f 100644 --- a/app/views/items/_adaptive_lend_button.html.erb +++ b/app/views/items/_adaptive_lend_button.html.erb @@ -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" %> diff --git a/app/views/notifications/_added_to_waitlist_notification.erb b/app/views/notifications/_added_to_waitlist_notification.erb new file mode 100644 index 00000000..cbf1ba37 --- /dev/null +++ b/app/views/notifications/_added_to_waitlist_notification.erb @@ -0,0 +1,72 @@ + + + + + +