forked from BrasilAPI/BrasilAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Isolate responsabilities in services
- Loading branch information
1 parent
a233012
commit e99f63e
Showing
4 changed files
with
94 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as Yup from 'yup'; | ||
import makeRequest from '../soapRequestService'; | ||
|
||
export function validateShippingData(data) { | ||
return Yup.object() | ||
.shape({ | ||
nCdEmpresa: Yup.string(), | ||
sDsSenha: Yup.string(), | ||
nCdServico: Yup.string().required(), | ||
sCepOrigem: Yup.string().required(), | ||
sCepDestino: Yup.string().required(), | ||
nVlPeso: Yup.string().required(), | ||
nCdFormato: Yup.string().required(), | ||
nVlComprimento: Yup.string().required(), | ||
nVlAltura: Yup.string().required(), | ||
nVlLargura: Yup.string().required(), | ||
nVlDiametro: Yup.string().required(), | ||
sCdMaoPropria: Yup.string(), | ||
nVlValorDeclarado: Yup.string(), | ||
sCdAvisoRecebimento: Yup.string(), | ||
}) | ||
.validate(data, { | ||
abortEarly: false, | ||
stripUnknown: true, | ||
}); | ||
} | ||
|
||
export async function calculateShipping(data) { | ||
const endpoint = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPreco'; | ||
const result = await makeRequest(endpoint, 'POST', { | ||
nCdEmpresa: data.nCdEmpresa, | ||
sDsSenha: data.sDsSenha, | ||
nCdServico: data.nCdServico, | ||
sCepOrigem: data.sCepOrigem, | ||
sCepDestino: data.sCepDestino, | ||
nVlPeso: data.nVlPeso, | ||
nCdFormato: data.nCdFormato, | ||
nVlComprimento: data.nVlComprimento, | ||
nVlAltura: data.nVlAltura, | ||
nVlLargura: data.nVlLargura, | ||
nVlDiametro: data.nVlDiametro, | ||
sCdMaoPropria: data.sCdMaoPropria, | ||
nVlValorDeclarado: data.nVlValorDeclarado, | ||
sCdAvisoRecebimento: data.sCdAvisoRecebimento, | ||
}); | ||
const shippingData = result.cResultado.Servicos[0].cServico[0]; | ||
Object.getOwnPropertyNames(shippingData).forEach((property) => { | ||
[shippingData[property]] = shippingData[property]; | ||
}); | ||
return shippingData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Parser } from 'xml2js'; | ||
import axios from 'axios'; | ||
|
||
function mountSearchParams(obj) { | ||
return Object.getOwnPropertyNames(obj) | ||
.map((key) => `${key}=${obj[key]}`) | ||
.join('&'); | ||
} | ||
|
||
function parseResponse(xml) { | ||
return new Promise((resolve, reject) => { | ||
const xmlParser = new Parser(); | ||
xmlParser.parseString(xml, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export default async function makeRequest(url, method, obj) { | ||
const data = mountSearchParams(obj); | ||
const options = { | ||
url, | ||
method, | ||
data, | ||
}; | ||
|
||
const response = await axios(options); | ||
return parseResponse(response.data); | ||
} |
This file was deleted.
Oops, something went wrong.