diff --git a/README.md b/README.md index 117a632..c1d63ae 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ As entidades atualmente permitidas para interação são: - [x] Situações - Módulos (`.situacoesModulos`) - [x] Situações - Transições (`.situacoesTransicoes`) - [x] Usuários (`.usuarios`) -- [ ] Vendedores (`.vendedores`) +- [x] Vendedores (`.vendedores`) Adicionaremos as restantes de acordo com as _releases_. diff --git a/src/bling.spec.ts b/src/bling.spec.ts index df31d11..6fa7611 100644 --- a/src/bling.spec.ts +++ b/src/bling.spec.ts @@ -37,6 +37,7 @@ import { Situacoes } from './entities/situacoes' import { SituacoesModulos } from './entities/situacoesModulos' import { SituacoesTransicoes } from './entities/situacoesTransicoes' import { Usuarios } from './entities/usuarios' +import { Vendedores } from './entities/vendedores' const chance = Chance() @@ -228,4 +229,8 @@ describe('Bling main module', () => { it('should retrieve usuários entity', () => { expect(createBling(chance.word()).usuarios).toBeInstanceOf(Usuarios) }) + + it('should retrieve vendedores entity', () => { + expect(createBling(chance.word()).vendedores).toBeInstanceOf(Vendedores) + }) }) diff --git a/src/bling.ts b/src/bling.ts index dcaf63e..c928374 100644 --- a/src/bling.ts +++ b/src/bling.ts @@ -36,6 +36,7 @@ import { Situacoes } from './entities/situacoes' import { SituacoesModulos } from './entities/situacoesModulos' import { SituacoesTransicoes } from './entities/situacoesTransicoes' import { Usuarios } from './entities/usuarios' +import { Vendedores } from './entities/vendedores' import { Newable } from './helpers/types/newable.type' import { getRepository } from './providers/ioc' import { IBlingRepository } from './repositories/bling.repository.interface' @@ -399,4 +400,14 @@ export default class Bling { public get usuarios(): Usuarios { return this.getModule(Usuarios) } + + /** + * + * Obtém a instância de interação com vendedores. + * + * @return {Vendedores} + */ + public get vendedores(): Vendedores { + return this.getModule(Vendedores) + } } diff --git a/src/entities/vendedores/__tests__/find-response.ts b/src/entities/vendedores/__tests__/find-response.ts new file mode 100644 index 0000000..d48dad6 --- /dev/null +++ b/src/entities/vendedores/__tests__/find-response.ts @@ -0,0 +1,20 @@ +export default { + data: { + id: 12345678, + descontoLimite: 10.12, + loja: { + id: 12345678 + }, + contato: { + id: 12345678, + nome: 'Vendedor', + situacao: 'A' + }, + comissoes: [ + { + descontoMaximo: 10, + aliquota: 2 + } + ] + } +} diff --git a/src/entities/vendedores/__tests__/get-response.ts b/src/entities/vendedores/__tests__/get-response.ts new file mode 100644 index 0000000..70737f3 --- /dev/null +++ b/src/entities/vendedores/__tests__/get-response.ts @@ -0,0 +1,14 @@ +export default { + data: { + id: 12345678, + descontoLimite: 10.12, + loja: { + id: 12345678 + }, + contato: { + id: 12345678, + nome: 'Vendedor', + situacao: 'A' + } + } +} diff --git a/src/entities/vendedores/__tests__/index.spec.ts b/src/entities/vendedores/__tests__/index.spec.ts new file mode 100644 index 0000000..e675220 --- /dev/null +++ b/src/entities/vendedores/__tests__/index.spec.ts @@ -0,0 +1,57 @@ +import { Chance } from 'chance' +import { Vendedores } from '..' +import { InMemoryBlingRepository } from '../../../repositories/bling-in-memory.repository' +import findResponse from './find-response' +import getResponse from './get-response' + +const chance = Chance() + +describe('Vendedores entity', () => { + let repository: InMemoryBlingRepository + let entity: Vendedores + + beforeEach(() => { + repository = new InMemoryBlingRepository() + entity = new Vendedores(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: 'vendedores', + params: { + limite: undefined, + pagina: undefined, + nomeContato: undefined, + situacaoContato: undefined, + idContato: undefined, + idLoja: undefined, + dataAlteracaoInicial: undefined, + dataAlteracaoFinal: undefined + } + }) + expect(response).toBe(getResponse) + }) + + it('should find successfully', async () => { + const spy = jest.spyOn(repository, 'show') + const idVendedor = chance.natural() + repository.setResponse(findResponse) + + const response = await entity.find({ idVendedor }) + + expect(spy).toHaveBeenCalledWith({ + endpoint: 'vendedores', + id: String(idVendedor) + }) + expect(response).toBe(findResponse) + }) +}) diff --git a/src/entities/vendedores/index.ts b/src/entities/vendedores/index.ts new file mode 100644 index 0000000..6183ec2 --- /dev/null +++ b/src/entities/vendedores/index.ts @@ -0,0 +1,57 @@ +import { Entity } from '../@shared/entity' +import { IFindParams, IFindResponse } from './interfaces/find.interface' +import { IGetParams, IGetResponse } from './interfaces/get.interface' + +/** + * Entidade para interação com vendedores. + * + * @see https://developer.bling.com.br/referencia#/Vendedores + */ +export class Vendedores extends Entity { + /** + * Obtém vendedores. + * + * @param {IGetParams} params Parâmetros da busca. + * + * @returns {Promise} + * @throws {BlingApiException|BlingInternalException} + * + * @see https://developer.bling.com.br/referencia#/Vendedores/get_vendedores + */ + public async get(params?: IGetParams): Promise { + return await this.repository.index({ + endpoint: 'vendedores', + params: { + pagina: params?.pagina, + limite: params?.limite, + nomeContato: params?.nomeContato, + situacaoContato: params?.situacaoContato, + idContato: params?.idContato, + idLoja: params?.idLoja, + dataAlteracaoInicial: this.prepareStringOrDateParam( + params?.dataAlteracaoInicial + ), + dataAlteracaoFinal: this.prepareStringOrDateParam( + params?.dataAlteracaoFinal + ) + } + }) + } + + /** + * Obtém um vendedor. + * + * @param {IFindParams} params Parâmetros da busca. + * + * @returns {Promise} + * @throws {BlingApiException|BlingInternalException} + * + * @see https://developer.bling.com.br/referencia#/Vendedores/get_vendedores__idVendedor_ + */ + public async find(params: IFindParams): Promise { + return await this.repository.show({ + endpoint: 'vendedores', + id: String(params.idVendedor) + }) + } +} diff --git a/src/entities/vendedores/interfaces/find.interface.ts b/src/entities/vendedores/interfaces/find.interface.ts new file mode 100644 index 0000000..f9f577b --- /dev/null +++ b/src/entities/vendedores/interfaces/find.interface.ts @@ -0,0 +1,25 @@ +import { ISituacao } from '../types/situacao.type' + +export interface IFindParams { + /** + * ID do vendedor + */ + idVendedor: number +} + +export interface IFindResponse { + data: { + id?: number + descontoLimite?: number + loja?: { id: number } + contato: { + id: number + nome: string + situacao: ISituacao + } + comissoes: { + descontoMaximo: number + aliquota: number + }[] + } +} diff --git a/src/entities/vendedores/interfaces/get.interface.ts b/src/entities/vendedores/interfaces/get.interface.ts new file mode 100644 index 0000000..b89bf10 --- /dev/null +++ b/src/entities/vendedores/interfaces/get.interface.ts @@ -0,0 +1,49 @@ +import { ISituacao } from '../types/situacao.type' + +export interface IGetParams { + /** + * N° da página da listagem + */ + pagina?: number + /** + * Quantidade de registros que devem ser exibidos por página + */ + limite?: number + /** + * Nome do contato do vendedor + */ + nomeContato?: string + /** + * Situação do contato do vendedor + */ + situacaoContato?: ISituacao + /** + * ID do contato do vendedor + */ + idContato?: number + /** + * ID da loja vinculada ao vendedor + */ + idLoja?: number + /** + * Data de alteração inicial + */ + dataAlteracaoInicial?: Date | string + /** + * Data de alteração final + */ + dataAlteracaoFinal?: Date | string +} + +export interface IGetResponse { + data: { + id?: number + descontoLimite?: number + loja?: { id: number } + contato: { + id: number + nome: string + situacao: ISituacao + } + } +} diff --git a/src/entities/vendedores/types/situacao.type.ts b/src/entities/vendedores/types/situacao.type.ts new file mode 100644 index 0000000..7352a0f --- /dev/null +++ b/src/entities/vendedores/types/situacao.type.ts @@ -0,0 +1,9 @@ +/** + * Tipagem referente à situação do vendedor. + * + * - `A`: Ativo + * - `I`: Inativo + * - `S`: Sem movimento + * - `E`: Excluído + */ +export type ISituacao = 'A' | 'I' | 'S' | 'E'