-
Notifications
You must be signed in to change notification settings - Fork 293
feat: buscar coordenadas do abrigo #87
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
base: develop
Are you sure you want to change the base?
Changes from all 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,17 +14,45 @@ 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 { | ||
private voluntaryIds: string[] = []; | ||
|
||
private async generatePayloadWithCoordinates( | ||
payload: | ||
| z.infer<typeof CreateShelterSchema> | ||
| z.infer<typeof FullUpdateShelterSchema>, | ||
) { | ||
if ((payload.latitude && payload.longitude) || !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. No cenário do fullUpdate em que o usuário altera o local do abrigo, os valores da latitude e longitude não serão atualizados |
||
return payload; | ||
} | ||
|
||
const { latitude, longitude } = await fetchShelterCoordinates( | ||
payload.address, | ||
); | ||
|
||
const updatedPayload = { | ||
...payload, | ||
}; | ||
|
||
if (latitude && longitude) { | ||
updatedPayload.latitude = latitude; | ||
updatedPayload.longitude = longitude; | ||
} | ||
|
||
return updatedPayload; | ||
} | ||
|
||
constructor(private readonly prismaService: PrismaService) { | ||
this.loadVoluntaryIds(); | ||
} | ||
|
||
async store(body: z.infer<typeof CreateShelterSchema>) { | ||
const payload = CreateShelterSchema.parse(body); | ||
const payload = CreateShelterSchema.parse( | ||
await this.generatePayloadWithCoordinates(body), | ||
); | ||
|
||
await this.prismaService.shelter.create({ | ||
data: { | ||
|
@@ -48,7 +76,10 @@ export class ShelterService { | |
} | ||
|
||
async fullUpdate(id: string, body: z.infer<typeof FullUpdateShelterSchema>) { | ||
const payload = FullUpdateShelterSchema.parse(body); | ||
const payload = FullUpdateShelterSchema.parse( | ||
await this.generatePayloadWithCoordinates(body), | ||
); | ||
|
||
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