Skip to content

Commit

Permalink
feat: remove buyer address when address is equal to pagarme or company
Browse files Browse the repository at this point in the history
  • Loading branch information
David Medeiros authored and David Medeiros committed Apr 4, 2022
1 parent 004f466 commit c2ce11d
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 26 deletions.
37 changes: 27 additions & 10 deletions src/providers/boleto-api-caixa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
find,
propEq,
pipe,
equals,
} = require('ramda')

const { encodeBase64 } = require('../../lib/encoding')
Expand All @@ -20,6 +21,7 @@ const {
} = require('./formatter')

const config = require('../../config/providers')
const { getPagarmeAddress } = require('../../resources/boleto/model')

const {
boleto_api_password: boletoApiPassword,
Expand Down Expand Up @@ -114,6 +116,30 @@ const defineInterest = (boleto) => {
return null
}

const buyerAddressIsEqualsToPagarme = boleto =>
equals(boleto.payer_address, boleto.company_address)

const buyerAddressIsEqualsToCompany = boleto =>
equals(boleto.payer_address, getPagarmeAddress())

const defineBuyerAddress = (boleto) => {
if (
buyerAddressIsEqualsToCompany(boleto) ||
buyerAddressIsEqualsToPagarme(boleto)
) {
return undefined
}
return {
street: path(['payer_address', 'street'], boleto),
number: String(path(['payer_address', 'street_number'], boleto)),
complement: path(['payer_address', 'complementary'], boleto),
district: path(['payer_address', 'neighborhood'], boleto),
zipCode: String(path(['payer_address', 'zipcode'], boleto)),
city: path(['payer_address', 'city'], boleto),
stateCode: formatStateCode(boleto, 'payer_address'),
}
}

const defineFees = (boleto) => {
const fine = defineFine(boleto)
const interest = defineInterest(boleto)
Expand All @@ -139,7 +165,6 @@ const getInstructions = (companyName, companyDocument, boleto) => {
const buildPayload = (boleto, operationId) => {
const formattedExpirationDate = formatDate(boleto.expiration_date)
const formattedRecipientStateCode = formatStateCode(boleto, 'company_address')
const formattedBuyerStateCode = formatStateCode(boleto, 'payer_address')
const companyName = pathOr('', ['company_name'], boleto)
const companyDocument = pathOr('', ['company_document_number'], boleto)

Expand Down Expand Up @@ -189,15 +214,7 @@ const buildPayload = (boleto, operationId) => {
number: String(path(['payer_document_number'], boleto)),
type: path(['payer_document_type'], boleto).toUpperCase(),
},
address: {
street: path(['payer_address', 'street'], boleto),
number: String(path(['payer_address', 'street_number'], boleto)),
complement: path(['payer_address', 'complementary'], boleto),
district: path(['payer_address', 'neighborhood'], boleto),
zipCode: String(path(['payer_address', 'zipcode'], boleto)),
city: path(['payer_address', 'city'], boleto),
stateCode: formattedBuyerStateCode,
},
address: defineBuyerAddress(boleto),
},
requestKey: operationId,
}
Expand Down
9 changes: 7 additions & 2 deletions src/resources/boleto/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const addBoletoCode = (boleto) => {
})
}

