Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create geocoding function #14

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_MAPS_API_KEY = process.env.GOOGLE_MAPS_API_KEY || '';
5 changes: 4 additions & 1 deletion src/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -35,7 +36,9 @@ export const jobRoutes: FastifyPluginAsync = async (app) => {
handler: async (req, reply) => {
const idOrIds = req.query['ids[]'];

const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];
const geoCodingService = new GeocodingService(app.orm);

const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds]

const manager = Container.get(EntityManager);
return await manager.findByIds(Job, ids);
Expand Down
45 changes: 45 additions & 0 deletions src/services/GeocodingService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 {
private orm: Connection;

constructor (orm: Connection) {
this.orm = orm;
}

public async getCoordsFromAddress (address: string) {
// Check the database for any coords associated with this 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());
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;
// TODO: save results to database
} catch (error) {
console.log(error);
}
}

return true;
}
}