Skip to content

Commit

Permalink
feat: adicionar logisticas - remessas
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBellas committed Dec 15, 2023
1 parent 64e8275 commit 92b627c
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Disponível para:
- [ ] PHP (em breve)
- [ ] C# (em breve)

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

**Atenção**: a versão 5.0.0+ do `bling-erp-api` utiliza a API v3 do Bling. Caso
deseje utilizar a API v2 do Bling,
[utilize a versão 4.0.0](https://github.com/AlexandreBellas/bling-erp-api/tree/v4.0.0).
Expand Down Expand Up @@ -79,6 +81,7 @@ Todas as entidades do Bling atualmente são permitidas para interação. São el
- [x] Logísticas (`.logisticas`)
- [x] Logísticas - Etiquetas (`.logisticasEtiquetas`)
- [x] Logísticas - Objetos (`.logisticasObjetos`)
- [x] Logísticas - Remessas (`.logisticasRemessas`)
- [x] Logísticas - Serviços (`.logisticasServicos`)
- [x] Naturezas de Operações (`.naturezasDeOperacoes`)
- [x] Notas Fiscais de Consumidor Eletrônicas (`.nfces`)
Expand Down
7 changes: 7 additions & 0 deletions src/bling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Homologacao } from './entities/homologacao'
import { Logisticas } from './entities/logisticas'
import { LogisticasEtiquetas } from './entities/logisticasEtiquetas'
import { LogisticasObjetos } from './entities/logisticasObjetos'
import { LogisticasRemessas } from './entities/logisticasRemessas'
import { LogisticasServicos } from './entities/logisticasServicos'
import { NaturezasDeOperacoes } from './entities/naturezasDeOperacoes'
import { Nfces } from './entities/nfces'
Expand Down Expand Up @@ -147,6 +148,12 @@ describe('Bling main module', () => {
)
})

it('should retrieve logísticas - remessas entity', () => {
expect(createBling(chance.word()).logisticasRemessas).toBeInstanceOf(
LogisticasRemessas
)
})

