Skip to content

Commit

Permalink
fix: Add error message when invited e-mail already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-laux committed May 17, 2019
1 parent a105e05 commit 237699d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
66 changes: 44 additions & 22 deletions imports/plugins/core/accounts/client/components/adminInviteForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _ from "lodash";
import { Components, registerComponent } from "@reactioncommerce/reaction-components";
import ReactionAlerts from "/imports/plugins/core/layout/client/templates/layout/alerts/inlineAlerts";
import { Reaction, i18next } from "/client/api";
import { getDefaultUserInviteGroup } from "../helpers/accountsHelper";
import { getDefaultUserInviteGroup, getUserByEmail } from "../helpers/accountsHelper";

/**
* @summary React component to display admin invite form
Expand Down Expand Up @@ -53,6 +53,30 @@ class AdminInviteForm extends Component {
alertArray: this.state.alertArray.filter((alert) => !_.isEqual(alert, oldAlert))
});

sendInvitation = (options) => Meteor.call("accounts/inviteShopMember", options, (error, result) => {
if (error) {
let messageKey;
// switching to use of package i18n keys (groupsInvite. namespace)
if (error.reason === "Unable to send invitation email.") {
messageKey = "admin.groupsInvite.unableToSendInvitationEmail";
} else if (error.reason === "cannot directly invite owner") {
messageKey = "admin.groupsInvite.inviteOwnerError";
} else if (error.reason === "cannot invite to group") {
messageKey = "admin.groupsInvite.cannotInvite";
} else if (error.reason === "Need to set a username or email") {
messageKey = "admin.groupsInvite.NeedToSetUsernameOrEmail";
} else {
messageKey = "admin.groupsInvite.errorSendingInvite";
}
ReactionAlerts.add(error.reason, "danger", Object.assign({}, alertOptions, { i18nKey: messageKey }));
}

if (result) {
this.setState({ name: "", email: "" });
Alerts.toast(i18next.t("accountsUI.info.invitationSent"), "success");
}
});

handleSubmit(event) {
event.preventDefault();
const { name, email, group, alertId } = this.state;
Expand All @@ -67,30 +91,28 @@ class AdminInviteForm extends Component {
);
}

const matchingAccount = getUserByEmail(email);
const options = { email, name, shopId: Reaction.getShopId(), groupId: group._id };
return Meteor.call("accounts/inviteShopMember", options, (error, result) => {
if (error) {
let messageKey;
// switching to use of package i18n keys (groupsInvite. namespace)
if (error.reason === "Unable to send invitation email.") {
messageKey = "admin.groupsInvite.unableToSendInvitationEmail";
} else if (error.reason === "cannot directly invite owner") {
messageKey = "admin.groupsInvite.inviteOwnerError";
} else if (error.reason === "cannot invite to group") {
messageKey = "admin.groupsInvite.cannotInvite";
} else if (error.reason === "Need to set a username or email") {
messageKey = "admin.groupsInvite.NeedToSetUsernameOrEmail";
} else {
messageKey = "admin.groupsInvite.errorSendingInvite";

if (matchingAccount) {
return Alerts.alert({
title: i18next.t("accountsUI.error.userWithEmailAlreadyExists"),
text: i18next.t("accountsUI.resendInvitationConfirm"),
type: "warning",
showCancelButton: true,
showCloseButton: true,
confirmButtonColor: "#98afbc",
cancelButtonColor: "#98afbc",
confirmButtonText: i18next.t("accountsUI.yes"),
cancelButtonText: i18next.t("app.cancel")
}, (isConfirm) => {
if (isConfirm) {
this.sendInvitation(options);
}
ReactionAlerts.add(error.reason, "danger", Object.assign({}, alertOptions, { i18nKey: messageKey }));
}
});
}

if (result) {
this.setState({ name: "", email: "" });
Alerts.toast(i18next.t("accountsUI.info.invitationSent"), "success");
}
});
return this.sendInvitation(options);
}

renderDropDownButton() {
Expand Down
11 changes: 11 additions & 0 deletions imports/plugins/core/accounts/client/helpers/accountsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ export function groupPermissions(packages) {
}, []);
}

/**
* @method getUserByEmail
* @memberOf Accounts
* @summary Returns a user that matches the
* @param email
* @returns {object}
*/
export function getUserByEmail(email) {
return Collections.Accounts.findOne({ "emails.address": email });
}

function getPermissionMap(permissions) {
const permissionMap = {};
permissions.forEach(({ label, permission }) => {
Expand Down
2 changes: 2 additions & 0 deletions private/data/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@
}
},
"accountsUI": {
"resendInvitationConfirm": "Do you want to send a new invitation?",
"yes": "Yes",
"usersOrders": "User's orders",
"oops": "Oops!",
"nonExistentAccount": "The user you're looking for does not exist.",
Expand Down

0 comments on commit 237699d

Please sign in to comment.