Skip to content

Instantly share code, notes, and snippets.

@Mozu-CS
Created December 16, 2015 20:46
Show Gist options
  • Save Mozu-CS/d3763f186f4006a56c2d to your computer and use it in GitHub Desktop.
Save Mozu-CS/d3763f186f4006a56c2d to your computer and use it in GitHub Desktop.
var FiddlerProxy = require('mozu-node-sdk/plugins/fiddler-proxy');
var apiContext = require('mozu-node-sdk/clients/platform/application')();
apiContext.plugins = [FiddlerProxy];
var util = require('util');
var _ = require('lodash');
function logResults(result) {
console.log(util.inspect(result))
}
function reportError(error) {
console.error(error.message, error);
}
var inStockNotificationResource = require('mozu-node-sdk/clients/commerce/inStockNotificationSubscription')(apiContext);
var customerAccountResource = require('mozu-node-sdk/clients/commerce/customer/customerAccount')(apiContext);
var locationResource = require('mozu-node-sdk/clients/commerce/location')(apiContext);
var productResource = require('mozu-node-sdk/clients/commerce/catalog/admin/product')(apiContext);
var productVariantResource = require('mozu-node-sdk/clients/commerce/catalog/admin/products/productVariation')(apiContext);
//Check current inStockNotifications
inStockNotificationResource.getInStockNotificationSubscriptions({pageSize: 200})
.then(function(notificationCollection) {
console.log(notificationCollection);
//Using lodash to poll the notificationCollection to see if Boba Fett already has an inStockNotification with his email address. We could add a specific product here too if needed.
if(_.findWhere(notificationCollection.items, {'email': '[email protected]'}) == undefined) {
//Retrieving customer account for Boba Fett by filtering on his email address
customerAccountResource.getAccounts({filter: "emailAddress eq [email protected]"})
.then(function(accountCollection){
console.log(accountCollection);
//Verifying that the first item is not undefined to avoid index out of bounds error.
if(_.first(accountCollection.items) != undefined) {
//Pulling Boba Fett's account from the items collection
var bobaAccount = accountCollection.items[0];
console.log(bobaAccount);
//Retrieving the location stored for this site (site is provided within mozu.config) associated with Direct Shipping
locationResource.getDirectShipLocation()
.then(function(location){
//Validating that the location exists
if(location) {
//Retrieving the base product for which Boba Fett wants an inStockNotification
productResource.getProducts({filter: "content.productName eq 'Mandalorian Armor'"})
.then(function(productCollection) {
//Pulling this first product from the productCollection
var product = _.first(productCollection.items);
//Verifying that the product exists
if(product != undefined) {
console.log(product);
//Retrieving the variant products for the base product
productVariantResource.getProductVariations({productCode: product.productCode})
.then(function(variantCollection){
//Pulling the variationProductCode with the value "Green" to use when creating the inStockNotification
var greenArmorCode = _.first(_.pluck(_.where(variantCollection.items, {'options': [{'content': {'stringValue': 'Green'}}]}), 'variationProductCode'));
//Validating that the Green Mandalorian Armor exists
if(greenArmorCode) {
//Creating the inStockNotification for Boba Fett to be notified when the green variation of Mandalorian Armor is back in stock.
//I utilized the following page in the documentation to locate the parameters to pass in:
//http://developer.mozu.com/content/api/APIResources/commerce/instocknotifications/Operations/AddInStockNotificationSubscription.htm
inStockNotificationResource.addInStockNotificationSubscription({customerId: bobaAccount.id, email: bobaAccount.emailAddress, locationCode: location.code, productCode: greenArmorCode })
.then(logResults, reportError);
}
}, reportError);
}
}, reportError);
}
}, reportError);
}
}, reportError);
} else {
//Delete the notification if it already exists.
console.log("Boba Fett has already created an inStockNotification for Green Mandalorian Armor.");
console.log("Deleting existing inStockNotification");
//Pull the correct notification to use the notification id with the delete call.
var notification = _.findWhere(notificationCollection.items, {'email': '[email protected]'});
console.log(notification);
//Delete the existing notification.
//I utilized the following page in the documentation to locate the parameters this call expects:
//http://developer.mozu.com/content/api/APIResources/commerce/instocknotifications/Operations/DeleteInStockNotificationSubscription.htm
inStockNotificationResource.deleteInStockNotificationSubscription({id: notification.id})
.then(function(){
console.log("Notification deleted.");
}, reportError);
}
}, reportError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment