Files
dtf-system/app/freight.py
Cauê Faleiros 90e3126075
All checks were successful
Build and deploy / Validate source (push) Successful in 1m48s
Build and deploy / Integration suite on a real stack (push) Successful in 4m25s
Build and deploy / Secret scan and release gate (push) Successful in 15s
Build and deploy / Publish images (push) Successful in 1m54s
feat: the cart offers Correios PAC and SEDEX next to Jadlog
Dropstar sent its Correios contract. The cart now asks every carrier at once
for the CEP and lists each option with its delivery time, cheapest first and
chosen by default: Jadlog .PACKAGE, Correios PAC and Correios SEDEX, plus
pickup. A carrier that does not answer is left out instead of blocking the
others, and the server quotes the customer's choice again at approval. The
order summary, the Kanban and Minha conta name the carrier chosen.

app/correios.py follows the CWS manuals: a token from the posting card (kept
until shortly before it expires, renewed once if refused), the contract's own
prices by its DR, and the delivery time. Each box is one object priced by its
sides; Correios work out the cubic weight. All carriers price the same
package, now in app/package.py. Only CORREIOS_CODIGO_ACESSO goes in the
stack; the user, card and contract are its defaults. python -m
app.correios_probe checks the contract and its service codes on the account.

Tested: tests/test_correios (token, contract parameters, prices in Brazilian
format, boxes, a code override, a failing carrier), the full suite in a
CI-style stack, and the cart in Brave on desktop and phone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 13:47:53 -03:00

47 lines
2.1 KiB
Python

"""Every delivery option the Site offers, from every carrier configured.
The cart asks for all options at once and shows them cheapest first; the
customer's choice is quoted again by the server when the cart is approved.
A carrier that does not answer leaves its options out instead of blocking the
others.
"""
from concurrent.futures import ThreadPoolExecutor
class FreightOptions:
name = 'carriers'
def __init__(self, carriers):
self.carriers = carriers
self.by_service = {service: carrier for carrier in carriers for service in carrier.services}
@property
def services(self):
return {service: carrier.services[service] for service, carrier in self.by_service.items()}
def carrier(self, name):
return next((c for c in self.carriers if c.name == name), None)
def quote(self, service, postal_code, metres=None, declared_cents=0):
if service == 'pickup':
return {'provider': self.name, 'service': 'pickup', 'total_cents': 0, 'postal_code': ''}
carrier = self.by_service.get(service)
if not carrier:
raise ValueError('Esta forma de entrega não está disponível. Escolha outra.')
return carrier.quote(service, postal_code, metres, declared_cents)
def options(self, postal_code, metres=None, declared_cents=0):
"""Each service's quote, cheapest first, and why any could not be quoted."""
services = list(self.by_service)
with ThreadPoolExecutor(max_workers=max(1, len(services))) as pool:
futures = {service: pool.submit(self.quote, service, postal_code, metres, declared_cents)
for service in services}
found, missing = [], []
for service, future in futures.items():
try:
found.append(future.result())
except ValueError as error:
missing.append({'service': service, 'description': self.services[service], 'error': str(error)})
found.sort(key=lambda option: (option['total_cents'], option.get('days') or 99))
return {'options': found, 'unavailable': missing}