Skip to content

Commit

Permalink
Merge f0bf311 into af0043d
Browse files Browse the repository at this point in the history
  • Loading branch information
TomNaessens committed Oct 12, 2019
2 parents af0043d + f0bf311 commit 837826a
Show file tree
Hide file tree
Showing 36 changed files with 529 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -83,6 +83,9 @@ group :development do
gem 'capistrano-rails', '~> 1.1'
gem 'capistrano-rbenv'
gem 'capistrano-passenger'

# Solves readline lib errors on OSX
gem 'rb-readline'
end

group :test do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -238,6 +238,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
rb-readline (0.5.5)
rbtree3 (0.5.0)
rdoc (4.3.0)
react-rails (1.10.0)
Expand Down Expand Up @@ -374,6 +375,7 @@ DEPENDENCIES
purecss-rails
rails (= 5.2.3)
rails-controller-testing
rb-readline
react-rails (~> 1.10.0)
rolify
rpush (~> 4.1)
Expand Down
31 changes: 31 additions & 0 deletions app/assets/images/zeuswpi.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -35,3 +35,7 @@ $(document).on('turbolinks:load', function() {
});
})
});

$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
33 changes: 33 additions & 0 deletions app/controllers/admin/bank_transfer_requests_controller.rb
@@ -0,0 +1,33 @@
class Admin::BankTransferRequestsController < AdminController
before_action :load_bank_transfer_request, only: [:approve, :decline]
authorize_resource :bank_transfer_request, id_param: :bank_transfer_request_id, only: [:approve, :decline]

def index
@bank_transfer_requests = BankTransferRequest.all
end

def approve
if @bank_transfer_request.approvable?
@bank_transfer_request.approve!
else
flash[:warning] = "This bank transfer request is not approvable."
end

redirect_to action: :index
end

def decline
if @bank_transfer_request.declinable?
@bank_transfer_request.decline!(params[:reason])
else
flash[:warning] = "This bank transfer request is not declinable."
end
redirect_to action: :index
end

private

def load_bank_transfer_request
@bank_transfer_request = BankTransferRequest.find params[:bank_transfer_request_id]
end
end
9 changes: 9 additions & 0 deletions app/controllers/admin_controller.rb
@@ -0,0 +1,9 @@
class AdminController < ApplicationController
before_action :require_admin

def require_admin
unless current_user.penning?
redirect_to root_path
end
end
end
48 changes: 48 additions & 0 deletions app/controllers/bank_transfer_requests_controller.rb
@@ -0,0 +1,48 @@
class BankTransferRequestsController < ApplicationController
load_and_authorize_resource :user, find_by: :name

before_action :load_bank_transfer_request, only: [:cancel]
authorize_resource :bank_transfer_request, id_param: :bank_transfer_request_id, only: [:cancel]

def index
@bank_transfer_requests = @user.bank_transfer_requests
@bank_transfer_request = BankTransferRequest.new
end

def create
@bank_transfer_request = BankTransferRequest.new(
create_params.merge(user: @user)
)
@bank_transfer_request.set_payment_code

if @bank_transfer_request.save
flash[:success] = "Bank transfer request ##{@bank_transfer_request.id} with payment code <strong>#{@bank_transfer_request.payment_code}</strong> created. Use this payment code in the description field of your bank transaction.".html_safe
Notification.create user: User.zeus, message: "#{@bank_transfer_request.user.name} just created a bank transfer request ##{@bank_transfer_request.id} for €#{@bank_transfer_request.amount} with payment code #{@bank_transfer_request.payment_code}."

@bank_transfer_request = BankTransferRequest.new
end

@bank_transfer_requests = @user.bank_transfer_requests
render :index
end

def cancel
if @bank_transfer_request.cancellable?
@bank_transfer_request.cancel!
else
flash[:warning] = "This bank transfer request is not cancellable."
end

redirect_to action: :index
end

private

def load_bank_transfer_request
@bank_transfer_request = BankTransferRequest.find params[:bank_transfer_request_id]
end

def create_params
params.require(:bank_transfer_request).permit(:amount)
end
end
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -10,4 +10,8 @@ def euro_from_cents(f)
nil
end
end

def form_errors(object)
render partial: 'form_errors', locals: { object: object }
end
end
13 changes: 12 additions & 1 deletion app/models/android_device_registration_token.rb
@@ -1,3 +1,14 @@
class AndroidDeviceRegistrationToken < ActiveRecord::Base
# == Schema Information
#
# Table name: android_device_registration_tokens
#
# id :integer not null, primary key
# token :string
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class AndroidDeviceRegistrationToken < ApplicationRecord
belongs_to :user
end
3 changes: 3 additions & 0 deletions app/models/application_record.rb
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
124 changes: 124 additions & 0 deletions app/models/bank_transfer_request.rb
@@ -0,0 +1,124 @@
# == Schema Information
#
# Table name: bank_transfer_requests
#
# id :integer not null, primary key
# user_id :integer
# amount_in_cents :integer not null
# status :string default("pending"), not null
# decline_reason :string
# payment_code :string not null
# created_at :datetime not null
# updated_at :datetime not null
#

