-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add district name in complaints with only coordinates
- Loading branch information
Showing
9 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,53 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { District } from 'src/model/district.entity'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
|
||
@Injectable() | ||
export class GeolocationService { | ||
constructor( | ||
private readonly httpService: HttpService, | ||
@InjectRepository(District) | ||
private readonly districtRepository: Repository<District>, | ||
) {} | ||
|
||
async findOrCreateDistrict(latitude: number, longitude: number): Promise<District> { | ||
// Llamada a la API de Google para obtener la información de ubicación | ||
const response = await firstValueFrom( | ||
this.httpService.get(`https://maps.googleapis.com/maps/api/geocode/json`, { | ||
params: { | ||
latlng: `${latitude},${longitude}`, | ||
key: process.env.GOOGLE_API_KEY, | ||
}, | ||
}), | ||
); | ||
|
||
const results = response.data.results; | ||
const districtName = this.extractDistrictFromResults(results); | ||
|
||
// Verificar si el distrito ya existe en la base de datos | ||
let district = await this.districtRepository.findOne({ where: { name: districtName } }); | ||
|
||
if (!district) { | ||
// Si el distrito no existe, créalo y guárdalo | ||
district = this.districtRepository.create({ name: districtName }); | ||
await this.districtRepository.save(district); | ||
} | ||
|
||
return district; | ||
} | ||
|
||
private extractDistrictFromResults(results: any[]): string { | ||
// Busca en los resultados un objeto que tenga el tipo 'locality' en su array de types | ||
const districtResult = results.find((result) => result.types.includes('locality') && result.types.includes('political')); | ||
|
||
// Si encuentra el distrito, devuelve su nombre completo; de lo contrario, devuelve null | ||
if (districtResult) { | ||
const districtComponent = districtResult.address_components.find((component) => component.types.includes('locality')); | ||
return districtComponent ? districtComponent.long_name : null; | ||
} | ||
return null; | ||
} | ||
} |
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