Skip to content

Commit

Permalink
feat(account invitations): rename account invitation to account share (
Browse files Browse the repository at this point in the history
…#42)

* feat(account invitations): rename account invitation to account share

* feat(account invitations): fixup! rename account invitation to account share

* feat(account invitations): fixup! fixup! rename account invitation to account share

---------

Co-authored-by: VladislavSokov <[email protected]>
  • Loading branch information
VladislavSokov and VladislavSokov committed Aug 30, 2023
1 parent 261d50f commit 15c1beb
Show file tree
Hide file tree
Showing 27 changed files with 170 additions and 157 deletions.
26 changes: 0 additions & 26 deletions app/controllers/accept_account_invitations_controller.rb

This file was deleted.

26 changes: 26 additions & 0 deletions app/controllers/accept_account_shares_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class AcceptAccountSharesController < ApplicationController
def show
return if user_signed_in?

session[:after_sign_in_url] = request.fullpath

shared_email = AccountShare.find_by!(token: ps.fetch(:token)).email
user = User.find_by(email: shared_email)
redirect_url = user ? new_user_session_url(email: shared_email) : new_user_registration_url(email: shared_email)

redirect_to redirect_url
end

def update
received_share.update!(accepted_at: Time.current)
redirect_to account_path(received_share.account_id)
end

private

helper_method memoize def received_share
AccountShare.unaccepted.for(current_user).find_by!(token: ps.fetch(:token))
end
end
30 changes: 0 additions & 30 deletions app/controllers/account_invitations_controller.rb

This file was deleted.

32 changes: 32 additions & 0 deletions app/controllers/account_shares_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class AccountSharesController < ApplicationController
before_action :authenticate_user!

def index; end

def new; end

def create
personal_account_share = AccountShare.create!(user_id: current_user.id,
account_id: account.id,
token: SecureRandom.urlsafe_base64(32),
**account_share_params)
AccountShareMailer.account_share(personal_account_share).deliver
redirect_to account_shares_path account
end

def destroy
personal_account_share = AccountShare.find(ps.fetch(:id))
personal_account_share.destroy
respond_to do |format|
format.html { redirect_to account_shares_url, notice: 'Account share was successfully destroyed.' }
end
end

private

def account_share_params
params.require(:account_share).permit(:name, :email)
end
end
2 changes: 1 addition & 1 deletion app/controllers/my_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def show; end
end

helper_method memoize def shared_accounts
Account.invitees(current_user)
Account.shared_for(current_user)
end
end
9 changes: 9 additions & 0 deletions app/mailers/account_share_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AccountShareMailer < ApplicationMailer
def account_share(account_share)
@account_share = account_share

mail to: account_share.email, subject: 'Share to manage an account!'
end
end
9 changes: 0 additions & 9 deletions app/mailers/invitation_mailer.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Account < ApplicationRecord
has_many :income_transactions, class_name: 'Transaction', foreign_key: :to_account_id
has_many :outcome_transactions, class_name: 'Transaction', foreign_key: :from_account_id
has_many :children, class_name: 'Account', foreign_key: :parent_id
has_many :account_invitations
has_many :account_shares

# an account has user optionally
has_one :user
Expand All @@ -19,10 +19,10 @@ class Account < ApplicationRecord
scope :visible_for, lambda { |current_user|
where(id: [current_user.account_id] +
current_user.account.child_ids +
invitees(current_user).pluck(:id))
shared_for(current_user).pluck(:id))
}

scope :invitees, ->(user) { where(id: AccountInvitation.accepted.for(user).pluck(:account_id)) }
scope :shared_for, ->(user) { where(id: AccountShare.accepted.for(user).pluck(:account_id)) }

memoize def balance
income_transactions.sum(:amount) - outcome_transactions.sum(:amount)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class AccountInvitation < ApplicationRecord
class AccountShare < ApplicationRecord
belongs_to :user
belongs_to :account

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
div.columns.is-centered.is-vcentered
div.column.is-two-third
h4.subtitle.has-text-centered
| Dear #{received_invitation.name}
| Dear #{received_share.name}
h6.subtitle.has-text-centered
| I am delighted to invite you to manage the account
| You've been shared access to manage the account
br
br
div.buttons.is-centered
= link_to 'Accept', accept_account_invitation_path, method: :patch, class: 'button is-success is-medium'
= link_to 'Accept', accept_account_share_path, method: :patch, class: 'button is-success is-medium'
span.has-text-grey-light

= link_to 'Cancel', root_path, class: 'button is-light is-medium'
7 changes: 0 additions & 7 deletions app/views/account_invitations/_account_invitation.html.slim

This file was deleted.

16 changes: 0 additions & 16 deletions app/views/account_invitations/new.html.erb

This file was deleted.

7 changes: 7 additions & 0 deletions app/views/account_share_mailer/account_share.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2>Dear <%= @account_share.name%>, welcome to managing my account!</h2>
<br>
<p>Click the link to approve the share</p>
<%= link_to 'Go and approve the share', accept_account_share_url(token: @account_share.token) %>
<br>
<!-- TODO: add curent user name -->
<p>Sincerely, </p>
7 changes: 7 additions & 0 deletions app/views/account_shares/_account_share.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tr
td= account_share.name
td= account_share.email
td= account_share.created_at.to_formatted_s(:short)
td= account_share.accepted_at&.to_formatted_s(:short)
td= link_to 'link', accept_account_share_url(token: account_share.token)
td= link_to :delete, account_share_path(account, account_share), method: :delete, data: {confirm: 'Delete this share?'}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
h2.title
| Account invitations
= link_to 'New invitation', new_account_invitation_path, class: 'button is-light'
| Account shares
= link_to 'New share', new_account_share_path, class: 'button is-light'

