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

Skip to content

Conversation

@aldeed
Copy link
Contributor

@aldeed aldeed commented Mar 8, 2019

Part of #4999
Impact: minor
Type: feature

Changes

A new addOrderFulfillmentGroup mutation allows you to add a fulfillment group to an existing order. You can provide new items for it and/or move items that are in another existing group.

The operator UI does not yet implement this.

  • addOrderFulfillmentGroup can be called internally to synchronize from an external system by setting context.isInternalCall to true
  • addOrderFulfillmentGroup can be called through GraphQL by any user with "orders" permission for the shop that owns the order
  • All tax, shipping, surcharge, and total calculations are rerun after adjusting the existing order groups and adding the new group.

Breaking changes

None

Testing

  1. Place an order.
  2. Verify that the mutation works as described.
  3. Verify that you can add a new fulfillment group when authenticated as a user with "orders" role, but not when authenticated as someone else or when not authenticated.

Specific Instructions

  1. Place an order with multiple items, and note the reference (displayed) ID.
  2. Go to http://localhost:3000 and log in as an admin with "orders" permission.
  3. In the browser console enter localStorage.getItem("Meteor.loginToken"). Copy the printed token.
  4. Go to http://localhost:3000/graphql-alpha in browser
  5. Expand HTTP HEADERS box and add "meteor-login-token": "JW6NV92VfuqlrhD1M1IbBovf2ELktkhGBPeMxW6y4bj" (the token you copied) property
  6. Use the primaryShop query to get the primary shop ID
  7. Use the following query to get your order by its reference ID and the primary shop ID
query {
  orderByReferenceId(id: "DEfA7uGPhri4rLvhZ", shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==") {
    _id
    totalItemQuantity
    fulfillmentGroups {
      totalItemQuantity
      items {
        nodes {
          _id
          title
          variantTitle
          price {
            amount
          }
          productConfiguration {
            productId
            productVariantId
          }
        }
      }
      selectedFulfillmentOption {
        fulfillmentMethod {
          _id
        }
      }
    }
  }
}
  1. Then use this mutation to add another group with some items and move one item from the other group:
mutation {
  addOrderFulfillmentGroup(
    input: {
      orderId: "cmVhY3Rpb24vb3JkZXI6Zk1ySHJpRnFQRGlod21yOEM=",
      fulfillmentGroup: {
        data: {
          shippingAddress: {
            fullName: "Abe Lincoln",
            address1: "2110 Main St",
            address2: "Suite 207",
            city: "Santa Monica",
            region: "CA",
            postal: "90405",
            country: "US",
            phone: "5551234567",
            isCommercial: false
          }
        }
        selectedFulfillmentMethodId: "cmVhY3Rpb24vZnVsZmlsbG1lbnRNZXRob2Q6Nk11Z0NmM1BuNXJwZk5rZTI=",
        shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==",
        type: shipping,
        items: [{
          price: 24.99,
          quantity: 1,
          productConfiguration: {
            productId: "cmVhY3Rpb24vcHJvZHVjdDpxdHA3UmNpYXVpZ3Y2TmVSVw==",
            productVariantId: "cmVhY3Rpb24vcHJvZHVjdDpLRm12V3ZrZkxqbTNRZEdtRQ=="
          }
        },
        {
          price: 24.99,
          quantity: 1,
          productConfiguration: {
            productId: "cmVhY3Rpb24vcHJvZHVjdDpxdHA3UmNpYXVpZ3Y2TmVSVw==",
            productVariantId: "cmVhY3Rpb24vcHJvZHVjdDpLRm12V3ZrZkxqbTNRZEdtRQ=="
          }
        }]
      }
    }
  ){
    order {
      _id
      fulfillmentGroups {
        _id
        items {
          edges {
            node {
              _id
            }
          }
        }
      }
    }
  }
}

Be sure to copy all of the IDs from your orderByReferenceId query. Use a price that is the correct price for the product+variant combination. Use the same selectedFulfillmentOption.fulfillmentMethod._id as the existing group.

@aldeed aldeed self-assigned this Mar 8, 2019
@aldeed aldeed added this to the πŸ” Uncompahgre milestone Mar 8, 2019
@aldeed aldeed force-pushed the feat-aldeed-add-order-group-mutation branch from 01ac097 to 3142561 Compare March 8, 2019 22:09
@aldeed aldeed marked this pull request as ready for review March 8, 2019 22:10
@aldeed aldeed requested a review from kieckhafer March 8, 2019 22:10
Signed-off-by: Eric Dobbertin <[email protected]>
@aldeed
Copy link
Contributor Author

