Skip to content

Commit

Permalink
feat: atualizar para v300
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas committed May 9, 2024
1 parent 76034db commit b70d510
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 9 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"*.spec.php": "test-ts"
},
"cSpell.words": [
"agrupador",
"agrupadores",
"aliquota",
"Amazônia",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Pacote de integração com a [API v3 do ERP Bling](https://developer.bling.com.br)
para Javascript/TypeScript. O mais completo existente.

Atualizado com a versão `v298` da API ([veja o registro de alterações](https://developer.bling.com.br/changelogs#2024-02-28)).
Atualizado com a versão `v300` da API ([veja o registro de alterações](https://developer.bling.com.br/changelogs#2024-02-28)).

**Atenção**: a versão 5.0.0+ do `bling-erp-api` para Javascript/TypeScript
utiliza a API v3 do Bling. Caso deseja utilizar a API v2 do Bling,
Expand Down
7 changes: 7 additions & 0 deletions src/bling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { SituacoesModulos } from './entities/situacoesModulos'
import { SituacoesTransicoes } from './entities/situacoesTransicoes'
import { Usuarios } from './entities/usuarios'
import { Vendedores } from './entities/vendedores'
import { CanaisDeVenda } from './entities/canaisDeVenda'

const chance = Chance()

Expand Down Expand Up @@ -245,4 +246,10 @@ describe('Bling main module', () => {
it('should retrieve vendedores entity', () => {
expect(createBling(chance.word()).vendedores).toBeInstanceOf(Vendedores)
})

it('should retrieve canais de venda entity', () => {
expect(createBling(chance.word()).canaisDeVenda).toBeInstanceOf(
CanaisDeVenda
)
})
})
18 changes: 10 additions & 8 deletions src/bling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Entity } from './entities/@shared/entity'
import { Borderos } from './entities/borderos'
import { CamposCustomizados } from './entities/camposCustomizados'
import { CanaisDeVenda } from './entities/canaisDeVenda'
import { CategoriasLojas } from './entities/categoriasLojas'
import { CategoriasProdutos } from './entities/categoriasProdutos'
import { CategoriasReceitasDespesas } from './entities/categoriasReceitasDespesas'
Expand Down Expand Up @@ -352,7 +353,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com produtos - fornecedores.
*
* @return {ProdutosFornecedores}
Expand All @@ -362,7 +362,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com produtos - fornecedores.
*
* @return {ProdutosLojas}
Expand All @@ -372,7 +371,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com produtos - variações.
*
* @return {ProdutosVariacoes}
Expand All @@ -382,7 +380,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com situações.
*
* @return {Situacoes}
Expand All @@ -392,7 +389,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com situações - módulos.
*
* @return {SituacoesModulos}
Expand All @@ -402,7 +398,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com situações - transições.
*
* @return {SituacoesTransicoes}
Expand All @@ -412,7 +407,6 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com usuários.
*
* @return {Usuarios}
Expand All @@ -422,12 +416,20 @@ export default class Bling {
}

/**
*
* Obtém a instância de interação com vendedores.
*
* @return {Vendedores}
*/
public get vendedores(): Vendedores {
return this.getModule(Vendedores)
}

/**
* Obtém a instância de interação com canais de venda.
*
* @return {CanaisDeVenda}
*/
public get canaisDeVenda(): CanaisDeVenda {
return this.getModule(CanaisDeVenda)
}
}
18 changes: 18 additions & 0 deletions src/entities/canaisDeVenda/__tests__/find-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
data: {
id: 12345678,
descricao: 'Loja de teste',
tipo: 'Shopee',
situacao: 1 as const,
filiais: [
{
cnpj: '12.345.678/9012-34',
unidadeNegocio: 'Empresa Teste',
deposito: {
id: 12345678
},
padrao: true
}
]
}
}
10 changes: 10 additions & 0 deletions src/entities/canaisDeVenda/__tests__/get-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
data: [
{
id: 12345678,
descricao: 'Loja de teste',
tipo: 'Shopee',
situacao: 1 as const
}
]
}
9 changes: 9 additions & 0 deletions src/entities/canaisDeVenda/__tests__/get-types-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
data: [
{
nome: 'Loja Integrada',
tipo: 'LojaIntegrada',
agrupador: 1 as const
}
]
}
79 changes: 79 additions & 0 deletions src/entities/canaisDeVenda/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Chance } from 'chance'
import { CanaisDeVenda } from '../'
import { InMemoryBlingRepository } from '../../../repositories/bling-in-memory.repository'
import findResponse from './find-response'
import getResponse from './get-response'
import getTypesResponse from './get-types-response'
import { IFindResponse } from '../interfaces/find.interface'
import { IGetResponse } from '../interfaces/get.interface'
import { IGetTypesResponse } from '../interfaces/get-types.interface'

const chance = Chance()