const validateModel = (boleto) => {
const defaultAddress = {
const getPagarmeAddress = () => (
{
zipcode: '04551010',
street: 'Rua Fidêncio Ramos',
street_number: '308',
Expand All @@ -107,6 +107,10 @@ const validateModel = (boleto) => {
city: 'São Paulo',
state: 'SP',
}
)

const validateModel = (boleto) => {
const defaultAddress = getPagarmeAddress()

const requiredAddressFields = [
'zipcode',
Expand Down Expand Up @@ -319,4 +323,5 @@ module.exports = {
buildModelResponse,
validateModel,
create,
getPagarmeAddress,
}
103 changes: 89 additions & 14 deletions test/unit/providers/boleto-api-caixa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
import {
getDocumentType,
} from '../../../../src/providers/boleto-api-caixa/formatter'
import {
getPagarmeAddress,
} from '../../../../src/resources/boleto/model'


const noStrictRules = {
Expand Down Expand Up @@ -113,15 +116,7 @@ test('buildPayload with payer_address incomplete', async (t) => {
const boleto = await createBoleto()
const payload = buildPayload(boleto, operationId)

t.deepEqual(payload.buyer.address, {
street: 'Rua Fidêncio Ramos',
number: '308',
complement: '9º andar, conjunto 91',
zipCode: '04551010',
district: 'Vila Olímpia',
city: 'São Paulo',
stateCode: 'SP',
})
t.deepEqual(payload.buyer.address, undefined)
})

test('buildPayload with the instruction field filled should return the received instruction with the default instruction', async (t) => {
Expand Down Expand Up @@ -405,7 +400,18 @@ test('buildPayload with interest per percentage and amount should return the int

test('buildPayload complete', async (t) => {
const operationId = cuid()
const boleto = await createBoleto({ company_name: 'company name test' })
const boleto = await createBoleto({
company_name: 'company name test',
payer_address: {
zipcode: '5555555',
street_number: '308',
street: 'Rua dos bancos',
complementary: '11º andar',
neighborhood: 'Brooklin',
city: 'São Paulo',
state: 'SP',
},
})
const payload = buildPayload(boleto, operationId)

let { instructions } = boleto
Expand Down Expand Up @@ -473,11 +479,11 @@ test('buildPayload complete', async (t) => {
number: boleto.payer_document_number,
},
address: {
street: 'Rua Fidêncio Ramos',
zipCode: '5555555',
number: '308',
complement: '9º andar, conjunto 91',
zipCode: '04551010',
district: 'Vila Olímpia',
street: 'Rua dos bancos',
complement: '11º andar',
district: 'Brooklin',
city: 'São Paulo',
stateCode: 'SP',
},
Expand All @@ -486,6 +492,75 @@ test('buildPayload complete', async (t) => {
})
})

test('when payer_address is different than company_address and pagarme address then address should be buyer address', async (t) => {
const operationId = cuid()
const boleto = await createBoleto({
company_name: 'company name test',
payer_address: {
zipcode: '5555555',
street_number: '308',
street: 'Rua dos bancos',
complementary: '11º andar',
neighborhood: 'Brooklin',
city: 'São Paulo',
state: 'SP',
},
})

const payload = buildPayload(boleto, operationId)

t.deepEqual(payload.buyer.address, {
zipCode: '5555555',
number: '308',
street: 'Rua dos bancos',
complement: '11º andar',
district: 'Brooklin',
city: 'São Paulo',
stateCode: 'SP',
})
})

test('when payer_address is equals than company_address then buyer address should be undefined', async (t) => {
const operationId = cuid()
const boleto = await createBoleto({
company_name: 'company name test',
payer_address: {
zipcode: '5555555',
street_number: '308',
street: 'Rua dos bancos',
complementary: '11º andar',
neighborhood: 'Brooklin',
city: 'São Paulo',
state: 'SP',
},
company_address: {
zipcode: '5555555',
street_number: '308',
street: 'Rua dos bancos',
complementary: '11º andar',
neighborhood: 'Brooklin',
city: 'São Paulo',
state: 'SP',
},
})

const payload = buildPayload(boleto, operationId)

t.deepEqual(payload.buyer.address, undefined)
})

test('when payer_address is equals than address pagarme then buyer address should be undefined', async (t) => {
const operationId = cuid()
const boleto = await createBoleto({
company_name: 'company name test',
payer_address: getPagarmeAddress(),
})

const payload = buildPayload(boleto, operationId)

t.deepEqual(payload.buyer.address, undefined)
})

test('buildPayload with payeeGuarantor fields null', async (t) => {
const operationId = cuid()
const boleto = await createBoleto({
Expand Down

0 comments on commit c2ce11d

Please sign in to comment.