Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(correios v2): Create app to integrate Correios new APIs #251

Merged
merged 26 commits into from
Nov 3, 2023

Conversation

wisley7l
Copy link
Member

@wisley7l wisley7l commented Oct 6, 2023

@wisley7l wisley7l changed the title feat(correios v2): Create app to integrate Correios new APIs [WIP]feat(correios v2): Create app to integrate Correios new APIs Oct 6, 2023
@wisley7l
Copy link
Member Author

@leomp12 portei o app do correios v2, até aqui sem muitos problemas.

Já com o fill-database fiz bastantes alterações (se esse era mesmo o caminho), pq vi que a api v2 do correios os paramentros para o calculo de frete inclui também as dimensões (que acabou gerando laços de repetição encadeados).

  • Isso pode ser um problema? por questões de performace e/ou custos do proprio firebase?

Então quando puder fazer a revisão aqui poderia ser mais criteriosa, para ver se fez ou não sentido as modificações feitas.

@wisley7l wisley7l requested a review from leomp12 October 10, 2023 19:49
@wisley7l wisley7l changed the title [WIP]feat(correios v2): Create app to integrate Correios new APIs feat(correios v2): Create app to integrate Correios new APIs Oct 10, 2023
@wisley7l wisley7l requested a review from leomp12 October 18, 2023 16:34
Copy link
Member

@leomp12 leomp12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não entendi porque mudou tanto (não em sintaxe, lógica mesmo) a implementação do https://github.com/ecomplus/app-correios-db/blob/master/functions/lib/integration/fill-database.js , esse negócio é menos trivial que parece e na prática enfileirar tantos eventos acaba dando num monte de BO, eu acho que era mais seguro algo mais parecido com um copia e cola da implementação que já funciona...

packages/apps/correios-v2/README.md Outdated Show resolved Hide resolved
packages/modules/src/firebase/call-app-module.ts Outdated Show resolved Hide resolved
packages/firebase/src/config.ts Outdated Show resolved Hide resolved
packages/apps/correios-v2/scripts/tests.sh Outdated Show resolved Hide resolved
packages/apps/correios-v2/src/correios-events.ts Outdated Show resolved Hide resolved
packages/apps/correios-v2/lib-mjs/calculate-correios.mjs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Antes chamava calculate-correios.mjs só pra evitar ter mil arquivos "complexos" (não só um index.ts importando tudo) com o mesmo nome.
Quer dizer, calculate-shipping.mjs pode ser de qualquer app de envio, e aqui como é um monorepo estão todos no mesmo codebase

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfeito.

packages/apps/correios/package.json Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
Copy link
Member

@leomp12 leomp12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tem mais um BO aqui que eu vi e vai dar erro na prática...
Quando preenche o DB não pode haver custo por serviço adicional (a request provavelmente tem que ir sem as flags do Correios lá inclusive), mas no cálculo do frete esse custo tem que ser posteriormente checado, se ativo, pelo subtotal do carrinho.
Então aqui está errado de duas formas:

  1. Quando você faz a request "sob demanda" e salva no banco ela é salva com o valor declarado;
  2. Quando você busca o resultado do banco o valor declarado (e outros serviços) não são posteriormente tratados como acontece aqui https://github.com/ecomplus/app-correios-db/blob/master/functions/routes/ecom/modules/calculate-shipping.js#L345-L363

Na prática, se a mesma loja receber dois carrinhos com mesmo peso e faixa de CEP, um por um produto de R$ 5.000,00 e outro por um produto de R$ 10,00, o segundo cálculo vai ficar errado buscado do banco por causa da diferença da taxa do valor declarado.

packages/apps/correios/lib-mjs/utils/constants-parsers.mjs Outdated Show resolved Hide resolved
Comment on lines 47 to 52
if (correios) {
const { nuContrato } = correios.$contract;
if (nuContrato) {
id = `${nuContrato}_${id}`;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Na verdade não deveria retornar um id sem número de contrato nunca, aliás em teoria nem deveria chegar aqui sem contrato...
O contrato é requerido

action.yml Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/utils/constants-parsers.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/utils/constants-parsers.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/utils/constants-parsers.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
}

if (addServiceVl.vlMinDeclarado) {
const realValue = (vlDeclarado - addServiceVl.vlMinDeclarado);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Porque essa subtração ? 🤔

nuRequisicao,
tpObjeto: '2',
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


// /*
try {
const psObjKg = parserWeight((pkg.weight.value) / 1000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não necessariamente é o peso físico, o que vale é o maior entre o peso físico e o cúbico

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Se atente a todos os parsers desse calculate-shipping do app de referência, por favor

packages/apps/correios/lib-mjs/calculate-shipping.mjs Outdated Show resolved Hide resolved
weightValue = weight.value;
break;
case 'mg':
weightValue = weight.value / 1000000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
weightValue = weight.value / 1000000;
weightValue = weight.value / 1000;

mg pra g né?

Comment on lines 199 to 202
// convert weight to kilograms
if (weightValue > 0) {
weightValue /= 1000;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converte o peso pra gramas e depois volta pra kg? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não era melhor você manter o weightValue somado em g e setar o cubicWeight também em g (aka 1000 vezes menor)?

Comment on lines +204 to +207
if (!configApp.free_no_weight_shipping || weightValue > 0) {
psObj += quantity * (cubicWeight < 5 || weightValue > cubicWeight
? weightValue : cubicWeight);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mas esse parâmetro não era pra ir em gramas? Não tá indo em kg aqui?

// no need to delete undefined fields

Object.keys(obj).forEach((key) => {
if (!obj[key]) delete obj[key];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!obj[key]) delete obj[key];
if (obj[key] === undefined) delete obj[key];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deixa passar string vazia, talvez valor 0...

leomp12
leomp12 previously approved these changes Nov 3, 2023
Copy link
Member

@leomp12 leomp12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bora 👍🏾 😄

return calculateWeights.map((psObj) => {
const promises = [];

const calculateParams = { ..._calculateParams, psObjeto: psObj * 1000 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tá meio confuso isso, por que não usar sempre gramas já que a API deles recebe em gramas agora?

@leomp12 leomp12 merged commit a1fb7e8 into main Nov 3, 2023
1 check failed
@leomp12 leomp12 deleted the feat/correios-v2 branch November 3, 2023 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants