feat: the Tiny probe reads what a complete order needs
Some checks failed
Some checks failed
"campos" tries each lookup a complete order needs (formas de recebimento, financial categories, sellers, intermediaries) and names the app permission to release when Tiny refuses one; it also shows Correios and Jadlog as Tiny keeps them, with their freight options. "exemplo NUMERO" prints an existing order's payment, carrier, seller, category and tags, without the customer or the address, so Site orders can copy the store's. Both only read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,10 @@ Tiny has no sandbox, so this is how the adapter is proven on the real account.
|
||||
python -m app.tiny_probe produtos [termo] list active products (read-only)
|
||||
python -m app.tiny_probe formas-envio [termo] list formas de envio, to find pickup (read-only)
|
||||
python -m app.tiny_probe conferir connection, the products or SKUs and pickup (read-only)
|
||||
python -m app.tiny_probe campos what a complete order needs from Tiny: payment methods,
|
||||
categories, sellers, Correios/Jadlog freight (read-only)
|
||||
python -m app.tiny_probe exemplo NUMERO the structure of an existing order, without the
|
||||
customer's details, to copy in Site orders (read-only)
|
||||
python -m app.tiny_probe pedido --cnpj ... --email ... --celular ... [--modo file] [--metros 1.3]
|
||||
show the test order; --confirmar creates it
|
||||
|
||||
@@ -64,6 +68,70 @@ def formas_envio(orders, termo, out):
|
||||
'. A retirada é a do tipo 6 (Customizado) que a equipe usa para retirar pessoalmente.')
|
||||
|
||||
|
||||
# What a complete order (as the store's own orders reach Tiny) looks up, each
|
||||
# with the app permission it needs in Tiny's Aplicativos screen.
|
||||
LOOKUPS = (('Formas de recebimento', '/formas-recebimento', 'Forma de Recebimento (Leitura)'),
|
||||
('Categorias de receita e despesa', '/categorias-receita-despesa', 'Contas Receber (Leitura)'),
|
||||
('Vendedores', '/vendedores', 'Contatos (Leitura)'),
|
||||
('Intermediadores', '/intermediadores', 'Intermediadores (Leitura)'))
|
||||
# The parts of an existing order worth copying; the customer and the address
|
||||
# are left out. Every other field is listed by name only.
|
||||
EXAMPLE_FIELDS = ('situacao', 'data', 'dataPrevista', 'naturezaOperacao', 'vendedor', 'transportador',
|
||||
'pagamento', 'categoria', 'ecommerce', 'intermediador', 'listaPreco', 'deposito',
|
||||
'numeroOrdemCompra', 'valorFrete', 'origemPedido', 'marcadores')
|
||||
|
||||
|
||||
def short(item):
|
||||
return json.dumps(item, ensure_ascii=False, separators=(', ', ': '))[:240]
|
||||
|
||||
|
||||
def listed(found):
|
||||
return found if isinstance(found, list) else (found.get('itens') or [])
|
||||
|
||||
|
||||
def campos(orders, out):
|
||||
ok = True
|
||||
for label, path, permission in LOOKUPS:
|
||||
try:
|
||||
found = listed(orders.request('GET', path, params={'limit': 100}))
|
||||
except tiny.TinyError as exc:
|
||||
ok = False
|
||||
denied = ': 403 ' in str(exc) or ': 401 ' in str(exc)
|
||||
out(f'{label}: ' + (f'sem permissão. Libere "{permission}" no aplicativo e reconecte.' if denied
|
||||
else str(exc)))
|
||||
continue
|
||||
out(f'{label}: {len(found)}')
|
||||
for item in found:
|
||||
out(' ' + short(item))
|
||||
# Correios and Jadlog, with whatever freight options Tiny keeps for them.
|
||||
methods = listed(orders.request('GET', '/formas-envio', params={'limit': 100}))
|
||||
for method in methods:
|
||||
if not any(word in str(method.get('nome', '')).lower() for word in ('correio', 'jadlog')):
|
||||
continue
|
||||
out(f"Forma de envio {method.get('id')} · tipo {method.get('tipo')} · {method.get('nome')}")
|
||||
try:
|
||||
out(' ' + short(orders.request('GET', f"/formas-envio/{method['id']}")))
|
||||
except tiny.TinyError as exc:
|
||||
out(f' {exc}')
|
||||
return ok
|
||||
|
||||
|
||||
def exemplo(orders, numero, out):
|
||||
found = listed(orders.request('GET', '/pedidos', params={'numero': numero, 'limit': 5}))
|
||||
match = [p for p in found if str(p.get('numeroPedido') or p.get('numero') or '') == str(numero)]
|
||||
if not match:
|
||||
out(f'Pedido {numero} não encontrado.')
|
||||
return False
|
||||
detail = orders.request('GET', f"/pedidos/{match[0]['id']}")
|
||||
try:
|
||||
detail['marcadores'] = orders.request('GET', f"/pedidos/{match[0]['id']}/marcadores")
|
||||
except tiny.TinyError as exc:
|
||||
detail['marcadores'] = f'não lidos: {exc}'
|
||||
out(json.dumps({k: detail[k] for k in EXAMPLE_FIELDS if k in detail}, ensure_ascii=False, indent=2))
|
||||
out('Outros campos (sem valores): ' + ', '.join(sorted(k for k in detail if k not in EXAMPLE_FIELDS)))
|
||||
return True
|
||||
|
||||
|
||||
def conferir(orders, out):
|
||||
ok = True
|
||||
for mode, setting in tiny.PRODUCT_SETTINGS.items():
|
||||
@@ -143,6 +211,9 @@ def main(argv=None, orders=None, out=print):
|
||||
methods = commands.add_parser('formas-envio')
|
||||
methods.add_argument('termo', nargs='?', default='')
|
||||
commands.add_parser('conferir')
|
||||
commands.add_parser('campos')
|
||||
example = commands.add_parser('exemplo')
|
||||
example.add_argument('numero')
|
||||
order = commands.add_parser('pedido')
|
||||
order.add_argument('--cnpj', '--cpf', dest='cnpj', required=True, help='CNPJ ou CPF do cliente')
|
||||
order.add_argument('--email', required=True)
|
||||
@@ -165,6 +236,10 @@ def main(argv=None, orders=None, out=print):
|
||||
return 0
|
||||
if args.command == 'conferir':
|
||||
return 0 if conferir(orders, out) else 1
|
||||
if args.command == 'campos':
|
||||
return 0 if campos(orders, out) else 1
|
||||
if args.command == 'exemplo':
|
||||
return 0 if exemplo(orders, args.numero, out) else 1
|
||||
cnpj = ''.join(ch for ch in args.cnpj if ch.isdigit())
|
||||
if len(cnpj) not in (11, 14):
|
||||
out('Informe um CPF (11 dígitos) ou um CNPJ (14 dígitos).')
|
||||
|
||||
Reference in New Issue
Block a user