Skip to content

Commit

Permalink
feat(kid login): fix account share and login for kids (#294)
Browse files Browse the repository at this point in the history
* feat(kid login): fix account share and login for kids

* feat(kid login): fixup! fix account share and login for kids
  • Loading branch information
VladislavSokov authored Nov 29, 2023
1 parent 6f5eec7 commit 1634307
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
7 changes: 1 addition & 6 deletions app/controllers/kid_account_shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ def create
parental_key = generate_unique_key
AccountShare.create!(user_id: current_user.id,
account_id: account.id,
token: SecureRandom.urlsafe_base64(32),
accepted_at: Time.current,
name: account.name,
email: email(parental_key),
email: "#{parental_key}@budgetingkid.com",
parental_key: parental_key)
redirect_to account_shares_path(account)
end
Expand All @@ -25,8 +24,4 @@ def generate_unique_key
return key unless User.exists?(parental_key: key)
end
end

def email(key)
account.email.blank? ? "#{key}@budgetingkid.com" : account.email
end
end
1 change: 1 addition & 0 deletions app/models/account_share.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AccountShare < ApplicationRecord
scope :accepted, -> { where.not(accepted_at: nil).and(where.not(accepted_at: DateTime.new(0))) }
scope :unaccepted, -> { where(accepted_at: nil) }
scope :visible, -> { includes(:account).where(accounts: { archived_at: nil }) }
scope :for_parents, -> { where(parental_key: nil) }

memoize def public?
email.nil? && name.nil? && parental_key.nil?
Expand Down
21 changes: 14 additions & 7 deletions app/services/kids_user_service.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# frozen_string_literal: true

class KidsUserService
extend Memoist

def initialize(share)
@key = share.parental_key
@account = share.account
@name = @account.name
@email = @account.email.blank? ? "#{@key}@budgetingkid.com" : @account.email
@share = share
end

def perform
user = User.includes(:account).find_by(parental_key: @key)
user = User.includes(:account).find_by(parental_key: parental_key)

return user if user

Expand All @@ -20,9 +19,17 @@ def perform

def create_user
ActiveRecord::Base.transaction do
user = User.create(email: @email, password: @key, parental_key: @key)
user.create_account(name: @email, email: @email)
user = User.create(email: email, password: parental_key, parental_key: parental_key)
user.create_account(name: email, email: email)
user
end
end

memoize def email
"#{parental_key}@budgetingkid.com"
end

memoize def parental_key
@share.parental_key
end
end
2 changes: 1 addition & 1 deletion app/services/send_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def recipients
end

def parent_emails
[*@account.account_shares.accepted.pluck(:email), @account.parent.user.email]
[*@account.account_shares.accepted.for_parents.pluck(:email), @account.parent.user.email]
end
end
33 changes: 15 additions & 18 deletions app/views/accounts/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
i.fa.fa-camera
span.file-label
| Add photo
.field.mb-4
= f.text_field :name, autofocus: true, class: "input", placeholder: "Name"
.field
= f.check_box :notification, class: "checkbox mr-2", id: 'notification-check-box'
= f.label :notification, "Report transactions"
.field.mb-4
= f.email_field :email, disabled: !account.notification, autocomplete: "email", id:"email-input", placeholder: "email", required: true
.notification.is-light.mt-1
- if current_user.parent?
| You can check the box and specify your kid's email to send them notifications about balance changes.
- else
| You can check the box and specify your email to recieve notifications about balance changes.
- if current_user.parent?
.field.mb-4
= f.text_field :name, autofocus: true, class: "input", placeholder: "Name"
.field
= f.check_box :notification, class: "checkbox mr-2", id: 'notification-check-box'
= f.label :notification, "Report transactions"
.field.mb-4
= f.email_field :email, disabled: !account.notification, autocomplete: "email", id:"email-input", placeholder: "email", required: true
.notification.is-light.mt-1
| You can check the box and specify your email to recieve notifications about balance changes.
.field.mb-4
= f.check_box :notify_parents, class: "checkbox mr-2"
= f.label :notify_parents, "Report transactions to parents"
Expand All @@ -49,10 +46,10 @@ javascript:
document.addEventListener('turbolinks:load', function () {
const checkbox = document.getElementById('notification-check-box');
const input = document.getElementById('email-input');
checkbox.addEventListener('click', function (e) {
input.disabled = !checkbox.checked
input.focus()
})
if (input) {
checkbox.addEventListener('click', function (e) {
input.disabled = !checkbox.checked
input.focus()
})
}
});
18 changes: 8 additions & 10 deletions app/views/accounts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@
| Balance
p.title.is-2.is-budgeting-kid-color.has-text-centered #{account.balance}
.buttons.mb-1
- if current_user.parent?
= button_tag type:"button", id:"increase-button", data: { url: account_topup_path(account) }, class: 'button mx-2 is-plus-color is-medium' do
figure.image.is-24x24
= image_tag('plus-circle-green.svg', alt: '+')
| &nbsp; Increase
= button_tag type:"button", id:"increase-button", data: { url: account_topup_path(account) }, class: 'button mx-2 is-plus-color is-medium' do
figure.image.is-24x24
= image_tag('plus-circle-green.svg', alt: '+')
| &nbsp; Increase
= button_tag type:"button", id:"decrease-button", data: { url: account_spend_path(account) }, class: 'button mx-2 is-minus-color is-medium' do
figure.image.is-24x24
= image_tag('minus-circle-red.svg', alt: '-')
Expand All @@ -93,11 +92,10 @@
| Balance
p.title.is-2.is-budgeting-kid-color.has-text-centered #{account.balance}
.buttons.mb-1
- if current_user.parent?
= button_tag type:"button", id:"increase-button", data: { url: account_topup_path(account) }, class: 'button mx-1 is-plus-color' do
figure.image.is-24x24
= image_tag('plus-circle-green.svg', alt: '+')
| &nbsp; Increase
= button_tag type:"button", id:"increase-button", data: { url: account_topup_path(account) }, class: 'button mx-1 is-plus-color' do
figure.image.is-24x24
= image_tag('plus-circle-green.svg', alt: '+')
| &nbsp; Increase
= button_tag type:"button", id:"decrease-button", data: { url: account_spend_path(account) }, class: 'button mx-1 is-minus-color' do
figure.image.is-24x24
= image_tag('minus-circle-red.svg', alt: '-')
Expand Down

0 comments on commit 1634307

Please sign in to comment.