all files / keystone/lib/list/ updateItem.js

12.5% Statements 3/24
0% Branches 0/12
0% Functions 0/5
13.04% Lines 3/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                              
function updateItem (item, data, options, callback) {
	if (typeof options === 'function') {
		callback = options;
		options = {};
	}
	var fieldCount = this.fieldsArray.length;
	var fieldsUpdated = 0;
	var updateErrors = {};
	if (this.fieldsArray.length) {
		this.fieldsArray.forEach(function (field) {
			field.updateItem(item, data, function (err) {
				if (err) {
					updateErrors[field.path] = err;
				}
				complete();
			});
		});
	} else {
		complete();
	}
	function complete () {
		if (++fieldsUpdated < fieldCount) return;
		if (Object.keys(updateErrors).length) {
			return callback({
				error: 'field errors',
				detail: updateErrors,
			});
		}
		item.save(function (err) {
			if (err) {
				return callback({
					error: 'database error',
					detail: err,
				});
			}
			return callback();
		});
	}
}
 
module.exports = updateItem;