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
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:
64
app/tiny.py
64
app/tiny.py
@@ -488,6 +488,16 @@ def search_forms(document):
|
||||
return list(dict.fromkeys((punctuated(d), d)))
|
||||
|
||||
|
||||
def digits(value):
|
||||
return ''.join(ch for ch in str(value or '') if ch.isdigit())
|
||||
|
||||
|
||||
def seller_id(contact):
|
||||
"""The seller id in a contact (vendedor.id), or None for none or 0."""
|
||||
seller = ((contact or {}).get('vendedor') or {}).get('id')
|
||||
return int(seller) if str(seller or '').isdigit() and int(seller) > 0 else None
|
||||
|
||||
|
||||
def list_sellers(orders, pages=10):
|
||||
"""Tiny's sellers (vendedores) as [{id, name}], for the Kanban user a
|
||||
Venda's seller comes from (Marcus, 2026-11-17). v3 names the seller on
|
||||
@@ -858,20 +868,60 @@ class TinyOrders:
|
||||
self.skus[code] = int(match[0]['id'])
|
||||
return self.skus[code]
|
||||
|
||||
def find_contact(self, cnpj):
|
||||
"""The id of the customer's existing contact, or None."""
|
||||
def contact_choice(self, cnpj):
|
||||
"""(the id of the customer's contact to use or None, the distinct
|
||||
sellers among the usable ones).
|
||||
|
||||
Tiny keeps deleted contacts (situação "E") and lists them beside the
|
||||
live one, a newer one first: #237 went to a deleted Vaneska without a
|
||||
seller while the active one had Kedma (Marcus, 2026-12-02). A deleted
|
||||
contact is never used. Among the rest an active one comes before an
|
||||
inactive one, and one with a seller before one without, since the
|
||||
order carries the seller of the customer's registration (2026-11-26);
|
||||
then Tiny's own order."""
|
||||
seen, usable, sellers = set(), [], set()
|
||||
for form in search_forms(cnpj):
|
||||
found = self.request('GET', '/contatos', params={'cpfCnpj': form, 'limit': 5})
|
||||
for entry in found.get('itens') or []:
|
||||
if ''.join(ch for ch in str(entry.get('cpfCnpj') or '') if ch.isdigit()) == cnpj:
|
||||
return entry['id']
|
||||
return None
|
||||
if entry['id'] in seen or digits(entry.get('cpfCnpj')) != cnpj:
|
||||
continue
|
||||
seen.add(entry['id'])
|
||||
detail = self.contact_detail(entry['id']) or entry
|
||||
situacao = detail.get('situacao', entry.get('situacao'))
|
||||
if situacao == 'E':
|
||||
continue
|
||||
seller = seller_id(detail)
|
||||
if seller:
|
||||
sellers.add(seller)
|
||||
usable.append(((situacao or 'A') != 'A', seller is None, len(usable), entry['id']))
|
||||
return (min(usable)[-1] if usable else None), sellers
|
||||
|
||||
def find_contact(self, cnpj):
|
||||
"""The id of the customer's contact to use (contact_choice), or None."""
|
||||
return self.contact_choice(cnpj)[0]
|
||||
|
||||
def contact_detail(self, contact_id):
|
||||
"""A contact as Tiny has it; None when Tiny refuses the reading, so
|
||||
the listing decides. A busy or unavailable Tiny retries the delivery."""
|
||||
try:
|
||||
return self.request('GET', f'/contatos/{contact_id}')
|
||||
except (TinyRateLimit, TinyUnavailable):
|
||||
raise
|
||||
except TinyError:
|
||||
return None
|
||||
|
||||
def contact(self, order):
|
||||
"""The customer's contact id, and the seller of an existing one."""
|
||||
cnpj = order['customer']['cnpj']
|
||||
known = self.find_contact(cnpj)
|
||||
known, sellers = self.contact_choice(cnpj)
|
||||
if known:
|
||||
if len(sellers) > 1:
|
||||
# Two live registrations of one customer with different
|
||||
# sellers: which one is right is Dropstar's to say, so the
|
||||
# order goes without, as before, and Sentry hears of it.
|
||||
logging.error('Tiny: the contacts of %s name different sellers (%s); the order goes without one',
|
||||
punctuated(cnpj), ', '.join(map(str, sorted(sellers))))
|
||||
return known, None
|
||||
return known, self.contact_seller(known)
|
||||
company = None
|
||||
if self.names and len(cnpj) == 14:
|
||||
@@ -895,7 +945,7 @@ class TinyOrders:
|
||||
logging.error('Tiny: could not read the seller of contact %s; the order goes without one: %s',
|
||||
contact_id, exc)
|
||||
return None
|
||||
return int(seller) if str(seller or '').isdigit() and int(seller) > 0 else None
|
||||
return seller_id({'vendedor': {'id': seller}})
|
||||
|
||||
def find(self, payload, days=SEARCH_DAYS):
|
||||
"""An order a previous delivery already created, or None."""
|
||||
|
||||
Reference in New Issue
Block a user