<commit_before>var couch = require('./couch.js');
var database = couch.db;

module.exports = function () {

	var addCircle = function (circle, callback) {
		circle.type = "circle";
		database.insert(circle, callback);
	};

	var getAllCircles = function (callback) {
		couch.view("circles/byName", callback);
	};

	var findCirclesByUser = function (user, callback) {
		var circleIds = [];

		for (var membershipKey in user.memberships) {
			var membership = user.memberships[membershipKey];
			circleIds.push(membership.circle);
		}

		couch.fetch(circleIds, callback);
	};


	var updateCircle = function (circle, callback) {
		var copyCircle = function (source, dest) {
			dest.name = source.name;
		};

		database.get(circle._id, function (err, circleToUpdate) {
			if (err) {
				return callback(err);
			}
			if (circleToUpdate.type !== "circle") {
				return callback({
					message: "Update circle: Attempt to update a non-circle."
				});
			}

			copyCircle(circle, circleToUpdate);
			database.insert(circleToUpdate, function (err, body) {
				if (err) {
					return callback(err);
				}
				circleToUpdate._rev = body.rev;
				return callback(null, circleToUpdate);
			});
		});
	};


	return {
		add: addCircle,
		getAll: getAllCircles,
		findByUser: findCirclesByUser,
		update: updateCircle
	};
}();<commit_msg>Fix: /data/circles: Do not return null docs<commit_after>var couch = require('./couch.js');
var database = couch.db;

module.exports = function () {

	var addCircle = function (circle, callback) {
		circle.type = "circle";
		database.insert(circle, callback);
	};

	var getAllCircles = function (callback) {
		couch.view("circles/byName", callback);
	};

	var findCirclesByUser = function (user, callback) {
		var circleIds = [];

		for (var membershipKey in user.memberships) {
			var membership = user.memberships[membershipKey];
			if (membership.circle) {
				circleIds.push(membership.circle);	
			}
		}

		couch.fetch(circleIds, callback);
	};


	var updateCircle = function (circle, callback) {
		var copyCircle = function (source, dest) {
			dest.name = source.name;
		};

		database.get(circle._id, function (err, circleToUpdate) {
			if (err) {
				return callback(err);
			}
			if (circleToUpdate.type !== "circle") {
				return callback({
					message: "Update circle: Attempt to update a non-circle."
				});
			}

			copyCircle(circle, circleToUpdate);
			database.insert(circleToUpdate, function (err, body) {
				if (err) {
					return callback(err);
				}
				circleToUpdate._rev = body.rev;
				return callback(null, circleToUpdate);
			});
		});
	};


	return {
		add: addCircle,
		getAll: getAllCircles,
		findByUser: findCirclesByUser,
		update: updateCircle
	};
}();
{'fraction_non_alphanumeric': 0.13175556340515718, 'fraction_numerical': 0.0, 'mean_word_length': 2.988732394366197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1647016', 'n_tokens_mistral': 1074, 'n_tokens_neox': 888, 'n_words': 204}