.columns
.column
Expand All @@ -18,4 +18,4 @@ h2.title
th Accepted
th Link
th Actions
= render partial: 'account_invitation', collection: account.account_invitations.order(created_at: :desc)
= render partial: 'account_share', collection: account.account_shares.order(created_at: :desc)
16 changes: 16 additions & 0 deletions app/views/account_shares/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="columns">
<div class="column is-three-quarters">
<%= link_to 'Back', account_shares_path, class: 'button is-light' %>
</div>
<div class="column is-flex is-justify-content-flex-end">
</div>
</div>
<%= form_with url: account_shares_path, method: :post do |form| %>
<div class="column is-one-third">
<%= form.label :name %>
<%= form.text_field :name, name: 'account_share[name]', type:"text", class: 'input'%>
<%= form.label :email %>
<%= form.email_field :email, name: 'account_share[email]', type: "email"%>
</div>
<%= form.submit :Create, class: 'button is-light' %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/accounts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
| Account details
= link_to 'Edit', edit_account_path(account), class: 'button is-light'
.column.is-flex.is-justify-content-flex-end
= link_to 'Account invitations', account_invitations_path(account), class: 'button is-light'
= link_to 'Account shares', account_shares_path(account), class: 'button is-light'

.columns
.column
Expand Down
7 changes: 0 additions & 7 deletions app/views/invitation_mailer/account_invitation.html.erb

This file was deleted.

4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
resource :topup, only: %i[new create]
resource :spend, only: %i[new create]
resources :objectives, only: %i[new create]
resources :invitations, only: %i[index new create destroy], controller: 'account_invitations'
resources :shares, only: %i[index new create destroy], controller: 'account_shares'
end

resources :accept_account_invitations, param: :token, only: %i[show update]
resources :accept_account_shares, param: :token, only: %i[show update]
resources :transactions, only: [:destroy]
resources :objectives, only: [:destroy]
end
7 changes: 7 additions & 0 deletions db/migrate/20230830131000_rename_account_invitations_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class RenameAccountInvitationsTable < ActiveRecord::Migration[6.1]
def change
rename_table :account_invitations, :account_shares
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_08_18_090609) do
ActiveRecord::Schema.define(version: 2023_08_30_131000) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -25,7 +25,7 @@
t.index ["to_account_id"], name: "index_account_automatic_topup_configs_on_to_account_id"
end

create_table "account_invitations", force: :cascade do |t|
create_table "account_shares", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "email"
t.string "name"
Expand Down Expand Up @@ -86,8 +86,8 @@

add_foreign_key "account_automatic_topup_configs", "accounts", column: "from_account_id"
add_foreign_key "account_automatic_topup_configs", "accounts", column: "to_account_id"
add_foreign_key "account_invitations", "accounts", on_delete: :cascade
add_foreign_key "account_invitations", "users", on_delete: :cascade
add_foreign_key "account_shares", "accounts", on_delete: :cascade
add_foreign_key "account_shares", "users", on_delete: :cascade
add_foreign_key "accounts", "accounts", column: "parent_id"
add_foreign_key "objectives", "accounts"
add_foreign_key "transactions", "accounts", column: "from_account_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

require 'rails_helper'

RSpec.describe AcceptAccountInvitationsController, type: :controller do
RSpec.describe AcceptAccountSharesController, type: :controller do
describe '#show' do
let(:account) { create(:account, :parent) }
let(:user) { create(:user, account: account) }
let(:second_user) { create(:user) }

let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) }
let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) }

subject do
get :show, params: { token: account_invitation.token }
get :show, params: { token: account_share.token }
end

context 'when the user not signed in' do
it 'redirects to new user session url with email' do
subject
expect(response).to redirect_to(new_user_session_url(email: account_invitation.email))
expect(response).to redirect_to(new_user_session_url(email: account_share.email))
end
end

context 'when the user not signed up' do
let(:account_invitation) { create(:account_invitation, user: user, account: account) }
let(:account_share) { create(:account_share, user: user, account: account) }

it 'redirects to registration url with email' do
subject
expect(response).to redirect_to(new_user_registration_url(email: account_invitation.email))
expect(response).to redirect_to(new_user_registration_url(email: account_share.email))
end
end

Expand All @@ -43,19 +43,19 @@
let(:user) { create(:user, account: account) }
let(:second_user) { create(:user, account: account) }

let!(:account_invitation) { create(:account_invitation, user: user, account: account, email: second_user.email) }
let!(:account_share) { create(:account_share, user: user, account: account, email: second_user.email) }

subject do
patch :update, params: { token: account_invitation.token }
patch :update, params: { token: account_share.token }
end

before { sign_in second_user }

it 'set the taime' do
subject
expect(AccountInvitation.find(account_invitation.id).accepted_at).not_to be_nil
expect(AccountShare.find(account_share.id).accepted_at).not_to be_nil
end
it { is_expected.to have_http_status(302) }
it { is_expected.to redirect_to(account_path(account_invitation.account_id)) }
it { is_expected.to redirect_to(account_path(account_share.account_id)) }
end
end
Loading

0 comments on commit 15c1beb

Please sign in to comment.