Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Model for statistics #205

Merged
merged 20 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def request_lend
redirect_to item_url(@item)
end

# rubocop:disable Metrics/AbcSize (reduce complexity in future)
# (reduce complexity in future)
def accept_lend
@notification = LendRequestNotification.find_by(item: @item)
@item.set_status_pending_pickup
Expand All @@ -116,11 +116,33 @@ def accept_lend

helpers.audit_accept_lend(@item)

LendingAcceptedNotification.create(item: @item, receiver: @notification.borrower, date: Time.zone.now,
active: false, unread: true)
redirect_to item_url(@item)
end

def start_lend
@item = Item.find(params[:id])
@job = Job.find_by(item: @item)
@job.destroy
@holder = current_user.id
@item.set_status_lent
@item.set_rental_start_time
@item.holder = @holder
@item.save
redirect_to item_url(@item)
end

def abort_lend
@item = Item.find(params[:id])
@job = Job.find_by(item: @item)
@job.destroy
@item.set_status_available
@item.holder = nil
@item.save
redirect_to item_url(@item)
end
# rubocop:enable Metrics/AbcSize

# rubocop:disable Metrics/AbcSize (reduce complexity in future)
def request_return
@item = Item.find(params[:id])
@item.set_status_pending_return
Expand All @@ -129,13 +151,12 @@ def request_return
helpers.audit_request_return(@item)

unless ReturnRequestNotification.find_by(item: @item)
@notification = ReturnRequestNotification.new(user: User.find(@item.owner), date: Time.zone.now, item: @item,
borrower: current_user, active: true, unread: true)
@notification = ReturnRequestNotification.new(receiver: User.find(@item.owner), date: Time.zone.now,
item: @item, borrower: current_user, active: true, unread: true)
@notification.save
end
redirect_to item_url(@item)
end
# rubocop:enable Metrics/AbcSize

def accept_return
@item = Item.find(params[:id])
Expand All @@ -155,15 +176,28 @@ def accept_return

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
@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: false, unread: true)
@declined_notification.save
@item.destroy

helpers.audit_deny_return(@item)
# Comment out because an item gets deleted, when declining a return
# helpers.audit_deny_return(@item)

redirect_to item_url(@item)
redirect_to notifications_path
end

def generate_qrcode
qr = RQRCode::QRCode.new("item:#{params[:id]}")
png = qr.as_png(size: 500)
dummy_png_file = StringIO.new png.to_blob
pdf = Prawn::Document.new(page_size: "A4")
pdf.image dummy_png_file, position: :center
send_data pdf.render, disposition: "attachment", type: "application/pdf"
end

private
Expand Down
1 change: 1 addition & 0 deletions app/helpers/audit_event_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def audit_accept_return(item)
create_audit_event(item, :accept_return)
end

# Not used because an item gets deleted when declining a return
def audit_deny_return(item)
create_audit_event(item, :deny_return)
end
antonykamp marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

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

36 changes: 30 additions & 6 deletions spec/features/notifications/return_request_notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,42 @@
triggering_user: user).count).to be(1)
end

it "deletes the notification upon clicking on 'Decline" do
it "deletes the notification upon clicking on 'Decline'" do
visit notifications_path(id: @notification.id)
expect(ReturnRequestNotification.exists?(@notification.id)).to be true
click_button('Decline')
expect(ReturnRequestNotification.exists?(@notification.id)).to be false
end

it "creates an audit when deleting the notification upon clicking on 'Decline'" do
visit notifications_path
click_button('Check')
# Comment out because a item gets deleted, when declining a return
# it "creates an audit when deleting the notification upon clicking on 'Decline'" do
# visit notifications_path
# click_button('Check')
# click_button('Decline')
# expect(AuditEvent.where(item: @notification.item.id, event_type: "deny_return",
# triggering_user: user).count).to be(1)
# end
antonykamp marked this conversation as resolved.
Show resolved Hide resolved

it "sends a return accepted notification upon clicking on 'Accept'" do
visit notifications_path(id: @notification.id)
expect(ReturnRequestNotification.exists?(@notification.id)).to be true
click_button('Accept')
@accepted_notification = Notification.find_by(receiver: @notification.item.holder,
actable_type: "ReturnAcceptedNotification")
expect(@accepted_notification.nil?).to be false
expect(ReturnAcceptedNotification.exists?(id: @accepted_notification.actable_id,
item: @notification.item)).to be true
end

it "sends a return denied notification upon clicking on 'Decline" do
visit notifications_path(id: @notification.id)
expect(ReturnRequestNotification.exists?(@notification.id)).to be true
click_button('Decline')
expect(AuditEvent.where(item: @notification.item.id, event_type: "deny_return",
triggering_user: user).count).to be(1)
SaturnHafen marked this conversation as resolved.
Show resolved Hide resolved
@declined_notification = Notification.find_by(receiver: @notification.item.holder,
actable_type: "ReturnDeclinedNotification")
expect(@declined_notification.nil?).to be false
expect(ReturnDeclinedNotification.exists?(id: @declined_notification.actable_id,
item_name: @notification.item.name)).to be true

end
end
11 changes: 10 additions & 1 deletion spec/features/request/accept_lend_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe "Requests handling", type: :feature do

it "owner can start lending by accepting the request and it creates audit events" do
it "if borrower send a lend request, the item status changes to pending_lend_request" do
owner = create(:max)
borrower = create(:peter)
item = create(:item, owner: owner.id)
Expand All @@ -11,6 +11,15 @@
click_button('Lend')
expect(item.reload.lend_status).to eq('pending_lend_request')
expect(AuditEvent.where(item: item, event_type: "request_lend").count).to be(1)
end

it "if owner accepts lend request, the item lend status changes to pending_pickup" do
owner = create(:max)
borrower = create(:peter)
item = create(:item, owner: owner.id)
sign_in borrower
visit item_path(item)
click_button('Lend')
sign_in owner
visit notifications_path
click_on('Lend Request')
Expand Down