Skip to content

Instantly share code, notes, and snippets.

@hnestmann
Created July 20, 2020 15:14
Show Gist options
  • Save hnestmann/4b022f53edfa6accbd080e66782751c8 to your computer and use it in GitHub Desktop.
Save hnestmann/4b022f53edfa6accbd080e66782751c8 to your computer and use it in GitHub Desktop.
/**
* parses the query string for custom expand parameter
* @returns {Object} a parameter object containing paging information based on request params or defauls
*/
function parseQueryString() {
var params = null;
var queryString = request.httpQueryString;
if (queryString.indexOf('c_expand=true') > -1) {
params = {};
// split params
queryString.split('&').forEach(function (element) {
var array = element.split('=');
if (array[0].indexOf('c_expand') > -1) {
// add c_expand things to result object
params[array[0].replace('c_expand_', '')] = array[1];
}
});
// add defaults
params.size = parseInt(params.size, 10) || 50;
params.start = parseInt(params.start, 10) || 0;
}
return params;
}
/**
* Returns a list of ocapi compliant objects of the same type
* @param {string} type the custom object type
* @returns {Object} a large object containg a keyed property and list of all attributes
*/
function getExpandedObjects(type) {
var params = parseQueryString();
var result = null;
if (params) {
result = {};
// get object type definition
var typeDef = dw.object.CustomObjectMgr.describe(type);
// get all custom objects
var expandedApiObjects = dw.object.CustomObjectMgr.getAllCustomObjects(type);
// splice out paged list
var page = expandedApiObjects.asList(params.start, params.size).toArray();
// if paging is necessary we add paging information
if (expandedApiObjects.count > params.size) {
// eslint-disable-next-line no-underscore-dangle
result._paging = {
totalCount: expandedApiObjects.count
};
if (expandedApiObjects.count > params.size + params.start) {
// eslint-disable-next-line no-underscore-dangle
result._paging.next = { c_expand_start: params.start + params.size };
}
}
// map all persistent api objects to ocapi objects
page.forEach(function (element) {
var attributes = {};
typeDef.getAttributeDefinitions().toArray().forEach(function (defintition) {
var attributeId = defintition.ID;
// add all custom objects to final object
if (!defintition.key && !defintition.system) {
if (element.custom[attributeId] && element.custom[attributeId].markup) {
attributes['c_' + attributeId] = element.custom[attributeId].markup;
} else {
attributes['c_' + attributeId] = element.custom[attributeId];
}
}
// attribute id becomes the custom object id
if (defintition.key) {
result[element.custom[attributeId]] = attributes;
}
});
});
}
return result;
}
/**
* Custom Object Modify Get Hook
* @param {Object} scriptObject - the database objec
* @param {Object} doc - the database objec
*/
exports.modifyGETResponse = function (scriptObject, doc) {
var expandedObjects = getExpandedObjects(doc.objectType);
if (expandedObjects) {
doc.c_expand = expandedObjects;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment