Class: Monban::Repository::Account::Sequel

Inherits:
Getto::Repository::Sequel
  • Object
show all
Defined in:
lib/monban/repository/account.rb

Instance Method Summary collapse

Instance Method Details

#account_exists?(account_id:) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/monban/repository/account.rb', line 10

def (account_id:)
  not db[:accounts]
    .where(id: )
    .empty?
end

#delete_account(account_id:) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/monban/repository/account.rb', line 147

def (account_id:)
  [
    #:account_public_ids, # DO NOT DELETE public_id
    :account_login_ids,
    :account_authy_ids,
    :account_password_hashes,
    :account_reset_password_tokens,
    :account_roles,
    :account_reset_password_emails,
  ].each do |table|
    db[table].where(account_id: ).delete
  end

  # nullify account_id instead of delete rows
  db[:account_public_ids]
    .where(account_id: )
    .update(account_id: nil)

  db[:accounts].where(id: ).delete
end

#insert_account(now:) ⇒ Object



59
60
61
62
63
64
# File 'lib/monban/repository/account.rb', line 59

def (now:)
  db[:accounts].insert(
    created_at: now,
  )
  last_insert_id
end

#insert_login_id(account_id:, login_id:, now:) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/monban/repository/account.rb', line 66

def (account_id:, login_id:, now:)
  db[:account_login_ids].insert(
    account_id: ,
    login_id:   ,
    created_at: now,
    updated_at: now,
  )
end

#login_id(account_id:) ⇒ Object



37
38
39
40
41
42
# File 'lib/monban/repository/account.rb', line 37

def (account_id:)
  db[:account_login_ids]
    .where(account_id: )
    .select(:login_id)
    .map{|hash| hash[:login_id]}.first
end

#login_id_account(login_id:) ⇒ Object



22
23
24
25
26
27
# File 'lib/monban/repository/account.rb', line 22

def (login_id:)
  db[:account_login_ids]
    .where(login_id: )
    .select(:account_id)
    .map{|hash| hash[:account_id]}.first
end

#login_id_exists?(login_id:) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/monban/repository/account.rb', line 16

def (login_id:)
  not db[:account_login_ids]
    .where(login_id: )
    .empty?
end

#reset_password_email(account_id:) ⇒ Object



51
52
53
54
55
56
# File 'lib/monban/repository/account.rb', line 51

def reset_password_email(account_id:)
  db[:account_reset_password_emails]
    .where(account_id: )
    .select(:email)
    .map{|hash| hash[:email]}.first
end

#reset_password_email_account(email:) ⇒ Object



29
30
31
32
33
34
# File 'lib/monban/repository/account.rb', line 29

def (email:)
  db[:account_reset_password_emails]
    .where(email: email)
    .select(:account_id)
    .map{|hash| hash[:account_id]}.first
end

#roles(account_id:) ⇒ Object



44
45
46
47
48
49
# File 'lib/monban/repository/account.rb', line 44

def roles(account_id:)
  db[:account_roles]
    .where(account_id: )
    .select(:role)
    .map{|r| r[:role]}
end

#update_login_id(account_id:, login_id:, now:) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/monban/repository/account.rb', line 95

def (account_id:, login_id:, now:)
  where = {
    account_id: ,
  }
  if .empty?
    db[:account_login_ids].where(where).delete
  else
    if db[:account_login_ids].where(where).empty?
      db[:account_login_ids].insert(
          account_id: ,
          login_id:   ,
          updated_at: now,
          created_at: now,
        )
    else
      db[:account_login_ids]
        .where(account_id: )
        .update(
          login_id:   ,
          updated_at: now,
        )
    end
  end
end

#update_reset_password_email(account_id:, email:, now:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/monban/repository/account.rb', line 76

def update_reset_password_email(account_id:, email:, now:)
  where = {
    account_id: ,
  }
  if db[:account_reset_password_emails].where(where).empty?
    db[:account_reset_password_emails].insert(
      account_id: ,
      email:      email,
      created_at: now,
      updated_at: now,
    )
  else
    db[:account_reset_password_emails].where(where).update(
      email:      email,
      updated_at: now,
    )
  end
end

#update_roles(account_id:, roles:, now:) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/monban/repository/account.rb', line 120

def update_roles(account_id:, roles:, now:)
  old_roles = db[:account_roles]
    .where(account_id: )
    .select(:role)
    .map{|hash| hash[:role]}

  roles.each do |role|
    unless old_roles.delete(role.to_s)
      db[:account_roles].insert(
        account_id: ,
        role:       role.to_s,
        created_at: now,
      )
    end
  end

  old_roles.each do |role|
    db[:account_roles]
      .where(
        account_id: ,
        role:       role,
      )
      .delete
  end
end