@@ -7,6 +7,7 @@ import mongoose from 'mongoose';
77import get from 'lodash/get' ;
88import { logger } from '@services/logger.service' ;
99import axios from 'axios' ;
10+ import { isEqual } from 'lodash' ;
1011
1112/**
1213 * Endpoint for custom feature layers
@@ -47,7 +48,10 @@ const getFeatureFromItem = (
4748 if ( latitude && longitude ) {
4849 const feature = {
4950 type : 'Feature' ,
50- geometry : [ latitude , longitude ] ,
51+ geometry : {
52+ type : 'Point' ,
53+ coordinates : [ latitude , longitude ] ,
54+ } ,
5155 properties : { ...item } ,
5256 } ;
5357 features . push ( feature ) ;
@@ -98,6 +102,7 @@ router.get('/feature', async (req, res) => {
98102 return res . status ( 404 ) . send ( i18next . t ( 'common.errors.dataNotFound' ) ) ;
99103 }
100104
105+ // todo(gis): improve how we find resource
101106 const resourceData = await Resource . findOne ( {
102107 $or : [
103108 {
@@ -117,29 +122,30 @@ router.get('/feature', async (req, res) => {
117122 ] ,
118123 } ) ;
119124
125+ if ( ! resourceData ) {
126+ return res . status ( 404 ) . send ( i18next . t ( 'common.errors.dataNotFound' ) ) ;
127+ }
128+
120129 let query : any ;
121130 let variables : any ;
122131
123- if (
124- resourceData &&
125- resourceData . aggregations &&
126- resourceData . aggregations . length > 0
127- ) {
132+ const aggregations = resourceData . aggregations || [ ] ;
133+ const aggregation = aggregations . find ( ( x ) => isEqual ( x . _id , id ) ) ;
134+ const layouts = resourceData . layouts || [ ] ;
135+ const layout = layouts . find ( ( x ) => isEqual ( x . _id , id ) ) ;
136+
137+ if ( aggregation ) {
128138 query = `query recordsAggregation($resource: ID!, $aggregation: ID!) {
129139 recordsAggregation(resource: $resource, aggregation: $aggregation)
130140 }` ;
131141 variables = {
132142 resource : resourceData . _id ,
133- aggregation : resourceData . aggregations [ 0 ] . _id ,
143+ aggregation : aggregation . _id ,
134144 } ;
135- } else if (
136- resourceData &&
137- resourceData . layouts &&
138- resourceData . layouts . length > 0
139- ) {
140- query = buildQuery ( resourceData . layouts [ 0 ] . query ) ;
145+ } else if ( layout ) {
146+ query = buildQuery ( layout . query ) ;
141147 variables = {
142- filter : resourceData . layouts [ 0 ] . query . filter ,
148+ filter : layout . query . filter ,
143149 } ;
144150 } else {
145151 return res . status ( 404 ) . send ( i18next . t ( 'common.errors.dataNotFound' ) ) ;
0 commit comments