@kigi/components 1.24.0 → 1.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kigi/components",
3
- "version": "1.24.0",
3
+ "version": "1.25.0",
4
4
  "description": "@kigi/components",
5
5
  "main": "src/components/index.ts",
6
6
  "scripts": {
@@ -342,40 +342,59 @@ class MbgAddressController {
342
342
 
343
343
  async searchAddressByCEP() {
344
344
  if (this.address && this.address.zipCode) {
345
- this.mbgAddressService.getCep(this.address.zipCode).then((response) => {
346
- if (response && response.data && response.data.cep) {
347
- const uf = getStatesBR().filter((state) => state.initial === response.data.uf)[0]
348
- const formalCode = response.data.ibge || ''
349
- const premisse = this.formatFromPremisse(response.data.logradouro)
350
- this.address = {
351
- zipCode: this.address.zipCode.replace('-', ''),
352
- neighbourhood: response.data.bairro ? response.data.bairro.trim() : 'Centro',
353
- localization: this.address.localization
354
- ? this.address.localization
355
- : (response.data.localidade || '').trim(),
356
- premisse: premisse || 'CEP Único',
357
- number: this.address.number || '',
358
- information: this.address.information || '',
359
- premisseSteps: this.address.premisseSteps || '',
360
- premisseType: this.getPremisseType(premisse),
361
- stateCode: formalCode.substring(0, 2),
362
- formalCode,
363
- uf,
364
- }
365
- if (!premisse) {
366
- if (this.notFoundPremisse) {
367
- this.notFoundPremisse()
368
- }
369
- this.openModalManual(false, this.address)
370
- return
345
+ const response = await this.mbgAddressService.getCep(this.address.zipCode);
346
+
347
+ if (response && response.data && response.data.cep) {
348
+ let street, neighborhood, city, state, ibgeCode = '';
349
+
350
+ if (response.key == 'viacep') {
351
+ street = response.data.logradouro;
352
+ neighborhood = response.data.bairro;
353
+ city = response.data.localization;
354
+ state = response.data.uf;
355
+ ibgeCode = response.data.ibge;
356
+ } else if (response.key == 'brasilApi') {
357
+ street = response.data.street;
358
+ neighborhood = response.data.neighborhood;
359
+ city = response.data.city;
360
+ state = response.data.state;
361
+ const responseIbge = await this.mbgAddressService.getIbgeCode(response.data.city)
362
+ ibgeCode = (responseIbge && responseIbge.data) && responseIbge.data.id.toString();
363
+ }
364
+
365
+ const uf = getStatesBR().filter((state) => state.initial === response.data.uf)[0]
366
+ const formalCode = ibgeCode || ''
367
+ const premisse = this.formatFromPremisse(street)
368
+
369
+ this.address = {
370
+ zipCode: this.address.zipCode.replace('-', ''),
371
+ neighbourhood: neighborhood ? neighborhood.trim() : 'Centro',
372
+ localization: city
373
+ ? city
374
+ : (response.data.localidade || '').trim(),
375
+ premisse: premisse || 'CEP Único',
376
+ number: this.address.number || '',
377
+ information: this.address.information || '',
378
+ premisseSteps: this.address.premisseSteps || '',
379
+ premisseType: this.getPremisseType(premisse),
380
+ stateCode: formalCode.substring(0, 2),
381
+ formalCode,
382
+ uf,
383
+ }
384
+
385
+ if (!premisse) {
386
+ if (this.notFoundPremisse) {
387
+ this.notFoundPremisse()
371
388
  }
372
- this.updateSteps()
373
- this.createFullName()
374
- this.$timeout(() => this.focusInputNumber())
375
- } else {
376
- this.notFound()
389
+ this.openModalManual(false, this.address)
390
+ return
377
391
  }
378
- })
392
+ this.updateSteps()
393
+ this.createFullName()
394
+ this.$timeout(() => this.focusInputNumber())
395
+ } else {
396
+ this.notFound()
397
+ }
379
398
  }
380
399
  }
381
400
 
@@ -2,6 +2,9 @@ class MbgAddressService {
2
2
  private apiLocation = 'https://anothers.kigisistemas.com.br/services-api'
3
3
  private googleKey = 'AIzaSyAATuKM3qZOaw0oArk_zNUJMOtGfwhoUEI'
4
4
  private googleAPI = 'https://maps.google.com/maps/api/geocode/json'
5
+ private viaCepAPI = 'https://viacep.com.br/ws'
6
+ private brasilAPI = 'https://brasilapi.com.br/api'
7
+ private ibgeGovAPI = 'https://servicodados.ibge.gov.br/api/v1/localidades/municipios'
5
8
 
6
9
  constructor(public $http) {}
7
10
 
@@ -21,9 +24,26 @@ class MbgAddressService {
21
24
  return this.$http.get(`${this.googleAPI}?address=${address}&key=${this.googleKey}`)
22
25
  }
23
26
 
24
- getCep(cep: string) {
25
- // return this.$http.get(`${this.apiLocation}/public/busca-cep/${cep}`)
26
- return this.$http.get(`https://viacep.com.br/ws/${cep}/json/`)
27
+ async getCep(zipCode: string) {
28
+ try {
29
+ return await this.$http.get(`${this.viaCepAPI}/${zipCode}/json/`).then(res => {
30
+ res.key = 'viacep';
31
+ return res;
32
+ })
33
+ } catch (error) {
34
+ return await this.$http.get(`${this.brasilAPI}/cep/v1/${zipCode}`).then(res => {
35
+ res.key = 'brasilApi';
36
+ return res;
37
+ })
38
+ }
39
+ }
40
+
41
+ async getIbgeCode(municipio: string) {
42
+ try {
43
+ return await this.$http.get(`${this.ibgeGovAPI}/${municipio}`)
44
+ } catch (e) {
45
+ console.log(`Erro ao obter código do IBGE`)
46
+ }
27
47
  }
28
48
 
29
49
  getAddress(uf, city, premisse, neighbourhood) {
@@ -1,6 +1,5 @@
1
1
  import './typeone.scss'
2
2
  import template from './typeone.html'
3
- import { isArray } from '@uirouter/angularjs'
4
3
 
5
4
  class MbgCardTypeOneController {
6
5
  public hideValue: boolean
@@ -28,7 +27,7 @@ class MbgCardTypeOneController {
28
27
  this.card.setNewValue = (value) => {
29
28
  this.setValue(value)
30
29
  }
31
- if (this.card.items && this.card.items.length > 0 && !isArray(this.card.items[0])) {
30
+ if (this.card.items && this.card.items.length > 0 && !Array.isArray(this.card.items[0])) {
32
31
  this.card.items = [this.card.items]
33
32
  }
34
33
  }
@@ -1,4 +1,4 @@
1
- import { Capitalize } from '@kigi/components/src/helpers/capitalize'
1
+ import { Capitalize } from '../../helpers/capitalize'
2
2
  import * as angular from 'angular'
3
3
  import './mbg-select.scss'
4
4
  import template from './mbg-select.html'