-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: buscar coordenadas do abrigo #87
base: develop
Are you sure you want to change the base?
Changes from 3 commits
13d2cea
353d616
379a398
aab3cc2
05404e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export class MapsApi { | ||
static key: string = process.env.MAPS_API_KEY || ''; | ||
static url: string = | ||
process.env.MAPS_API_URL || 'https://maps.googleapis.com/maps/api'; | ||
|
||
static async getCoordinates( | ||
address: string, | ||
): Promise<{ lat: number | null; lng: number | null }> { | ||
try { | ||
const response = await fetch( | ||
`${MapsApi.url}/geocode/json?address=${encodeURI(address)}&key=${MapsApi.key}`, | ||
); | ||
if (!response.ok) { | ||
throw new Error( | ||
`[MAPS API] Failed to fetch coordinates. Status: ${response.status}`, | ||
); | ||
} | ||
|
||
const data = await response.json(); | ||
const location = data?.results?.[0]?.geometry?.location; | ||
if (!location || !location.lat || !location.lng) { | ||
throw new Error('Invalid response from maps API'); | ||
} | ||
|
||
return location; | ||
} catch (error: any) { | ||
console.error(`[MAPS API] Error fetching coordinates: ${error.message}`); | ||
return { lat: null, lng: null }; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import { SearchSchema } from '../types'; | |
import { ShelterSearch, parseTagResponse } from './ShelterSearch'; | ||
import { SupplyPriority } from '../supply/types'; | ||
import { IFilterFormProps } from './types/search.types'; | ||
import { fetchShelterCoordinates } from '../utils'; | ||
|
||
@Injectable() | ||
export class ShelterService { | ||
|
@@ -26,6 +27,14 @@ export class ShelterService { | |
async store(body: z.infer<typeof CreateShelterSchema>) { | ||
const payload = CreateShelterSchema.parse(body); | ||
|
||
const { latitude, longitude } = await fetchShelterCoordinates( | ||
payload.address, | ||
); | ||
if (latitude && longitude) { | ||
payload.latitude = latitude; | ||
payload.longitude = longitude; | ||
} | ||
|
||
await this.prismaService.shelter.create({ | ||
data: { | ||
...payload, | ||
|
@@ -49,6 +58,16 @@ export class ShelterService { | |
|
||
async fullUpdate(id: string, body: z.infer<typeof FullUpdateShelterSchema>) { | ||
const payload = FullUpdateShelterSchema.parse(body); | ||
if (payload.address) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Que tal passar para um método privado essa lógica, que se repete aqui e no método store? Assim evita duplicidade e isola o preenchimento de coordenadas... esse método pode ser chamado apenas na hora de concatenar o payload na resposta, algo do tipo:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Show, obrigado. |
||
const { latitude, longitude } = await fetchShelterCoordinates( | ||
payload.address, | ||
); | ||
if (latitude && longitude) { | ||
payload.latitude = latitude; | ||
payload.longitude = longitude; | ||
} | ||
} | ||
|
||
await this.prismaService.shelter.update({ | ||
where: { | ||
id, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,9 +41,26 @@ const FullUpdateShelterSchema = ShelterSchema.omit({ | |
updatedAt: true, | ||
}).partial(); | ||
|
||
interface ShelterData { | ||
id: string; | ||
name: string; | ||
pix: string | null; | ||
address: string; | ||
petFriendly: boolean | null; | ||
shelteredPeople: number | null; | ||
latitude: number | null; | ||
longitude: number | null; | ||
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Desculpem a intromissão, não entendo nada de TS, mas notei que estão usando aqui dois atributos para representar um ponto e o mesmo está refletido também na tabela do banco de dados. No Postgres temos um tipo de dado nativo dele chamado |
||
capacity: number | null; | ||
contact: string | null; | ||
verified: boolean; | ||
createdAt: string; | ||
updatedAt: string | null; | ||
} | ||
|
||
export { | ||
ShelterSchema, | ||
CreateShelterSchema, | ||
UpdateShelterSchema, | ||
FullUpdateShelterSchema, | ||
ShelterData, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
o NestJS possui um modulo http para fazer esse tipo de request