|
| 1 | +(function() { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var _ = require('lodash'), |
| 5 | + RestHandlers = require(__dirname + '/restHandlers'), |
| 6 | + blueprint = require(__dirname + '/blueprint'), |
| 7 | + inflection = require(__dirname + '/inflection'); |
| 8 | + |
| 9 | + function CollectionItem(kind, data, itemId, options, undefinedProperty) { |
| 10 | + |
| 11 | + var wrapCollectionItems = require(__dirname + '/wrapCollectionItems'), |
| 12 | + restHandlers = new RestHandlers(options); |
| 13 | + |
| 14 | + this.id = itemId; |
| 15 | + |
| 16 | + var currentItem = this; |
| 17 | + |
| 18 | + _.each(data, function(value, key) { |
| 19 | + currentItem[key] = value; |
| 20 | + }); |
| 21 | + |
| 22 | + var changedData = {}; |
| 23 | + |
| 24 | + this.save = function(saveCallback) { |
| 25 | + restHandlers.editItem(data.id, kind, changedData, saveCallback); |
| 26 | + changedData = {}; |
| 27 | + return currentItem; |
| 28 | + }; |
| 29 | + |
| 30 | + this.remove = function(successCallback) { |
| 31 | + return restHandlers.removeItem(data.id, kind, {}, successCallback); |
| 32 | + }; |
| 33 | + |
| 34 | + this.get = function(key) { |
| 35 | + return (!_.isUndefined(data[key]) ? data[key] : undefinedProperty); |
| 36 | + }; |
| 37 | + |
| 38 | + this.merge = function(withId, callback) { |
| 39 | + return restHandlers.mergeItem(data.id, withId, kind, callback); |
| 40 | + }; |
| 41 | + |
| 42 | + this.duplicate = function(callback) { |
| 43 | + return restHandlers.duplicateItem(data.id, kind, callback); |
| 44 | + }; |
| 45 | + |
| 46 | + this.toObject = function() { |
| 47 | + var obj = {}; |
| 48 | + _.each(_.keys(this), (function(key) { |
| 49 | + if (typeof this[key] !== 'function') { |
| 50 | + obj[key] = _.clone(this[key]); |
| 51 | + } |
| 52 | + }).bind(this)); |
| 53 | + return obj; |
| 54 | + }; |
| 55 | + |
| 56 | + this.set = function(key, value) { |
| 57 | + if (key === 'id') { |
| 58 | + throw new Error(inflection.capitalize(kind) + ' ID cannot be changed.'); |
| 59 | + } |
| 60 | + var changeValue = function(keyToChange, valueToChange) { |
| 61 | + var isObject = (typeof data[keyToChange] == 'object' && data[keyToChange]), |
| 62 | + isSameType = (typeof data[keyToChange] === typeof valueToChange), |
| 63 | + isUndefinedOrNull = (_.isNull(data[keyToChange]) || typeof data[keyToChange] == 'undefined'); |
| 64 | + |
| 65 | + if (isObject || isSameType || (!isSameType && isUndefinedOrNull)) { |
| 66 | + data[keyToChange] = valueToChange; |
| 67 | + currentItem[keyToChange] = valueToChange; |
| 68 | + changedData[keyToChange] = valueToChange; |
| 69 | + } |
| 70 | + else { |
| 71 | + throw new Error('Can not change ' + keyToChange + ' - ' + typeof data[keyToChange] + ' must be given.'); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + if (typeof key === 'object' && typeof value === 'undefined') { |
| 76 | + _.each(key, function(cValue, cKey) { |
| 77 | + changeValue(cKey, cValue); |
| 78 | + }); |
| 79 | + } |
| 80 | + else { |
| 81 | + changeValue(key, value); |
| 82 | + } |
| 83 | + |
| 84 | + return currentItem; |
| 85 | + }; |
| 86 | + |
| 87 | + // generator of sub-items management (like deal getProducts, addProduct, updateProduct, deleteProduct): |
| 88 | + var generateSubMethod = function(methodType, kind, relatedObject) { |
| 89 | + var methodSuffix = relatedObject.substr(0,1).toUpperCase() + relatedObject.substr(1); |
| 90 | + |
| 91 | + if (methodType !== 'get') { |
| 92 | + methodSuffix = inflection.singularize(methodSuffix); |
| 93 | + } |
| 94 | + |
| 95 | + var objectKey = kind + '/' + currentItem.id + '/' + relatedObject; |
| 96 | + |
| 97 | + currentItem[methodType + methodSuffix] = function(params, callback) { |
| 98 | + |
| 99 | + callback = (_.isFunction(params) && _.isUndefined(callback) ? params : callback); |
| 100 | + params = (_.isFunction(params) && _.isUndefined(callback) ? {} : params); |
| 101 | + if (!_.isFunction(callback)) { |
| 102 | + callback = function() {}; |
| 103 | + } |
| 104 | + |
| 105 | + var relatedObjectPath = relatedObject; |
| 106 | + |
| 107 | + if (blueprint.selfManagedRelatedObjects.indexOf(relatedObject) !== -1) { |
| 108 | + relatedObjectPath = objectKey; |
| 109 | + } |
| 110 | + |
| 111 | + switch (methodType) { |
| 112 | + case 'get': |
| 113 | + return restHandlers.listItems(objectKey, params, function(error, data, additionalData, req, res){ |
| 114 | + var collectionItems = wrapCollectionItems(data, relatedObjectPath, options); |
| 115 | + callback(error, collectionItems, additionalData, req, res); |
| 116 | + }); |
| 117 | + case 'add': |
| 118 | + return restHandlers.addItem(objectKey, params, function(error, data, additionalData, req, res){ |
| 119 | + var collectionItems = wrapCollectionItems(data, relatedObjectPath, options); |
| 120 | + callback(error, collectionItems, additionalData, req, res); |
| 121 | + }); |
| 122 | + case 'update': |
| 123 | + return restHandlers.editItem(params.id, objectKey, params, function(error, data, additionalData, req, res){ |
| 124 | + var collectionItems = wrapCollectionItems(data, relatedObjectPath, options); |
| 125 | + callback(error, collectionItems, additionalData, req, res); |
| 126 | + }); |
| 127 | + case 'delete': |
| 128 | + return restHandlers.removeItem(false, objectKey, params, function(error, data, additionalData, req, res){ |
| 129 | + var collectionItems = wrapCollectionItems(data, relatedObjectPath, options); |
| 130 | + callback(error, collectionItems, additionalData, req, res); |
| 131 | + }); |
| 132 | + default: |
| 133 | + break; |
| 134 | + } |
| 135 | + }; |
| 136 | + }; |
| 137 | + |
| 138 | + // attaches get-methods for sub-objects (like deal products, organization persons, etc): |
| 139 | + if (_.isObject(blueprint.apiRelatedObjects[kind])) { |
| 140 | + _.each(blueprint.apiRelatedObjects[kind], function(relatedObject) { |
| 141 | + generateSubMethod('get', kind, relatedObject); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + // attaches editing capabilities for sub-objects that should have them: |
| 146 | + if (_.isArray(blueprint.editableSubItems[kind])) { |
| 147 | + _.each(blueprint.editableSubItems[kind], function(relatedObject) { |
| 148 | + |
| 149 | + generateSubMethod('add', kind, relatedObject); |
| 150 | + generateSubMethod('update', kind, relatedObject); |
| 151 | + generateSubMethod('delete', kind, relatedObject); |
| 152 | + |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + return currentItem; |
| 157 | + } |
| 158 | + |
| 159 | + module.exports = CollectionItem; |
| 160 | + |
| 161 | +})(); |
0 commit comments