Allow the issuer of a request to cancel it

This commit is contained in:
Tom Naessens 2019-05-31 14:50:55 +12:00
parent 923c1ab286
commit f93e588a1f
7 changed files with 49 additions and 14 deletions

View file

@ -1,11 +1,11 @@
class RequestsController < ApplicationController class RequestsController < ApplicationController
load_and_authorize_resource :user, find_by: :name load_and_authorize_resource :user, find_by: :name
before_action :load_request, only: [:confirm, :decline] before_action :load_request, only: [:confirm, :decline, :cancel]
authorize_resource :request, id_param: :request_id, only: [:confirm, :decline] authorize_resource :request, id_param: :request_id, only: [:confirm, :decline, :cancel]
def index def index
@requests = @user.incoming_requests.group_by(&:status) @requests = @user.requests.group_by(&:status)
respond_to do |format| respond_to do |format|
format.html { } format.html { }
format.json { render json: @requests } format.json { render json: @requests }
@ -22,6 +22,11 @@ class RequestsController < ApplicationController
redirect_to root_path redirect_to root_path
end end
def cancel
@request.cancel!
redirect_to root_path
end
private private
def load_request def load_request

View file

@ -17,7 +17,7 @@
class Request < ActiveRecord::Base class Request < ActiveRecord::Base
include BaseTransaction include BaseTransaction
enum status: [:open, :confirmed, :declined] enum status: [:open, :confirmed, :declined, :cancelled]
def confirm! def confirm!
return unless open? return unless open?
@ -36,6 +36,15 @@ class Request < ActiveRecord::Base
update_attributes status: :declined update_attributes status: :declined
end end
def cancel!
return unless open?
Notification.create user: creditor, message: cancelled_message unless issuer == creditor
Notification.create user: debtor, message: cancelled_message unless issuer == debtor
update_attributes status: :cancelled
end
private private
def confirmed_message def confirmed_message
@ -45,4 +54,8 @@ class Request < ActiveRecord::Base
def declined_message def declined_message
"#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"." "#{debtor.name} refuses to pay €#{amount/100.0} for \"#{message}\"."
end end
def cancelled_message
"#{issuer.name} cancelled the request to pay #{debtor.name}#{amount/100.0} for \"#{message}\" to #{creditor.name}."
end
end end

View file

@ -35,6 +35,10 @@ class User < ActiveRecord::Base
Transaction.where('creditor_id = ?', id).or(Transaction.where('debtor_id = ?', id)) Transaction.where('creditor_id = ?', id).or(Transaction.where('debtor_id = ?', id))
end end
def requests
Request.where('debtor_id = ?', id).or(Request.where('creditor_id = ?', id).or(Request.where('issuer_id = ?', id)))
end
def calculate_balance! def calculate_balance!
balance = incoming_transactions.sum(:amount) - balance = incoming_transactions.sum(:amount) -
outgoing_transactions.sum(:amount) outgoing_transactions.sum(:amount)

View file

@ -7,6 +7,7 @@ class UserAbility
can :manage, :all if user.penning? can :manage, :all if user.penning?
can :create, Request, creditor_id: user.id can :create, Request, creditor_id: user.id
can [:confirm, :decline], Request, debtor_id: user.id can [:confirm, :decline], Request, debtor_id: user.id
can :cancel, Request, issuer_id: user.id
can [:read, :reset_key, :add_registration_token], User, id: user.id can [:read, :reset_key, :add_registration_token], User, id: user.id
can :manage, Notification, user_id: user.id can :manage, Notification, user_id: user.id
can :create, Transaction do |t| can :create, Transaction do |t|

View file

@ -2,24 +2,34 @@
%table.pure-table %table.pure-table
%thead %thead
%tr %tr
%th Peer
%th Issuer %th Issuer
%th Creditor
%th Debtor
%th Amount %th Amount
%th Message %th Message
- if actions - if actions
%th Accept %th Actions
%th Decline
%tbody %tbody
- (requests || []).each do |r| - (requests || []).each do |r|
%tr %tr
%td= r.creditor.name
%td= r.issuer.name %td= r.issuer.name
%td= r.creditor.name
%td= r.debtor.name
%td= r.amount_f %td= r.amount_f
%td= r.message %td= r.message
- if actions - if actions
%td %td
= link_to request_confirm_path(r), method: :post do - if can?(:confirm, r)
%span.glyphicon.glyphicon-ok = link_to request_confirm_path(r), method: :post, class: "btn btn-success btn-xs" do
%td %i.glyphicon.glyphicon-ok
= link_to request_decline_path(r), method: :post do Accept
%span.glyphicon.glyphicon-remove &nbsp;
- if can?(:decline, r)
= link_to request_decline_path(r), method: :post, class: "btn btn-danger btn-xs" do
%i.glyphicon.glyphicon-remove
Decline
&nbsp;
- if can?(:cancel, r)
= link_to request_cancel_path(r), method: :post, class: "btn btn-warning btn-xs" do
%i.glyphicon.glyphicon-trash
Cancel

View file

@ -1,3 +1,4 @@
= render 'index', actions: true, title: 'Open Requests', requests: @requests['open'] = render 'index', actions: true, title: 'Open Requests', requests: @requests['open']
= render 'index', actions: false, title: 'Confirmed Requests', requests: @requests['confirmed'] = render 'index', actions: false, title: 'Confirmed Requests', requests: @requests['confirmed']
= render 'index', actions: false, title: 'Declined Requests', requests: @requests['declined'] = render 'index', actions: false, title: 'Declined Requests', requests: @requests['declined']
= render 'index', actions: false, title: 'Cancelled Requests', requests: @requests['cancelled']

View file

@ -18,6 +18,7 @@ Rails.application.routes.draw do
resources :requests, only: [:index], shallow: true do resources :requests, only: [:index], shallow: true do
post :confirm post :confirm
post :decline post :decline
post :cancel
end end
resources :notifications, only: [:index], shallow: true do resources :notifications, only: [:index], shallow: true do
post :read post :read