Skip to content

Commit 699cd01

Browse files
authored
COMPENF-648-Changed location parameters from param types to query types (#115)
1 parent de86cc1 commit 699cd01

File tree

6 files changed

+74
-44
lines changed

6 files changed

+74
-44
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Param } from '@nestjs/common';
1+
import { Controller, Get, Query } from '@nestjs/common';
22
import { BcGeoCoderService } from './bc_geo_coder.service';
33
import { Roles } from '../../auth/decorators/roles.decorator';
44
import { Role } from '../../enum/role.enum';
@@ -7,10 +7,10 @@ import { Role } from '../../enum/role.enum';
77
export class BcGeoCoderController {
88
constructor(private readonly bcGeoCoderService: BcGeoCoderService) {}
99

10-
@Get('/address/:query')
10+
@Get('/address')
1111
@Roles(Role.COS_OFFICER)
12-
findAll(@Param('query') query: string) {
13-
return this.bcGeoCoderService.findAll(query);
12+
findAll(@Query('localityName') localityName?: string, @Query('addressString') addressString?: string) {
13+
return this.bcGeoCoderService.findAll(localityName,addressString);
1414
}
1515

1616
}

backend/src/external_api/bc_geo_coder/bc_geo_coder.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("BcGeoCoderService", () => {
5050

5151
jest.spyOn(httpService, 'get').mockReturnValue(of(mockResponse));
5252
const features: Feature = await service.findAll(
53-
"2975 Jutland Road, Victoria, BC"
53+
"Victoria","2975 Jutland Road"
5454
);
5555

5656
expect(features.features[0].geometry.coordinates).toEqual(mockCoordinates);
Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
1-
import { Injectable, Logger } from '@nestjs/common';
2-
import { HttpService } from '@nestjs/axios';
3-
import { catchError, firstValueFrom } from 'rxjs';
4-
import { AxiosError} from 'axios';
5-
import { Feature } from 'src/types/bc_geocoder/bcGeocoderType';
1+
import { Injectable, Logger } from "@nestjs/common";
2+
import { HttpService } from "@nestjs/axios";
3+
import { catchError, firstValueFrom } from "rxjs";
4+
import { AxiosError } from "axios";
5+
import { Feature } from "src/types/bc_geocoder/bcGeocoderType";
66

77
@Injectable()
88
export class BcGeoCoderService {
9+
private readonly logger = new Logger(BcGeoCoderService.name);
910

10-
private readonly logger = new Logger(BcGeoCoderService.name);
11-
12-
constructor(private readonly httpService: HttpService) {}
13-
14-
async findAll(query: string): Promise<Feature> {
15-
const maxResults = 10;
16-
const apiUrl = `${process.env.BC_GEOCODER_API_URL}/addresses.json?addressString=${query}&locationDescriptor=any&maxResults=${maxResults}&interpolation=adaptive&echo=true&brief=false&autoComplete=true&setBack=0&outputSRS=4326&minScore=2&provinceCode=BC`;
17-
18-
const { data } = await firstValueFrom(
19-
this.httpService.get<any>(apiUrl).pipe(
20-
catchError((error: AxiosError) => {
21-
this.logger.error(error.response.data);
22-
throw 'Error getting BC Geocoder response';
23-
}),
24-
),
25-
);
26-
return data;
11+
constructor(private readonly httpService: HttpService) {}
2712

13+
// given a localityName (community, for example) and an address, return the Feature given by BC Geocoder
14+
async findAll(localityName: string, addressString: string): Promise<Feature> {
15+
const maxResults = 10;
16+
let apiUrl: string;
17+
this.logger.debug(
18+
`Calling BC Geocoder. Parameters sent to backend were localityName: ${localityName} and addressString: ${addressString}`
19+
);
20+
if (addressString && localityName) {
21+
this.logger.debug(
22+
`Calling BC Geocoder with address ${addressString} and community ${localityName}`
23+
);
24+
apiUrl = `${process.env.BC_GEOCODER_API_URL}/addresses.json?addressString=${addressString}&locationDescriptor=any&maxResults=${maxResults}&interpolation=adaptive&echo=true&brief=false&autoComplete=true&setBack=0&outputSRS=4326&minScore=2&localityName=${localityName}&provinceCode=BC`;
25+
} else if (localityName) {
26+
this.logger.debug(`Calling BC Geocoder with community ${localityName}`);
27+
apiUrl = `${process.env.BC_GEOCODER_API_URL}/addresses.json?locationDescriptor=any&maxResults=${maxResults}&interpolation=adaptive&echo=true&brief=false&autoComplete=true&setBack=0&outputSRS=4326&minScore=2&localityName=${localityName}&provinceCode=BC`;
28+
} else if (addressString && addressString.length > 0) {
29+
this.logger.debug(`Calling BC Geocoder with address ${addressString}`);
30+
apiUrl = `${process.env.BC_GEOCODER_API_URL}/addresses.json?addressString=${addressString}&locationDescriptor=any&maxResults=${maxResults}&interpolation=adaptive&echo=true&brief=false&autoComplete=true&setBack=0&outputSRS=4326&minScore=2&provinceCode=BC`;
2831
}
29-
32+
if (apiUrl) {
33+
const { data } = await firstValueFrom(
34+
this.httpService.get<any>(apiUrl).pipe(
35+
catchError((error: AxiosError) => {
36+
this.logger.error(error.response);
37+
throw "Error getting BC Geocoder response";
38+
})
39+
)
40+
);
41+
return data;
42+
}
43+
}
3044
}

frontend/src/app/components/common/bc-geocoder-autocomplete.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FC, useEffect, useState } from "react";
22
import CreatableSelect from "react-select/creatable";
33
import { useAppDispatch, useAppSelector } from "../../hooks/hooks";
44
import {
5-
getComplaintLocation,
5+
getComplaintLocationByAddress,
66
selectComplaintLocation,
77
} from "../../store/reducers/complaints";
88

@@ -37,7 +37,7 @@ export const BCGeocoderAutocomplete: FC<Props> = ({
3737

3838
useEffect(() => {
3939
const fetchAddresses = async (inputValue: string) => {
40-
dispatch(getComplaintLocation(`${inputValue}`));
40+
dispatch(getComplaintLocationByAddress(inputValue));
4141

4242
try {
4343
if (complaintLocation) {
@@ -49,15 +49,15 @@ export const BCGeocoderAutocomplete: FC<Props> = ({
4949
if (options) {
5050
setAddressOptions(options);
5151
}
52-
} else {
53-
console.log("Feature length 0");
5452
}
5553
}
5654
} catch (error) {
5755
console.error("Error fetching addresses:", error);
5856
}
5957
};
60-
fetchAddresses(inputValue);
58+
if (inputValue) {
59+
fetchAddresses(inputValue);
60+
}
6161
}, [inputValue, maxResults]);
6262

6363
return (

frontend/src/app/components/containers/complaints/details/complaint-location.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export const ComplaintLocation: FC<Props> = ({ complaintType, draggable }) => {
3333
lat = +coordinates[0];
3434
lng = +coordinates[1];
3535
} else if (complaintLocation) {
36-
lat = complaintLocation?.features[0].geometry.coordinates[1];
37-
lng = complaintLocation?.features[0].geometry.coordinates[0];
36+
lat = complaintLocation?.features[0].geometry?.coordinates[1];
37+
lng = complaintLocation?.features[0].geometry?.coordinates[0];
3838
}
3939
return (
4040
<div className="comp-complaint-details-location-block">

frontend/src/app/store/reducers/complaints.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ export const getWildlifeComplaintByComplaintIdentifier =
224224
location_summary_text,
225225
cos_geo_org_unit: { area_name },
226226
} = ceComplaint;
227-
228-
dispatch(
229-
getComplaintLocation(`${location_summary_text ?? ""} ${area_name}`)
230-
);
227+
dispatch(getComplaintLocation(area_name, location_summary_text));
231228
}
232229

233230
dispatch(setComplaint({ ...response }));
@@ -258,9 +255,7 @@ export const getAllegationComplaintByComplaintIdentifier =
258255
cos_geo_org_unit: { area_name },
259256
} = ceComplaint;
260257

261-
dispatch(
262-
getComplaintLocation(`${location_summary_text ?? ""} ${area_name}`)
263-
);
258+
dispatch(getComplaintLocation(area_name, location_summary_text));
264259
}
265260

266261
dispatch(setComplaint({ ...response }));
@@ -293,14 +288,35 @@ export const getZoneAtAGlanceStats =
293288
}
294289
};
295290

296-
export const getComplaintLocation =
291+
export const getComplaintLocationByAddress =
297292
(address: string): AppThunk =>
298293
async (dispatch) => {
299294
try {
300295
const parameters = generateApiParameters(
301-
`${config.API_BASE_URL}/bc-geo-coder/address/${address}`
296+
`${config.API_BASE_URL}/bc-geo-coder/address?addressString=${address}`
302297
);
298+
const response = await get<Feature>(dispatch, parameters);
299+
dispatch(setComplaintLocation(response));
300+
} catch (error) {
301+
//-- handle the error message
302+
}
303+
};
304+
305+
export const getComplaintLocation =
306+
(area: string, address?: string): AppThunk =>
307+
async (dispatch) => {
308+
try {
309+
let parameters;
303310

311+
if (address && area) {
312+
parameters = generateApiParameters(
313+
`${config.API_BASE_URL}/bc-geo-coder/address?localityName=${area}&addressString=${address}`
314+
);
315+
} else {
316+
parameters = generateApiParameters(
317+
`${config.API_BASE_URL}/bc-geo-coder/address?localityName=${area}`
318+
);
319+
}
304320
const response = await get<Feature>(dispatch, parameters);
305321
dispatch(setComplaintLocation(response));
306322
} catch (error) {

0 commit comments

Comments
 (0)