Skip to content

Commit 54a4565

Browse files
meamalpbamal-foaps
andauthored
give new response when existing user is assigned a new role (#5208)
Co-authored-by: Amal PB <amal@foaps.co>
1 parent 5c0283e commit 54a4565

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

app/controllers/admin/users_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ def create
4141
validate_role_resource_params
4242
klass = Role::TITLE_TO_RESOURCE[params[:resource_type].to_sym]
4343
resource = klass&.find(params[:resource_id])
44+
existing_user = User.find_by(email: user_params[:email])
4445
UserInviteService.invite(
4546
name: user_params[:name],
4647
email: user_params[:email],
4748
roles: [params[:resource_type].to_sym],
4849
resource: resource
4950
)
50-
flash[:notice] = "Created a new user!"
51+
flash[:notice] = existing_user ? "Added new role to existing user" : "Created a new user!"
5152
redirect_to admin_users_path
5253
rescue => e
5354
flash.now[:error] = "Failed to create user: #{e}"

spec/requests/admin/users_requests_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,34 @@
152152
expect(new_user.has_role?(Role::ORG_ADMIN, organization)).to be_falsey
153153
end
154154

155+
context "flash notice behavior" do
156+
context "when creating a new user" do
157+
it "shows 'Created a new user!' message" do
158+
post admin_users_path, params: {
159+
user: { name: "New User", email: "new@example.com" },
160+
resource_type: Role::ORG_USER,
161+
resource_id: organization.id
162+
}
163+
expect(response).to redirect_to(admin_users_path)
164+
expect(flash[:notice]).to eq("Created a new user!")
165+
end
166+
end
167+
168+
context "when adding a role to an existing user" do
169+
let!(:existing_user) { create(:user, email: "existing@example.com", organization: organization) }
170+
171+
it "shows 'Added new role to existing user' message" do
172+
post admin_users_path, params: {
173+
user: { name: existing_user.name, email: existing_user.email },
174+
resource_type: Role::PARTNER,
175+
resource_id: partner.id
176+
}
177+
expect(response).to redirect_to(admin_users_path)
178+
expect(flash[:notice]).to eq("Added new role to existing user")
179+
end
180+
end
181+
end
182+
155183
it "creates an org admin" do
156184
post admin_users_path, params: {
157185
user: { name: "New Org Admin", email: organization.email },

0 commit comments

Comments
 (0)