<commit_before>var keystone		= require('keystone'),
	_				= require('underscore'),
	async			= require('async'),
	middleware		= require('./middleware'),
	Child			= keystone.list('Child');
	familyService	= require('./service_family'),
// TODO: combine the below two functions, the only difference in the filter in the find()
exports.getAllChildren = ( req, res, done ) => {

	let locals = res.locals;

	Child.model.find()
				.populate( 'gender' )
				.populate( 'race' )
				.populate( 'languages' )
				.populate( 'disabilities' )
				.populate( 'otherConsiderations' )
				.populate( 'recommendedFamilyConstellation' )
				.populate( 'otherFamilyConstellationConsideration' )
				.populate( 'status' )
				.populate( 'legalStatus' )
				.exec()
				.then( children => {
					// TODO: This filter should happen in a .where() above
					// Filter out all children who don't have a status of 'active'
					children = _.filter( children, child => {
						return child.status.childStatus === 'active' && child.isVisibleInGallery;
					});

					_.each( children, child => {
						// adjust the image to the blank male/female image if needed
						exports.setNoChildImage( req, res, child );
						// set extra information needed for rendering the child to the page
						child.age						= middleware.getAge( child.birthDate );
			    		child.ageConverted				= middleware.convertDate( child.birthDate );
			    		child.registrationDateConverted	= middleware.convertDate( child.registrationDate );
					});

					locals.allChildren = children;
					// execute done function if async is used to continue the flow of execution
					done()

				}, err => {

					console.log( err );
					// execute done function if async is used to continue the flow of execution
					done();

				});
};

exports.getUnrestrictedChildren = ( req, res, done ) => {

	var locals = res.locals;

	Child.model.find()
				.where( 'siteVisibility', 'everyone' )
				.populate( 'gender' )
				.populate( 'race' )
				.populate( 'languages' )
				.populate( 'disabilities' )
				.populate( 'otherConsiderations' )
				.populate( 'recommendedFamilyConstellation' )
				.populate( 'otherFamilyConstellationConsideration' )
				.populate( 'status' )
				.populate( 'legalStatus' )
				.exec()
				.then( children => {
					// TODO: This filter should happen in a .where() above
					// Filter out all children who don't have a status of 'active'
					children = _.filter( children, child => {
						return child.status.childStatus === 'active' && child.isVisibleInGallery;
					});

					_.each(children, child => {
						// adjust the image to the blank male/female image if needed
						exports.setNoChildImage( req, res, child );
						// set extra information needed for rendering the child to the page
						child.age						= middleware.getAge( child.birthDate );
			    		child.ageConverted				= middleware.convertDate( child.birthDate );
			    		child.registrationDateConverted	= middleware.convertDate( child.registrationDate );

					});

					locals.allChildren = children;
					// execute done function if async is used to continue the flow of execution
					done()

				}, err => {

					console.log( err );
					// execute done function if async is used to continue the flow of execution
					done();

				});
};

/* Sets the images for display in the gallery to a blank male/female face in the following cases:
 *	1. No image was uploaded for the child
 *	2. The child has been identified as legal risk
 */