aldeed commented Mar 8, 2019

@kieckhafer This is now top priority of the order mutation PRs. After this is merged, it should be quick to use these same util functions to fix the issues in the other two PRs.

@kieckhafer
Copy link
Member

kieckhafer commented Mar 9, 2019

Could you add an example query here? I'm having some trouble trying to figure out what's exactly required, especially with all the fulfillmentGroup data...

This is what I'm using as my mutation query:

mutation {
  addOrderFulfillmentGroup(
    input: {
      orderId: "cmVhY3Rpb24vb3JkZXI6UGJnZUc1NnZodHVvTWF3NFI=",
      moveItemIds: ["cmVhY3Rpb24vb3JkZXJJdGVtOkEzN01BaWpNTG5hN0VpOFFl"],
      fulfillmentGroup: {
        selectedFulfillmentMethodId: "cmVhY3Rpb24vb3JkZXJGdWxmaWxsbWVudEdyb3VwOlJNeHFtajZNaHFuTXZOaVdo",
        shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==",
        type: shipping
      }
    }
  ){
    order {
      _id
      fulfillmentGroups {
        _id
        items {
          edges {
            node {
              _id
            }
          }
        }
      }
    }
  }
}

I'm getting this error:

{
  "errors": [
    {
      "message": "Cannot read property 'map' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addOrderFulfillmentGroup"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property 'map' of undefined",
            "    at decodeCartItemsOpaqueIds (imports/plugins/core/graphql/server/no-meteor/xforms/cart.js:29:10)",
            "    at Promise.asyncApply (imports/plugins/core/orders/server/no-meteor/resolvers/Mutation/addOrderFulfillmentGroup.js:32:14)",
            "    at /home/node/.meteor/packages/promise/.0.11.2.1gwulfb.vjny++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40"
          ]
        }
      },
      "errorId": "cjt0qgspa000e68pbftmi98oo",
      "type": "unknown"
    }
  ],
  "data": null
}

Aside from maybe having the IDs wrong (which I think woudl give me a different error), any idea what it might be? It is using / trying to use decodeCartItemsOpaqueIds, should this be an orderItems thing instead or cart?

@aldeed
Copy link
Contributor Author

aldeed commented Mar 11, 2019

@kieckhafer I think this was due to the new group having no items, which I hadn't tested but is supposed to work. I've added a test and pushed some fixes now.

Despite the name, decodeCartItemsOpaqueIds should work fine for order items because it is only decoding the product IDs, and that schema is the same for cart and order items.

@kieckhafer
Copy link
Member

@aldeed made it past the error I was seeing before, at this point now:

mutation {
  addOrderFulfillmentGroup(
    input: {
      orderId: "cmVhY3Rpb24vb3JkZXI6UGJnZUc1NnZodHVvTWF3NFI=",
      moveItemIds: ["cmVhY3Rpb24vb3JkZXJJdGVtOkEzN01BaWpNTG5hN0VpOFFl"],
      fulfillmentGroup: {
        selectedFulfillmentMethodId: "cmVhY3Rpb24vZnVsZmlsbG1lbnRNZXRob2Q6Skg1NGFOY2tvOEhwZTRzZFg=",
        shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==",
        type: shipping,
        items: {
          price: 2500.00,
          quantity: 1,
          productConfiguration: {
            productId: "cmVhY3Rpb24vcHJvZHVjdDpCQ1RNWjZIVHhGU3BwSkVTaw==",
            productVariantId: "cmVhY3Rpb24vcHJvZHVjdDpDSm9SQm05dlJyb3JjOW14Wg=="
          }
        }
        
      }
    }
  ){
    order {
      _id
      fulfillmentGroups {
        _id
        items {
          edges {
            node {
              _id
            }
          }
        }
      }
    }
  }
}

Returns the error:

