Add LndHub db/models, and quick stats for admin views
This commit is contained in:
21
app/models/lndhub_account.rb
Normal file
21
app/models/lndhub_account.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
class LndhubAccount < LndhubBase
|
||||
self.table_name = "accounts"
|
||||
self.inheritance_column = :_type_disabled
|
||||
|
||||
has_many :ledgers, class_name: "LndhubAccountLedger",
|
||||
foreign_key: "account_id"
|
||||
|
||||
belongs_to :user, class_name: "LndhubUser",
|
||||
foreign_key: "user_id"
|
||||
|
||||
scope :current, -> { where(type: "current") }
|
||||
scope :outgoing, -> { where(type: "outgoing") }
|
||||
scope :incoming, -> { where(type: "incoming") }
|
||||
scope :fees, -> { where(type: "fees") }
|
||||
|
||||
scope :with_balances, -> {
|
||||
current.joins(:user).joins(:ledgers)
|
||||
.group("accounts.id", "users.login")
|
||||
.select("accounts.id, users.login, SUM(account_ledgers.amount) AS balance")
|
||||
}
|
||||
end
|
||||
3
app/models/lndhub_account_ledger.rb
Normal file
3
app/models/lndhub_account_ledger.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class LndhubAccountLedger < LndhubBase
|
||||
self.table_name = "account_ledgers"
|
||||
end
|
||||
4
app/models/lndhub_base.rb
Normal file
4
app/models/lndhub_base.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
class LndhubBase < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
establish_connection :lndhub
|
||||
end
|
||||
15
app/models/lndhub_user.rb
Normal file
15
app/models/lndhub_user.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class LndhubUser < LndhubBase
|
||||
self.table_name = "users"
|
||||
self.inheritance_column = :_type_disabled
|
||||
|
||||
has_many :accounts, class_name: "LndhubAccount",
|
||||
foreign_key: "user_id"
|
||||
|
||||
belongs_to :user, class_name: "User",
|
||||
primary_key: "ln_account",
|
||||
foreign_key: "login"
|
||||
|
||||
def balance
|
||||
accounts.current.first.ledgers.sum("account_ledgers.amount")
|
||||
end
|
||||
end
|
||||
@@ -5,6 +5,11 @@ class User < ApplicationRecord
|
||||
has_many :invitations, dependent: :destroy
|
||||
has_many :donations, dependent: :nullify
|
||||
|
||||
has_one :lndhub_user, class_name: "LndhubUser", inverse_of: "user",
|
||||
primary_key: "ln_account", foreign_key: "login"
|
||||
|
||||
has_many :accounts, through: :lndhub_user
|
||||
|
||||
validates_uniqueness_of :cn
|
||||
validates_length_of :cn, :minimum => 3
|
||||
validates_uniqueness_of :email
|
||||
|
||||
Reference in New Issue
Block a user