exports.setNoChildImage = ( req, res, child ) => {
	// Constant definitions.  TODO: Change these to const instead of var once support for ES2015 improves
	const NO_IMAGE_MALE_GALLERY				= 'images/no-image-male_gallery.png',
		  NO_IMAGE_MALE_DETAILS				= 'images/no-image-male_details.png',
		  NO_IMAGE_FEMALE_GALLERY			= 'images/no-image-female_gallery.png',
		  NO_IMAGE_FEMALE_DETAILS			= 'images/no-image-female_details.png',
		  NO_IMAGE_OTHER_GALLERY			= 'images/no-image-other_gallery.png',
		  NO_IMAGE_OTHER_DETAILS			= 'images/no-image-other_details.png',
		  NO_IMAGE_SIBLING_GROUP_GALLERY	= 'images/no-image-sibling-group_gallery.png',
		  NO_IMAGE_SIBLING_GROUP_DETAILS	= 'images/no-image-sibling-group_details.png';
	
	// if the child is part of a sibling group
	if( child.mustBePlacedWithSiblings ) {
		// and is missing an image or is legal risk
		if( !child.siblingGroupImage.secure_url || child.legalStatus.legalStatus === 'legal risk' ) {
			// set the images to the placeholders for sibling groups
			child.siblingGroupDetailImage = NO_IMAGE_SIBLING_GROUP_DETAILS;
			child.siblingGroupGalleryImage = NO_IMAGE_SIBLING_GROUP_GALLERY;
		}
	// if the child is not part of a sibling group
	} else {
		// and is missing an image or is legal risk
		if( !child.image.secure_url || child.legalStatus.legalStatus === 'legal risk' ) {
			// and the child is male
			if( child.gender.gender === 'male' ) {
				// set the images to the placeholder for male children
				child.detailImage = NO_IMAGE_MALE_DETAILS;
				child.galleryImage = NO_IMAGE_MALE_GALLERY;
			// but if the child is female<commit_msg>#155 | update the gallery logic to restrict child images appearing to the wrong users<commit_after>var keystone		= require('keystone'),
	_				= require('underscore'),
	async			= require('async'),
	middleware		= require('./middleware'),
	Child			= keystone.list('Child');
	familyService	= require('./service_family'),
// TODO: combine the below two functions, the only difference in the filter in the find()
exports.getAllChildren = ( req, res, done ) => {

	let locals = res.locals;

	Child.model.find()
				.populate( 'gender' )
				.populate( 'race' )
				.populate( 'languages' )
				.populate( 'disabilities' )
				.populate( 'otherConsiderations' )
				.populate( 'recommendedFamilyConstellation' )
				.populate( 'otherFamilyConstellationConsideration' )
				.populate( 'status' )
				.populate( 'legalStatus' )
				.exec()
				.then( children => {
					// TODO: This filter should happen in a .where() above
					// Filter out all children who don't have a status of 'active'
					children = _.filter( children, child => {
						return child.status.childStatus === 'active' && child.isVisibleInGallery;
					});

					_.each( children, child => {
						// adjust the image to the blank male/female image if needed
						exports.setNoChildImage( req, res, child, locals.targetChildren === 'all' );
						// set extra information needed for rendering the child to the page
						child.age						= middleware.getAge( child.birthDate );
			    		child.ageConverted				= middleware.convertDate( child.birthDate );
			    		child.registrationDateConverted	= middleware.convertDate( child.registrationDate );
					});

					locals.allChildren = children;
					// execute done function if async is used to continue the flow of execution
					done()

				}, err => {

					console.log( err );
					// execute done function if async is used to continue the flow of execution
					done();

				});
};

exports.getUnrestrictedChildren = ( req, res, done ) => {

	var locals = res.locals;
	// find all children who are active, and are either visible to everyone or have the 'child is visible on MARE web' checkbox checked
	Child.model.find({ $or: [
						{ 'siteVisibility': 'everyone' },
						{ 'isVisibleInGallery': true } ] } )
				.populate( 'gender' )
				.populate( 'race' )
				.populate( 'languages' )
				.populate( 'disabilities' )
				.populate( 'otherConsiderations' )
				.populate( 'recommendedFamilyConstellation' )
				.populate( 'otherFamilyConstellationConsideration' )
				.populate( 'status' )
				.populate( 'legalStatus' )
				.exec()
				.then( children => {
					// Filter out all children who don't have a status of 'active'
					children = _.filter( children, child => {
						return child.status.childStatus === 'active';
					});
					// loop through each child
					_.each( children, child => {
						// adjust the image to the blank male/female image if needed
						exports.setNoChildImage( req, res, child, locals.targetChildren === 'all' );
						// set extra information needed for rendering the child to the page
						child.age						= middleware.getAge( child.birthDate );
			    		child.ageConverted				= middleware.convertDate( child.birthDate );
			    		child.registrationDateConverted	= middleware.convertDate( child.registrationDate );

					});

					locals.allChildren = children;
					// execute done function if async is used to continue the flow of execution
					done()

				}, err => {

					console.log( err );
					// execute done function if async is used to continue the flow of execution
					done();

				});
};

/* Sets the images for display in the gallery to a blank male/female face in the following cases:
 *	1. No image was uploaded for the child
 *	2. The child has been identified as legal risk
 */
exports.setNoChildImage = ( req, res, child, canViewAllChildren ) => {

	const NO_IMAGE_MALE_GALLERY				= 'images/no-image-male_gallery.png',
		  NO_IMAGE_MALE_DETAILS				= 'images/no-image-male_details.png',
		  NO_IMAGE_FEMALE_GALLERY			= 'images/no-image-female_gallery.png',
		  NO_IMAGE_FEMALE_DETAILS			= 'images/no-image-female_details.png',
		  NO_IMAGE_OTHER_GALLERY			= 'images/no-image-other_gallery.png',
		  NO_IMAGE_OTHER_DETAILS			= 'images/no-image-other_details.png',
		  NO_IMAGE_SIBLING_GROUP_GALLERY	= 'images/no-image-sibling-group_gallery.png',
		  NO_IMAGE_SIBLING_GROUP_DETAILS	= 'images/no-image-sibling-group_details.png';	
	// if the child is part of a sibling group
	if( child.mustBePlacedWithSiblings ) {
		// and is missing an image or is legal risk
		if( !child.siblingGroupImage.secure_url || child.legalStatus.legalStatus === 'legal risk' ) {
			// set the images to the placeholders for sibling groups
			child.siblingGroupDetailImage = NO_IMAGE_SIBLING_GROUP_DETAILS;
			child.siblingGroupGalleryImage = NO_IMAGE_SIBLING_GROUP_GALLERY;
		}
	// if the child is not part of a sibling group
	} else {
		if( !child.image.secure_url								// and is missing an image
			|| child.legalStatus.legalStatus === 'legal risk' 	// or is legal risk
			|| ( child.siteVisibility !== 'everyone' 			// or if the child is not visible to everyone
			 	&& !canViewAllChildren ) ) {					// 	and the user wouldn't have permission without the 'child is visible on MARE web' checkbox being checked
			// and the child is male
			if( child.gender.gender === 'male' ) {
				// set the images to the placeholder for male children
				child.detailImage = NO_IMAGE_MALE_DETAILS;
				child.galleryImage = NO_IMAGE_MALE_GALLERY;
			// but if the child is female
{'fraction_non_alphanumeric': 0.10923815801864582, 'fraction_numerical': 0.0010358790846595725, 'mean_word_length': 3.1339042428960684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '2251362', 'n_tokens_mistral': 3956, 'n_tokens_neox': 3196, 'n_words': 937}