File tree Expand file tree Collapse file tree 11 files changed +219
-13
lines changed Expand file tree Collapse file tree 11 files changed +219
-13
lines changed Original file line number Diff line number Diff line change @@ -26,3 +26,8 @@ DB_CONNECTION_STRING=[db_client]://[db_username]:[db_user_password]@localhost:[d
26
26
DB_DIALECT = pg
27
27
DB_POOL_MIN = 2
28
28
DB_POOL_MAX = 10
29
+
30
+ #
31
+ # API
32
+ #
33
+ GOOGLE_MAPS_API_KEY = YOUR_API_KEY
Original file line number Diff line number Diff line change 32
32
"@fastify/static" : " 6.10.2" ,
33
33
"@fastify/swagger" : " 8.9.0" ,
34
34
"@fastify/swagger-ui" : " 1.9.3" ,
35
+ "@googlemaps/google-maps-services-js" : " 3.3.38" ,
35
36
"@types/bcryptjs" : " 2.4.3" ,
36
37
"@types/convict" : " 6.1.3" ,
37
- "@types/swagger-jsdoc" : " 6.0.1" ,
38
38
"@types/nodemailer" : " 6.4.9" ,
39
+ "@types/swagger-jsdoc" : " 6.0.1" ,
39
40
"bcryptjs" : " 2.4.3" ,
40
41
"convict" : " 6.2.4" ,
41
42
"dotenv" : " 16.3.1" ,
42
43
"drizzle-kit" : " 0.19.12" ,
43
44
"drizzle-orm" : " 0.28.2" ,
44
45
"fastify" : " 4.21.0" ,
45
46
"fastify-plugin" : " 4.5.1" ,
46
- "jose" : " 4.14.4" ,
47
47
"handlebars" : " 4.7.8" ,
48
+ "jose" : " 4.14.4" ,
48
49
"nodemailer" : " 6.9.4" ,
49
50
"pg" : " 8.11.3" ,
50
51
"pino" : " 8.15.0" ,
Original file line number Diff line number Diff line change @@ -133,6 +133,14 @@ class Config implements IConfig {
133
133
default : null ,
134
134
} ,
135
135
} ,
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
+ } ,
136
144
} ) ;
137
145
}
138
146
}
Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ type EnvironmentSchema = {
22
22
SMTP_TLS : boolean ;
23
23
SENDGRID_SENDER_EMAIL : string ;
24
24
} ;
25
+ API : {
26
+ GOOGLE_MAPS_API_KEY : string ;
27
+ } ;
25
28
} ;
26
29
27
30
export { type EnvironmentSchema } ;
Original file line number Diff line number Diff line change
1
+ const METERS_IN_ONE_KM = 1000 ;
2
+
3
+ export { METERS_IN_ONE_KM } ;
Original file line number Diff line number Diff line change
1
+ export { TravelMode } from '@googlemaps/google-maps-services-js' ;
Original file line number Diff line number Diff line change
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 } ;
Original file line number Diff line number Diff line change
1
+ export {
2
+ type Client ,
3
+ type Distance ,
4
+ } from '@googlemaps/google-maps-services-js' ;
Original file line number Diff line number Diff line change
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 } ;
Original file line number Diff line number Diff line change
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 } ;
You can’t perform that action at this time.
0 commit comments