fix: Tiny contacts and orders are searched by the punctuated CPF/CNPJ, as Tiny keeps them
All checks were successful
Build and deploy / Validate source (push) Successful in 4s
Build and deploy / Integration suite on a real stack (push) Successful in 3m17s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m2s

An existing customer was not found by the bare digits, so each Site order
would have created a duplicate contact, and a resent order a duplicate order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-21 15:08:31 -03:00
parent a093b7d82a
commit c266bea684
3 changed files with 80 additions and 20 deletions

View File

@@ -167,17 +167,16 @@ def conferir(orders, out):
def pedido(orders, payload, confirm, out, wait=time.sleep):
cnpj = payload['order']['customer']['cnpj']
contacts = orders.request('GET', '/contatos', params={'cpfCnpj': cnpj, 'limit': 5}).get('itens') or []
known = [c for c in contacts if ''.join(ch for ch in str(c.get('cpfCnpj') or '') if ch.isdigit()) == cnpj]
known = orders.find_contact(cnpj)
if known:
out(f"Contato {cnpj}: já existe no Tiny (id {known[0]['id']})")
out(f"Contato {tiny.punctuated(cnpj)}: já existe no Tiny (id {known})")
else:
# Named as the worker names a new customer: the company in the CNPJ registry.
company = orders.names(cnpj) if orders.names else None
name = tiny.contact_payload(payload['order'], company)['nome']
out(f'Contato {cnpj}: não existe; será criado como "{name}"')
out(f'Contato {tiny.punctuated(cnpj)}: não existe; será criado como "{name}"')
out('Pedido que será enviado:')
out(json.dumps(tiny.order_payload(payload, known[0]['id'] if known else 0, product_id=orders.product_id,
out(json.dumps(tiny.order_payload(payload, known or 0, product_id=orders.product_id,
shipping=orders.shipping),
ensure_ascii=False, indent=2))
out(f'Marcador: {tiny.TAG} (incluído logo depois de criar o pedido)')