Skip to content

Commit

Permalink
Fix error when inviting already invited user (#2433)
Browse files Browse the repository at this point in the history
* Refactor invite function to send invites only to non invited members

* Refactor error message to show status code

* Change the error message to use inline Alert

* Clear input fields when there is error

* Use Reaction's blaze inlineAlerts for error message

* Eliminate console error on inviteShopMember call
  • Loading branch information
Awa Desmoline authored and brent-hoover committed Jun 22, 2017
1 parent 3f252fe commit 543ddc0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 67 deletions.
1 change: 1 addition & 0 deletions client/modules/accounts/templates/members/memberForm.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template name="memberForm">
<div class="panel panel-default">
<div class="panel-body">
{{> inlineAlerts placement="memberform"}}
<form class="">
<div class="form-group">
<label for="member-form-name"><span data-i18n="accountsUI.name">Name</span></label>
Expand Down
13 changes: 10 additions & 3 deletions client/modules/accounts/templates/members/memberForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Reaction, i18next } from "/client/api";
import { Meteor } from "meteor/meteor";
import { Template } from "meteor/templating";


/**
* memberForm events
*
Expand All @@ -19,16 +20,22 @@ Template.memberForm.events({
let message;
if (error.reason === "Unable to send invitation email.") {
message = i18next.t("accountsUI.error.unableToSendInvitationEmail");
} else if (error.reason === "A user with this email address already exists") {
message = i18next.t("accountsUI.error.userWithEmailAlreadyExists");
} else if (error.reason !== "") {
message = error;
} else {
message = `${i18next.t("accountsUI.error.errorSendingEmail")
} ${error}`;
}
Alerts.toast(message, "error", {
html: true,
timeout: 10000

Alerts.inline(message, "warning", {
placement: "memberform",
autoHide: 1000
});

template.$("input[type=text], input[type=email]").val("");

return false;
}
if (result) {
Expand Down
1 change: 1 addition & 0 deletions private/data/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@
"emailDoesntMatchTheCriteria": "Email doesn't match the criteria.",
"errorSendingEmail": "Error sending email, possible configuration issue.",
"unableToSendInvitationEmail": "Unable to send invitation email.",
"userWithEmailAlreadyExists": "A user with this email address already exists",
"failedToLogout": "Failed to logout.",
"invalidLoginToken": "Invalid login token",
"loginForbidden": "Login forbidden",
Expand Down
123 changes: 59 additions & 64 deletions server/methods/accounts/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,64 @@ export function inviteShopMember(shopId, email, name) {
currentUserName = "Admin";
}

// Compile Email with SSR
const tpl = "accounts/inviteShopMember";
const subject = "accounts/inviteShopMember/subject";
SSR.compileTemplate(tpl, Reaction.Email.getTemplate(tpl));
SSR.compileTemplate(subject, Reaction.Email.getSubject(tpl));

// Get shop logo, if available. If not, use default logo from file-system
let emailLogo;
if (Array.isArray(shop.brandAssets)) {
const brandAsset = _.find(shop.brandAssets, (asset) => asset.type === "navbarBrandImage");
const mediaId = Media.findOne(brandAsset.mediaId);
emailLogo = path.join(Meteor.absoluteUrl(), mediaId.url());
} else {
emailLogo = Meteor.absoluteUrl() + "resources/email-templates/shop-logo.png";
}

const token = Random.id();

const dataForEmail = {
// Shop Data
shop: shop,
contactEmail: shop.emails[0].address,
homepage: Meteor.absoluteUrl(),
emailLogo: emailLogo,
copyrightDate: moment().format("YYYY"),
legalName: shop.addressBook[0].company,
physicalAddress: {
address: shop.addressBook[0].address1 + " " + shop.addressBook[0].address2,
city: shop.addressBook[0].city,
region: shop.addressBook[0].region,
postal: shop.addressBook[0].postal
},
shopName: shop.name,
socialLinks: {
display: true,
facebook: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/facebook-icon.png",
link: "https://www.facebook.com"
},
googlePlus: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/google-plus-icon.png",
link: "https://plus.google.com"
},
twitter: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/twitter-icon.png",
link: "https://www.twitter.com"
}
},
// Account Data
user: Meteor.user(),
currentUserName,
invitedUserName: name,
url: MeteorAccounts.urls.enrollAccount(token)
};

const user = Meteor.users.findOne({
"emails.address": email
});
Expand All @@ -515,84 +573,21 @@ export function inviteShopMember(shopId, email, name) {
throw new Error("Can't find user");
}

const token = Random.id();

Meteor.users.update(userId, {
$set: {
"services.password.reset": { token, email, when: new Date() },
"name": name
}
});

// Get shop logo, if available. If not, use default logo from file-system
let emailLogo;
if (Array.isArray(shop.brandAssets)) {
const brandAsset = _.find(shop.brandAssets, (asset) => asset.type === "navbarBrandImage");
const mediaId = Media.findOne(brandAsset.mediaId);
emailLogo = path.join(Meteor.absoluteUrl(), mediaId.url());
} else {
emailLogo = Meteor.absoluteUrl() + "resources/email-templates/shop-logo.png";
}

const dataForEmail = {
// Shop Data
shop: shop,
contactEmail: shop.emails[0].address,
homepage: Meteor.absoluteUrl(),
emailLogo: emailLogo,
copyrightDate: moment().format("YYYY"),
legalName: shop.addressBook[0].company,
physicalAddress: {
address: shop.addressBook[0].address1 + " " + shop.addressBook[0].address2,
city: shop.addressBook[0].city,
region: shop.addressBook[0].region,
postal: shop.addressBook[0].postal
},
shopName: shop.name,
socialLinks: {
display: true,
facebook: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/facebook-icon.png",
link: "https://www.facebook.com"
},
googlePlus: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/google-plus-icon.png",
link: "https://plus.google.com"
},
twitter: {
display: true,
icon: Meteor.absoluteUrl() + "resources/email-templates/twitter-icon.png",
link: "https://www.twitter.com"
}
},
// Account Data
user: Meteor.user(),
currentUserName,
invitedUserName: name,
url: MeteorAccounts.urls.enrollAccount(token)
};

// Compile Email with SSR
const tpl = "accounts/inviteShopMember";
const subject = "accounts/inviteShopMember/subject";
SSR.compileTemplate(tpl, Reaction.Email.getTemplate(tpl));
SSR.compileTemplate(subject, Reaction.Email.getSubject(tpl));

Reaction.Email.send({
to: email,
from: `${shop.name} <${shop.emails[0].address}>`,
subject: SSR.render(subject, dataForEmail),
html: SSR.render(tpl, dataForEmail)
});
} else {
Reaction.Email.send({
to: email,
from: `${shop.name} <${shop.emails[0].address}>`,
subject: SSR.render(subject, dataForEmail),
html: SSR.render(tpl, dataForEmail)
});
throw new Meteor.Error("409", "A user with this email address already exists");
}
return true;
}
Expand Down

0 comments on commit 543ddc0

Please sign in to comment.