Files
dtf-system/app/correios_probe.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

71 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Read-only check against Dropstar's real Correios contract, run by hand.
A quote creates no shipment and costs nothing.
python -m app.correios_probe [--cep 01310100 ...] [--peso 0.5 ...]
It answers whether the user, access code and posting card are accepted (and
which contract and DR the token carries), whether the contract prices PAC and
SEDEX with the codes in use, and what they cost and take for one box (the
JADLOG_CAIXA_CM sides) to a few regions. The first failure stops the run: an
account problem would repeat on every line.
In production, from the worker's console (Portainer > Containers > worker >
Console), with CORREIOS_CODIGO_ACESSO in the stack:
python -m app.correios_probe
"""
import argparse
import os
import sys
from decimal import Decimal
from .core.secrets import load as load_secret_files
from .correios import SERVICES, CorreiosError, CorreiosQuotes
from .package import box_cm
DESTINATIONS = {'01310100': 'São Paulo', '20040002': 'Rio de Janeiro', '80010000': 'Curitiba',
'50030230': 'Recife', '69005010': 'Manaus'}
WEIGHTS = ['0.5', '2', '5']
def money(cents):
return f'R$ {cents / 100:.2f}'.replace('.', ',')
def run(quotes, sides, ceps, weights, out):
try:
quotes.bearer()
except CorreiosError as error:
out(f'ERRO no token: {error}')
return 1
out(f'Token ok · cartão {quotes.card} · contrato {quotes.contract or "-"} · DR {quotes.dr or "-"} '
f'· válido até {quotes.expires:%d/%m %H:%M}')
out(f"Origem {quotes.origin} · caixa {' × '.join(str(side) for side in sides)} cm")
for service, (setting, default, label) in SERVICES.items():
code = (os.environ.get(setting) or default).strip()
for cep in ceps:
try:
days = quotes.days(code, cep)
for weight in weights:
cents = quotes.price(code, cep, int(Decimal(weight) * 1000), sides)
out(f"{label} ({code})\t{weight} kg\t{cep}\t{DESTINATIONS.get(cep, '')}\t{money(cents)}\t"
f"{days if days is not None else '?'} dia(s)")
except CorreiosError as error:
out(f'ERRO {label} ({code}) para {cep}: {error}')
return 1
return 0
def main(argv=None):
parser = argparse.ArgumentParser(prog='python -m app.correios_probe')
parser.add_argument('--cep', action='append', help='destination CEP (repeatable)')
parser.add_argument('--peso', action='append', help='weight in kg (repeatable)')
args = parser.parse_args(argv)
load_secret_files()
sides = box_cm(os.environ.get('JADLOG_CAIXA_CM') or '63x15.5x15.5')
return run(CorreiosQuotes(), sides, args.cep or list(DESTINATIONS), args.peso or WEIGHTS, print)
if __name__ == '__main__':
sys.exit(main())