From 93cb28994063c3542cf316b20513a809197fee1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Mon, 26 Oct 2026 11:47:21 -0300 Subject: [PATCH] feat: Site orders carry DTF- as Tiny's numeroPedidoEcommerce Dropstar asked on 2026-10-23 for the Kanban number in numeroPedidoEcommerce. It goes there on every order now, next to numeroOrdemCompra, which the resend check still searches. Tiny's v3 model takes the e-commerce block without a channel id; TINY_ECOMMERCE_ID adds one and can now be set in the stack. The supervised test order reads the number back from Tiny and fails if Tiny did not keep it. Co-Authored-By: Claude Opus 5.5 --- app/tiny.py | 8 ++++++-- app/tiny_probe.py | 14 +++++++++++--- docker-compose.yml | 3 +++ docs/CONTEXT.md | 7 +++++-- tests/test_tiny.py | 20 ++++++++++++++++++++ 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/app/tiny.py b/app/tiny.py index 496bc62..046ef96 100644 --- a/app/tiny.py +++ b/app/tiny.py @@ -20,7 +20,9 @@ sum, 1,30 m = 1 × "1 metro" + 1 × "30 cm", at the price the customer paid. Idempotency: the outbox may deliver the same event more than once. Every order carries numeroOrdemCompra = "DTF-", and before creating one the customer's recent orders are searched for that number, so a second delivery -finds the first order instead of creating another. +finds the first order instead of creating another. The same number goes in +numeroPedidoEcommerce (Dropstar, 2026-10-23), which n8n reads as +numero_ecommerce. Customer notices: the client sends WhatsApp messages from Tiny's order situação (Tiny webhook -> their middleware -> n8n), on Aprovado, Pronto para @@ -499,9 +501,11 @@ def order_payload(payload, contact_id, today=None, product_id=None, shipping=Non pedido['enderecoEntrega'] = {**address(destination), 'enderecoNro': destination['number'], 'nomeDestinatario': destination['recipient']} del pedido['enderecoEntrega']['numero'] + # The channel id is optional in Tiny's v3 model; TINY_ECOMMERCE_ID adds it. + pedido['ecommerce'] = {'numeroPedidoEcommerce': purchase_order(payload['number'])} ecommerce = os.environ.get('TINY_ECOMMERCE_ID', '') if ecommerce.isdigit(): - pedido['ecommerce'] = {'id': int(ecommerce), 'numeroPedidoEcommerce': purchase_order(payload['number'])} + pedido['ecommerce']['id'] = int(ecommerce) return pedido diff --git a/app/tiny_probe.py b/app/tiny_probe.py index cdfe0d6..5dff2fe 100644 --- a/app/tiny_probe.py +++ b/app/tiny_probe.py @@ -198,11 +198,19 @@ def pedido(orders, payload, confirm, out, wait=time.sleep): out(f'FALHA: a busca por {tiny.purchase_order(payload["number"])} não encontrou o pedido. ' 'Um reenvio criaria um duplicado; o 2º envio não foi feito.') return False + # What Tiny kept of the e-commerce number, read back from the order. + wanted = tiny.purchase_order(payload['number']) + shop = orders.request('GET', f"/pedidos/{first['tiny_id']}").get('ecommerce') or {} + out(f"E-commerce no Tiny: numeroPedidoEcommerce {shop.get('numeroPedidoEcommerce') or '(vazio)'}" + + (f" · canal {shop.get('nome') or '?'} (id {shop['id']})" if shop.get('id') else ' · sem canal')) + numbered = shop.get('numeroPedidoEcommerce') == wanted + if not numbered: + out(f'FALHA: o Tiny não guardou {wanted} em numeroPedidoEcommerce.') second = orders.deliver('teste-integracao', payload) out(f"2º envio: {second['status']} · Tiny id {second['tiny_id']}") - passed = second['status'] == 'already-created' and second['tiny_id'] == first['tiny_id'] - out('OK: o reenvio encontrou o mesmo pedido.' if passed else 'FALHA: o reenvio não devolveu o mesmo pedido.') - return passed + found_again = second['status'] == 'already-created' and second['tiny_id'] == first['tiny_id'] + out('OK: o reenvio encontrou o mesmo pedido.' if found_again else 'FALHA: o reenvio não devolveu o mesmo pedido.') + return numbered and found_again finally: # Whatever happened above, the test order does not stay open. cancelar(orders, first['tiny_id'], out) diff --git a/docker-compose.yml b/docker-compose.yml index 7785e28..ed50ff6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -106,6 +106,9 @@ x-app-environment: &app-environment TINY_SKU_METRO_UV_AVULSA: ${TINY_SKU_METRO_UV_AVULSA:-} # The client's "retirar pessoalmente" forma de envio id, on pickup orders. TINY_FORMA_ENVIO_RETIRADA: ${TINY_FORMA_ENVIO_RETIRADA:-} + # Optional e-commerce channel id for Site orders. Without it the order still + # carries DTF- as numeroPedidoEcommerce, with no channel. + TINY_ECOMMERCE_ID: ${TINY_ECOMMERCE_ID:-} # Sets "Aprovada" on paid orders and "Pronto para envio" on finished pickup # orders, which the client's Tiny -> n8n notices send to customers. Turn on # only together with TINY_ADAPTER=tiny and after n8n stops sending the diff --git a/docs/CONTEXT.md b/docs/CONTEXT.md index 8e8a4d4..45cf0f4 100644 --- a/docs/CONTEXT.md +++ b/docs/CONTEXT.md @@ -398,8 +398,11 @@ Done on 2026-10-26: the single price per product Dropstar asked for on 2026-10-23 (see Commercial rules), on the Site and the server. Still to do (2026-10-26): -- The DTF number in Tiny's `numeroPedidoEcommerce` (today it is the purchase - order, `numeroOrdemCompra`); confirm at the supervised Tiny test order. +- The DTF number in Tiny's `numeroPedidoEcommerce`: sent since 2026-10-26, + next to the purchase order `numeroOrdemCompra` (which the resend check + searches), with no channel unless `TINY_ECOMMERCE_ID` is set; Tiny's v3 API + cannot list channels or take one by name. Confirm at the supervised Tiny + test order: the probe reads the number back and fails if Tiny dropped it. - Waiting on Dropstar: the DTF SITE category's numeric id (CATEGORY in app/tiny.py is still DUPLICADO DROP), their flow image (removing Fila de impressão, a Revisão column and a Revisor profile, the only one to move to diff --git a/tests/test_tiny.py b/tests/test_tiny.py index 91a1fac..dd24531 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -131,6 +131,10 @@ class TinyTests(FakeTinyCase): pedido = order_payload(PAID, 777, date(2026, 9, 24)) self.assertEqual((pedido['idContato'], pedido['numeroOrdemCompra'], pedido['data']), (777, 'DTF-42', '2026-09-24')) + # The DTF number is also the e-commerce order number; no channel unless one is set. + self.assertEqual(pedido['ecommerce'], {'numeroPedidoEcommerce': 'DTF-42'}) + with mock.patch.dict(os.environ, {'TINY_ECOMMERCE_ID': '555'}): + self.assertEqual(order_payload(PAID, 777)['ecommerce'], {'numeroPedidoEcommerce': 'DTF-42', 'id': 555}) item = pedido['itens'][0] self.assertEqual((item['produto'], item['quantidade'], item['valorUnitario']), ({'id': 102}, 2.8, 24.9)) @@ -615,11 +619,27 @@ class TinyProbeTests(FakeTinyCase): self.assertEqual(len(self.orders), 1) self.assertEqual(self.orders[0]['itens'][0]['produto'], {'id': 104}) self.assertIn('2º envio: already-created', text) + self.assertIn('E-commerce no Tiny: numeroPedidoEcommerce DTF-TESTE-', text) # Then cancelled through the API, and read back: it does not stay open. self.assertEqual(self.orders[0]['situacao'], 2) self.assertIn(f"Pedido nº {self.orders[0]['numeroPedido']} cancelado no Tiny.", text) self.assertEqual(self.tags[self.orders[0]['id']], [{'descricao': 'SITE DTF'}]) + def test_a_number_tiny_did_not_keep_fails_the_supervised_order(self): + # Tiny may drop the e-commerce block of an order with no channel. + def dropping(request): + if request.method == 'POST' and request.url.path.endswith('/pedidos'): + body = {k: v for k, v in json.loads(request.content).items() if k != 'ecommerce'} + request = httpx.Request(request.method, request.url, headers=request.headers, content=json.dumps(body)) + return self.handler(request) + self.tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(dropping), today=date(2026, 9, 24)) + code, text = self.probe('pedido', '--cnpj', '11222333000181', '--email', 'a@example.test', + '--celular', '16999999999', '--confirmar') + self.assertEqual(code, 1, text) + self.assertIn('numeroPedidoEcommerce (vazio) · sem canal', text) + self.assertIn('FALHA: o Tiny não guardou DTF-TESTE-', text) + self.assertEqual(self.orders[0]['situacao'], 2) + def test_cancelar_touches_only_test_orders(self): self.tiny.deliver('k1', PAID) real = self.orders[0]