export default async function getLayerGeom(req) {
  const { pg, params, query } = req;

  const { id } = params;
  if (!id) return { message: 'id is empty', status: 403 };

  const selectTable = `select source_path,geometry_column from gis.services where service_id='${id}'`;
  const { source_path: table, geometry_column: geometryColumn = 'geom' } = await pg.one(selectTable);
  if (!table) return;
  const selectGeom = `
    SELECT 'FeatureCollection' As type,
                               json_agg(q) as features
                        FROM (
                                SELECT 'Feature' as type,
                                     row_number() over() as id, st_asgeojson(
                                    st_force2d(${geometryColumn}),
                                        6,
                                        0
                                    ) :: json as geometry
                              from ${table}
                              where ${geometryColumn} is not null) q`;
  if (query?.sql) return selectGeom;
  const { rows } = await pg.query(selectGeom);

  return { message: rows, status: 200 };

}
