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

@@ -65,6 +65,7 @@ class FakeTinyCase(unittest.TestCase):
path = request.url.path.removeprefix('/public-api/v3')
self.calls.append((request.method, path))
assert request.headers['authorization'] == 'Bearer access-1'
# Like Tiny: cpfCnpj matches the text the contact was saved with, punctuation and all.
if request.method == 'GET' and path == '/contatos':
cnpj = request.url.params.get('cpfCnpj')
return httpx.Response(200, json={'itens': [c for c in self.contacts if cnpj in (None, c['cpfCnpj'])]})
@@ -94,8 +95,11 @@ class FakeTinyCase(unittest.TestCase):
next(o for o in self.orders if o['id'] == wanted)['situacao'] = json.loads(request.content)['situacao']
return httpx.Response(204)
if request.method == 'GET' and path == '/pedidos':
cnpj = request.url.params.get('cpfCnpj')
owners = {c['id'] for c in self.contacts if cnpj in (None, c['cpfCnpj'])}
return httpx.Response(200, json={'itens': [{'id': o['id'], 'numeroPedido': o['numeroPedido']}
for o in self.orders]})
for o in self.orders
if cnpj is None or o.get('idContato') in owners]})
if path.startswith('/pedidos/') and path.endswith('/marcadores'):
wanted = int(path.split('/')[2])
if request.method == 'GET':
@@ -135,7 +139,7 @@ class TinyTests(FakeTinyCase):
pedido['enderecoEntrega']['nomeDestinatario']), ('14400000', '10', 'Loja Teste'))
contact = contact_payload(PAID['order'])
self.assertEqual((contact['tipoPessoa'], contact['cpfCnpj'], contact['endereco']['uf']),
('J', '11222333000181', 'SP'))
('J', '11.222.333/0001-81', 'SP'))
pickup = order_payload({**PAID, 'order': {**PAID['order'], 'destination': None,
'freight': {'service': 'pickup', 'total_cents': 0}}}, 1)
self.assertNotIn('enderecoEntrega', pickup)
@@ -150,10 +154,29 @@ class TinyTests(FakeTinyCase):
self.assertEqual(self.calls.count(('POST', '/contatos')), 1)
def test_existing_contact_is_reused(self):
self.contacts.append({'id': 321, 'cpfCnpj': '11222333000181'})
# As Tiny keeps it: found only by the punctuated CNPJ.
self.contacts.append({'id': 321, 'cpfCnpj': '11.222.333/0001-81'})
self.tiny.deliver('k1', PAID)
self.assertNotIn(('POST', '/contatos'), self.calls)
self.assertEqual(self.orders[0]['idContato'], 321)
# The order is found again by the same contact: a resend creates nothing.
self.assertEqual(self.tiny.deliver('k1', PAID)['status'], 'already-created')
self.assertEqual(len(self.orders), 1)
def test_a_contact_typed_without_punctuation_is_found_too(self):
self.contacts.append({'id': 322, 'cpfCnpj': '11222333000181'})
self.tiny.deliver('k1', PAID)
self.assertNotIn(('POST', '/contatos'), self.calls)
self.assertEqual(self.orders[0]['idContato'], 322)
self.assertEqual(self.tiny.deliver('k1', PAID)['status'], 'already-created')
self.assertEqual(len(self.orders), 1)
def test_an_existing_cpf_contact_is_found_by_its_punctuated_cpf(self):
self.contacts.append({'id': 760799765, 'cpfCnpj': '130.323.978-71', 'nome': 'Teste Fernandes'})
person = {**PAID, 'order': {**PAID['order'], 'customer': {**PAID['order']['customer'], 'cnpj': '13032397871'}}}
self.tiny.deliver('k-cpf', person)
self.assertNotIn(('POST', '/contatos'), self.calls)
self.assertEqual(self.orders[0]['idContato'], 760799765)
def test_a_cpf_customer_is_a_person_and_not_looked_up_in_the_company_registry(self):
looked_up = []
@@ -161,7 +184,7 @@ class TinyTests(FakeTinyCase):
person = {**PAID, 'order': {**PAID['order'], 'customer': {**PAID['order']['customer'], 'cnpj': '52998224725'}}}
self.tiny.deliver('k-cpf', person)
self.assertEqual((self.contacts[0]['tipoPessoa'], self.contacts[0]['cpfCnpj'], self.contacts[0]['nome']),
('F', '52998224725', 'Loja Teste'))
('F', '529.982.247-25', 'Loja Teste'))
# With the name the Site now asks for, the contact is the person's.
named = {**person['order'], 'customer': {**person['order']['customer'], 'name': 'Maria da Silva'}}
self.assertEqual(contact_payload(named)['nome'], 'Maria da Silva')
@@ -575,6 +598,14 @@ class TinyProbeTests(FakeTinyCase):
self.assertEqual(self.orders, [])
self.assertTrue(all(method == 'GET' for method, _ in self.calls))
def test_an_existing_contact_is_shown_as_reused(self):
self.contacts.append({'id': 760799765, 'cpfCnpj': '130.323.978-71'})
code, text = self.probe('pedido', '--cpf', '13032397871', '--email', 'a@example.test',
'--celular', '16999999999')
self.assertEqual(code, 0)
self.assertIn('Contato 130.323.978-71: já existe no Tiny (id 760799765)', text)
self.assertIn('"idContato": 760799765', text)
def test_confirmed_order_is_created_once_and_found_on_resend(self):
code, text = self.probe('pedido', '--cnpj', '11222333000181', '--email', 'a@example.test',
'--celular', '16999999999', '--modo', 'uv', '--confirmar')