Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all Hooks with appEvents #4915

Merged
merged 46 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
359bba0
feat: change all "afterAccountsUpdate" hooks to "afterAccountUpdate"
aldeed Dec 17, 2018
4f7e821
feat: change afterAccountsInsert hook to afterAccountCreate app event
aldeed Dec 17, 2018
84a8c97
feat: change afterAccountsRemove hook to afterAccountDelete app event
aldeed Dec 17, 2018
b95d418
feat: implement consistent afterOrderUpdate app event
aldeed Dec 17, 2018
7f557d5
feat: replace afterRemoveProduct hook with afterProductDelete app event
aldeed Dec 17, 2018
9abd2e8
feat: remove afterPublishProductToCatalog hook in favor of app event
aldeed Dec 17, 2018
6b35364
fix: convert catalog hooks to app events
aldeed Dec 17, 2018
ba0943e
feat: remove afterSecurityInit hook
aldeed Dec 17, 2018
92b8ba2
feat: remove onCreateUser hook
aldeed Dec 17, 2018
b24d254
feat: remove onLogin hook
aldeed Dec 17, 2018
3e6d91e
feat: remove onImport hooks
aldeed Dec 17, 2018
8c7f21a
feat: remove beforeCoreInit and onCoreInit hooks
aldeed Dec 17, 2018
e293c29
feat: implement a standard way for plugins to add roles to groups
aldeed Dec 17, 2018
449faab
Merge branch 'develop' into feat-aldeed-remove-hooks
aldeed Jan 11, 2019
1c0f5f6
refactor: convert hook to afterOrderPaymentCapture event
aldeed Jan 11, 2019
4f8d003
refactor: refactor code and remove hooks
aldeed Jan 11, 2019
8980ddd
refactor: remove unused "beforeUpdateOrderWorkflow" hook
aldeed Jan 11, 2019
0f038a0
refactor: remove unused "afterInsertProduct" hook
aldeed Jan 11, 2019
ee04d50
refactor: convert "onJobServerStart" hook to "jobServerStart" app event
aldeed Jan 11, 2019
6c7f223
refactor: remove unused "onOrderRefundCreated" hook
aldeed Jan 11, 2019
4d65d8f
refactor: convert "afterCoreInit" hook to app event
aldeed Jan 11, 2019
a7c3e1f
refactor: replace "onGenerateSitemap" hook
aldeed Jan 11, 2019
2d04fbd
feat: remove @reactioncommerce/hooks NPM package
aldeed Jan 11, 2019
84efd7c
chore: remove never-used file
aldeed Jan 11, 2019
b579507
fix: await the findOne call
aldeed Jan 11, 2019
53b6257
fix: convert missed "afterCoreInit" hook
aldeed Jan 11, 2019
0cf7193
fix: add missing update selectors
aldeed Jan 11, 2019
52b743b
test: attempt to fix timing failures of app tests
aldeed Jan 11, 2019
6471d28
fix: update to bootstrap 3.4.0 to fix Snyk security warning
aldeed Jan 12, 2019
6173c9c
fix: make updatedFields param optional
aldeed Jan 15, 2019
1a1f6ea
feat: improve code in addressBookAdd
aldeed Jan 15, 2019
9e749de
fix: check for address being removed
aldeed Jan 15, 2019
de278eb
Merge branch 'develop' into feat-aldeed-remove-hooks
aldeed Jan 18, 2019
95dca1c
feat: remove MethodHooks feature
aldeed Jan 18, 2019
7d0c0d2
feat: change updatedAccount arg to account for afterAccountUpdate
aldeed Jan 18, 2019
1089250
feat: new and consistent arg for afterCartUpdate
aldeed Jan 18, 2019
3aa20a7
feat: improved afterCartCreate args
aldeed Jan 18, 2019
6b5f282
refactor: improved sendEmail event args
aldeed Jan 18, 2019
433310d
feat: add createdBy to afterShopCreate emit args
aldeed Jan 18, 2019
86fac68
refactor: update afterMedia event args
aldeed Jan 18, 2019
3b2bfbd
feat: improved afterOrderApprovePayment event args
aldeed Jan 18, 2019
3f25b85
feat: improved afterOrderCreate event args
aldeed Jan 18, 2019
05fb5b8
feat: updated afterNotificationCreate event args
aldeed Jan 18, 2019
80c3723
Merge branch 'develop' into feat-aldeed-remove-hooks
aldeed Jan 22, 2019
ab97849
fix: consume correct event
aldeed Jan 22, 2019
425c43d
chore: fix lint
aldeed Jan 22, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions imports/node-app/mockmeteor/meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import ReactionError from "@reactioncommerce/reaction-error";

