commit
This commit is contained in:
43
src/sales/cep/cep.controller.ts
Normal file
43
src/sales/cep/cep.controller.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
https://docs.nestjs.com/controllers#controllers
|
||||
*/
|
||||
|
||||
import { Controller, Get, Param, Query } from '@nestjs/common';
|
||||
import { CepService } from './cep.service';
|
||||
import { GoogleService } from 'src/google/google.service';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('Cep')
|
||||
@Controller('api/v1/cep')
|
||||
export class CepController {
|
||||
|
||||
constructor(
|
||||
private cepService: CepService,
|
||||
private googleService: GoogleService,
|
||||
) {}
|
||||
|
||||
@Get('find/:cep')
|
||||
findCep(@Param('cep') cep: string){
|
||||
//return this.cepService.findCep(cep);
|
||||
return this.cepService.findViaCep(cep);
|
||||
}
|
||||
@Get('find/viacep/:cep')
|
||||
findViaCep(@Param('cep') cep: string){
|
||||
return this.cepService.findViaCep(cep);
|
||||
}
|
||||
|
||||
@Get('geolocation/:cep')
|
||||
geolocationCep(@Param('cep') cep: string){
|
||||
return this.cepService.geoLocalicationCep(cep);
|
||||
}
|
||||
|
||||
@Get('google')
|
||||
async geolocationGoogle(@Query() query){
|
||||
const address = query['address'];
|
||||
const addressNumber = query['addressNumber'];
|
||||
const neighborhood = query['neighborhood'];
|
||||
const city = query['city'];
|
||||
const state = query['state'];
|
||||
return this.googleService.getGeocoder(address, addressNumber, neighborhood, city, state);
|
||||
}
|
||||
}
|
||||
27
src/sales/cep/cep.module.ts
Normal file
27
src/sales/cep/cep.module.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { CepService } from './cep.service';
|
||||
import { CepController } from './cep.controller';
|
||||
/*
|
||||
https://docs.nestjs.com/modules
|
||||
*/
|
||||
|
||||
import { HttpModule, Module } from '@nestjs/common';
|
||||
import { GoogleService } from 'src/google/google.service';
|
||||
import { ListsService } from 'src/backoffice/lists/lists.service';
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
HttpModule
|
||||
// HttpModule.register({
|
||||
// timeout: 5000,
|
||||
// maxRedirects: 5,
|
||||
// }),
|
||||
],
|
||||
controllers: [
|
||||
CepController,],
|
||||
providers: [
|
||||
CepService,
|
||||
GoogleService,
|
||||
ListsService],
|
||||
})
|
||||
export class CepModule { }
|
||||
88
src/sales/cep/cep.service.ts
Normal file
88
src/sales/cep/cep.service.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
/*
|
||||
https://docs.nestjs.com/providers#services
|
||||
*/
|
||||
|
||||
import { HttpService, Injectable } from '@nestjs/common';
|
||||
import { catchError, firstValueFrom, switchMap } from 'rxjs';
|
||||
import { Cep } from 'src/domain/models/cep.model';
|
||||
import { GeolocationCep } from 'src/domain/models/geolocation-cep.model';
|
||||
import { ViaCep } from 'src/domain/models/via-cep.model';
|
||||
|
||||
@Injectable()
|
||||
export class CepService {
|
||||
constructor(
|
||||
private readonly httpService: HttpService) { }
|
||||
|
||||
async findCep(cep: string): Promise<Cep> {
|
||||
const url = `http://eduardoestevaogyn-d90e3e3eb6249000.api.findcep.com/v1/cep/${cep}.json`;
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService.get<Cep>(url,
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Referer': 'EVAB02XJN87NY'
|
||||
}
|
||||
}).pipe(
|
||||
catchError((error) => {
|
||||
console.log(error);
|
||||
throw error.response;
|
||||
}),
|
||||
)
|
||||
);
|
||||
|
||||
const dataGeoloacation = await this.geoLocalicationCep(cep);
|
||||
|
||||
return { ...data, ...dataGeoloacation.location };
|
||||
}
|
||||
|
||||
async findViaCep(cep: string): Promise<Cep> {
|
||||
const url = `http://viacep.com.br/ws/${cep}/json/`;
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService.get<ViaCep>(url
|
||||
).pipe(
|
||||
catchError((error) => {
|
||||
console.log(error);
|
||||
throw error.response;
|
||||
}),
|
||||
)
|
||||
);
|
||||
|
||||
const dataGeoloacation = { location: { latitude: 0, longitude: 0} };
|
||||
|
||||
const dataCep: Cep = {
|
||||
bairro : data.bairro,
|
||||
cep: data.cep,
|
||||
cidade: data.localidade,
|
||||
codigo_ibge: data.ibge,
|
||||
complemento: data.complemento,
|
||||
logradouro: data.logradouro,
|
||||
nome: null,
|
||||
status: null,
|
||||
tipo: null,
|
||||
uf: data.uf
|
||||
}
|
||||
|
||||
return { ...dataCep, ...dataGeoloacation.location };
|
||||
}
|
||||
|
||||
async geoLocalicationCep(cep: string): Promise<GeolocationCep> {
|
||||
const url = `http://eduardoestevaogyn-d90e3e3eb6249000.api.findcep.com/v1/geolocation/cep/${cep}`;
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService.get<GeolocationCep>(url,
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Referer': 'EVAB02XJN87NY'
|
||||
}
|
||||
}).pipe(
|
||||
catchError((error) => {
|
||||
console.log(error);
|
||||
throw error.response;
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user