Created
March 11, 2016 23:29
-
-
Save Mozu-CS/8d1ca5bd2c85c027056f to your computer and use it in GitHub Desktop.
Sample for documentation of using user-claims for resources and for removing user-claims for resources. The
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//First, pull in the pieces of the Node SDK used to make API requests. | |
//Each of these Factories will need to receive an apiContext object to gain authorization to make API requests. | |
var CartExtendedPropertiesResourceFactory = require('mozu-node-sdk/clients/commerce/carts/extendedProperty'); | |
var OrderResourceFactory = require('mozu-node-sdk/clients/commerce/order'); | |
module.exports = function(context, callback) { | |
//Wrap functionality in a try/catch block. This prevents errors from stalling the Mozu Storefront. | |
try { | |
//The context.apiContext contains both app-claims and user-claims. | |
//The user-claims allow for making API requests scoped to the user who triggered this Arc.js action. | |
//The app-claims allow for making API requests scoped beyond the permissions of a user and are used to affect data in the Mozu workflow. | |
//Since Order API calls are scoped outside of the permissions that a user is allowed, you'll remove the user-claims from the Order Resource you create. | |
//Note: The original context.apiContext object is not altered. | |
var orderResource = OrderResourceFactory(context.apiContext); | |
orderResource.context["user-claims"] = null; | |
//Carts are scoped directly to the users who own them. | |
//You'll retain the user-claims on this Cart Extended Property Resource. | |
var cartExtendedPropertyResource = CartExtendedPropertiesResourceFactory(context.apiContext); | |
var cart = context.get.cart(); | |
var productCodeString = ''; | |
var extendedProperty; | |
var hasExistingProperty = (cart.extendedProperty && cart.extendedProperty.length > 0) ? true : false; | |
orderResource.getOrders({ filter: 'userId eq ' + cart.userId }) | |
.then(function(orderCollection) { | |
if (orderCollection.totalCount > 0) { | |
console.log(orderCollection.totalCount); | |
var reduceHandler = function(previousOrderItem, currentOrderItem) { | |
productCodeString += previousOrderItem.product.productCode + "," + currentOrderItem.product.productCode + ','; | |
}; | |
for (var i = 0; i < context.configuration.orderHistoryNumberOfOrders; i++) { | |
orderCollection.items[i].items.reduce(reduceHandler); | |
} | |
productCodeString = productCodeString.slice(0, productCodeString.lastIndexOf(',')); | |
if (productCodeString.length > 0) { | |
extendedProperty = [{ | |
key: context.configuration.extendedPropertyKey, | |
value: productCodeString | |
}]; | |
if (hasExistingProperty) { | |
cart.extendedProperties.forEach(function(property, index) { | |
if (property.key === context.configuration.extendedPropertyKey) { | |
hasExistingProperty = true; | |
} | |
}); | |
if (existingProperty) { | |
return cartExtendedPropertyResource.deleteExtendedProperty({ key: context.configuration.extendedPropertyKey }) | |
.then(function() { | |
return cartExtendedPropertyResource.addExtendedProperties({}, { body: extendedProperty }); | |
}); | |
} | |
} | |
return cartExtendedPropertyResource.addExtendedProperties({}, { body: extendedProperty }); | |
} else { | |
callback(); | |
} | |
} else { | |
callback(); | |
} | |
}) | |
.then(function() { | |
callback(); | |
}) | |
//You can also utilize the catch handler with JavaScript promises. | |
//Be sure to execute callback in case of errors to refrain from halting Mozu Storefront execution. | |
.catch(function(err) { | |
console.error(err); | |
callback(); | |
}); | |
//Within the catch block, be sure to execute callback in case of errors. | |
//Be sure to execute callback in case of errors to refrain from halting Mozu Storefront execution. | |
} catch (error) { | |
console.error(error); | |
callback(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment