Last active
October 12, 2018 14:47
-
-
Save rgriffith/241afacf7f9a50108da81cd8136a02cf to your computer and use it in GitHub Desktop.
Uses Cascade CMS REST API to iterate over XHTML/SD Blocks which subscribe to a specific Data Definition and adds a tag to each one.
This file contains 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
// To install module, run: `npm i node-fetch` | |
const fetch = require("node-fetch"); | |
const AUTHENTICATION = {username: '...', password: '...'}; | |
const REST_API_URL = 'https://CASCADE_URL/api/v1/'; | |
const SITE_NAME = 'www.example.com'; | |
const DATA_DEFINITION_NAME = 'foo'; | |
const TAG_TO_PUSH = 'bar'; | |
function postData(restEndpoint, data = {}) { | |
data.authentication = AUTHENTICATION; | |
return fetch(REST_API_URL + restEndpoint, { | |
method: "POST", | |
headers: {"Content-Type": "application/json; charset=utf-8"}, | |
body: JSON.stringify(data), | |
}) | |
.then(response => response.json()); | |
} | |
postData('listSubscribers/structureddatadefinition/'+SITE_NAME+'/'+DATA_DEFINITION_NAME) | |
.then(data => { | |
if (!data.subscribers.length) { | |
console.log('No subscribers found.'); | |
return; | |
} | |
data.subscribers.forEach(subscriber => { | |
if (subscriber.type !== 'block_XHTML_DATADEFINITION') { | |
console.log('Subscriber was not an XHTML/SD Block, skipping.'); | |
return; | |
} | |
console.log('Reading Block with ID: ', subscriber.id); | |
postData('read/block/' + subscriber.id) | |
.then(readResponse => { | |
let blockData = readResponse.asset.xhtmlDataDefinitionBlock; | |
console.log('Updating Block tags'); | |
blockData.tags.push({name: TAG_TO_PUSH}); | |
postData('edit/block/' + subscriber.id, { | |
asset: { | |
xhtmlDataDefinitionBlock: blockData | |
} | |
}) | |
.then(editResponse => console.log('Updated Block tags')) | |
.catch(error => console.error(error)); | |
}) | |
.catch(error => console.error(error)); | |
}); | |
}) | |
.catch(error => console.error(error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment