Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kieckhafer
Copy link
Member

@kieckhafer kieckhafer commented Oct 7, 2019

Resolves #5691
Impact: minor
Type: feature|style|refactor|chore

Remove or refactor all code that calls appEvents from meteor code.

Refactored into mutations

Removed

  • updateShipmentStatus I don't believe this function is called anywhere in the app - it doesn't even seem to be attached to a meteor method anymore - so we can mostly likely just delete this file
  • workflow/pushItemWorkflow We want to remove coreItemWorkflow anyway, maybe this is the time? In addition, this is called from the checkout plugin, which I don't believe we don't use anymore, so maybe we remove this plugin?

TODO

  • shop/createShop I don't believe this is used anymore, we can most likely just delete this function, along with the Blaze Template where it's being called.

Testing

  1. See that the app functions as expected
  2. See that discount code applying and removing works with new mutations. We have no UI for this at the moment, so you need to do it by creating a Cart, then taking that cart data and using GraphiQL or another GQL interface to perform the action
mutation {
  applyDiscountCodeToCart(input: {
    cartId: "ENCODED_CART_ID",
    discountCode: "CODE",
    shopId: "SHOP_ID",
    token: null
  }) {
    clientMutationId
  }
}

A discount code is a payment, so the discount code id should be encoded with reaction/payment

mutation {
  removeDiscountCodeFROMCart(input: {
    cartId: "ENCODED_CART_ID",
    discountCodeID: "ENCODED_PAYMENT_ID",
    shopId: "SHOP_ID",
    token: null
  }) {
    clientMutationId
  }
}

@kieckhafer kieckhafer requested a review from aldeed October 8, 2019 05:05
@kieckhafer
Copy link
Member Author

@aldeed The following Meteor methods have been removed or refactored to no longer use appEvents:
discounts/codes/apply
discounts/codes/remove
updateShipmentStatus
workflow/pushItemWorkflow

I believe this only leaves shop/createShop as the last place where we use appEvents in Meteor. I forgot if we said that was being addressed in the accounts PR or not.

I think either way, this can be reviewed / merged to incorporate all the other changes, and we can handle shop/createShop separately, so I'd say this is ready for a review.

@kieckhafer kieckhafer marked this pull request as ready for review October 8, 2019 05:07
Signed-off-by: Erik Kieckhafer <[email protected]>
// Should it be `discounts`, or `cart`?
if (!userHasPermission(["owner", "admin", "discounts"], shopId)) {
throw new ReactionError("access-denied", "Access Denied");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since discounts is more for defining/editing discounts, I'd create a new role discounts/apply for here.

// How do we determine this check if the user is the cart owner?
if (!userHasPermission(["admin", "owner", "discounts"], shopId)) {
throw new ReactionError("access-denied", "Access Denied");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This one seems unnecessary given that getCart will already limit to carts that belong to the account/token, and then you have a separate admin check below it.


if (participatingShops.length > 1) {
throw new ReactionError("not-implemented", "discounts.multiShopError", "Discounts cannot be applied to a multi-shop cart or order");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nothing from line 63 down to here is necessary. Carts can no longer contain items from multiple shops.


// validate basic limit handling
if (accountLimitExceeded === true || discountLimitExceeded === true) {
return { i18nKeyLabel: "Code is expired", i18nKey: "discounts.codeIsExpired" };
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to throw a ReactionError and let the client translate the error to the i18n key.

// How do we determine this check if the user is the cart owner?
if (!userHasPermission(["admin", "owner", "discounts"], shopId)) {
throw new ReactionError("access-denied", "Access Denied");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as other file. This check doesn't seem necessary, and the later check can use a new discounts/apply. Doesn't seem necessary to have a separate discounts/remove role. If you can apply, you should be able to remove, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that makes sense. Will keep discounts/apply

// Should it be `discounts`, or `cart`?
if (!userHasPermission(["owner", "admin", "discounts"], shopId)) {
throw new ReactionError("access-denied", "Access Denied");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you already have shopId, you can move this permission check above the Cart.findOne, but be sure to pass shopId into the findOne query.

// object from the existing cart, then pass to `saveCart`
// to re-run cart through all transforms and validations.
const updatedCartBilling = cart.billing.filter((doc) => doc._id !== discountCodeId);
cart.billing = updatedCartBilling;
Copy link
Contributor

Choose a reason for hiding this comment

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

You could add a check for whether cart.billing.length and updatedCartBilling.length are the same. If so, the discount code ID was invalid so throw an error.


const updatedCartWithRemovedDiscountCode = await context.mutations.removeDiscountCodeFromCart(context, {
cartId: decodeCartOpaqueId(cartId),
discountCodeId: decodePaymentOpaqueId(discountCodeId),
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though we treat these as payments in the database, I'd make a separate namespace for it (reaction/discountCode) to keep it future-proof.

@kieckhafer
Copy link
Member Author

@aldeed comments addressed.

and rename discountCodeId to discountId

Signed-off-by: Eric Dobbertin <[email protected]>
@aldeed aldeed merged commit 3edd291 into develop Oct 8, 2019
@aldeed aldeed deleted the refactor-kieckhafer-movePackageToNodeApp-cleanupTasks-appEvents branch October 8, 2019 19:26
@willopez willopez mentioned this pull request Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants