Skip to content

Commit 9bebe3f

Browse files
authored
Merge pull request #240 from AlexVlaso/task/th-230-eval-order-price
th-230: Eval order price on the BE
2 parents 4d3217c + e312186 commit 9bebe3f

File tree

11 files changed

+219
-13
lines changed

11 files changed

+219
-13
lines changed

backend/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ DB_CONNECTION_STRING=[db_client]://[db_username]:[db_user_password]@localhost:[d
2626
DB_DIALECT=pg
2727
DB_POOL_MIN=2
2828
DB_POOL_MAX=10
29+
30+
#
31+
# API
32+
#
33+
GOOGLE_MAPS_API_KEY=YOUR_API_KEY

backend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@
3232
"@fastify/static": "6.10.2",
3333
"@fastify/swagger": "8.9.0",
3434
"@fastify/swagger-ui": "1.9.3",
35+
"@googlemaps/google-maps-services-js": "3.3.38",
3536
"@types/bcryptjs": "2.4.3",
3637
"@types/convict": "6.1.3",
37-
"@types/swagger-jsdoc": "6.0.1",
3838
"@types/nodemailer": "6.4.9",
39+
"@types/swagger-jsdoc": "6.0.1",
3940
"bcryptjs": "2.4.3",
4041
"convict": "6.2.4",
4142
"dotenv": "16.3.1",
4243
"drizzle-kit": "0.19.12",
4344
"drizzle-orm": "0.28.2",
4445
"fastify": "4.21.0",
4546
"fastify-plugin": "4.5.1",
46-
"jose": "4.14.4",
4747
"handlebars": "4.7.8",
48+
"jose": "4.14.4",
4849
"nodemailer": "6.9.4",
4950
"pg": "8.11.3",
5051
"pino": "8.15.0",

backend/src/libs/packages/config/config.package.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ class Config implements IConfig {
133133
default: null,
134134
},
135135
},
136+
API: {
137+
GOOGLE_MAPS_API_KEY: {
138+
doc: 'Key for Google maps API',
139+
format: String,
140+
env: 'GOOGLE_MAPS_API_KEY',
141+
default: null,
142+
},
143+
},
136144
});
137145
}
138146
}

backend/src/libs/packages/config/libs/types/environment-schema.type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type EnvironmentSchema = {
2222
SMTP_TLS: boolean;
2323
SENDGRID_SENDER_EMAIL: string;
2424
};
25+
API: {
26+
GOOGLE_MAPS_API_KEY: string;
27+
};
2528
};
2629

2730
export { type EnvironmentSchema };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const METERS_IN_ONE_KM = 1000;
2+
3+
export { METERS_IN_ONE_KM };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { TravelMode } from '@googlemaps/google-maps-services-js';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { METERS_IN_ONE_KM } from '../constants/constants.js';
2+
3+
const convertMetersToKm = (meters: number): number => {
4+
return meters / METERS_IN_ONE_KM;
5+
};
6+
7+
export { convertMetersToKm };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {
2+
type Client,
3+
type Distance,
4+
} from '@googlemaps/google-maps-services-js';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { TravelMode } from './libs/enums/enums.js';
2+
import { convertMetersToKm } from './libs/helpers/helpers.js';
3+
import { type Client, type Distance } from './libs/types/types.js';
4+
5+
class MapService {
6+
private client: Client;
7+
8+
private key: string;
9+
10+
public constructor(client: Client, key: string) {
11+
this.client = client;
12+
this.key = key;
13+
}
14+
15+
public async getDistance(
16+
startPoint: string,
17+
endPoint: string,
18+
): Promise<Distance> {
19+
const response = await this.client.distancematrix({
20+
params: {
21+
key: this.key,
22+
origins: [startPoint],
23+
destinations: [endPoint],
24+
mode: TravelMode.driving,
25+
},
26+
});
27+
const [resultArray] = response.data.rows;
28+
const [resultElement] = resultArray.elements;
29+
30+
return resultElement.distance;
31+
}
32+
33+
public async getPriceByDistance({
34+
startPoint,
35+
endPoint,
36+
pricePerKm,
37+
}: {
38+
startPoint: string;
39+
endPoint: string;
40+
pricePerKm: number;
41+
}): Promise<number> {
42+
const distance = await this.getDistance(startPoint, endPoint);
43+
const km = convertMetersToKm(distance.value);
44+
const orderPrice = (pricePerKm * km).toFixed(2);
45+
46+
return Number(orderPrice);
47+
}
48+
}
49+
50+
export { MapService };

backend/src/packages/map/map.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Client } from '@googlemaps/google-maps-services-js';
2+
3+
import { config } from '~/libs/packages/config/config.js';
4+
5+
import { MapService } from './map.service.js';
6+
7+
const mapService = new MapService(
8+
new Client({}),
9+
config.ENV.API.GOOGLE_MAPS_API_KEY,
10+
);
11+
12+
export { mapService };

0 commit comments

Comments
 (0)