diff --git a/app/tiny_probe.py b/app/tiny_probe.py index 2558b74..70bd93f 100644 --- a/app/tiny_probe.py +++ b/app/tiny_probe.py @@ -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) diff --git a/tests/test_tiny.py b/tests/test_tiny.py index 6c283d9..fc8ce4c 100644 --- a/tests/test_tiny.py +++ b/tests/test_tiny.py @@ -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, [])