Skip to content

Commit

Permalink
Merge pull request #5820 from trojanh/refactor-4642-make-cartToken-co…
Browse files Browse the repository at this point in the history
…nsistent

refactor: cart token param name consistent to `cartToken`
  • Loading branch information
willopez committed Dec 6, 2019
2 parents 7c80262 + 2f0d3df commit 474c70a
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`when missing anonymousCartId, throws 1`] = `"anonymousCartId is required"`;

exports[`when missing anonymousCartToken, throws 1`] = `"anonymousCartToken is required"`;
exports[`when missing cartToken, throws 1`] = `"cartToken is required"`;

exports[`when no matching anonymous cart is found, throws 1`] = `"Anonymous cart not found"`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`throws when no account and no token passed 1`] = `"A token is required when updating an anonymous cart"`;
exports[`throws when no account and no cartToken passed 1`] = `"A cartToken is required when updating an anonymous cart"`;
6 changes: 3 additions & 3 deletions src/core-services/cart/mutations/addCartItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import addCartItemsUtil from "../util/addCartItems.js";
* optionally retry with the corrected price or quantity.
*/
export default async function addCartItems(context, input, options = {}) {
const { cartId, items, token } = input;
const { cartId, items, cartToken } = input;
const { collections, accountId = null } = context;
const { Cart } = collections;

Expand All @@ -26,11 +26,11 @@ export default async function addCartItems(context, input, options = {}) {
selector = { _id: cartId, accountId };
} else {
// Anonymous cart
if (!token) {
if (!cartToken) {
throw new ReactionError("not-found", "Cart not found");
}

selector = { _id: cartId, anonymousAccessToken: hashToken(token) };
selector = { _id: cartId, anonymousAccessToken: hashToken(cartToken) };
}

const cart = await Cart.findOne(selector);
Expand Down
2 changes: 1 addition & 1 deletion src/core-services/cart/mutations/addCartItems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test("add an item to an existing anonymous cart", async () => {
const result = await addCartItems(mockContext, {
cartId: "cartId",
items,
token: "TOKEN"
cartToken: "TOKEN"
});

expect(result).toEqual({
Expand Down
8 changes: 4 additions & 4 deletions src/core-services/cart/mutations/reconcileCarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ import reconcileCartsMerge from "./reconcileCartsMerge.js";
* @param {Object} context - an object containing the per-request state
* @param {Object} input - mutation input
* @param {String} input.anonymousCartId - The anonymous cart ID
* @param {String} input.anonymousCartToken - The anonymous cart token
* @param {String} input.cartToken - The anonymous cart token
* @param {String} [input.mode] - The reconciliation mode, "merge", "keepAccountCart", or "keepAnonymousCart". Default "merge"
* @returns {Promise<Object>} Object in which `cart` property is set to the updated account cart
*/
export default async function reconcileCarts(context, input) {
const { accountId, collections, user } = context;
const { Cart } = collections;
const { anonymousCartId, anonymousCartToken, mode = "merge" } = input;
const { anonymousCartId, cartToken, mode = "merge" } = input;

if (!accountId) throw new ReactionError("access-denied", "Access Denied");
if (!anonymousCartId) throw new ReactionError("invalid-param", "anonymousCartId is required");
if (!anonymousCartToken) throw new ReactionError("invalid-param", "anonymousCartToken is required");
if (!cartToken) throw new ReactionError("invalid-param", "cartToken is required");

const accountCartSelector = { accountId };
const anonymousCartSelector = { _id: anonymousCartId, anonymousAccessToken: hashToken(anonymousCartToken) };
const anonymousCartSelector = { _id: anonymousCartId, anonymousAccessToken: hashToken(cartToken) };

const carts = await Cart.find({
$or: [accountCartSelector, anonymousCartSelector]
Expand Down
22 changes: 11 additions & 11 deletions src/core-services/cart/mutations/reconcileCarts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock("./reconcileCartsMerge", () => jest.fn().mockImplementation(() => Prom

const accountId = "accountId";
const anonymousCartId = "anonymousCartId";
const anonymousCartToken = "anonymousCartToken";
const cartToken = "cartToken";
const shopId = "shopId";

const mockCarts = [
Expand All @@ -34,7 +34,7 @@ test("when mode is keepAccountCart, returns the result of reconcileCartsKeepAcco

const result = await reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
mode: "keepAccountCart",
shopId
});
Expand All @@ -49,7 +49,7 @@ test("when mode is keepAnonymousCart, returns the result of reconcileCartsKeepAn

const result = await reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
mode: "keepAnonymousCart",
shopId
});
Expand All @@ -64,7 +64,7 @@ test("when mode is merge, returns the result of reconcileCartsMerge", async () =

const result = await reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
mode: "merge",
shopId
});
Expand All @@ -79,7 +79,7 @@ test("when mode is undefined, returns the result of reconcileCartsMerge", async

const result = await reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
shopId
});

Expand All @@ -93,7 +93,7 @@ test("when there is no account cart yet, returns the result of convertAnonymousC

const result = await reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
mode: "keepAccountCart",
shopId
});
Expand All @@ -108,7 +108,7 @@ test("when not authenticated, throw access denied", async () => {

const promise = reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
shopId
});

Expand All @@ -120,14 +120,14 @@ test("when missing anonymousCartId, throws", async () => {
mockContext.user = { accountId, roles: ["guest"] };

const promise = reconcileCarts(mockContext, {
anonymousCartToken,
cartToken,
shopId
});

return expect(promise).rejects.toThrowErrorMatchingSnapshot();
});

test("when missing anonymousCartToken, throws", async () => {
test("when missing cartToken, throws", async () => {
mockContext.accountId = accountId;
mockContext.user = { accountId, roles: ["guest"] };

Expand All @@ -146,7 +146,7 @@ test("when no matching anonymous cart is found, throws", async () => {

const promise = reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
shopId
});

Expand All @@ -160,7 +160,7 @@ test("when unknown mode is passed, throws", async () => {

const promise = reconcileCarts(mockContext, {
anonymousCartId,
anonymousCartToken,
cartToken,
mode: "foo",
shopId
});
Expand Down
12 changes: 6 additions & 6 deletions src/core-services/cart/mutations/removeCartItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const inputSchema = new SimpleSchema({
minCount: 1
},
"cartItemIds.$": String,
"token": {
"cartToken": {
type: String,
optional: true
}
Expand All @@ -22,23 +22,23 @@ const inputSchema = new SimpleSchema({
* @param {Object} input - Necessary input
* @param {String} input.cartId - The ID of the cart in which all of the items exist
* @param {String[]} input.cartItemIds - Array of cart item IDs to remove
* @param {String} input.token - The token if the cart is an anonymous cart
* @param {String} input.cartToken - The cartToken if the cart is an anonymous cart
* @returns {Promise<Object>} An object containing the updated cart in a `cart` property
*/
export default async function removeCartItems(context, input) {
inputSchema.validate(input || {});

const { accountId, collections } = context;
const { Cart } = collections;
const { cartId, cartItemIds, token } = input;
const { cartId, cartItemIds, cartToken } = input;

const selector = { _id: cartId };
if (token) {
selector.anonymousAccessToken = hashToken(token);
if (cartToken) {
selector.anonymousAccessToken = hashToken(cartToken);
} else if (accountId) {
selector.accountId = accountId;
} else {
throw new ReactionError("invalid-param", "A token is required when updating an anonymous cart");
throw new ReactionError("invalid-param", "A cartToken is required when updating an anonymous cart");
}

const cart = await Cart.findOne(selector);
Expand Down
4 changes: 2 additions & 2 deletions src/core-services/cart/mutations/removeCartItems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test("removes multiple items from anonymous cart", async () => {
const result = await removeCartItems(mockContext, {
cartId: "cartId",
cartItemIds,
token: "TOKEN"
cartToken: "TOKEN"
});
mockContext.accountId = cachedAccountId;

Expand All @@ -61,7 +61,7 @@ test("removes multiple items from anonymous cart", async () => {
});
});

test("throws when no account and no token passed", async () => {
test("throws when no account and no cartToken passed", async () => {
const cachedAccountId = mockContext.accountId;
mockContext.accountId = null;

Expand Down
8 changes: 4 additions & 4 deletions src/core-services/cart/mutations/setEmailOnAnonymousCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ReactionError from "@reactioncommerce/reaction-error";

const inputSchema = new SimpleSchema({
cartId: String,
token: String,
cartToken: String,
email: {
type: String,
regEx: SimpleSchema.RegEx.Email
Expand All @@ -17,19 +17,19 @@ const inputSchema = new SimpleSchema({
* @param {Object} context - an object containing the per-request state
* @param {Object} input - an object of all mutation arguments that were sent by the client
* @param {String} input.cartId - An anonymous cart ID
* @param {String} input.token - The token for accessing the anonymous cart
* @param {String} input.cartToken - The cartToken for accessing the anonymous cart
* @param {String} input.email - The email address to associate with this cart
* @returns {Promise<Object>} An object with `cart` property containing the updated cart
*/
export default async function setEmailOnAnonymousCart(context, input) {
inputSchema.validate(input || {});

const { collections: { Cart } } = context;
const { cartId, email, token } = input;
const { cartId, email, cartToken } = input;

const cart = await Cart.findOne({
_id: cartId,
anonymousAccessToken: hashToken(token)
anonymousAccessToken: hashToken(cartToken)
});
if (!cart) throw new ReactionError("not-found", "Cart not found");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const dbCart = {
_id: "cartId"
};
const email = "email@address.com";
const token = "TOKEN";
const cartToken = "TOKEN";
const hashedToken = "+YED6SF/CZIIVp0pXBsnbxghNIY2wmjIVLsqCG4AN80=";

beforeAll(() => {
Expand All @@ -20,7 +20,7 @@ test("sets the email address on an anonymous cart", async () => {
const result = await setEmailOnAnonymousCart(mockContext, {
cartId: "cartId",
email,
token
cartToken
});

expect(mockContext.collections.Cart.findOne).toHaveBeenCalledWith({
Expand Down
6 changes: 3 additions & 3 deletions src/core-services/cart/mutations/updateCartItemsQuantity.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const inputSchema = new SimpleSchema({
type: SimpleSchema.Integer,
min: 0
},
"token": {
"cartToken": {
type: String,
optional: true
}
Expand All @@ -28,13 +28,13 @@ const inputSchema = new SimpleSchema({
* @param {String} input.items - Array of items to update
* @param {Number} input.items.cartItemId - The cart item ID
* @param {Object} input.items.quantity - The new quantity, which must be an integer of 0 or greater
* @param {String} input.token - The token if the cart is an anonymous cart
* @param {String} input.cartToken - The cartToken if the cart is an anonymous cart
* @returns {Promise<Object>} An object containing the updated cart in a `cart` property
*/
export default async function updateCartItemsQuantity(context, input) {
inputSchema.validate(input || {});

const { cartId, items, token: cartToken } = input;
const { cartId, items, cartToken } = input;

const cart = await getCartById(context, cartId, { cartToken, throwIfNotFound: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test("updates the quantity of multiple items in anonymous cart", async () => {
quantity: 2
}
],
token: "TOKEN"
cartToken: "TOKEN"
});
mockContext.accountId = cachedAccountId;

Expand Down
4 changes: 2 additions & 2 deletions src/core-services/cart/queries/anonymousCartByCartId.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ReactionError from "@reactioncommerce/reaction-error";
* @param {String} [params.token] - Anonymous cart token
* @returns {Promise<Object>|undefined} - A Cart document, if one is found
*/
export default async function anonymousCartByCartId(context, { cartId, token } = {}) {
export default async function anonymousCartByCartId(context, { cartId, cartToken } = {}) {
const { collections } = context;
const { Cart } = collections;

Expand All @@ -22,6 +22,6 @@ export default async function anonymousCartByCartId(context, { cartId, token } =

return Cart.findOne({
_id: cartId,
anonymousAccessToken: hashToken(token)
anonymousAccessToken: hashToken(cartToken)
});
}
6 changes: 3 additions & 3 deletions src/core-services/cart/resolvers/Mutation/addCartItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { decodeCartItemsOpaqueIds, decodeCartOpaqueId } from "../../xforms/id.js
* @param {String} args.input.cartId - The opaque ID of the cart to add the items to.
* @param {String} [args.input.clientMutationId] - An optional string identifying the mutation call
* @param {String} args.input.items - An array of cart items to add to the new cart. Must not be empty.
* @param {String} [args.input.token] - The anonymous access token that was returned from `createCart`.
* @param {String} [args.input.cartToken] - The anonymous access cartToken that was returned from `createCart`.
* Required for anonymous carts.
* @param {Object} context - an object containing the per-request state
* @returns {Promise<Object>} AddCartItemsPayload
*/
export default async function addCartItems(parentResult, { input }, context) {
const { cartId: opaqueCartId, clientMutationId = null, items: itemsInput, token } = input;
const { cartId: opaqueCartId, clientMutationId = null, items: itemsInput, cartToken } = input;
const cartId = decodeCartOpaqueId(opaqueCartId);
const items = decodeCartItemsOpaqueIds(itemsInput);

Expand All @@ -27,7 +27,7 @@ export default async function addCartItems(parentResult, { input }, context) {
} = await context.mutations.addCartItems(context, {
cartId,
items,
token
cartToken
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import addCartItems from "./addCartItems.js";

const internalCartId = "555";
const opaqueCartId = "cmVhY3Rpb24vY2FydDo1NTU=";
const token = "TOKEN";
const cartToken = "TOKEN";
const productId = "444";
const opaqueProductId = "cmVhY3Rpb24vcHJvZHVjdDo0NDQ=";
const productVariantId = "555";
Expand Down Expand Up @@ -36,7 +36,7 @@ test("correctly passes through to mutations.addCartItems", async () => {
cartId: opaqueCartId,
clientMutationId: "clientMutationId",
items,
token
cartToken
}
}, context);

Expand All @@ -54,6 +54,6 @@ test("correctly passes through to mutations.addCartItems", async () => {
},
quantity: 1
}],
token
cartToken
});
});
Loading

0 comments on commit 474c70a

Please sign in to comment.