archive/v1/client/findParentType.js
function getParent(type, id, parentType, root) {
		return _client.retrieve({
			type: type,
			id: id,
			properties: ['parent']
		})
		.then(function(obj) {
			
			if (obj.length > 0) {
				
				obj  = _.first(obj);
				type = _.get(obj, 'parent.type');
				id   = _.get(obj, 'parent.id');
				
				// if there is no parent type or id, the parent could not be found, throw an error
				if (!type || !id) {
					throw {
						errorCode: 404,
						message: 'could not find parent type'
					};
				}
				
				// if the parent is a match
				if (type === parentType) {
					
					// if a root search, keep looking for parent objects of the same type
					if (root === true) {
						return _client.retrieve({
							type: type,
							id: id,
							properties: ['parent']
						})
						.then(function(parent) {
							if (_.get(parent, 'parent.type') === parentType) {
								return getParent(type, id, parentType, root);
							}
							return {
								id: id,
								type: parentType
							};
						});
					}
					return {
						id: id,
						type: parentType
					};
				}
				
				return getParent(type, id, parentType, root);
			}
			
			throw {
				errorCode: 404,
				message: 'Object not found'
			};
		});
	}
function getParent(type, id, parentType, root) {
		return _client.retrieve({
			type: type,
			id: id,
			properties: ['parent']
		})
		.then(function(obj) {
			
			if (obj.length > 0) {
				
				obj  = _.first(obj);
				type = _.get(obj, 'parent.type');
				id   = _.get(obj, 'parent.id');
				
				// if there is no parent type or id, the parent could not be found, throw an error
				if (!type || !id) {
					throw {
						errorCode: 404,
						message: 'could not find parent type'
					};
				}
				
				// if the parent is a match
				if (type === parentType) {
					
					// if a root search, keep looking for parent objects of the same type
					if (root === true) {
						return _client.retrieve({
							type: type,
							id: id,
							properties: ['parent']
						})
						.then(function(parent) {
							if (_.get(parent, 'parent.type') === parentType) {
								return getParent(type, id, parentType, root);
							}
							return {
								id: id,
								type: parentType
							};
						});
					}
					return {
						id: id,
						type: parentType
					};
				}
				
				return getParent(type, id, parentType, root);
			}
			
			throw {
				errorCode: 404,
				message: 'Object not found'
			};
		});
	}
function getParent ( type , id , parentType , root ) { return _client . retrieve ( { type : type , id : id , properties : [ 'parent' ] } ) . then ( function ( obj ) { if ( obj . length > 0 ) { obj = _ . first ( obj ) ; type = _ . get ( obj , 'parent.type' ) ; id = _ . get ( obj , 'parent.id' ) ; // if there is no parent type or id, the parent could not be found, throw an error if ( ! type || ! id ) { throw { errorCode : 404 , message : 'could not find parent type' } ; } // if the parent is a match if ( type === parentType ) { // if a root search, keep looking for parent objects of the same type if ( root === true ) { return _client . retrieve ( { type : type , id : id , properties : [ 'parent' ] } ) . then ( function ( parent ) { if ( _ . get ( parent , 'parent.type' ) === parentType ) { return getParent ( type , id , parentType , root ) ; } return { id : id , type : parentType } ; } ) ; } return { id : id , type : parentType } ; } return getParent ( type , id , parentType , root ) ; } throw { errorCode : 404 , message : 'Object not found' } ; } ) ; }
recursively looks for a parent type
recursively looks for a parent type