done with deocoding api + generating data mock files

This commit is contained in:
Kfir Dayan 2023-04-24 21:25:01 +03:00
parent 91ee1479b0
commit c7f6066365
11 changed files with 91 additions and 15 deletions

9
README.md Normal file
View file

@ -0,0 +1,9 @@
examples -
POST localhost:3000/resolve-address
{
"searchTerm": "38%20Upper%20Montagu%20Street%2C%20London%20W1H%201LJ%2C%20United%20Kingdom"
}

1
data/holidays.json Normal file
View file

@ -0,0 +1 @@
["2023-05-01","2023-06-12","2023-09-25"]

1
data/timeSlots.json Normal file
View file

@ -0,0 +1 @@
["08:00 - 10:00","10:00 - 12:00","12:00 - 14:00","14:00 - 16:00","16:00 - 18:00","18:00 - 20:00"]

9
package-lock.json generated
View file

@ -13,6 +13,7 @@
"@types/express": "^4.17.17",
"axios": "^1.3.6",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"express": "^4.18.2"
},
"devDependencies": {
@ -450,6 +451,14 @@
"node": ">=0.3.1"
}
},
"node_modules/dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==",
"engines": {
"node": ">=12"
}
},
"node_modules/dynamic-dedupe": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dynamic-dedupe/-/dynamic-dedupe-0.3.0.tgz",

View file

@ -15,6 +15,7 @@
"@types/express": "^4.17.17",
"axios": "^1.3.6",
"body-parser": "^1.20.2",
"dotenv": "^16.0.3",
"express": "^4.18.2"
},
"devDependencies": {

View file

@ -7,4 +7,4 @@ app.use(express.json());
app.use(routes);
export default app;
export default app;

View file

@ -1,19 +1,25 @@
import axios from 'axios';
import { Address } from './types';
const dotenv = require('dotenv');
dotenv.config();
const GEOCODING_API_KEY = process.env.GEOCODING_API_KEY;
export interface Address {
street: string;
line1: string;
line2: string;
country: string;
postcode: string;
}
export const resolveAddress = async (searchTerm: string): Promise<Address> => {
const response = await axios.get(`https://api.geoapify.com/v1/geocode/search?text=${searchTerm}&apiKey=${GEOCODING_API_KEY}`);
return response.data.features[0].properties;
// will return at least - street, line1, line2, country, postcode
export const resolveAddress = async (searchTerm: string): Promise<void | Address> => {
const response = await axios.get(`https://api.geoapify.com/v1/geocode/search?text=${searchTerm}&format=json&apiKey=${GEOCODING_API_KEY}`);
if (response.data.results.length > 0) {
const result = response.data.results[0];
return {
country: result.country,
street: result.street,
line1: result.address_line1,
line2: result.address_line2,
postcode: result.postcode
}
} else {
throw new Error('No results found');
}
};

View file

@ -1,8 +1,14 @@
import { Request, Response } from 'express';
import { resolveAddress } from './geocoding';
import { Address } from './types';
export const resolveAddressHandler = (req: Request, res: Response) => {
// TODO: Implement resolve address functionality using
const address = resolveAddress(req.body.searchTerm);
address.then((result) => {
res.status(200).json(result);
})
};
export const timeslotsHandler = (req: Request, res: Response) => {

View file

@ -1,7 +1,43 @@
import app from './app';
import fs from 'fs';
const env = process.env.NODE_ENV || 'development';
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("STARTING ... ")
if(env === 'development') {
console.log("Generating mock data files")
generateMockDataFiles();
}
console.log(`Server is running on port ${PORT}`);
});
});
const generateMockDataFiles = () => {
if (!fs.existsSync('./data')) {
fs.mkdirSync('./data');
}
if (!fs.existsSync('./data/timeSlots.json')) {
const timeSlots = [
"08:00 - 10:00",
"10:00 - 12:00",
"12:00 - 14:00",
"14:00 - 16:00",
"16:00 - 18:00",
"18:00 - 20:00"
]
fs.writeFileSync('./data/timeSlots.json', JSON.stringify(timeSlots));
}
if (!fs.existsSync('./data/holidays.json')) {
const holidays = [
"2023-05-01",
"2023-06-12",
"2023-09-25"
]
fs.writeFileSync('./data/holidays.json', JSON.stringify(holidays));
}
}

View file

@ -10,4 +10,4 @@ router.post('/timeslots', timeslotsHandler);
router.post('/deliveries', deliveriesHandler);
router.delete('/deliveries/:id', cancelDeliveryHandler);
export default router;
export default router;

7
src/types/index.ts Normal file
View file

@ -0,0 +1,7 @@
export interface Address {
street: string;
line1: string;
line2: string;
country: string;
postcode: string;
}