Skip to content

Commit

Permalink
create invitation presenter (#1772)
Browse files Browse the repository at this point in the history
* create invitation presenter

* use jbuilder for update team action

* send deleted id for invitation

* send updated invitation object

* send full name in team update

* use helper for avatar

* refactor and review comments

* show false for pending invite

* fix test

---------

Co-authored-by: “Apoorv <“tiwari.apoorv1316@gmail.com”>
  • Loading branch information
apoorv1316 and “Apoorv authored Apr 8, 2024
1 parent 683fdd6 commit f60a0ec
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 25 deletions.
14 changes: 10 additions & 4 deletions app/controllers/internal_api/v1/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ class InternalApi::V1::InvitationsController < InternalApi::V1::ApplicationContr

def create
authorize :invitation
Invitations::CreateInvitationService.new(invitation_params, current_company, current_user).process
render json: { notice: I18n.t("invitation.create.success.message") }, status: :created
invitation = Invitations::CreateInvitationService.new(invitation_params, current_company, current_user).process
render :create, locals: {
invitation:
},
status: :created
end

def update
authorize @invitation

@invitation.update!(invitation_params)
render json: { notice: I18n.t("invitation.update.success.message") }, status: :ok
render :update, locals: {
invitation: @invitation
}, status: :ok
end

def destroy
authorize @invitation

invitation_id = @invitation.id
@invitation.destroy!
render json: { notice: I18n.t("invitation.delete.success.message") }, status: :ok
render json: { id: invitation_id, notice: I18n.t("invitation.delete.success.message") }, status: :ok
end

def resend
Expand Down
9 changes: 2 additions & 7 deletions app/controllers/internal_api/v1/team_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ def index

def update
authorize employment, policy_class: TeamPolicy

Team::UpdateService.new(
user = Team::UpdateService.new(
user_params:, current_company:, new_role: params[:role], user: employment.user).process

render json: {
user: employment.user,
notice: I18n.t("team.update.success.message")
}, status: :ok
render :update, locals: { user:, employment: }, status: :ok
end

def update_team_members
Expand Down
21 changes: 10 additions & 11 deletions app/presenters/team_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,28 @@ def formatted_invitations
last_name: member.last_name,
email: member.recipient_email,
role: member.role,
status: invited_user_status,
status: invited_user_status(member),
is_team_member: false,
data_type: "Invitation"
}
end
end

def team_member_status(member)
return unless is_current_user_employee && member.unconfirmed_email?

I18n.t("team.reconfirmation")
user_employed_at_current_company && member.confirmed_at?
end

def invited_user_status
return unless is_current_user_employee

I18n.t("team.invitation")
def invited_user_status(member)
user_employed_at_current_company && member.accepted_at?
end

def is_current_user_employee
is_current_user_employee ||= current_user.has_any_role?(
def user_employed_at_current_company
user_employed_at_current_company ||= current_user.has_any_role?(
{ name: :owner, resource: current_company },
{ name: :admin, resource: current_company })
{ name: :admin, resource: current_company },
{ name: :employee, resource: current_company },
{ name: :book_keeper, resource: current_company }
)
end

def employment_data(user)
Expand Down
1 change: 1 addition & 0 deletions app/services/invitations/create_invitation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def process
set_company
set_sender
invitation.save!
invitation
end

private
Expand Down
1 change: 1 addition & 0 deletions app/services/team/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def process
user.skip_reconfirmation!
user.update!(user_params)
update_company_user_role
user
end
end

Expand Down
16 changes: 16 additions & 0 deletions app/views/internal_api/v1/invitations/_invitation.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

json.key_format! camelize: :lower
json.deep_format_keys!

json.invitation do
json.id invitation.id
json.name invitation.full_name
json.first_name invitation.first_name
json.last_name invitation.last_name
json.email invitation.recipient_email
json.role invitation.role
json.status invitation.accepted_at?
json.is_team_member invitation.accepted_at?
json.profile_picture image_url "avatar.svg"
end
4 changes: 4 additions & 0 deletions app/views/internal_api/v1/invitations/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

json.partial! "invitation", locals: { invitation: }
json.notice I18n.t("invitation.create.success.message")
4 changes: 4 additions & 0 deletions app/views/internal_api/v1/invitations/update.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

json.partial! "invitation", locals: { invitation: }
json.notice I18n.t("invitation.update.success.message")
18 changes: 18 additions & 0 deletions app/views/internal_api/v1/team/update.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

json.key_format! camelize: :lower
json.deep_format_keys!

json.user do
json.extract! user, :id, :first_name, :last_name, :email
json.name user.full_name
json.role user.primary_role(current_company)
json.status user.confirmed_at?
json.is_team_member true
json.data_type "Team"
json.profile_picture user_avatar user
json.employment_type employment&.employment_type
json.joined_at_date employment&.joined_at
end

json.notice I18n.t("team.update.success.message")
6 changes: 3 additions & 3 deletions spec/requests/internal_api/v1/team/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
end

expected_team_data = [{
"id" => user.id, "name" => user.full_name, "email" => user.email, "role" => "admin", "status" => nil
"id" => user.id, "name" => user.full_name, "email" => user.email, "role" => "admin", "status" => true
}]

expected_invited_user_data = [{
"id" => invitation.id, "name" => invitation.full_name, "email" => invitation.recipient_email, "role" => "employee", "status" => I18n.t("team.invitation")
"id" => invitation.id, "name" => invitation.full_name, "email" => invitation.recipient_email, "role" => "employee", "status" => false
}]

expect(actual_team_data).to eq(expected_team_data)
Expand All @@ -66,7 +66,7 @@

service_arr = [{
"id" => user.id, "firstName" => user.first_name, "lastName" => user.last_name, "name" => "#{user.first_name} #{user.last_name}",
"email" => user.email, "role" => "admin", "status" => nil, "isTeamMember" => true, "employmentType" => employment.employment_type, "joinedAtDate" => employment.joined_at.strftime("%Y-%m-%d"),
"email" => user.email, "role" => "admin", "status" => true, "isTeamMember" => true, "employmentType" => employment.employment_type, "joinedAtDate" => employment.joined_at.strftime("%Y-%m-%d"),
"profilePicture" => user.avatar_url
}]

Expand Down

0 comments on commit f60a0ec

Please sign in to comment.