All files / lib queryRelatedRecords.js

14.81% Statements 4/27
0% Branches 0/16
0% Functions 0/4
15.38% Lines 4/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x     1x   1x                                                                                                          
const _ = require('lodash')
const { getCollectionCrs, getGeometryTypeFromGeojson } = require('./helpers')
const {
  QueryFields
} = require('./helpers/fields')
 
module.exports = queryRelatedRecords
 
function queryRelatedRecords (data, params = {}) {
  const response = {
    relatedRecordGroups: []
  }
 
  if (!params.returnCountOnly) response.fields = QueryFields.create(data, params)
 
  const geomType = getGeometryTypeFromGeojson(data)
  if (geomType) {
    response.geomType = geomType
    response.spatialReference = getCollectionCrs(data)
    response.hasZ = false
    response.hasM = false
  }
 
  if (data.features) {
    response.relatedRecordGroups = data.features.map(featureCollection => {
      return convertFeaturesToRelatedRecordGroups(featureCollection, params.returnCountOnly)
    })
  }
 
  return response
}
 
function convertFeaturesToRelatedRecordGroups ({ features, properties }, returnCountOnly = false) {
  const recordGroup = {
    objectId: properties.objectid
  }
 
  if (returnCountOnly) {
    // allow for preprocessing of count within provider
    if (properties.count || properties.count === 0) {
      recordGroup.count = properties.count
    } else {
      recordGroup.count = _.get(features, 'length', 0)
    }
 
    return recordGroup
  }
 
  if (features) {
    recordGroup.relatedRecords = features.map(({ geometry, properties }) => {
      return {
        attributes: properties,
        geometry: geometry
      }
    })
  }
 
  return recordGroup
}