From 163430787e0aaa6390fb2e1e779bd40c3fea0038 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov <62177364+VladislavSokov@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:28:07 +0300 Subject: [PATCH] feat(kid login): fix account share and login for kids (#294) * feat(kid login): fix account share and login for kids * feat(kid login): fixup! fix account share and login for kids --- .../kid_account_shares_controller.rb | 7 +--- app/models/account_share.rb | 1 + app/services/kids_user_service.rb | 21 ++++++++---- app/services/send_notification.rb | 2 +- app/views/accounts/edit.html.slim | 33 +++++++++---------- app/views/accounts/show.html.slim | 18 +++++----- 6 files changed, 40 insertions(+), 42 deletions(-) diff --git a/app/controllers/kid_account_shares_controller.rb b/app/controllers/kid_account_shares_controller.rb index 3a7bd418..d0574377 100644 --- a/app/controllers/kid_account_shares_controller.rb +++ b/app/controllers/kid_account_shares_controller.rb @@ -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 @@ -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 diff --git a/app/models/account_share.rb b/app/models/account_share.rb index 8bb71dfe..e664efa1 100644 --- a/app/models/account_share.rb +++ b/app/models/account_share.rb @@ -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? diff --git a/app/services/kids_user_service.rb b/app/services/kids_user_service.rb index 3f6b1a90..02b87788 100644 --- a/app/services/kids_user_service.rb +++ b/app/services/kids_user_service.rb @@ -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 @@ -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 diff --git a/app/services/send_notification.rb b/app/services/send_notification.rb index d9ab2811..bbccf105 100644 --- a/app/services/send_notification.rb +++ b/app/services/send_notification.rb @@ -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 diff --git a/app/views/accounts/edit.html.slim b/app/views/accounts/edit.html.slim index 993f417b..3491f83a 100644 --- a/app/views/accounts/edit.html.slim +++ b/app/views/accounts/edit.html.slim @@ -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" @@ -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() + }) + } }); - diff --git a/app/views/accounts/show.html.slim b/app/views/accounts/show.html.slim index 552885a0..bf30acfb 100644 --- a/app/views/accounts/show.html.slim +++ b/app/views/accounts/show.html.slim @@ -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: '+') - |   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: '+') + |   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: '-') @@ -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: '+') - |   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: '+') + |   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: '-')