export const Meteor = {
Error: ReactionError,
isAppTest: false,
isClient: false,
isServer: true,
isTest: false,
settings: {
public: {}
}
Expand Down
11 changes: 11 additions & 0 deletions imports/plugins/core/accounts/register.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Reaction from "/imports/plugins/core/core/server/Reaction";
import mutations from "./server/no-meteor/mutations";
import queries from "./server/no-meteor/queries";
import { registerPluginHandler } from "./server/no-meteor/registration";
import resolvers from "./server/no-meteor/resolvers";
import schemas from "./server/no-meteor/schemas";
import startup from "./server/no-meteor/startup";
import { ENROLL_URI_BASE } from "./server/util/getDataForEmail";

/**
Expand All @@ -16,6 +18,15 @@ Reaction.registerPackage({
name: "reaction-accounts",
icon: "fa fa-users",
autoEnable: true,
addRolesToGroups: [{
allShops: true,
groups: ["guest", "customer"],
roles: ["account/verify", "reset-password", ENROLL_URI_BASE]
}],
functionsByType: {
registerPluginHandler: [registerPluginHandler],
startup: [startup]
},
graphQL: {
resolvers,
schemas
Expand Down
24 changes: 4 additions & 20 deletions imports/plugins/core/accounts/server/init.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import Hooks from "@reactioncommerce/hooks";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import { Accounts, Groups } from "/lib/collections";
import { ENROLL_URI_BASE } from "./util/getDataForEmail";
import appEvents from "/imports/node-app/core/util/appEvents";
import sendVerificationEmail from "./util/sendVerificationEmail";

// set default admin user's account as "owner"
Hooks.Events.add("afterCreateDefaultAdminUser", (user) => {
const group = Groups.findOne({ slug: "owner", shopId: Reaction.getShopId() });
Accounts.update({ _id: user._id }, { $set: { groups: [group._id] } });
Hooks.Events.run("afterAccountsUpdate", null, {
accountId: user._id,
updatedFields: ["groups"]
});
});

Hooks.Events.add("afterCoreInit", () => {
Reaction.addRolesToGroups({
allShops: true,
groups: ["guest", "customer"],
roles: ["account/verify", "reset-password", ENROLL_URI_BASE]
});
appEvents.on("afterAddUnverifiedEmailToUser", ({ email, userId }) => {
sendVerificationEmail(userId, email);
});
27 changes: 15 additions & 12 deletions imports/plugins/core/accounts/server/methods/addressBookRemove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hooks from "@reactioncommerce/hooks";
import { check, Match } from "meteor/check";
import { Accounts } from "/lib/collections";
import appEvents from "/imports/node-app/core/util/appEvents";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";

Expand All @@ -27,6 +27,10 @@ export default function addressBookRemove(addressId, accountUserId) {
throw new ReactionError("access-denied", "Access denied");
}

const addressBeingRemoved = account.profile && Array.isArray(account.profile.addressBook) &&
account.profile.addressBook.find((addressBookItem) => addressId === addressBookItem._id);
if (!addressBeingRemoved) throw new ReactionError("not-found", "Address Not Found");

const updatedAccountResult = Accounts.update({
userId,
"profile.addressBook._id": addressId
Expand All @@ -38,17 +42,16 @@ export default function addressBookRemove(addressId, accountUserId) {
}
}, { bypassCollection2: true });

// forceIndex when removing an address
Hooks.Events.run("afterAccountsUpdate", userId, {
accountId: account._id,
updatedFields: ["forceIndex"]
});

// If the address remove was successful, then return the removed address
if (updatedAccountResult === 1) {
// Pull the address from the account before it was updated and return it
return account.profile.addressBook.find((removedAddress) => addressId === removedAddress._id);
if (updatedAccountResult !== 1) {
throw new ReactionError("server-error", "Unable to remove address from account");
}

throw new ReactionError("server-error", "Unable to remove address from account");
const updatedAccount = Accounts.findOne({ userId });
Promise.await(appEvents.emit("afterAccountUpdate", {
account: updatedAccount,
updatedBy: authUserId
}));

// If the address remove was successful, then return the removed address
return addressBeingRemoved;
}
30 changes: 13 additions & 17 deletions imports/plugins/core/accounts/server/methods/addressBookUpdate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import _ from "lodash";
import Hooks from "@reactioncommerce/hooks";
import { Meteor } from "meteor/meteor";
import { check, Match } from "meteor/check";
import { Accounts } from "/lib/collections";
import * as Schemas from "/lib/collections/schemas";
import appEvents from "/imports/node-app/core/util/appEvents";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";

Expand Down Expand Up @@ -77,6 +77,10 @@ export default function addressBookUpdate(address, accountUserId, type) {
userId
}, accountsUpdateQuery);

if (updatedAccountResult !== 1) {
throw new ReactionError("server-error", "Unable to update account address");
}

// Create an array which contains all fields that have changed
// This is used for search, to determine if we need to re-index
const updatedFields = [];
Expand All @@ -86,22 +90,14 @@ export default function addressBookUpdate(address, accountUserId, type) {
}
});

// Run afterAccountsUpdate hook to update Accounts Search
Hooks.Events.run("afterAccountsUpdate", userId, {
accountId: account._id,
const updatedAccount = Accounts.findOne({ userId });
Promise.await(appEvents.emit("afterAccountUpdate", {
account: updatedAccount,
updatedBy: authUserId,
updatedFields
});

// If the address update was successful, then return the full updated address
if (updatedAccountResult === 1) {
// Find the account
const updatedAccount = Accounts.findOne({
userId
});

// Pull the updated address and return it
return updatedAccount.profile.addressBook.find((updatedAddress) => address._id === updatedAddress._id);
}
}));

throw new ReactionError("server-error", "Unable to update account address");
// If the address update was successful, then return the full updated address.
// Since we just pushed into `profile.addressBook`, we know it will exist.
return updatedAccount.profile.addressBook.find((updatedAddress) => address._id === updatedAddress._id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check that updatedAccount.profile.addressBook exists here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer

}
12 changes: 8 additions & 4 deletions imports/plugins/core/accounts/server/methods/group/addUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Hooks from "@reactioncommerce/hooks";
import Logger from "@reactioncommerce/logger";
import { Roles } from "meteor/alanning:roles";
import { check } from "meteor/check";
import { Meteor } from "meteor/meteor";
import appEvents from "/imports/node-app/core/util/appEvents";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import { Accounts, Groups } from "/lib/collections";
Expand Down Expand Up @@ -81,10 +81,14 @@ export default function addUser(userId, groupId) {
try {
setUserPermissions({ _id: userId }, permissions, shopId);
Accounts.update({ userId }, { $set: { groups: newGroups } });
Hooks.Events.run("afterAccountsUpdate", loggedInUserId, {
accountId: account._id,

const updatedAccount = Accounts.findOne({ userId });
Promise.await(appEvents.emit("afterAccountUpdate", {
account: updatedAccount,
updatedBy: loggedInUserId,
updatedFields: ["groups"]
});
}));

if (slug === "owner") {
if (shopId === Reaction.getPrimaryShopId()) {
changeMarketplaceOwner({ userId, permissions });
Expand Down
14 changes: 9 additions & 5 deletions imports/plugins/core/accounts/server/methods/group/removeUser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hooks from "@reactioncommerce/hooks";
import Logger from "@reactioncommerce/logger";
import { check } from "meteor/check";
import appEvents from "/imports/node-app/core/util/appEvents";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import { Accounts, Groups } from "/lib/collections";
Expand Down Expand Up @@ -37,11 +37,15 @@ export default function removeUser(userId, groupId) {

try {
setUserPermissions(user, defaultCustomerGroupForShop.permissions, shopId);
Accounts.update({ _id: userId, groups: groupId }, { $set: { "groups.$": defaultCustomerGroupForShop._id } }); // replace the old id with new id
Hooks.Events.run("afterAccountsUpdate", Reaction.getUserId(), {
accountId: userId,
Accounts.update({ userId, groups: groupId }, { $set: { "groups.$": defaultCustomerGroupForShop._id } }); // replace the old id with new id

const updatedAccount = Accounts.findOne({ userId });
Promise.await(appEvents.emit("afterAccountUpdate", {
account: updatedAccount,
updatedBy: Reaction.getUserId(),
updatedFields: ["groups"]
});
}));

return defaultCustomerGroupForShop;
} catch (error) {
Logger.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default function markAddressValidationBypassed(value = true) {

const updatedCart = Cart.findOne({ accountId: account._id });
if (updatedCart) {
Promise.await(appEvents.emit("afterCartUpdate", updatedCart));
Promise.await(appEvents.emit("afterCartUpdate", {
cart: updatedCart,
updatedBy: userId
}));
}

return updateResult;
Expand Down
Loading