{
  "errors": [
    {
      "message": "Cannot read property '_id' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addOrderFulfillmentGroup"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: Cannot read property '_id' of undefined",
            "    at rates.find.rate (imports/plugins/core/orders/server/no-meteor/util/addShipmentMethodToGroup.js:40:3)",
            "    at Array.find (native)",
            "    at Promise.asyncApply (imports/plugins/core/orders/server/no-meteor/util/addShipmentMethodToGroup.js:40:37)",
            "    at 
....

Seems to be stemming from rates needed here: https://github.com/reactioncommerce/reaction/blob/feat-aldeed-add-order-group-mutation/imports/plugins/core/orders/server/no-meteor/util/addShipmentMethodToGroup.js#L40

Seems like there is a lot of data needed to pass to this GQL query in order to make it work. It'll probably be easily accessible in the context of make the query in the Admin, but trying to figure out what's needed without having it available is a bit tough. Do you have an example of a query you used with all the needed data?

aldeed added 3 commits March 11, 2019 13:42
in addShipmentMethodToGroup

Signed-off-by: Eric Dobbertin <[email protected]>
items from fulfillment group

Signed-off-by: Eric Dobbertin <[email protected]>
@aldeed
Copy link
Contributor Author

aldeed commented Mar 11, 2019

Updated the description with more specific steps and mutation example.

Copy link
Member

@kieckhafer kieckhafer left a comment

Choose a reason for hiding this comment

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

I am able to continuously add Fulfillment groups, with no limit, just by running the mutation over and over.

I started out with an order with 1 fulfillment group containing 2 items, each with a quantity of 5 (10 quantity overall).

I created a new fulfillmentGroup using this mutation and moved 1 item of 1 quantity over, and then had 2 fulfillmentGroups, one with the original items, now quantiies 5& 4, and one with the new item, quantity 1. This worked as expected.

Now the problem started. I just kept running the mutation over and over, without changing any of the input, and was able to create unlimited new fulfillmentGroups with the single item, quantity 1.

So after clicking it 16 times, I ended up with 17 fulfillmentGroups:

  • The original group, now with two items, one quantity 5 and one quantity 4`.
  • 16 new fulfillmentGroups, each with one item, quantity 1.

The good news is that the order in general updated as it should when we split it up, so all those functions are working. I now had an order with 24 items in it:
order_json

https://gist.github.com/kieckhafer/4f7eb122cee4b744e72f3134c5cbdafc

This is the mutation I ran:

mutation {
  addOrderFulfillmentGroup(
    input: {
      orderId: "cmVhY3Rpb24vb3JkZXI6UGJnZUc1NnZodHVvTWF3NFI=",
      moveItemIds: ["cmVhY3Rpb24vb3JkZXJJdGVtOkEzN01BaWpNTG5hN0VpOFFl"],
      fulfillmentGroup: {
        data: {
          shippingAddress: {
            fullName: "Eric Dobbertin",
            address1: "2110 Main St",
            address2: "Suite 207",
            city: "Santa Monica",
            region: "CA",
            postal: "90405",
            country: "US",
            phone: "6083209547",
            isCommercial: false
          }
        }
        selectedFulfillmentMethodId: "cmVhY3Rpb24vZnVsZmlsbG1lbnRNZXRob2Q6Skg1NGFOY2tvOEhwZTRzZFg=",
        shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==",
        type: shipping,
        items: {
          price: 2500.00,
          quantity: 1,
          productConfiguration: {
            productId: "cmVhY3Rpb24vcHJvZHVjdDpCQ1RNWjZIVHhGU3BwSkVTaw==",
            productVariantId: "cmVhY3Rpb24vcHJvZHVjdDpDSm9SQm05dlJyb3JjOW14Wg=="
          }
        }
        
      }
    }
  ){
    order {
      _id
      fulfillmentGroups {
        _id
        items {
          edges {
            node {
              _id
            }
          }
        }
      }
    }
  }
}

@kieckhafer
Copy link
Member

See above... one big issue, but if that gets resolved, unless it triggers something else, it should be good to go.

@aldeed
Copy link
Contributor Author

aldeed commented Mar 12, 2019

@kieckhafer This was confusing to me at first, but I think it's actually working as expected. Since you keep sending the same mutation, with the same order item ID in moveItemIds array, it just keeps moving it. It does (1) create new group with the new item you are providing in it, followed by (2) move order item "A37MAijMLna7Ei8Qe" into that group, giving it a total of 2 items. Then you do it again and order item "A37MAijMLna7Ei8Qe" moves into that new group, leaving the previous new group with just the one item now.

Can you confirm that's what's happening? If so, it sounds correct.

@aldeed
Copy link
Contributor Author

aldeed commented Mar 12, 2019

If you were to remove the items from the new group input, it should fail after the first time. Because now you'd be moving the item out of a group that has only that one item, which is not allowed.

@aldeed aldeed requested a review from kieckhafer March 12, 2019 18:24
@kieckhafer
Copy link
Member

kieckhafer commented Mar 12, 2019

Keeping my original comment for posterity, but what was happening below is what is supposed to happen. For clarification, I was moving an item from one group to another, PLUS adding an additional item. I was basically doing both functions of this mutation at once.


Makes sense what you said above, but something is still off... either that, or I'm not completely understanding what this mutation should be doing. My thought it splitting an item quantity off a group should move a certain quantity that item to a new group, but keep the original item in the original group, with a decremented quantity.

...

I created an order that was 1 item of quantity 5 Red variants, and 1 item of quantity 5 Green variants. I then used the mutation to split 1 green variant into it's own Fulfillment group.

What should happen is I have one fulfillment group with 1 item of quantity 5 Red variants, and 1 item of quantity 4 green variants, and then a second fulfillment group that is 1 item of quantity 4 green variants. Is that correct?

What happened was I now have a fulfillment group that has 1 item of quantity 5 Red variants (this seems correct), but also a fulfillment group that has 1 item of quantity 5 Green, as well as 1 item of quantity 1 green.

MUTATION:

mutation {
  addOrderFulfillmentGroup(
    input: {
      orderId: "cmVhY3Rpb24vb3JkZXI6Y0Z1dlFIZFJtNTR0RXlzanc=",
      moveItemIds: ["cmVhY3Rpb24vb3JkZXJJdGVtOktmdmVZblJETWlvdEJmSjY2"],
      fulfillmentGroup: {
        data: {
          shippingAddress: {
            fullName: "Erik K New Group",
            address1: "2110 Main St",
            address2: "Suite 207",
            city: "Santa Monica",
            region: "CA",
            postal: "90405",
            country: "US",
            phone: "6083209547",
            isCommercial: false
          }
        }
        selectedFulfillmentMethodId: "cmVhY3Rpb24vZnVsZmlsbG1lbnRNZXRob2Q6b0F3M01CRnNwUm5oQ0N4R3E=",
        shopId: "cmVhY3Rpb24vc2hvcDpKOEJocTN1VHRkZ3daeDNyeg==",
        type: shipping,
        items: {
          price: 12.99,
          quantity: 1,
          productConfiguration: {
            productId: "cmVhY3Rpb24vcHJvZHVjdDpCQ1RNWjZIVHhGU3BwSkVTaw==",
            productVariantId: "cmVhY3Rpb24vcHJvZHVjdDpDSm9SQm05dlJyb3JjOW14Wg=="
          }
        }
        
      }
    }
  ){
    order {
      _id
      fulfillmentGroups {
        _id
        items {
          edges {
            node {
              _id
            }
          }
        }
      }
    }
  }
}

Reaction___Checkout

Robo_3T_-_1_1

Robo_3T_-_1_1

Robo_3T_-_1_1

* items array before calculating shipping, tax, surcharges, and totals.
* @param {Object} [billingAddress] The primary billing address for the order, if known
* @param {String|null} [cartId] ID of the cart from which the order is being placed, if applicable
* @param {Object} currencyCode Currency code for all money values
Copy link
Member

Choose a reason for hiding this comment

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

Should currencyCode be a string instead of an `object?

GraphiQL

@@ -0,0 +1,52 @@
import xformOrderGroupToCommonOrder from "./xformOrderGroupToCommonOrder";

/**
Copy link
Member

Choose a reason for hiding this comment

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

You mention we don't do surcharges per group in other changes here: https://github.com/reactioncommerce/reaction/pull/5027/files#diff-8ce970cbd524f077298082ab81660a4bR107

Is this still the case even with this new code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My comment should be clearer. We calculate them per group but they're only stored on the whole order, so we merge them all into one array. That's still true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible that's not correct, but I'd have to add surcharges to fulfillment group schema if we want to change it. Seems like something we can figure out later, when surcharges are more in use.

@kieckhafer kieckhafer merged commit 7f5f595 into develop Mar 12, 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