Class: RogIQ::Commands::Accounts

Inherits:
Base
  • Object
show all
Defined in:
lib/rogiq/commands/accounts.rb

Instance Method Summary collapse

Instance Method Details

#listObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rogiq/commands/accounts.rb', line 9

def list
  RogIQ.load_rails!
  scope = ::Account.order(created_at: :desc)
  if options[:search].present?
    q = "%#{options[:search]}%"
    scope = scope.where("name ILIKE ? OR slug ILIKE ?", q, q)
  end
  rows = scope.limit(options[:limit]).map do |a|
    [a.id, a.name, a.slug, a.status, a.]
  end
  fmt.output(headers: %w[id name slug status account_type], rows: rows)
end

#show(identifier) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rogiq/commands/accounts.rb', line 23

def show(identifier)
  RogIQ.load_rails!
  a = RogIQ::Helpers.(identifier)
  unless a
    fmt.error_msg("Account not found")
    exit 1
  end

  fmt.output(
    id: a.id,
    name: a.name,
    slug: a.slug,
    status: a.status,
    account_type: a.,
    billing_status: a.try(:billing_status),
    created_at: a.created_at
  )
end

#subscription(identifier) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rogiq/commands/accounts.rb', line 81

def subscription(identifier)
  RogIQ.load_rails!
  a = RogIQ::Helpers.(identifier)
  unless a
    fmt.error_msg("Account not found")
    exit 1
  end

  sub = a.subscription
  plan = sub&.subscription_plan
  fmt.output(
    account_id: a.id,
    subscription: sub&.slice(:id, :status, :trial_ends_at, :current_period_start, :current_period_end, :cancel_at_period_end),
    plan: plan&.slice(:id, :name, :slug)
  )
end

#usage(identifier) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rogiq/commands/accounts.rb', line 43

def usage(identifier)
  RogIQ.load_rails!
  a = RogIQ::Helpers.(identifier)
  unless a
    fmt.error_msg("Account not found")
    exit 1
  end

  meters = ::UsageMeter.where(account_id: a.id).order(period_end: :desc).limit(20).map do |m|
    {
      metric_type: m.metric_type,
      period_start: m.period_start,
      period_end: m.period_end,
      current_value: m.current_value
    }
  end
  wallet = a.rogue_coin_wallet
  overages = a.usage_overages.status_pending.limit(20)

  fmt.output(
    account_id: a.id,
    rogue_coin_balance: wallet&.balance,
    rogue_coin_lifetime_spent: wallet&.lifetime_spent,
    usage_meters: meters,
    open_overages: overages.map do |o|
      {
        id: o.id,
        metric: o.metric_type,
        overage_amount: o.overage_amount,
        total_charge: o.total_charge.to_f,
        status: o.status,
        created_at: o.created_at
      }
    end
  )
end

#users(identifier) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rogiq/commands/accounts.rb', line 99

def users(identifier)
  RogIQ.load_rails!
  a = RogIQ::Helpers.(identifier)
  unless a
    fmt.error_msg("Account not found")
    exit 1
  end

  rows = a..includes(:user, :role).where(status: "active").map do |au|
    [au.user&.email, au.role&.name, au.status]
  end
  fmt.output(headers: %w[email role status], rows: rows)
end