fix: Tiny's test order names a new customer as the real orders do
All checks were successful
Build and deploy / Validate source (push) Successful in 8s
Build and deploy / Integration suite on a real stack (push) Successful in 3m30s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m7s

The supervised test order would have created Dropstar as a Tiny contact named
after its e-mail: the console tool did not look the CNPJ up in the company
registry as the worker does. It now uses the same lookup, and the preview says
which name the new contact will get.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-14 13:38:46 -03:00
parent 5a2e2db49a
commit 5a431f8422
2 changed files with 17 additions and 3 deletions

View File

@@ -101,8 +101,13 @@ 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]
out(f"Contato {cnpj}: " + (f"já existe no Tiny (id {known[0]['id']})" if known
else 'não existe; será criado com o e-mail como nome'))
if known:
out(f"Contato {cnpj}: já existe no Tiny (id {known[0]['id']})")
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('Pedido que será enviado:')
out(json.dumps(tiny.order_payload(payload, known[0]['id'] if known else 0, product_id=orders.product_id,
pickup_id=orders.pickup_id()),
@@ -148,7 +153,9 @@ def main(argv=None, orders=None, out=print):
args = parser.parse_args(argv)
if orders is None:
load_secret_files()
orders = tiny.TinyOrders(auth=tiny.TinyAuth())
from .cnpj import company
# The same names for new contacts as the worker uses (app/worker.py).
orders = tiny.TinyOrders(auth=tiny.TinyAuth(), names=company)
try:
if args.command == 'produtos':
produtos(orders, args.termo, out)

View File

@@ -336,6 +336,13 @@ class TinySkuTests(FakeTinyCase):
text = '\n'.join(lines)
self.assertIn('"quantidade": 1', text)
self.assertIn('"valorUnitario": 4.47', text)
# A new contact is previewed with the name it will get.
self.assertIn('será criado como "a@example.test"', text)
self.tiny.names = lambda cnpj: {'legal': 'EMPRESA TESTE LTDA', 'trade': 'Teste'}
lines.clear()
tiny_probe.main(['pedido', '--cnpj', '11222333000182', '--email', 'a@example.test',
'--celular', '16999999999'], orders=self.tiny, out=lines.append)
self.assertIn('será criado como "EMPRESA TESTE LTDA"', '\n'.join(lines))
self.assertEqual(self.orders, [])