From 7bd8728770f17a12444cabae6824b45c94d32725 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 10 Jan 2022 20:01:31 -0600 Subject: [PATCH 1/8] wip: create rough draft geocoding function --- src/config.ts | 1 + src/utils.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/config.ts b/src/config.ts index fcf649b..a02ff74 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,3 +18,4 @@ export const REDIS_HOST = process.env.REDIS_HOST || 'localhost'; export const REDIS_PORT = parseNumber(process.env.REDIS_PORT) || 6379; export const REDIS_PASSWORD = process.env.REDIS_PASSWORD || ''; export const GOOGLE_RECAPTCHA_SECRET_KEY = process.env.GOOGLE_RECAPTCHA_SECRET_KEY || ''; +export const GOOGLE_GEOCODING_SECRET_KEY = process.env.GOOGLE_GEOCODING_SECRET_KEY || ''; diff --git a/src/utils.ts b/src/utils.ts index b36a611..923ab8a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,6 @@ +import fetch from 'cross-fetch'; +import { GOOGLE_GEOCODING_SECRET_KEY } from 'config'; + export function parseNumber(str?: string): number | undefined { const parsed = Number(str); @@ -7,3 +10,26 @@ export function parseNumber(str?: string): number | undefined { export function _throw(msg: string): never { throw msg; } + +export async function useThisFunctionWheneverYouFindYourselfInTheSituationWhereYouHaveAnAddressButYouWantCoordinates(address: string): Promise { + // Prepare the address for submission to Google's API + address = address.replace(/ /g, "%20"); + let requestUrl: string = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${GOOGLE_MAPS_API_KEY}` + + // Submit it to the API + const geocodingRes = await fetch(requestUrl) + .catch(err => { + console.log(err); + }); + + console.log(geocodingRes); + + // Process the response + const coordinates: object = { + lat: geocodingRes.geometry.location.lat, + lng: geocodingRes.geometry.location.lng + }; + + // Output the resulting Coordinates + return coordinates; +} From a7747918bff7cd5bed53e16d03ed7fa666c459bf Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 17 Jan 2022 13:27:25 -0600 Subject: [PATCH 2/8] feat: finished getCoordsFromAddress function --- src/utils.ts | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 923ab8a..be04a52 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import fetch from 'cross-fetch'; -import { GOOGLE_GEOCODING_SECRET_KEY } from 'config'; +import { GOOGLE_GEOCODING_SECRET_KEY } from './config'; export function parseNumber(str?: string): number | undefined { const parsed = Number(str); @@ -11,25 +11,21 @@ export function _throw(msg: string): never { throw msg; } -export async function useThisFunctionWheneverYouFindYourselfInTheSituationWhereYouHaveAnAddressButYouWantCoordinates(address: string): Promise { - // Prepare the address for submission to Google's API - address = address.replace(/ /g, "%20"); - let requestUrl: string = `https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${GOOGLE_MAPS_API_KEY}` - - // Submit it to the API - const geocodingRes = await fetch(requestUrl) - .catch(err => { - console.log(err); - }); - - console.log(geocodingRes); - - // Process the response - const coordinates: object = { - lat: geocodingRes.geometry.location.lat, - lng: geocodingRes.geometry.location.lng - }; - - // Output the resulting Coordinates - return coordinates; +export async function getCoordsFromAddress(address: string): Promise { + const urlAddress = address.replace(/ /g, "%20"); + const requestUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_GEOCODING_SECRET_KEY}` + const coords = { + lat: '0', + lng: '0', + } + try { + const geocodingRes = await fetch(requestUrl) + .then((res) => res.json()); + const coordRes = geocodingRes.results[0].geometry.location + coords.lat = coordRes.lat; + coords.lng = coordRes.lng; + } catch (error) { + console.log(error); + } + return coords; } From 47f6773dd02e626bd9a9f6b84f5a5c141a4426f3 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 17 Jan 2022 18:07:54 -0600 Subject: [PATCH 3/8] wip: add todos --- src/services/gmail-service.ts | 0 src/utils.ts | 3 +++ 2 files changed, 3 insertions(+) create mode 100644 src/services/gmail-service.ts diff --git a/src/services/gmail-service.ts b/src/services/gmail-service.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/utils.ts b/src/utils.ts index be04a52..5f26fe9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,6 +11,9 @@ export function _throw(msg: string): never { throw msg; } +// TODO: Query the database for address first, then hit the database. +// TODO: Move this to a dedicated services file + export async function getCoordsFromAddress(address: string): Promise { const urlAddress = address.replace(/ /g, "%20"); const requestUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_GEOCODING_SECRET_KEY}` From 05caea5adf76c71c0a9d31d5ed1896dc845e58c4 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 17 Jan 2022 18:14:30 -0600 Subject: [PATCH 4/8] wip: add todos --- src/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 5f26fe9..b13e096 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,7 +11,8 @@ export function _throw(msg: string): never { throw msg; } -// TODO: Query the database for address first, then hit the database. +// TODO: Query the database for address first, then hit the database.:q + // TODO: Move this to a dedicated services file export async function getCoordsFromAddress(address: string): Promise { From 1d3cbe049a804ad21dba9aa1ad1dec72473cdbf2 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 17 Jan 2022 18:21:57 -0600 Subject: [PATCH 5/8] fix: rename erroneous file --- src/services/{gmail-service.ts => geocoding-service.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/services/{gmail-service.ts => geocoding-service.ts} (100%) diff --git a/src/services/gmail-service.ts b/src/services/geocoding-service.ts similarity index 100% rename from src/services/gmail-service.ts rename to src/services/geocoding-service.ts From f24ae46ae9dfe9d5bccb625d8929d60aec3a1703 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 24 Jan 2022 18:08:22 -0600 Subject: [PATCH 6/8] wip: export logic to new geocoding class --- src/config.ts | 2 +- src/services/GeocodingService.ts | 29 +++++++++++++++++++++++++++++ src/services/geocoding-service.ts | 0 src/utils.ts | 26 -------------------------- 4 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 src/services/GeocodingService.ts delete mode 100644 src/services/geocoding-service.ts diff --git a/src/config.ts b/src/config.ts index a02ff74..d656ff2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,4 +18,4 @@ export const REDIS_HOST = process.env.REDIS_HOST || 'localhost'; export const REDIS_PORT = parseNumber(process.env.REDIS_PORT) || 6379; export const REDIS_PASSWORD = process.env.REDIS_PASSWORD || ''; export const GOOGLE_RECAPTCHA_SECRET_KEY = process.env.GOOGLE_RECAPTCHA_SECRET_KEY || ''; -export const GOOGLE_GEOCODING_SECRET_KEY = process.env.GOOGLE_GEOCODING_SECRET_KEY || ''; +export const GOOGLE_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY || ''; diff --git a/src/services/GeocodingService.ts b/src/services/GeocodingService.ts new file mode 100644 index 0000000..0601e4e --- /dev/null +++ b/src/services/GeocodingService.ts @@ -0,0 +1,29 @@ +import {GOOGLE_MAPS_API_KEY} from "../config"; +import fetch from "cross-fetch"; + +export default class GeocodingService { + public async getCoordsFromAddress(address: string): Promise { + // Check the database for any coords associated with this address. + let coords = await LocationModel.search(address); + // Otherwise, ask Google for the coords of this address. + if (!coords) { + const urlAddress = address.replace(/ /g, "%20"); + const requestUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_MAPS_API_KEY}` + coords = { + lat: '0', + lng: '0', + } + try { + const geocodingRes = await fetch(requestUrl) + .then((res) => res.json()); + const coordRes = geocodingRes.results[0].geometry.location + coords.lat = coordRes.lat; + coords.lng = coordRes.lng; + } catch (error) { + console.log(error); + } + } + + return coords; + } +} diff --git a/src/services/geocoding-service.ts b/src/services/geocoding-service.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils.ts b/src/utils.ts index b13e096..b36a611 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,3 @@ -import fetch from 'cross-fetch'; -import { GOOGLE_GEOCODING_SECRET_KEY } from './config'; - export function parseNumber(str?: string): number | undefined { const parsed = Number(str); @@ -10,26 +7,3 @@ export function parseNumber(str?: string): number | undefined { export function _throw(msg: string): never { throw msg; } - -// TODO: Query the database for address first, then hit the database.:q - -// TODO: Move this to a dedicated services file - -export async function getCoordsFromAddress(address: string): Promise { - const urlAddress = address.replace(/ /g, "%20"); - const requestUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_GEOCODING_SECRET_KEY}` - const coords = { - lat: '0', - lng: '0', - } - try { - const geocodingRes = await fetch(requestUrl) - .then((res) => res.json()); - const coordRes = geocodingRes.results[0].geometry.location - coords.lat = coordRes.lat; - coords.lng = coordRes.lng; - } catch (error) { - console.log(error); - } - return coords; -} From 32e426b882e17112233cda9827c523923f82b0d7 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Mon, 24 Jan 2022 19:36:58 -0600 Subject: [PATCH 7/8] wip: attempt to query the database --- src/routes/jobs.ts | 2 ++ src/services/GeocodingService.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/routes/jobs.ts b/src/routes/jobs.ts index 082c383..c225398 100644 --- a/src/routes/jobs.ts +++ b/src/routes/jobs.ts @@ -3,6 +3,7 @@ import type { FromSchema } from 'json-schema-to-ts'; import { Job } from '../entities/Job'; import { Container } from 'typedi'; import { EntityManager } from 'typeorm'; +import GeocodingService from "../services/GeocodingService"; const findJobParams = { type: 'object', @@ -36,6 +37,7 @@ export const jobRoutes: FastifyPluginAsync = async (app) => { const idOrIds = req.query['ids[]']; const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds]; + GeocodingService.getCoordsFromAddress('1840 S Weller Ave, Springfield, MO 65804'); const manager = Container.get(EntityManager); return await manager.findByIds(Job, ids); diff --git a/src/services/GeocodingService.ts b/src/services/GeocodingService.ts index 0601e4e..6f976c0 100644 --- a/src/services/GeocodingService.ts +++ b/src/services/GeocodingService.ts @@ -2,12 +2,16 @@ import {GOOGLE_MAPS_API_KEY} from "../config"; import fetch from "cross-fetch"; export default class GeocodingService { - public async getCoordsFromAddress(address: string): Promise { + constructor (app) { + this.app = app; + } + + public async getCoordsFromAddress (address: string) { // Check the database for any coords associated with this address. - let coords = await LocationModel.search(address); + let coords = await app.orm.manager.findByAddress(address); // Otherwise, ask Google for the coords of this address. if (!coords) { - const urlAddress = address.replace(/ /g, "%20"); + const urlAddress = encodeURIComponent(address.trim()); const requestUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_MAPS_API_KEY}` coords = { lat: '0', @@ -19,6 +23,7 @@ export default class GeocodingService { const coordRes = geocodingRes.results[0].geometry.location coords.lat = coordRes.lat; coords.lng = coordRes.lng; + // TODO: save results to database } catch (error) { console.log(error); } From e7f97d64cfdf635f43ad534cf02494a083a17503 Mon Sep 17 00:00:00 2001 From: Ethan Zitting Date: Sun, 5 Jun 2022 19:31:30 -0500 Subject: [PATCH 8/8] wip: fix merge conflicts --- src/routes/jobs.ts | 5 +++-- src/services/GeocodingService.ts | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/routes/jobs.ts b/src/routes/jobs.ts index c225398..cdf105f 100644 --- a/src/routes/jobs.ts +++ b/src/routes/jobs.ts @@ -36,8 +36,9 @@ export const jobRoutes: FastifyPluginAsync = async (app) => { handler: async (req, reply) => { const idOrIds = req.query['ids[]']; - const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds]; - GeocodingService.getCoordsFromAddress('1840 S Weller Ave, Springfield, MO 65804'); + const geoCodingService = new GeocodingService(app.orm); + + const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds] const manager = Container.get(EntityManager); return await manager.findByIds(Job, ids); diff --git a/src/services/GeocodingService.ts b/src/services/GeocodingService.ts index 6f976c0..99d95b5 100644 --- a/src/services/GeocodingService.ts +++ b/src/services/GeocodingService.ts @@ -1,14 +1,25 @@ import {GOOGLE_MAPS_API_KEY} from "../config"; import fetch from "cross-fetch"; +import {Connection} from "typeorm"; +import {Location} from "../entities/Location"; +import {getManager} from "typeorm"; export default class GeocodingService { - constructor (app) { - this.app = app; + private orm: Connection; + + constructor (orm: Connection) { + this.orm = orm; } public async getCoordsFromAddress (address: string) { // Check the database for any coords associated with this address. - let coords = await app.orm.manager.findByAddress(address); + const location = await getManager() + .createQueryBuilder(Location, "locations") + .where("locations.address = :address", { + address: '1840 S Weller Ave, Springfield, MO 65804' + }) + .getOne(); + // Otherwise, ask Google for the coords of this address. if (!coords) { const urlAddress = encodeURIComponent(address.trim()); @@ -29,6 +40,6 @@ export default class GeocodingService { } } - return coords; + return true; } }