class BankTransferRequest < ApplicationRecord
belongs_to :user, required: true

enum status: {
pending: "pending",
approved: "approved",
declined: "declined",
cancelled: "cancelled"
}

validates :amount_in_cents, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :payment_code, presence: true, uniqueness: true

PAYMENT_CODE_PREFIX = "TAB"

def set_payment_code
self.payment_code = self.class.generate_payment_code
end

def amount
from_cents read_attribute(:amount_in_cents)
end

def amount=(value)
write_attribute(:amount_in_cents, to_cents(value))
end

def approve!
if self.declined?
message = "Declined bank transfer request ##{self.id} with code #{self.payment_code} has been reversed and is now accepted."
else
message = "Bank transfer request ##{self.id} with code #{self.payment_code} has been approved."
end
Transaction.create!(
debtor: User.zeus,
creditor: self.user,
issuer: User.zeus,
amount: self.amount_in_cents,
message: message
)
Notification.create user: self.user, message: message

self.approved!
end

def decline!(reason=nil)
self.decline_reason = reason
if self.approved?
# Transaction needs to be reversed
message = "Approved bank transfer request ##{self.id} with code #{self.payment_code} has been reversed and is now declined."

Transaction.create!(
debtor: self.user,
creditor: User.zeus,
issuer: User.zeus,
amount: self.amount_in_cents,
message: message
)
else
message = "Bank transfer request ##{self.id} with code #{self.payment_code} has been declined."
end

Notification.create user: self.user, message: message

self.declined!
end

def cancel!
self.cancelled!
end

def approvable?
self.pending? || self.declined?
end

def declinable?
self.pending? || self.approved?
end

def cancellable?
self.pending?
end


def self.generate_payment_code
random = rand(10**15)
return sprintf("#{PAYMENT_CODE_PREFIX}%02d%015d", random % 97, random)
end

def self.find_payment_code_from_csv(csvline)
match = /#{PAYMENT_CODE_PREFIX}\d+/.match(csvline)
if match
return self.find_by_payment_code(match[0])
else
return false
end
end

private

def from_cents(value)
(value || 0) / 100.0
end

def to_cents(value)
if value.is_a? String then value.sub!(',', '.') end
(value.to_f * 100).to_int
end

end
2 changes: 1 addition & 1 deletion app/models/client.rb
Expand Up @@ -9,7 +9,7 @@
# updated_at :datetime not null
#

class Client < ActiveRecord::Base
class Client < ApplicationRecord
rolify
has_many :issued_transactions, as: :issuer, class_name: 'Transaction'
before_create :generate_key
Expand Down
2 changes: 1 addition & 1 deletion app/models/notification.rb
Expand Up @@ -10,7 +10,7 @@
# updated_at :datetime not null
#

class Notification < ActiveRecord::Base
class Notification < ApplicationRecord
belongs_to :user
after_save :send_gcm_notification

Expand Down
6 changes: 3 additions & 3 deletions app/models/request.rb
Expand Up @@ -5,16 +5,16 @@
# id :integer not null, primary key
# debtor_id :integer not null
# creditor_id :integer not null
# issuer_id :integer not null
# issuer_type :string not null
# issuer_id :integer not null
# amount :integer default(0), not null
# message :string
# status :integer default(0)
# status :integer default("open")
# created_at :datetime not null
# updated_at :datetime not null
#

class Request < ActiveRecord::Base
class Request < ApplicationRecord
include BaseTransaction

enum status: [:open, :confirmed, :declined, :cancelled]
Expand Down
14 changes: 13 additions & 1 deletion app/models/role.rb
@@ -1,4 +1,16 @@
class Role < ActiveRecord::Base
# == Schema Information
#
# Table name: roles
#
# id :integer not null, primary key
# name :string
# resource_type :string
# resource_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Role < ApplicationRecord
has_and_belongs_to_many :clients, join_table: :clients_roles

belongs_to :resource,
Expand Down
2 changes: 1 addition & 1 deletion app/models/transaction.rb
Expand Up @@ -14,7 +14,7 @@
# id_at_client :integer
#

class Transaction < ActiveRecord::Base
class Transaction < ApplicationRecord
include BaseTransaction
include TransactionHelpers

Expand Down
5 changes: 4 additions & 1 deletion app/models/user.rb
Expand Up @@ -8,9 +8,10 @@
# penning :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
# key :string
#

class User < ActiveRecord::Base
class User < ApplicationRecord
include FriendlyId
friendly_id :name, use: :finders
devise :timeoutable, :omniauthable, :omniauth_providers => [:zeuswpi]
Expand All @@ -27,6 +28,8 @@ class User < ActiveRecord::Base

has_many :issued_transactions, as: :issuer, class_name: 'Transaction'

has_many :bank_transfer_requests

validates :name, presence: true, uniqueness: true

scope :humans, -> { where.not(id: self.zeus) }
Expand Down

0 comments on commit 837826a

Please sign in to comment.