From b70d510d83ec51c627ee8289808d6ffbe79b9418 Mon Sep 17 00:00:00 2001 From: Alexandre Batistella Bellas Date: Thu, 9 May 2024 17:33:20 -0300 Subject: [PATCH] feat: atualizar para v300 --- .vscode/settings.json | 1 + README.md | 2 +- src/bling.spec.ts | 7 ++ src/bling.ts | 18 +++-- .../canaisDeVenda/__tests__/find-response.ts | 18 +++++ .../canaisDeVenda/__tests__/get-response.ts | 10 +++ .../__tests__/get-types-response.ts | 9 +++ .../canaisDeVenda/__tests__/index.spec.ts | 79 +++++++++++++++++++ src/entities/canaisDeVenda/index.ts | 70 ++++++++++++++++ .../interfaces/find.interface.ts | 20 +++++ .../interfaces/get-types.interface.ts | 13 +++ .../canaisDeVenda/interfaces/get.interface.ts | 19 +++++ .../canaisDeVenda/types/agrupador.type.ts | 9 +++ .../canaisDeVenda/types/situacao.type.ts | 7 ++ src/entities/estoques/__tests__/index.spec.ts | 11 +++ .../interfaces/find-balance.interface.ts | 4 + .../interfaces/get-balances.interface.ts | 4 + .../produtos/__tests__/find-response.ts | 2 + .../produtos/__tests__/get-response.ts | 1 + .../produtos/interfaces/find.interface.ts | 2 + .../produtos/interfaces/get.interface.ts | 1 + .../__tests__/find-response.ts | 2 + .../interfaces/find.interface.ts | 2 + 23 files changed, 302 insertions(+), 9 deletions(-) create mode 100644 src/entities/canaisDeVenda/__tests__/find-response.ts create mode 100644 src/entities/canaisDeVenda/__tests__/get-response.ts create mode 100644 src/entities/canaisDeVenda/__tests__/get-types-response.ts create mode 100644 src/entities/canaisDeVenda/__tests__/index.spec.ts create mode 100644 src/entities/canaisDeVenda/index.ts create mode 100644 src/entities/canaisDeVenda/interfaces/find.interface.ts create mode 100644 src/entities/canaisDeVenda/interfaces/get-types.interface.ts create mode 100644 src/entities/canaisDeVenda/interfaces/get.interface.ts create mode 100644 src/entities/canaisDeVenda/types/agrupador.type.ts create mode 100644 src/entities/canaisDeVenda/types/situacao.type.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 44408a8..1e69f7f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,6 +7,7 @@ "*.spec.php": "test-ts" }, "cSpell.words": [ + "agrupador", "agrupadores", "aliquota", "Amazônia", diff --git a/README.md b/README.md index f98456d..4066854 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/src/bling.spec.ts b/src/bling.spec.ts index 588e4eb..0f37189 100644 --- a/src/bling.spec.ts +++ b/src/bling.spec.ts @@ -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() @@ -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 + ) + }) }) diff --git a/src/bling.ts b/src/bling.ts index 5d3a053..cf441cc 100644 --- a/src/bling.ts +++ b/src/bling.ts @@ -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' @@ -352,7 +353,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com produtos - fornecedores. * * @return {ProdutosFornecedores} @@ -362,7 +362,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com produtos - fornecedores. * * @return {ProdutosLojas} @@ -372,7 +371,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com produtos - variações. * * @return {ProdutosVariacoes} @@ -382,7 +380,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com situações. * * @return {Situacoes} @@ -392,7 +389,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com situações - módulos. * * @return {SituacoesModulos} @@ -402,7 +398,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com situações - transições. * * @return {SituacoesTransicoes} @@ -412,7 +407,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com usuários. * * @return {Usuarios} @@ -422,7 +416,6 @@ export default class Bling { } /** - * * Obtém a instância de interação com vendedores. * * @return {Vendedores} @@ -430,4 +423,13 @@ export default class Bling { 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) + } } diff --git a/src/entities/canaisDeVenda/__tests__/find-response.ts b/src/entities/canaisDeVenda/__tests__/find-response.ts new file mode 100644 index 0000000..a90e2c9 --- /dev/null +++ b/src/entities/canaisDeVenda/__tests__/find-response.ts @@ -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 + } + ] + } +} diff --git a/src/entities/canaisDeVenda/__tests__/get-response.ts b/src/entities/canaisDeVenda/__tests__/get-response.ts new file mode 100644 index 0000000..b4720f2 --- /dev/null +++ b/src/entities/canaisDeVenda/__tests__/get-response.ts @@ -0,0 +1,10 @@ +export default { + data: [ + { + id: 12345678, + descricao: 'Loja de teste', + tipo: 'Shopee', + situacao: 1 as const + } + ] +} diff --git a/src/entities/canaisDeVenda/__tests__/get-types-response.ts b/src/entities/canaisDeVenda/__tests__/get-types-response.ts new file mode 100644 index 0000000..dfe36f6 --- /dev/null +++ b/src/entities/canaisDeVenda/__tests__/get-types-response.ts @@ -0,0 +1,9 @@ +export default { + data: [ + { + nome: 'Loja Integrada', + tipo: 'LojaIntegrada', + agrupador: 1 as const + } + ] +} diff --git a/src/entities/canaisDeVenda/__tests__/index.spec.ts b/src/entities/canaisDeVenda/__tests__/index.spec.ts new file mode 100644 index 0000000..464cf20 --- /dev/null +++ b/src/entities/canaisDeVenda/__tests__/index.spec.ts @@ -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) + }) +}) diff --git a/src/entities/canaisDeVenda/index.ts b/src/entities/canaisDeVenda/index.ts new file mode 100644 index 0000000..2f30bf4 --- /dev/null +++ b/src/entities/canaisDeVenda/index.ts @@ -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} + * @throws {BlingApiException|BlingInternalException} + * + * @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda + */ + public async get(params?: IGetParams): Promise { + 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} + * @throws {BlingApiException|BlingInternalException} + * + * @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda__idCanalVenda_ + */ + public async find(params: IFindParams): Promise { + 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} + * + * @see https://developer.bling.com.br/referencia#/Canais%20de%20Venda/get_canais_venda_tipos + */ + public async getTypes(params?: IGetTypesParams): Promise { + return await this.repository.index({ + endpoint: 'canais-venda/tipos', + params: { agrupador: params?.agrupador } + }) + } +} diff --git a/src/entities/canaisDeVenda/interfaces/find.interface.ts b/src/entities/canaisDeVenda/interfaces/find.interface.ts new file mode 100644 index 0000000..0aca7bf --- /dev/null +++ b/src/entities/canaisDeVenda/interfaces/find.interface.ts @@ -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 + }[] + } +} diff --git a/src/entities/canaisDeVenda/interfaces/get-types.interface.ts b/src/entities/canaisDeVenda/interfaces/get-types.interface.ts new file mode 100644 index 0000000..6a4518b --- /dev/null +++ b/src/entities/canaisDeVenda/interfaces/get-types.interface.ts @@ -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 + }[] +} diff --git a/src/entities/canaisDeVenda/interfaces/get.interface.ts b/src/entities/canaisDeVenda/interfaces/get.interface.ts new file mode 100644 index 0000000..c2f359b --- /dev/null +++ b/src/entities/canaisDeVenda/interfaces/get.interface.ts @@ -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 + }[] +} diff --git a/src/entities/canaisDeVenda/types/agrupador.type.ts b/src/entities/canaisDeVenda/types/agrupador.type.ts new file mode 100644 index 0000000..dfdc30c --- /dev/null +++ b/src/entities/canaisDeVenda/types/agrupador.type.ts @@ -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 diff --git a/src/entities/canaisDeVenda/types/situacao.type.ts b/src/entities/canaisDeVenda/types/situacao.type.ts new file mode 100644 index 0000000..d3cf399 --- /dev/null +++ b/src/entities/canaisDeVenda/types/situacao.type.ts @@ -0,0 +1,7 @@ +/** + * Tipagem referente à situação de um canal de venda. + * + * - `1`: Habilitado + * - `2`: Desabilitado + */ +export type ISituacao = 1 | 2 diff --git a/src/entities/estoques/__tests__/index.spec.ts b/src/entities/estoques/__tests__/index.spec.ts index 07b4858..101742e 100644 --- a/src/entities/estoques/__tests__/index.spec.ts +++ b/src/entities/estoques/__tests__/index.spec.ts @@ -5,6 +5,9 @@ import createResponse, { createRequestBody } from './create-response' import findResponse from './find-balance-response' import getResponse from './get-balances-response' import updateResponse, { updateRequestBody } from './update-response' +import { IFindBalanceResponse } from '../interfaces/find-balance.interface' +import { IGetBalancesResponse } from '../interfaces/get-balances.interface' +import { ICreateResponse } from '../interfaces/create.interface' const chance = Chance() @@ -38,6 +41,8 @@ describe('Estoques entity', () => { params: { idsProdutos } }) expect(response).toBe(findResponse) + const typingResponseTest: IFindBalanceResponse = findResponse + expect(typingResponseTest).toBe(findResponse) }) it('should get balances successfully', async () => { @@ -55,6 +60,8 @@ describe('Estoques entity', () => { params: { idsProdutos } }) expect(response).toBe(getResponse) + const typingResponseTest: IGetBalancesResponse = getResponse + expect(typingResponseTest).toBe(getResponse) }) it('should create successfully', async () => { @@ -68,6 +75,8 @@ describe('Estoques entity', () => { body: createRequestBody }) expect(response).toBe(createResponse) + const typingResponseTest: ICreateResponse = createResponse + expect(typingResponseTest).toBe(createResponse) }) it('should update successfully', async () => { @@ -86,5 +95,7 @@ describe('Estoques entity', () => { body: updateRequestBody }) expect(response).toBe(updateResponse) + const typingResponseTest: null = updateResponse + expect(typingResponseTest).toBe(updateResponse) }) }) diff --git a/src/entities/estoques/interfaces/find-balance.interface.ts b/src/entities/estoques/interfaces/find-balance.interface.ts index c4a26f6..974ab05 100644 --- a/src/entities/estoques/interfaces/find-balance.interface.ts +++ b/src/entities/estoques/interfaces/find-balance.interface.ts @@ -7,6 +7,10 @@ export interface IFindBalanceParams { * IDs dos produtos */ idsProdutos: number[] + /** + * Código do produto + */ + codigo?: string } export interface IFindBalanceResponse { diff --git a/src/entities/estoques/interfaces/get-balances.interface.ts b/src/entities/estoques/interfaces/get-balances.interface.ts index 07521fd..fc3c0f9 100644 --- a/src/entities/estoques/interfaces/get-balances.interface.ts +++ b/src/entities/estoques/interfaces/get-balances.interface.ts @@ -3,6 +3,10 @@ export interface IGetBalancesParams { * IDs dos produtos */ idsProdutos: number[] + /** + * Código do produto + */ + codigo?: string } export interface IGetBalancesResponse { diff --git a/src/entities/produtos/__tests__/find-response.ts b/src/entities/produtos/__tests__/find-response.ts index 81a798a..e3fa9f0 100644 --- a/src/entities/produtos/__tests__/find-response.ts +++ b/src/entities/produtos/__tests__/find-response.ts @@ -1,6 +1,7 @@ export default { data: { id: 123456789, + idProdutoPai: 123456789, nome: 'Produto 1', codigo: 'CODE_123', preco: 1, @@ -124,6 +125,7 @@ export default { variacoes: [ { id: 123456789, + idProdutoPai: 123456789, nome: 'Produto 1', codigo: 'CODE_123', preco: 1, diff --git a/src/entities/produtos/__tests__/get-response.ts b/src/entities/produtos/__tests__/get-response.ts index e552c6e..42da148 100644 --- a/src/entities/produtos/__tests__/get-response.ts +++ b/src/entities/produtos/__tests__/get-response.ts @@ -2,6 +2,7 @@ export default { data: [ { id: 123456789, + idProdutoPai: 123456789, nome: 'Produto 1', codigo: 'CODE_123', preco: 1, diff --git a/src/entities/produtos/interfaces/find.interface.ts b/src/entities/produtos/interfaces/find.interface.ts index 9426477..6b3920f 100644 --- a/src/entities/produtos/interfaces/find.interface.ts +++ b/src/entities/produtos/interfaces/find.interface.ts @@ -18,6 +18,7 @@ export interface IFindParams { export interface IFindResponse { data: { id?: number + idProdutoPai?: number nome: string codigo?: string preco?: number @@ -114,6 +115,7 @@ export interface IFindResponse { }[] variacoes: { id?: number + idProdutoPai?: number nome: string codigo?: string preco?: number diff --git a/src/entities/produtos/interfaces/get.interface.ts b/src/entities/produtos/interfaces/get.interface.ts index 968b4a1..1540981 100644 --- a/src/entities/produtos/interfaces/get.interface.ts +++ b/src/entities/produtos/interfaces/get.interface.ts @@ -66,6 +66,7 @@ export interface IGetParams { export interface IGetResponse { data: { id?: number + idProdutoPai?: number nome: string codigo?: string preco?: number diff --git a/src/entities/produtosVariacoes/__tests__/find-response.ts b/src/entities/produtosVariacoes/__tests__/find-response.ts index c699c21..3c976b8 100644 --- a/src/entities/produtosVariacoes/__tests__/find-response.ts +++ b/src/entities/produtosVariacoes/__tests__/find-response.ts @@ -1,6 +1,7 @@ export default { data: { id: 123456789, + idProdutoPai: 123456789, nome: 'Produto 1', codigo: 'CODE_123', preco: 1, @@ -108,6 +109,7 @@ export default { variacoes: [ { id: 123456789, + idProdutoPai: 123456789, nome: 'Produto 1', codigo: 'CODE_123', preco: 1, diff --git a/src/entities/produtosVariacoes/interfaces/find.interface.ts b/src/entities/produtosVariacoes/interfaces/find.interface.ts index c0ddf04..3b48d79 100644 --- a/src/entities/produtosVariacoes/interfaces/find.interface.ts +++ b/src/entities/produtosVariacoes/interfaces/find.interface.ts @@ -18,6 +18,7 @@ export interface IFindParams { export interface IFindResponse { data: { id?: number + idProdutoPai?: number nome: string codigo?: string preco?: number @@ -104,6 +105,7 @@ export interface IFindResponse { }[] variacoes: { id?: number + idProdutoPai?: number nome: string codigo?: string preco?: number