const keystone		= require( 'keystone' ),
	  Child			= require( '../../models/Child' ),
	  SocialWorker	= keystone.list( 'Social Worker' );
 
// TODO: this implementation is ugly, it needs to be rewritten in a non-crappy way
exports.getSocialWorkerById = ( id, container, field, done ) => { 
	// if the id isn't set
	if( !id ) {
		// return control to the calling context
		return done();
	}
	// fetch the social worker record
	SocialWorker.model.findById( id )
		.exec()
		.then( socialWorker => {
			// set the field from the outer context to the social worker
			container[ field ] = socialWorker;
			// return control to the parent context
			done();
		});
};

exports.fetchRegisteredChildren = ( id ) => {

	return new Promise( ( resolve, reject ) => {
		// if the id isn't set
		if( !id ) {
			// resolve the promise with an empty array to prevent downstream array checks from failing
			return resolve( [] );
		}

		keystone.list( 'Child' ).model
			.find( { $or: [
						{ adoptionWorker: id },
						{ recruitmentWorker: id } ] } )
			.lean()
			.exec()
			.then( children => {
				resolve( children );
			}, err => {
				// log the error for debugging purposes
				console.error( `an error occurred fetching the children registered by social worker with id ${ id } - ${ err }` );
				// allow further processing beyond this middleware
				reject();
			});
	});
}
@@ -7,18 +7,17 @@
 keystone
-%09%09
+ 
 = requir
@@ -35,284 +35,127 @@
 e' )
-,%0A%09  Child%09%09%09= require( '../../models/Child' ),%0A%09  SocialWorker%09= keystone.list( 'Social Worker' );%0A %0A// TODO: this implementation is ugly, it needs to be rewritten in a non-crappy way%0Aexports.getSocialWorkerById = ( id, container, field, done
+;%0A %0Aexports.getSocialWorkerById = id =%3E %7B %0A%0A%09return new Promise( ( resolve, reject
  ) =%3E %7B
- 
 %0A
+%09
 %09// if 
-the
+no
  id 
-isn't set%0A
+was passed in%0A%09
 %09if(
@@ -169,69 +169,131 @@
 %7B%0A%09%09
+%09
 // re
-turn control to the calling context%0A%09%09return done(
+ject the promise with details%0A%09%09%09return reject( %60error fetching social worker by id - no id value passed in%60 
 );%0A
+%09
 %09%7D%0A
+%09
 %09// 
@@ -328,26 +328,51 @@
 rd%0A%09
+%09keystone.list( '
 Social
+ 
 Worker
+' )
 .model
+%0A%09%09%09
 .fin
@@ -385,24 +385,25 @@
  id )%0A%09%09
+%09
 .exec()%0A
 %09%09.then(
@@ -394,16 +394,17 @@
 .exec()%0A
+%09
 %09%09.then(
@@ -429,92 +429,303 @@
 %0A%09%09%09
+%09
 // 
-set the field from the outer context to the social worker%0A%09%09%09container%5B field %5D =
+if no social worker was found with a matching id%0A%09%09%09%09if( !socialWorker ) %7B%0A%09%09%09%09%09// reject the promise with the reason why%0A%09%09%09%09%09reject( %60error fetching social worker by id - no social worker found with id $%7B id %7D%60)%0A%09%09%09%09%7D%0A%09%09%09%09// resolve the promise with the returned social worker%0A%09%09%09%09resolve(
  soc
@@ -737,16 +737,18 @@
 rker
+ )
 ;%0A%09%09%09// 
 retu
@@ -747,57 +747,193 @@
 %09// 
-return control to the parent context%0A%09%09%09done();%0A%09
+if an error occurred fetching from the database%0A%09%09%09%7D, err =%3E %7B%0A%09%09%09%09// reject the promise with details of the error%0A%09%09%09%09reject( %60error fetching social worker by id - $%7B err %7D%60 );%0A%09%09%09%7D);%0A
 %09%7D);

update getSocialWorkerById