utilities/handler-helper.js
async function _addManyHandler(
  ownerModel,
  ownerId,
  childModel,
  associationName,
  request,
  Log
) {
  try {
    // EXPL: make a copy of the payload so that request.payload remains unchanged
    let payload = request.payload.map(item => {
      return _.isObject(item) ? _.assignIn({}, item) : item
    })
    if (_.isEmpty(request.payload)) {
      throw Boom.badRequest('Payload is empty.')
    }

    let ownerObject = await ownerModel
      .findOne({ _id: ownerId })
      .select(associationName)
    if (ownerObject) {
      try {
        if (
          ownerModel.routeOptions &&
          ownerModel.routeOptions.add &&
          ownerModel.routeOptions.add[associationName] &&
          ownerModel.routeOptions.add[associationName].pre
        ) {
          payload = await ownerModel.routeOptions.add[associationName].pre(
            payload,
            request,
            Log
          )
        }
      } catch (err) {
        handleError(
          err,
          'There was a preprocessing error while setting the association.',
          Boom.badRequest,
          Log
        )
      }

      let childIds = []
      // EXPL: the payload is an array of Ids
      if (
        typeof payload[0] === 'string' ||
        payload[0] instanceof String ||
        payload[0]._bsontype === 'ObjectID'
      ) {
        childIds = payload
      } else {
        // EXPL: the payload contains extra fields
        childIds = payload.map(object => {
          return object.childId
        })
      }

      for (let childId of childIds) {
        try {
          await _setAssociation(
            ownerModel,
            ownerObject,
            childModel,
            childId,
            associationName,
            payload,
            Log
          )
        } catch (err) {
          handleError(
            err,
            'There was an internal error while setting the associations.',
            Boom.badImplementation,
            Log
          )
        }
      }
      return true
    } else {
      throw Boom.notFound('No owner resource was found with that id.')
    }
  } catch (err) {
    handleError(err, null, null, Log)
  }
}
async function _addManyHandler(
  ownerModel,
  ownerId,
  childModel,
  associationName,
  request,
  Log
) {
  try {
    // EXPL: make a copy of the payload so that request.payload remains unchanged
    let payload = request.payload.map(item => {
      return _.isObject(item) ? _.assignIn({}, item) : item
    })
    if (_.isEmpty(request.payload)) {
      throw Boom.badRequest('Payload is empty.')
    }

    let ownerObject = await ownerModel
      .findOne({ _id: ownerId })
      .select(associationName)
    if (ownerObject) {
      try {
        if (
          ownerModel.routeOptions &&
          ownerModel.routeOptions.add &&
          ownerModel.routeOptions.add[associationName] &&
          ownerModel.routeOptions.add[associationName].pre
        ) {
          payload = await ownerModel.routeOptions.add[associationName].pre(
            payload,
            request,
            Log
          )
        }
      } catch (err) {
        handleError(
          err,
          'There was a preprocessing error while setting the association.',
          Boom.badRequest,
          Log
        )
      }

      let childIds = []
      // EXPL: the payload is an array of Ids
      if (
        typeof payload[0] === 'string' ||
        payload[0] instanceof String ||
        payload[0]._bsontype === 'ObjectID'
      ) {
        childIds = payload
      } else {
        // EXPL: the payload contains extra fields
        childIds = payload.map(object => {
          return object.childId
        })
      }

      for (let childId of childIds) {
        try {
          await _setAssociation(
            ownerModel,
            ownerObject,
            childModel,
            childId,
            associationName,
            payload,
            Log
          )
        } catch (err) {
          handleError(
            err,
            'There was an internal error while setting the associations.',
            Boom.badImplementation,
            Log
          )
        }
      }
      return true
    } else {
      throw Boom.notFound('No owner resource was found with that id.')
    }
  } catch (err) {
    handleError(err, null, null, Log)
  }
}
async function _addManyHandler ( ownerModel , ownerId , childModel , associationName , request , Log ) { try { // EXPL: make a copy of the payload so that request.payload remains unchanged let payload = request . payload . map ( item => { return _ . isObject ( item ) ? _ . assignIn ( { } , item ) : item } ) if ( _ . isEmpty ( request . payload ) ) { throw Boom . badRequest ( 'Payload is empty.' ) } let ownerObject = await ownerModel . findOne ( { _id : ownerId } ) . select ( associationName ) if ( ownerObject ) { try { if ( ownerModel . routeOptions && ownerModel . routeOptions . add && ownerModel . routeOptions . add [ associationName ] && ownerModel . routeOptions . add [ associationName ] . pre ) { payload = await ownerModel . routeOptions . add [ associationName ] . pre ( payload , request , Log ) } } catch ( err ) { handleError ( err , 'There was a preprocessing error while setting the association.' , Boom . badRequest , Log ) } let childIds = [ ] // EXPL: the payload is an array of Ids if ( typeof payload [ 0 ] === 'string' || payload [ 0 ] instanceof String || payload [ 0 ] . _bsontype === 'ObjectID' ) { childIds = payload } else { // EXPL: the payload contains extra fields childIds = payload . map ( object => { return object . childId } ) } for ( let childId of childIds ) { try { await _setAssociation ( ownerModel , ownerObject , childModel , childId , associationName , payload , Log ) } catch ( err ) { handleError ( err , 'There was an internal error while setting the associations.' , Boom . badImplementation , Log ) } } return true } else { throw Boom . notFound ( 'No owner resource was found with that id.' ) } } catch ( err ) { handleError ( err , null , null , Log ) } }
Adds multiple associations to a document.
@param ownerModel: The model that is being added to.
@param ownerId: The id of the owner document.
@param childModel: The model that is being added.
@param associationName: The name of the association from the ownerModel's perspective.
@param request: The Hapi request object, or a container for the wrapper payload.
@param Log: A logging object
@returns {object} A promise returning true if the add succeeds.
@private
Adds multiple associations to a document .