Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Mozu-CS/48e38739fd95f805cfa4 to your computer and use it in GitHub Desktop.

Select an option

Save Mozu-CS/48e38739fd95f805cfa4 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 customerAccountResource = require('mozu-node-sdk/clients/commerce/customer/customerAccount')(apiContext);
var customerAccountSegmentResource = require('mozu-node-sdk/clients/commerce/customer/accounts/customerSegment')(apiContext);
var customerSegmentResource = require('mozu-node-sdk/clients/commerce/customer/customerSegment')(apiContext);
//Retrieving customer account for C-3PO by filtering on his email address
customerAccountResource.getAccounts({filter: "emailAddress eq [email protected]"})
.then(function(accountCollection) {
//Verifying that the first item in the accountCollection exists
if(_.first(accountCollection.items)) {
//Assigning the first item in the accountCollection to a variable -- this should be C-3PO's account.
var threepioAccount = _.first(accountCollection.items);
console.log(threepioAccount);
//Retrieving the segments C-3PO currently belongs to.
customerAccountSegmentResource.getAccountSegments({accountId: threepioAccount.id})
.then(function(segmentsOnThreepioCollection) {
console.log(segmentsOnThreepioCollection);
//Checking if C-3PO already belongs to the Droids Customer Segment
if(_.findWhere(segmentsOnThreepioCollection.items, {'name': 'Droids'})) {
console.log("C-3PO is already a member of the Droids Customer Segment.");
console.log("Removing C-3PO from the Droids Customer Segment");
//Assigning the Droids Customer Segment to a variable
var droidSegment = _.findWhere(segmentsOnThreepioCollection.items, {'name': 'Droids'});
//Removing C-3PO from the Droids Customer Segment since he already was a member.
customerSegmentResource.removeSegmentAccount({id: droidSegment.id, accountId: threepioAccount.id})
//http://developer.mozu.com/content/api/APIResources/commerce/customer/Operations/RemoveSegmentAccount.htm
.then(function() {
console.log("C-3PO successfully removed from the Droids Customer Segment.")
}, reportError);
} else {
//Since C-3PO doesn't already belong to the Droids Customer Segment, we'll need to add him.
//Retrieving the Droids Customer Segment with a filter
customerSegmentResource.getSegments({filter: "name eq 'Droids'"})
.then(function(segmentCollection) {
console.log(segmentCollection);
//Verifying that the first item in the segmentCollection exists
//We could make this even more defensive by adding a check against the name field in the first item in segmentCollection
if(_.first(segmentCollection.items)) {
//Assigning the Droids Customer Segment to a variable
var droidSegment = _.first(segmentCollection.items);
//Creating a variable for the accountsId -- one of the needed variables for the call we'll place in just a moment.
var accountIds = [threepioAccount.id];
console.log(droidSegment);
console.log(accountIds);
//customerSegmentResource.addSegmentAccounts({id: droidSegment.id, accountIds: accountIds})
//While the above line may make sense, it will fail. You will see the following types of errors every so often in the service.
//The service is expecting a flat array object, however, passing the object with a named parameter to the Node SDK like this results in an array named "accountIds".
//Luckily we can send any value directly to the HTTP object with the optional second object parameter that the Node SDK expects.
//Here, we still need the segment id passed in as a named parameter to get added as to the URL.
//We can use the body field to add the accountIds array directly to the request body created by the Node SDK.
//The second options object exposes the HTTP object (amongst other things) so you can directly manipulate the fields therein.
//The documentation on Github covers these options under the Making Calls header:
//https://github.com/Mozu/mozu-node-sdk
customerSegmentResource.addSegmentAccounts({id: droidSegment.id}, {body: accountIds})
//http://developer.mozu.com/content/api/APIResources/commerce/customer/Operations/AddSegmentAccounts.htm
.then(function() {
console.log("C-3PO has been added to the Droids Customer Segment.")
}, reportError);
}
}, reportError);
}
}, reportError);
}
}, reportError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment