diff --git a/src/common/elastic/utils.ts b/src/common/elastic/utils.ts index 5a7d4ce1..eb82cd3e 100644 --- a/src/common/elastic/utils.ts +++ b/src/common/elastic/utils.ts @@ -1,17 +1,13 @@ +import { WGS84Coordinate } from '../interfaces'; + /* eslint-disable @typescript-eslint/naming-convention */ export const boundingBox = ( bbox: number[] ): { geo_bounding_box: { geometry: { - top_left: { - lon: number; - lat: number; - }; - bottom_right: { - lon: number; - lat: number; - }; + top_left: WGS84Coordinate; + bottom_right: WGS84Coordinate; }; }; } => ({ @@ -29,17 +25,12 @@ export const boundingBox = ( }, }); -export const geoDistance = (params: { - radius: number; - lon: number; - lat: number; -}): { +export const geoDistance = ( + params: WGS84Coordinate & { radius: number } +): { geo_distance: { distance: string; - geometry: { - lon: number; - lat: number; - }; + geometry: WGS84Coordinate; }; } => ({ geo_distance: { diff --git a/src/common/interfaces.ts b/src/common/interfaces.ts index 7d98367f..ca49a24a 100644 --- a/src/common/interfaces.ts +++ b/src/common/interfaces.ts @@ -36,3 +36,8 @@ export interface Geometry { export interface FeatureCollection extends GeoJSONFeatureCollection { features: T[]; } + +export interface WGS84Coordinate { + lat: number; + lon: number; +} diff --git a/src/common/utils.ts b/src/common/utils.ts index e618aa7c..2354277a 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -6,7 +6,7 @@ import { Tile } from '../tile/models/tile'; import { Route } from '../route/models/route'; import { FIELDS } from './constants'; import { utmProjection, wgs84Projection } from './projections'; -import { FeatureCollection } from './interfaces'; +import { FeatureCollection, WGS84Coordinate } from './interfaces'; export const formatResponse = (elasticResponse: estypes.SearchResponse): FeatureCollection => ({ type: 'FeatureCollection', @@ -78,16 +78,9 @@ export const convertWgs84ToUTM = ( }; /* eslint-enable @typescript-eslint/naming-convention */ -export const convertUTMToWgs84 = ( - x: number, - y: number, - zone: number -): { - lat: number; - lng: number; -} => { +export const convertUTMToWgs84 = (x: number, y: number, zone: number): WGS84Coordinate => { const [longitude, latitude] = proj4(utmProjection(zone), wgs84Projection, [x, y]); - return { lat: latitude, lng: longitude }; + return { lat: latitude, lon: longitude }; }; export const validateTile = (tile: { tileName: string; subTileNumber: number[] }): boolean => { diff --git a/src/item/controllers/itemController.ts b/src/item/controllers/itemController.ts index a06d2457..1a8f5776 100644 --- a/src/item/controllers/itemController.ts +++ b/src/item/controllers/itemController.ts @@ -7,17 +7,9 @@ import { injectable, inject } from 'tsyringe'; import { SERVICES } from '../../common/constants'; import { ItemManager } from '../models/itemManager'; import { Item } from '../models/item'; -import { GeoContext } from '../../common/interfaces'; +import { FeatureCollection, GeoContext } from '../../common/interfaces'; -type GetResourceHandler = RequestHandler< - undefined, - { - type: string; - features: (Item | undefined)[]; - }, - undefined, - GetItemsQueryParams ->; +type GetItemsHandler = RequestHandler, undefined, GetItemsQueryParams>; export interface GetItemsQueryParams { command_name: string; @@ -40,7 +32,7 @@ export class ItemController { this.createdResourceCounter = meter.createCounter('created_resource'); } - public getItems: GetResourceHandler = async (req, res, next) => { + public getItems: GetItemsHandler = async (req, res, next) => { try { const { command_name: commandName, tile, sub_tile, geo_context, reduce_fuzzy_match, size } = req.query; const response = await this.manager.getItems( diff --git a/src/latLon/DAL/latLonRepository.ts b/src/latLon/DAL/latLonRepository.ts index 1e884426..e173010c 100644 --- a/src/latLon/DAL/latLonRepository.ts +++ b/src/latLon/DAL/latLonRepository.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import { FactoryFunction } from 'tsyringe'; import { LatLon as LatLonDb } from './latLon'; +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type const createLatLonRepository = (dataSource: DataSource) => { return dataSource.getRepository(LatLonDb).extend({ async latLonToTile({ x, y, zone }: { x: number; y: number; zone: number }): Promise { diff --git a/src/latLon/controllers/latLonController.ts b/src/latLon/controllers/latLonController.ts index ae1ca70f..00633d6a 100644 --- a/src/latLon/controllers/latLonController.ts +++ b/src/latLon/controllers/latLonController.ts @@ -7,7 +7,7 @@ import { injectable, inject } from 'tsyringe'; import { SERVICES } from '../../common/constants'; import { LatLonManager } from '../models/latLonManager'; import { Tile } from '../../tile/models/tile'; -import { FeatureCollection } from '../../common/interfaces'; +import { FeatureCollection, WGS84Coordinate } from '../../common/interfaces'; type GetLatLonToTileHandler = RequestHandler< undefined, @@ -23,29 +23,16 @@ type GetTileToLatLonHandler = RequestHandler, type GetLatLonToMgrsHandler = RequestHandler; -type GetMgrsToLatLonHandler = RequestHandler< - undefined, - { - lat: number; - lon: number; - }, - undefined, - GetMgrsToLatLonQueryParams ->; +type GetMgrsToLatLonHandler = RequestHandler; -export interface GetLatLonToTileQueryParams { - lat: number; - lon: number; -} +export interface GetLatLonToTileQueryParams extends WGS84Coordinate {} export interface GetTileToLatLonQueryParams { tile: string; sub_tile_number: number[]; } -export interface GetLatLonToMgrsQueryParams { - lat: number; - lon: number; +export interface GetLatLonToMgrsQueryParams extends WGS84Coordinate { accuracy?: number; } export interface GetMgrsToLatLonQueryParams { diff --git a/src/latLon/models/latLonManager.ts b/src/latLon/models/latLonManager.ts index cbe322e5..df0214bc 100644 --- a/src/latLon/models/latLonManager.ts +++ b/src/latLon/models/latLonManager.ts @@ -8,7 +8,7 @@ import { convertWgs84ToUTM, validateTile, validateWGS84Coordinate } from '../../ import { convertTilesToUTM, getSubTileByBottomLeftUtmCoor, validateResult } from '../utlis'; import { BadRequestError } from '../../common/errors'; import { Tile } from '../../tile/models/tile'; -import { FeatureCollection } from '../../common/interfaces'; +import { FeatureCollection, WGS84Coordinate } from '../../common/interfaces'; @injectable() export class LatLonManager { @@ -22,7 +22,7 @@ export class LatLonManager { this.dbSchema = this.config.get('db.postgresql.schema'); } - public async latLonToTile({ lat, lon }: { lat: number; lon: number }): Promise<{ + public async latLonToTile({ lat, lon }: WGS84Coordinate): Promise<{ tileName: string; subTileNumber: number[]; }> { diff --git a/src/latLon/utlis/index.ts b/src/latLon/utlis/index.ts index ae6e67de..aedc39ef 100644 --- a/src/latLon/utlis/index.ts +++ b/src/latLon/utlis/index.ts @@ -89,7 +89,7 @@ export const getSubTileByBottomLeftUtmCoor = ( if (typeof coordiante === 'string') { throw new Error('coordinate is string'); } - polygon.coordinates[0].push([coordiante.lng, coordiante.lat]); + polygon.coordinates[0].push([coordiante.lon, coordiante.lat]); } result.features[0].geometry = polygon; diff --git a/src/route/DAL/queries.ts b/src/route/DAL/queries.ts index 628a0ef3..c07f5309 100644 --- a/src/route/DAL/queries.ts +++ b/src/route/DAL/queries.ts @@ -1,7 +1,6 @@ import { estypes } from '@elastic/elasticsearch'; import { boundingBox, geoDistance } from '../../common/elastic/utils'; -import { Route } from '../models/route'; -import { GeoContext } from '../../common/interfaces'; +import { GeoContext, WGS84Coordinate } from '../../common/interfaces'; export interface RouteQueryParams { commandName: string; @@ -61,17 +60,7 @@ export const queryForControlPointInRoute = (params: RouteQueryParams & Required< }, }, ...(params.geo?.bbox ? [boundingBox(params.geo.bbox)] : []), - ...(params.geo?.radius - ? [ - geoDistance( - params.geo as { - radius: number; - lon: number; - lat: number; - } - ), - ] - : []), + ...(params.geo?.radius ?? 0 ? [geoDistance(params.geo as WGS84Coordinate & { radius: number })] : []), ], filter: [ { diff --git a/src/route/controllers/routeController.ts b/src/route/controllers/routeController.ts index 2232fde4..ce388fd3 100644 --- a/src/route/controllers/routeController.ts +++ b/src/route/controllers/routeController.ts @@ -5,20 +5,11 @@ import { RequestHandler } from 'express'; import httpStatus from 'http-status-codes'; import { injectable, inject } from 'tsyringe'; import { SERVICES } from '../../common/constants'; - import { RouteManager } from '../models/routeManager'; import { Route } from '../models/route'; -import { GeoContext } from '../../common/interfaces'; +import { FeatureCollection, GeoContext } from '../../common/interfaces'; -type GetResourceHandler = RequestHandler< - undefined, - { - type: string; - features: (Route | undefined)[]; - }, - undefined, - GetRoutesQueryParams ->; +type GetRoutesHandler = RequestHandler, undefined, GetRoutesQueryParams>; export interface GetRoutesQueryParams { command_name: string; @@ -40,7 +31,7 @@ export class RouteController { this.createdResourceCounter = meter.createCounter('created_resource'); } - public getRoutes: GetResourceHandler = async (req, res, next) => { + public getRoutes: GetRoutesHandler = async (req, res, next) => { try { const { command_name: commandName, control_point, geo_context, reduce_fuzzy_match, size } = req.query; const response = await this.manager.getRoutes( diff --git a/src/tile/controllers/tileController.ts b/src/tile/controllers/tileController.ts index 27992fda..3e86c335 100644 --- a/src/tile/controllers/tileController.ts +++ b/src/tile/controllers/tileController.ts @@ -5,16 +5,13 @@ import { RequestHandler } from 'express'; import httpStatus from 'http-status-codes'; import { injectable, inject } from 'tsyringe'; import { SERVICES } from '../../common/constants'; - import { TileManager } from '../models/tileManager'; import { Tile } from '../models/tile'; +import { FeatureCollection } from '../../common/interfaces'; -type GetResourceHandler = RequestHandler< +type GetTilesHandler = RequestHandler< undefined, - | { - type: string; - features: (Tile | undefined)[]; - } + | FeatureCollection | { type: string; message: string; @@ -42,7 +39,7 @@ export class TileController { this.createdResourceCounter = meter.createCounter('created_resource'); } - public getTiles: GetResourceHandler = async (req, res, next) => { + public getTiles: GetTilesHandler = async (req, res, next) => { try { const { tile, sub_tile, reduce_fuzzy_match, size } = req.query; const response = await this.manager.getTiles( diff --git a/tests/unit/common/utils.spec.ts b/tests/unit/common/utils.spec.ts index 62076f0b..df63e54c 100644 --- a/tests/unit/common/utils.spec.ts +++ b/tests/unit/common/utils.spec.ts @@ -10,6 +10,7 @@ import { } from '../../../src/common/utils'; import config from '../../../config/test.json'; import { FIELDS } from '../../../src/common/constants'; +import { WGS84Coordinate } from '../../../src/common/interfaces'; import { itemElasticResponse, itemExpectedFormattedResponse, @@ -40,7 +41,7 @@ describe('utils', () => { it('should convert UTM to WGS84', () => { const result = convertUTMToWgs84(630048, 4330433, 29); - expect(result).toMatchObject({ lat: 39.11335578352079, lng: -7.495780486809503 }); + expect(result).toMatchObject({ lat: 39.11335578352079, lon: -7.495780486809503 }); }); it('should convert WGS84 to UTM', () => { @@ -60,7 +61,7 @@ describe('utils', () => { expect(validateTile(tile as never)).toBe(expected); }); - test.each([ + test.each<[WGS84Coordinate, boolean]>([ [{ lon: 50, lat: 50 }, true], [{ lon: 190, lat: 50 }, false], [{ lon: 50, lat: 190 }, false],