describe('Canais de Venda entity', () => {
let repository: InMemoryBlingRepository
let entity: CanaisDeVenda

beforeEach(() => {
repository = new InMemoryBlingRepository()
entity = new CanaisDeVenda(repository)
})

afterEach(() => {
jest.restoreAllMocks()
})

it('should get successfully', async () => {
const spy = jest.spyOn(repository, 'index')
repository.setResponse(getResponse)

const response = await entity.get()

expect(spy).toHaveBeenCalledWith({
endpoint: 'canais-venda',
params: {
pagina: undefined,
limite: undefined,
tipos: undefined,
situacao: undefined,
agrupador: undefined
}
})
expect(response).toBe(getResponse)
const typingResponseTest: IGetResponse = getResponse
expect(typingResponseTest).toBe(getResponse)
})

it('should find successfully', async () => {
const spy = jest.spyOn(repository, 'show')
const idCanalVenda = chance.natural()
repository.setResponse(findResponse)

const response = await entity.find({ idCanalVenda })

expect(spy).toHaveBeenCalledWith({
endpoint: 'canais-venda',
id: String(idCanalVenda)
})
expect(response).toBe(findResponse)
const typingResponseTest: IFindResponse = findResponse
expect(typingResponseTest).toBe(findResponse)
})

it('should get types successfully', async () => {
const spy = jest.spyOn(repository, 'index')
repository.setResponse(getTypesResponse)

const response = await entity.getTypes()

expect(spy).toHaveBeenCalledWith({
endpoint: 'canais-venda/tipos',
params: {
agrupador: undefined
}
})
expect(response).toBe(getTypesResponse)
const typingResponseTest: IGetTypesResponse = getTypesResponse
expect(typingResponseTest).toBe(getTypesResponse)
})
})
70 changes: 70 additions & 0 deletions src/entities/canaisDeVenda/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Entity } from '../@shared/entity'
import { IFindParams, IFindResponse } from './interfaces/find.interface'
import {
IGetTypesParams,
IGetTypesResponse
} from './interfaces/get-types.interface'
import { IGetParams, IGetResponse } from './interfaces/get.interface'

/**
* Entidade para interação com Canais de Venda.
*
* @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda
*/
export class CanaisDeVenda extends Entity {
/**
* Obtém canais de venda.
*
* @param {IGetParams} params Parâmetros da busca.
*
* @returns {Promise<IGetResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda
*/
public async get(params?: IGetParams): Promise<IGetResponse> {
return await this.repository.index({
endpoint: 'canais-venda',
params: {
pagina: params?.pagina,
limite: params?.limite,
tipos: params?.tipos,
situacao: params?.situacao,
agrupador: params?.agrupador
}
})
}

/**
* Obtém um canal de venda.
*
* @param {IFindParams} params Parâmetros da busca.
*
* @returns {Promise<IFindResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda__idCanalVenda_
*/
public async find(params: IFindParams): Promise<IFindResponse> {
return await this.repository.show({
endpoint: 'canais-venda',
id: String(params.idCanalVenda)
})
}

/**
* Obtém os tipos de canais de venda.
*
* @param {IGetTypesParams} params Parâmetros da busca.
*
* @returns {Promise<IGetTypesResponse>}
*
* @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda_tipos
*/
public async getTypes(params?: IGetTypesParams): Promise<IGetTypesResponse> {
return await this.repository.index({
endpoint: 'canais-venda/tipos',
params: { agrupador: params?.agrupador }
})
}
}
20 changes: 20 additions & 0 deletions src/entities/canaisDeVenda/interfaces/find.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ISituacao } from '../types/situacao.type'

export interface IFindParams {
idCanalVenda: number
}

export interface IFindResponse {
data: {
id: number
descricao: string
tipo: string
situacao: ISituacao
filiais: {
cnpj: string
unidadeNegocio: string
deposito: { id: number }
padrao: boolean
}[]
}
}
13 changes: 13 additions & 0 deletions src/entities/canaisDeVenda/interfaces/get-types.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IAgrupador } from '../types/agrupador.type'

export interface IGetTypesParams {
agrupador?: IAgrupador
}

export interface IGetTypesResponse {
data: {
nome: string
tipo: string
agrupador: IAgrupador
}[]
}
19 changes: 19 additions & 0 deletions src/entities/canaisDeVenda/interfaces/get.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IAgrupador } from '../types/agrupador.type'
import { ISituacao } from '../types/situacao.type'

export interface IGetParams {
pagina?: number
limite?: number
tipos?: string[]
situacao?: ISituacao
agrupador?: IAgrupador
}

export interface IGetResponse {
data: {
id: number
descricao: string
tipo: string
situacao: ISituacao
}[]
}
9 changes: 9 additions & 0 deletions src/entities/canaisDeVenda/types/agrupador.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Tipagem referente ao agrupador de um canal de venda.
*
* - `1`: Loja virtual
* - `2`: Hub
* - `3`: Marketplace
* - `4`: API
*/
export type IAgrupador = 1 | 2 | 3 | 4
7 changes: 7 additions & 0 deletions src/entities/canaisDeVenda/types/situacao.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Tipagem referente à situação de um canal de venda.
*
* - `1`: Habilitado
* - `2`: Desabilitado
*/
export type ISituacao = 1 | 2
Loading

0 comments on commit b70d510

Please sign in to comment.