it('should retrieve logísticas - serviços entity', () => {
expect(createBling(chance.word()).logisticasServicos).toBeInstanceOf(
LogisticasServicos
Expand Down
10 changes: 10 additions & 0 deletions src/bling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Homologacao } from './entities/homologacao'
import { Logisticas } from './entities/logisticas'
import { LogisticasEtiquetas } from './entities/logisticasEtiquetas'
import { LogisticasObjetos } from './entities/logisticasObjetos'
import { LogisticasRemessas } from './entities/logisticasRemessas'
import { LogisticasServicos } from './entities/logisticasServicos'
import { NaturezasDeOperacoes } from './entities/naturezasDeOperacoes'
import { Nfces } from './entities/nfces'
Expand Down Expand Up @@ -251,6 +252,15 @@ export default class Bling {
return this.getModule(LogisticasObjetos)
}

/**
* Obtém a instância de interação com logísticas - remessas.
*
* @return {LogisticasRemessas}
*/
public get logisticasRemessas(): LogisticasRemessas {
return this.getModule(LogisticasRemessas)
}

/**
* Obtém a instância de interação com logísticas - serviços.
*
Expand Down
15 changes: 15 additions & 0 deletions src/entities/logisticasRemessas/__tests__/create-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
data: {
id: 12345678
}
}

export const createRequestBody = {
numeroPlp: '749fdc73',
situacao: -3 as const,
descricao: 'Remessa_18092023',
logistica: {
id: 12345678
},
objetos: ['12']
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default null
56 changes: 56 additions & 0 deletions src/entities/logisticasRemessas/__tests__/find-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export default {
data: {
id: 12345678,
numeroPlp: '749fdc73',
situacao: -3 as const,
descricao: 'Remessa_18092023',
dataCriacao: '2023-09-18',
logistica: {
id: 12345678
},
objetos: [
{
id: 1235456,
remessa: {
id: 12345678
},
pedidoVenda: {
id: 12345678
},
notaFiscal: {
id: 12345678
},
servico: {
id: 12345678,
nome: 'SEDEX 10 A VISTA',
codigo: '04790'
},
rastreamento: {
codigo: 'EC272330554BR',
descricao: 'Criado',
situacao: 1 as const,
origem: 'São Paulo, SP',
destino: 'São Paulo, SP',
ultimaAlteracao: '2020-11-11 16:40:33',
url: 'https://www.rastreamento.exemplo.com.br/EC272330554BR'
},
dimensao: {
peso: 1.5,
altura: 1.5,
largura: 1.5,
comprimento: 1.5,
diametro: 1.5
},
embalagem: {
id: 12345678
},
dataSaida: '2022-12-01',
prazoEntregaPrevisto: 15,
fretePrevisto: 59.9,
valorDeclarado: 55.9,
avisoRecebimento: false,
maoPropria: false
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
data: [
{
id: 12345678,
numeroPlp: '749fdc73',
situacao: -3 as const,
descricao: 'Remessa_18092023',
dataCriacao: '2023-09-18',
objetos: [6423813145]
}
]
}
116 changes: 116 additions & 0 deletions src/entities/logisticasRemessas/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { Chance } from 'chance'
import { LogisticasRemessas } from '..'
import { InMemoryBlingRepository } from '../../../repositories/bling-in-memory.repository'
import { ICreateResponse } from '../interfaces/create.interface'
import { IFindResponse } from '../interfaces/find.interface'
import { IGetByLogisticResponse } from '../interfaces/get-by-logistic.interface'
import { IUpdateResponse } from '../interfaces/update.interface'
import createResponse, { createRequestBody } from './create-response'
import deleteResponse from './delete-response'
import findResponse from './find-response'
import getByLogisticResponse from './get-by-logistic-response'
import updateResponse, { updateRequestBody } from './update-response'

const chance = Chance()

describe('Logísticas - Remessas entity', () => {
let repository: InMemoryBlingRepository
let entity: LogisticasRemessas

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

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

it('should delete successfully', async () => {
const idRemessa = chance.natural()
const spy = jest.spyOn(repository, 'destroy')
repository.setResponse(deleteResponse)

const response = await entity.delete({ idRemessa })

expect(spy).toHaveBeenCalledWith({
endpoint: 'logisticas/remessas',
id: String(idRemessa)
})
expect(response).toBe(deleteResponse)

const typingResponseTest: null = deleteResponse
expect(typingResponseTest).toBe(deleteResponse)
})

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

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

expect(spy).toHaveBeenCalledWith({
endpoint: 'logisticas/remessas',
id: String(idRemessa)
})
expect(response).toBe(findResponse)

const typingResponseTest: IFindResponse = findResponse
expect(typingResponseTest).toBe(findResponse)
})

it('should get by logistic successfully', async () => {
const spy = jest.spyOn(repository, 'show')
const idLogistica = chance.natural()
repository.setResponse(getByLogisticResponse)

const response = await entity.getByLogistic({ idLogistica })

expect(spy).toHaveBeenCalledWith({
endpoint: 'logisticas',
id: `${idLogistica}/remessas`
})
expect(response).toBe(getByLogisticResponse)

const typingResponseTest: IGetByLogisticResponse = getByLogisticResponse
expect(typingResponseTest).toBe(getByLogisticResponse)
})

it('should create successfully', async () => {
const spy = jest.spyOn(repository, 'store')
repository.setResponse(createResponse)

const response = await entity.create(createRequestBody)

expect(spy).toHaveBeenCalledWith({
endpoint: 'logisticas/remessas',
body: createRequestBody
})
expect(response).toBe(createResponse)

const typingResponseTest: ICreateResponse = createResponse
expect(typingResponseTest).toBe(createResponse)
})

it('should update successfully', async () => {
const spy = jest.spyOn(repository, 'replace')
const idRemessa = chance.natural()
repository.setResponse(updateResponse)

const response = await entity.update({
idRemessa,
...updateRequestBody
})

expect(spy).toHaveBeenCalledWith({
endpoint: 'logisticas/remessas',
id: String(idRemessa),
body: updateRequestBody
})
expect(response).toBe(updateResponse)

const typingResponseTest: IUpdateResponse = updateResponse
expect(typingResponseTest).toBe(updateResponse)
})
})
11 changes: 11 additions & 0 deletions src/entities/logisticasRemessas/__tests__/update-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
data: {
id: 12345678
}
}

export const updateRequestBody = {
numeroPlp: '749fdc73',
situacao: -3 as const,
descricao: 'Remessa_18092023'
}
112 changes: 112 additions & 0 deletions src/entities/logisticasRemessas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Entity } from '../@shared/entity'
import { ICreateBody, ICreateResponse } from './interfaces/create.interface'
import { IDeleteParams } from './interfaces/delete.interface'
import { IFindParams, IFindResponse } from './interfaces/find.interface'
import {
IGetByLogisticParams,
IGetByLogisticResponse
} from './interfaces/get-by-logistic.interface'
import {
IUpdateBody,
IUpdateParams,
IUpdateResponse
} from './interfaces/update.interface'

/**
* Entidade para interação com Logísticas - Remessas.
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas
*/
export class LogisticasRemessas extends Entity {
/**
* Remove uma remessa de postagem.
*
* @param {IDeleteParams} params Parâmetros da remoção.
*
* @returns {Promise<null>} Não há retorno.
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas/delete_logisticas_remessas__idRemessa_
*/
public async delete(params: IDeleteParams): Promise<null> {
return await this.repository.destroy({
endpoint: 'logisticas/remessas',
id: String(params.idRemessa)
})
}

/**
* Obtém uma remessa de postagem.
*
* @param {IFindParams} params Parâmetros da busca.
*
* @returns {Promise<IFindResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas/get_logisticas_remessas__idRemessa_
*/
public async find(params: IFindParams): Promise<IFindResponse> {
return await this.repository.show({
endpoint: 'logisticas/remessas',
id: String(params.idRemessa)
})
}

/**
* Obtém as remessas de postagem de uma logística.
*
* @param {IGetByLogisticParams} params Parâmetros da busca.
*
* @returns {Promise<IGetByLogisticResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas/get_logisticas__idLogistica__remessas
*/
public async getByLogistic(
params: IGetByLogisticParams
): Promise<IGetByLogisticResponse> {
return await this.repository.show({
endpoint: 'logisticas',
id: `${params.idLogistica}/remessas`
})
}

/**
* Cria uma remessa de postagem de uma logística.
*
* @param {ICreateBody} body O conteúdo para a criação.
*
* @returns {Promise<ICreateResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas/post_logisticas_remessas
*/
public async create(body: ICreateBody): Promise<ICreateResponse> {
return await this.repository.store({
endpoint: 'logisticas/remessas',
body
})
}

/**
* Altera uma remessa de postagem.
*
* @param {IUpdateParams & IUpdateBody} params Os parâmetros da atualização.
*
* @return {Promise<IUpdateResponse>}
* @throws {BlingApiException|BlingInternalException}
*
* @see https://developer.bling.com.br/referencia#/Log%C3%ADsticas%20-%20Remessas/put_logisticas_remessas__idRemessa_
*/
public async update(
params: IUpdateParams & IUpdateBody
): Promise<IUpdateResponse> {
const { idRemessa, ...body } = params

return await this.repository.replace({
endpoint: 'logisticas/remessas',
id: String(idRemessa),
body
})
}
}
Loading

0 comments on commit 92b627c

Please sign in to comment.