-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validate radius geocoding preferences (#3718)
* fix: validate radius geocoding preferences * fix: move to more generic * fix: add test * fix: one more test * fix: change to true/false
- Loading branch information
1 parent
bf944b3
commit a8b13b0
Showing
9 changed files
with
383 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
backend/core/src/applications/services/geocoding.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { Test, TestingModule } from "@nestjs/testing" | ||
import { GeocodingService } from "./geocoding.service" | ||
import { Address } from "../../shared/entities/address.entity" | ||
import { getRepositoryToken } from "@nestjs/typeorm" | ||
import { Application } from "../entities/application.entity" | ||
import { ValidationMethod } from "../../multiselect-question/types/validation-method-enum" | ||
import { Listing } from "../../listings/entities/listing.entity" | ||
import { InputType } from "../../shared/types/input-type" | ||
|
||
describe("GeocodingService", () => { | ||
let service: GeocodingService | ||
const applicationRepoUpdate = jest.fn() | ||
const mockApplicationRepo = { | ||
createQueryBuilder: jest.fn(), | ||
update: applicationRepoUpdate, | ||
} | ||
const date = new Date() | ||
const listingAddress: Address = { | ||
id: "id", | ||
createdAt: date, | ||
updatedAt: date, | ||
city: "Washington", | ||
county: null, | ||
state: "DC", | ||
street: "1600 Pennsylvania Avenue", | ||
street2: null, | ||
zipCode: "20500", | ||
latitude: 38.8977, | ||
longitude: -77.0365, | ||
} | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
GeocodingService, | ||
{ | ||
provide: getRepositoryToken(Application), | ||
useValue: mockApplicationRepo, | ||
}, | ||
], | ||
}).compile() | ||
|
||
service = await module.resolve(GeocodingService) | ||
}) | ||
|
||
describe("verifyRadius", () => { | ||
it("should return 'unknown' if lat and long not there", () => { | ||
expect( | ||
service.verifyRadius( | ||
{ | ||
...listingAddress, | ||
latitude: null, | ||
longitude: null, | ||
}, | ||
5, | ||
listingAddress | ||
) | ||
).toBe("unknown") | ||
}) | ||
it("should return 'true' if within radius", () => { | ||
expect( | ||
service.verifyRadius( | ||
{ | ||
...listingAddress, | ||
latitude: 38.89485, | ||
longitude: -77.04251, | ||
}, | ||
5, | ||
listingAddress | ||
) | ||
).toBe("true") | ||
}) | ||
it("should return 'false' if not within radius", () => { | ||
expect( | ||
service.verifyRadius( | ||
{ | ||
...listingAddress, | ||
latitude: 39.284205, | ||
longitude: -76.621698, | ||
}, | ||
5, | ||
listingAddress | ||
) | ||
).toBe("false") | ||
}) | ||
it("should return 'true' if same lat long", () => { | ||
expect( | ||
service.verifyRadius( | ||
{ | ||
...listingAddress, | ||
}, | ||
5, | ||
listingAddress | ||
) | ||
).toBe("true") | ||
}) | ||
}) | ||
describe("validateRadiusPreferences", () => { | ||
const listing = { | ||
buildingAddress: listingAddress, | ||
listingMultiselectQuestions: [ | ||
{ | ||
multiselectQuestion: { | ||
options: [ | ||
{ | ||
text: "Geocoding option by radius", | ||
collectAddress: true, | ||
radiusSize: 5, | ||
validationMethod: ValidationMethod.radius, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} | ||
const preferenceAddress = { ...listingAddress, latitude: 38.89485, longitude: -77.04251 } | ||
const application = { | ||
id: "applicationId", | ||
preferences: [ | ||
{ | ||
key: "Geocoding preference", | ||
options: [ | ||
{ | ||
key: "Geocoding option by radius", | ||
checked: true, | ||
extraData: [ | ||
{ | ||
type: InputType.address, | ||
value: preferenceAddress, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
it("should save the validated value as extraData", async () => { | ||
await service.validateRadiusPreferences( | ||
(application as unknown) as Application, | ||
listing as Listing | ||
) | ||
expect(applicationRepoUpdate).toBeCalledWith( | ||
{ id: "applicationId" }, | ||
{ | ||
preferences: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
key: "Geocoding preference", | ||
options: [ | ||
{ | ||
checked: true, | ||
extraData: [ | ||
{ | ||
type: "address", | ||
value: preferenceAddress, | ||
}, | ||
{ key: "geocodingVerified", type: "text", value: "true" }, | ||
], | ||
key: "Geocoding option by radius", | ||
}, | ||
], | ||
}), | ||
]), | ||
} | ||
) | ||
}) | ||
}) | ||
}) |
101 changes: 101 additions & 0 deletions
101
backend/core/src/applications/services/geocoding.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { point } from "@turf/helpers" | ||
import buffer from "@turf/buffer" | ||
import booleanPointInPolygon from "@turf/boolean-point-in-polygon" | ||
import { InjectRepository } from "@nestjs/typeorm" | ||
import { Repository } from "typeorm" | ||
import { Address } from "../../shared/entities/address.entity" | ||
import { Application } from "../entities/application.entity" | ||
import { Listing } from "../../listings/entities/listing.entity" | ||
import { ValidationMethod } from "../../multiselect-question/types/validation-method-enum" | ||
import { MultiselectOption } from "../../multiselect-question/types/multiselect-option" | ||
import { ApplicationMultiselectQuestion } from "../entities/application-multiselect-question.entity" | ||
import { ApplicationMultiselectQuestionOption } from "../types/application-multiselect-question-option" | ||
import { InputType } from "../../shared/types/input-type" | ||
import { GeocodingValues } from "../../shared/types/geocoding-values" | ||
|
||
export class GeocodingService { | ||
constructor( | ||
@InjectRepository(Application) private readonly repository: Repository<Application> | ||
) {} | ||
|
||
public async validateGeocodingPreferences(application: Application, listing: Listing) { | ||
await this.validateRadiusPreferences(application, listing) | ||
} | ||
|
||
verifyRadius( | ||
preferenceAddress: Address, | ||
radius: number, | ||
listingAddress: Address | ||
): GeocodingValues { | ||
try { | ||
if (preferenceAddress.latitude && preferenceAddress.longitude) { | ||
const preferencePoint = point([ | ||
Number.parseFloat(preferenceAddress.longitude.toString()), | ||
Number.parseFloat(preferenceAddress.latitude.toString()), | ||
]) | ||
const listingPoint = point([ | ||
Number.parseFloat(listingAddress.longitude.toString()), | ||
Number.parseFloat(listingAddress.latitude.toString()), | ||
]) | ||
const calculatedBuffer = buffer(listingPoint.geometry, radius, { units: "miles" }) | ||
return booleanPointInPolygon(preferencePoint, calculatedBuffer) | ||
? GeocodingValues.true | ||
: GeocodingValues.false | ||
} | ||
} catch (e) { | ||
console.log("error happened while calculating radius") | ||
} | ||
return GeocodingValues.unknown | ||
} | ||
|
||
public async validateRadiusPreferences(application: Application, listing: Listing) { | ||
// Get all radius preferences from the listing | ||
const radiusPreferenceOptions: MultiselectOption[] = listing.listingMultiselectQuestions.reduce( | ||
(options, multiselectQuestion) => { | ||
const newOptions = multiselectQuestion.multiselectQuestion.options?.filter( | ||
(option) => option.validationMethod === ValidationMethod.radius | ||
) | ||
return [...options, ...newOptions] | ||
}, | ||
[] | ||
) | ||
// If there are any radius preferences do the calculation and save the new preferences | ||
if (radiusPreferenceOptions.length) { | ||
const preferences: ApplicationMultiselectQuestion[] = application.preferences.map( | ||
(preference) => { | ||
const newPreferenceOptions: ApplicationMultiselectQuestionOption[] = preference.options.map( | ||
(option) => { | ||
const addressData = option.extraData.find((data) => data.type === InputType.address) | ||
if (option.checked && addressData) { | ||
const foundOption = radiusPreferenceOptions.find( | ||
(preferenceOption) => preferenceOption.text === option.key | ||
) | ||
if (foundOption) { | ||
const geocodingVerified = this.verifyRadius( | ||
addressData.value as Address, | ||
foundOption.radiusSize, | ||
listing.buildingAddress | ||
) | ||
return { | ||
...option, | ||
extraData: [ | ||
...option.extraData, | ||
{ | ||
key: "geocodingVerified", | ||
type: InputType.text, | ||
value: geocodingVerified, | ||
}, | ||
], | ||
} | ||
} | ||
} | ||
return option | ||
} | ||
) | ||
return { ...preference, options: newPreferenceOptions } | ||
} | ||
) | ||
await this.repository.update({ id: application.id }, { preferences: preferences }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum GeocodingValues { | ||
true = "true", | ||
false = "false", | ||
unknown = "unknown", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.