A platform for community discussion. Free, open, simple.
FIX: Improve error message when updating group notification settings (#37184)
Régis Hanol23982658b2b4
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rbindex a776cf98576..adbbb1d7811 100644--- a/app/controllers/groups_controller.rb+++ b/app/controllers/groups_controller.rb@@ -165,7 +165,9 @@ class GroupsController < ApplicationControllerrender status: :unprocessable_entity,json: {user_count: user_count,- errors: [I18n.t("invalid_params", message: :update_existing_users)],+ errors: [+ I18n.t("groups.errors.update_existing_users_required", count: user_count),+ ],})enddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 07f6fdc7c5f..6e02bdde2c7 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -654,6 +654,7 @@ en:other: "Maximum %{count} email domains can be counted at once"no_invites_with_discourse_connect: "You can invite only registered users when DiscourseConnect is enabled"no_invites_without_local_logins: "You can invite only registered users when local logins are disabled"+ update_existing_users_required: "This change affects %{count} existing group members. You must specify the 'update_existing_users' parameter (true or false) to indicate whether to apply the new notification defaults to existing members."default_names:everyone: "everyone"admins: "admins"diff --git a/frontend/discourse/app/components/group-manage-save-button.gjs b/frontend/discourse/app/components/group-manage-save-button.gjsindex ffee5c1446b..d1f3ac7df22 100644--- a/frontend/discourse/app/components/group-manage-save-button.gjs+++ b/frontend/discourse/app/components/group-manage-save-button.gjs@@ -21,18 +21,12 @@ export default class GroupManageSaveButton extends Component {saving = null;disabled = false;- updateExistingUsers = null;@discourseComputed("saving")savingText(saving) {return saving ? i18n("saving") : i18n("save");}- @action- setUpdateExistingUsers(value) {- this.updateExistingUsers = value;- }-_wouldLoseAccess() {if (this.currentUser.admin) {return false;@@ -51,7 +45,7 @@ export default class GroupManageSaveButton extends Component {}@action- async save() {+ async save(updateExistingUsers = null) {if (this.beforeSave) {this.beforeSave();}@@ -82,8 +76,8 @@ export default class GroupManageSaveButton extends Component {this.set("saving", true);const opts = {};- if (this.updateExistingUsers !== null) {- opts.update_existing_users = this.updateExistingUsers;+ if (updateExistingUsers !== null) {+ opts.update_existing_users = updateExistingUsers;}try {@@ -94,10 +88,7 @@ export default class GroupManageSaveButton extends Component {return;}- this.setProperties({- saved: true,- updateExistingUsers: null,- });+ this.set("saved", true);if (this.afterSave) {this.afterSave();@@ -105,7 +96,7 @@ export default class GroupManageSaveButton extends Component {} catch (error) {const json = error.jqXHR?.responseJSON;if (error.jqXHR?.status === 422 && json?.user_count) {- this.editGroupNotifications(json);+ this.editGroupNotifications(json.user_count);} else {popupAjaxError(error);}@@ -115,14 +106,12 @@ export default class GroupManageSaveButton extends Component {}@action- async editGroupNotifications(json) {- await this.modal.show(GroupDefaultNotificationsModal, {- model: {- count: json.user_count,- setUpdateExistingUsers: this.setUpdateExistingUsers,- },- });- this.save();+ async editGroupNotifications(count) {+ const updateExistingUsers = await this.modal.show(+ GroupDefaultNotificationsModal,+ { model: { count } }+ );+ this.save(updateExistingUsers);}<template>diff --git a/frontend/discourse/app/components/modal/group-default-notifications.gjs b/frontend/discourse/app/components/modal/group-default-notifications.gjsindex aad5fcd57da..07e0009d002 100644--- a/frontend/discourse/app/components/modal/group-default-notifications.gjs+++ b/frontend/discourse/app/components/modal/group-default-notifications.gjs@@ -7,14 +7,12 @@ import { i18n } from "discourse-i18n";export default class GroupDefaultNotifications extends Component {@actionupdateExistingUsers() {- this.args.model.setUpdateExistingUsers(true);- this.args.closeModal();+ this.args.closeModal(true);}@actioncancel() {- this.args.model.setUpdateExistingUsers(false);- this.args.closeModal();+ this.args.closeModal(false);}<template>diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rbindex 18b89f061ff..ec01f5119a5 100644--- a/spec/requests/groups_controller_spec.rb+++ b/spec/requests/groups_controller_spec.rb@@ -1175,7 +1175,9 @@ RSpec.describe GroupsController doexpect(response.status).to eq(422)expect(response.parsed_body["user_count"]).to eq(group.group_users.count)- expect(response.parsed_body["errors"].first).to include("update_existing_users")+ expect(response.parsed_body["errors"].first).to eq(+ I18n.t("groups.errors.update_existing_users_required", count: group.group_users.count),+ )expect(group_user1.reload.notification_level).to eq(NotificationLevels.all[:watching])expect(group_user2.reload.notification_level).to eq(NotificationLevels.all[:watching])diff --git a/spec/system/groups/group_spec.rb b/spec/system/groups/group_spec.rbindex a482e8357ab..a73657afd68 100644--- a/spec/system/groups/group_spec.rb+++ b/spec/system/groups/group_spec.rb@@ -87,4 +87,66 @@ describe "Group", type: :system doexpect(page).to have_current_path("/g")endend++ describe "update default notification level" do+ let(:default_notifications_modal) { PageObjects::Modals::GroupDefaultNotifications.new }+ fab!(:user1, :user)+ fab!(:user2, :user)++ before do+ group.update!(default_notification_level: NotificationLevels.all[:regular])+ group.add(user1)+ group.add(user2)+ end++ it "shows modal when changing notification level affects existing users" do+ group_page.visit(group)+ group_page.click_manage+ group_page.click_interaction+ group_page.select_default_notification_level(NotificationLevels.all[:tracking])+ group_page.click_save++ expect(default_notifications_modal).to be_open+ expect(default_notifications_modal).to have_content(+ I18n.t("js.groups.default_notifications.modal_description", count: 2),+ )+ end++ it "applies changes to existing users when clicking Yes" do+ group_page.visit(group)+ group_page.click_manage+ group_page.click_interaction+ group_page.select_default_notification_level(NotificationLevels.all[:tracking])+ group_page.click_save++ expect(default_notifications_modal).to be_open+ default_notifications_modal.click_yes++ expect(group_page).to have_saved+ expect(user1.group_users.find_by(group: group).reload.notification_level).to eq(+ NotificationLevels.all[:tracking],+ )+ expect(user2.group_users.find_by(group: group).reload.notification_level).to eq(+ NotificationLevels.all[:tracking],+ )+ end++ it "does not apply changes to existing users when clicking No" do+ original_level = user1.group_users.find_by(group: group).notification_level++ group_page.visit(group)+ group_page.click_manage+ group_page.click_interaction+ group_page.select_default_notification_level(NotificationLevels.all[:tracking])+ group_page.click_save++ expect(default_notifications_modal).to be_open+ default_notifications_modal.click_no++ expect(group_page).to have_saved+ expect(user1.group_users.find_by(group: group).reload.notification_level).to eq(+ original_level,+ )+ end+ endenddiff --git a/spec/system/page_objects/modals/group_default_notifications.rb b/spec/system/page_objects/modals/group_default_notifications.rbnew file mode 100644index 00000000000..dec8e5d4f0a--- /dev/null+++ b/spec/system/page_objects/modals/group_default_notifications.rb@@ -0,0 +1,15 @@+# frozen_string_literal: true++module PageObjects+ module Modals+ class GroupDefaultNotifications < PageObjects::Modals::Base+ def click_yes+ footer.find(".btn-primary").click+ end++ def click_no+ footer.find(".btn:not(.btn-primary)").click+ end+ end+ end+enddiff --git a/spec/system/page_objects/pages/group.rb b/spec/system/page_objects/pages/group.rbindex 3ee04b29389..7542ffb1563 100644--- a/spec/system/page_objects/pages/group.rb+++ b/spec/system/page_objects/pages/group.rb@@ -42,9 +42,27 @@ module PageObjectspage.find(".user-secondary-navigation li", text: "Membership").clickend+ def click_interaction+ page.find(+ ".user-secondary-navigation li",+ text: I18n.t("js.groups.manage.interaction.title"),+ ).click+ end+def click_savepage.find(".group-manage-save").clickend++ def has_saved?+ page.has_css?(".group-manage-save-button span", text: I18n.t("js.saved"))+ end++ def select_default_notification_level(level)+ select_kit =+ PageObjects::Components::SelectKit.new(".groups-form-default-notification-level")+ select_kit.expand+ select_kit.select_row_by_value(level)+ endendendend