fix: a Tiny order goes to the customer's live contact, never a deleted one, and takes its seller (Marcus, 2026-12-02, #237)
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 9m6s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m25s

Tiny lists deleted contacts (situação "E") beside the live one, a newer one
first. The integration took the first contact with the CPF/CNPJ, so #237
(DTF-237) went to a deleted Vaneska without a seller while the active one
had Kedma, and Dropstar added the seller by hand on every such order. The
contact is now chosen among the live ones: active before inactive, one with
a seller before one without, then Tiny's order. Live registrations naming
different sellers send none and reach Sentry, since which is right is
Dropstar's to say. Only deleted ones: a new contact, as for a new customer.

"tiny_probe vendedores [--desde]" (read-only) sorts every order sent to Tiny
since the seller rule (2026-11-27) by why its seller is what it is, so the
other orders can be checked; "vendedor NUMERO" now shows each contact's
situação.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-12-02 16:23:47 -03:00
parent 6c7af233bc
commit f23b64bebf
3 changed files with 216 additions and 14 deletions

View File

@@ -254,6 +254,44 @@ class TinyTests(FakeTinyCase):
self.tiny.deliver('k1', PAID)
self.assertEqual(self.orders[0]['vendedor'], {'id': 977231477})
def test_a_deleted_duplicate_is_never_used(self):
# #237 (Marcus, 2026-12-02): Tiny listed a deleted Vaneska without a
# seller first; the active one has Kedma.
self.contacts.append({'id': 760864611, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'E'})
self.contacts.append({'id': 760800822, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'A',
'vendedor': {'id': 977226210, 'nome': 'Kedma da Silva'}})
receipt = self.tiny.deliver('k1', PAID)
self.assertEqual(self.orders[0]['idContato'], 760800822)
self.assertEqual(self.orders[0]['vendedor'], {'id': 977226210})
self.assertEqual(receipt['seller'], 977226210)
def test_an_active_contact_with_a_seller_comes_first(self):
self.contacts.append({'id': 400, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'I',
'vendedor': {'id': 7, 'nome': 'Inativo'}})
self.contacts.append({'id': 401, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'A'})
self.contacts.append({'id': 402, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'A',
'vendedor': {'id': 977226210, 'nome': 'Kedma'}})
self.assertEqual(self.tiny.find_contact('11222333000181'), 402)
# Only inactive and seller-less ones: the active one, without a seller.
self.contacts[:] = [c for c in self.contacts if c['id'] != 402]
self.contacts[0]['vendedor'] = None
self.assertEqual(self.tiny.find_contact('11222333000181'), 401)
def test_live_duplicates_with_different_sellers_go_without_and_reach_sentry(self):
self.contacts.append({'id': 500, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'A', 'vendedor': {'id': 7}})
self.contacts.append({'id': 501, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'A', 'vendedor': {'id': 8}})
with self.assertLogs(level='ERROR') as logged:
receipt = self.tiny.deliver('k1', PAID)
self.assertNotIn('vendedor', self.orders[0])
self.assertIsNone(receipt['seller'])
self.assertIn('different sellers', logged.output[0])
def test_only_a_deleted_contact_makes_a_new_one(self):
self.contacts.append({'id': 600, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'E', 'vendedor': {'id': 7}})
self.tiny.deliver('k1', PAID)
self.assertNotEqual(self.orders[0]['idContato'], 600)
self.assertNotIn('vendedor', self.orders[0])
def test_the_registration_seller_wins_over_who_assembled_a_venda(self):
self.contacts.append({'id': 321, 'cpfCnpj': '11.222.333/0001-81', 'vendedor': {'id': 977231477}})
venda = {**PAID, 'order': {**PAID['order'], 'seller': {'id': 2001, 'name': 'Kedma'}}}
@@ -923,11 +961,37 @@ class TinyProbeTests(FakeTinyCase):
self.assertIn('A integração encontra o contato id 321 e lê o vendedor: id 977226210', text)
self.assertIn('Conclusão: a integração lê o vendedor hoje e o pedido ficou sem ele.', text)
self.assertNotIn('PUT', [method for method, _ in self.calls if method != 'POST'])
# A second contact with the same document, found first, is named.
self.contacts.insert(0, {'id': 320, 'cpfCnpj': '11.222.333/0001-81'})
# #237: the order went to a deleted duplicate, listed first.
self.contacts.insert(0, {'id': 320, 'cpfCnpj': '11.222.333/0001-81', 'situacao': 'E'})
order['cliente'] = {'id': 320, 'cpfCnpj': '11.222.333/0001-81'}
code, text = self.probe('vendedor', order['numeroPedido'])
self.assertIn('Contatos com o mesmo CPF/CNPJ: 2', text)
self.assertIn('Conclusão: a busca da integração cai em outro contato que não o do pedido.', text)
self.assertIn('id 320 · excluído · vendedor nenhum', text)
self.assertIn('Conclusão: hoje a integração escolhe outro contato (id 321), não o do pedido (id 320).', text)
def test_vendedores_sorts_every_sent_order_by_why(self):
document = '11.222.333/0001-81'
self.contacts += [{'id': 320, 'cpfCnpj': document, 'situacao': 'E'},
{'id': 321, 'cpfCnpj': document, 'situacao': 'A', 'vendedor': {'id': 977226210, 'nome': 'Kedma'}},
{'id': 330, 'cpfCnpj': '22.333.444/0001-81', 'situacao': 'A'}]
def order(contact, number):
self.orders.append({'id': 9100 + number, 'numeroPedido': str(number), 'cliente': {'id': contact}})
return {'number': number, 'tiny_id': 9100 + number, 'tiny_number': str(number)}
rows = [{**order(320, 1), 'document': document}, # #237: a deleted duplicate
{**order(321, 2), 'document': document}, # the active one with Kedma
{**order(330, 3), 'document': '22.333.444/0001-81', 'assembler': {'id': 2001}},
{**order(330, 4), 'document': '22.333.444/0001-81'}]
from app import tiny_probe
lines = []
tiny_probe.main(['vendedores'], orders=self.tiny, out=lines.append, rows=rows)
text = '\n'.join(lines)
self.assertIn('Pedidos enviados ao Tiny: 4', text)
self.assertIn('1 ligado a um contato excluído', text)
self.assertIn('#1 · Tiny 1 · vendedor no pedido hoje: nenhum', text)
self.assertIn('1 contato do pedido com vendedor no cadastro', text)
self.assertIn('1 cliente sem vendedor no cadastro; Venda do Kanban', text)
self.assertIn('1 cliente sem vendedor em nenhum cadastro', text)
self.assertTrue(all(method == 'GET' for method, _ in self.calls))
def test_cancelar_touches_only_test_orders(self):
self.tiny.deliver('k1', PAID)