Add LndHub db/models, and quick stats for admin views

This commit is contained in:
Râu Cao
2023-02-10 13:12:36 +08:00
parent 09d99ce9c2
commit dd482d7f2e
23 changed files with 282 additions and 37 deletions

View 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

View File

@@ -0,0 +1,3 @@
class LndhubAccountLedger < LndhubBase
self.table_name = "account_ledgers"
end

View 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
View 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

View File

@@ -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