Compare commits
114 Commits
ui-v1
...
959ed37b15
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
959ed37b15 | ||
|
|
172544b173 | ||
|
|
43d9a3c637 | ||
|
|
ac1d8743b1 | ||
|
|
545a8c0f2d | ||
|
|
8997e22f5e | ||
|
|
b3c5f63553 | ||
|
|
581fbe6be4 | ||
|
|
b1fbaad2bd | ||
|
|
1b445f0459 | ||
|
|
f567e6da89 | ||
|
|
c5b1bd37a6 | ||
|
|
e81b2b1681 | ||
|
|
a5c1ca53c0 | ||
|
|
4b804a2c85 | ||
|
|
c3c56a7f21 | ||
|
|
bc1fb60468 | ||
|
|
894e75fd94 | ||
|
|
d7b1eaae6d | ||
|
|
c19941555e | ||
|
|
8236ad8f0a | ||
|
|
dfa297d9b5 | ||
|
|
05da6d78ad | ||
|
|
7877489563 | ||
|
|
0218f119aa | ||
|
|
ca7cf7f701 | ||
|
|
67825a983d | ||
|
|
90c574a673 | ||
|
|
5616db1345 | ||
|
|
1fb2e5d55a | ||
|
|
7a908060fc | ||
|
|
b7abd401ec | ||
|
|
4b8b00443e | ||
|
|
26c46ccc1d | ||
|
|
b9ebdc0fdb | ||
|
|
2c664a1cc8 | ||
|
|
352590e63a | ||
|
|
b54c635cfc | ||
|
|
f6ce624b29 | ||
|
|
ef95dcc327 | ||
|
|
0b86fe79fc | ||
|
|
24ee4e9eab | ||
|
|
223854e392 | ||
|
|
0b55ebce7f | ||
|
|
bee7f8f245 | ||
|
|
c5db1375de | ||
|
|
3d8bca3af7 | ||
|
|
f3fc733352 | ||
|
|
4ce44feb70 | ||
|
|
01b94da36d | ||
|
|
25fa890e92 | ||
|
|
4386fc0737 | ||
|
|
70a76feb23 | ||
|
|
20982a945f | ||
|
|
37715ef223 | ||
|
|
b7e2ea12d0 | ||
|
|
933bd30cbd | ||
|
|
eb254da501 | ||
|
|
8786a33c8d | ||
|
|
20403c5132 | ||
|
|
1b57313496 | ||
|
|
a02711ef2b | ||
|
|
8dddf0fa25 | ||
|
|
f4aacb5776 | ||
|
|
64e47ee481 | ||
|
|
2b313a1cc8 | ||
|
|
7116ebe50a | ||
|
|
9e69c48d2d | ||
|
|
64c1260a9f | ||
|
|
b5bb03cbb4 | ||
|
|
2495edf188 | ||
|
|
f5fed013ab | ||
|
|
593ddf82c8 | ||
|
|
8b5aafa299 | ||
|
|
e47f88a6d6 | ||
|
|
5f2be7ea20 | ||
|
|
4437232d27 | ||
|
|
8bc57195e8 | ||
|
|
0a575a3be7 | ||
|
|
15abd589a8 | ||
|
|
32c060e1b0 | ||
|
|
6f5d183365 | ||
|
|
2215da8d5e | ||
|
|
88e65290e1 | ||
|
|
690b15ece1 | ||
|
|
e20c8673bb | ||
|
|
4de2151e9a | ||
|
|
641aafc87d | ||
|
|
875a7ef9c7 | ||
|
|
c436936fed | ||
|
|
0ed6de4bd5 | ||
|
|
eae3dad306 | ||
|
|
7b6675d4d4 | ||
|
|
4df74221d2 | ||
|
|
43043c361c | ||
|
|
536510b148 | ||
|
|
a1877bdf0a | ||
|
|
275ebf72c4 | ||
|
|
9bea187ea4 | ||
|
|
bd56170248 | ||
|
|
60b7336b37 | ||
|
|
04e4cbc953 | ||
|
|
aec5b1d054 | ||
|
|
8b42e684ca | ||
|
|
e768dcb489 | ||
|
|
122c645f72 | ||
|
|
988b252f9d | ||
|
|
56021d8451 | ||
|
|
b6a90411f1 | ||
|
|
cbbbdb351a | ||
|
|
b81ff8d03d | ||
|
|
177882e77b | ||
|
|
2e03b0362b | ||
|
|
99a558ddd9 |
@@ -37,6 +37,11 @@ TINY_ADAPTER=fake
|
|||||||
# TINY_PRODUCT_UV_FOLHA=...
|
# TINY_PRODUCT_UV_FOLHA=...
|
||||||
# TINY_PRODUCT_UV_AVULSA=...
|
# TINY_PRODUCT_UV_AVULSA=...
|
||||||
# TINY_ADAPTER=tiny # only once connected and tested: creates real orders
|
# TINY_ADAPTER=tiny # only once connected and tested: creates real orders
|
||||||
|
# Jadlog price quotes go in jadlog.env, not here (see app/jadlog_probe.py):
|
||||||
|
# JADLOG_TOKEN=...
|
||||||
|
# JADLOG_CNPJ=... # the "Usuário" Jadlog issued, the CNPJ that contracts freight
|
||||||
|
# JADLOG_CONTA=... # conta corrente
|
||||||
|
# JADLOG_CONTRATO= # only if Jadlog issued a contract number
|
||||||
WHATSAPP_ADAPTER=fake
|
WHATSAPP_ADAPTER=fake
|
||||||
STORAGE_ADAPTER=s3-local
|
STORAGE_ADAPTER=s3-local
|
||||||
MOCK_FREIGHT_CENTS=1500
|
MOCK_FREIGHT_CENTS=1500
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ jobs:
|
|||||||
# one a developer exercises on localhost.
|
# one a developer exercises on localhost.
|
||||||
- name: API and workflow regressions
|
- name: API and workflow regressions
|
||||||
run: |
|
run: |
|
||||||
for suite in smoke_test workflow_test security_test scanning_test payment_test quote_pagination_test print_file_test; do
|
for suite in smoke_test workflow_test security_test scanning_test payment_test quote_pagination_test kanban_test reconcile_test order_actions_test print_file_test thumbnail_backfill_test launch_reset_test; do
|
||||||
echo "--- $suite"
|
echo "--- $suite"
|
||||||
$COMPOSE exec -T \
|
$COMPOSE exec -T \
|
||||||
-e SITE_BASE_URL=http://site \
|
-e SITE_BASE_URL=http://site \
|
||||||
@@ -86,13 +86,14 @@ jobs:
|
|||||||
# the generator's geometry. The provider suites use a fake transport: they
|
# the generator's geometry. The provider suites use a fake transport: they
|
||||||
# prove the documented contract, not the integration.
|
# prove the documented contract, not the integration.
|
||||||
- name: Print-file geometry and provider adapters
|
- name: Print-file geometry and provider adapters
|
||||||
run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_mercadopago tests.test_tiny -v
|
run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_mercadopago tests.test_tiny tests.test_cnpj tests.test_payment_messages tests.test_jadlog tests.test_quote_review tests.test_large_files tests.test_grade_check -v
|
||||||
|
|
||||||
- name: Runtime and retention regressions
|
- name: Runtime and retention regressions
|
||||||
run: |
|
run: |
|
||||||
$COMPOSE exec -T api python -m tests.retention_test
|
$COMPOSE exec -T api python -m tests.retention_test
|
||||||
$COMPOSE exec -T api python -m tests.runtime_security_test
|
$COMPOSE exec -T api python -m tests.runtime_security_test
|
||||||
$COMPOSE exec -T api python -m tests.tiny_oauth_test
|
$COMPOSE exec -T api python -m tests.tiny_oauth_test
|
||||||
|
$COMPOSE exec -T backup python -m tests.backup_test
|
||||||
|
|
||||||
# Run Chrome on the Compose network. It must resolve the same storage:9000
|
# Run Chrome on the Compose network. It must resolve the same storage:9000
|
||||||
# hostname used in presigned URLs, and absence of Chrome must fail CI.
|
# hostname used in presigned URLs, and absence of Chrome must fail CI.
|
||||||
@@ -106,7 +107,7 @@ jobs:
|
|||||||
if: failure()
|
if: failure()
|
||||||
run: |
|
run: |
|
||||||
$COMPOSE ps || true
|
$COMPOSE ps || true
|
||||||
$COMPOSE logs --tail 200 api worker site kanban || true
|
$COMPOSE logs --tail 200 api worker backup site kanban || true
|
||||||
|
|
||||||
- name: Tear down
|
- name: Tear down
|
||||||
if: always()
|
if: always()
|
||||||
@@ -206,24 +207,47 @@ jobs:
|
|||||||
- name: Image vulnerabilities
|
- name: Image vulnerabilities
|
||||||
run: |
|
run: |
|
||||||
image="${TRIVY_IMAGE:-aquasec/trivy:0.58.1}"
|
image="${TRIVY_IMAGE:-aquasec/trivy:0.58.1}"
|
||||||
failed=0
|
# One database download for the four scans, kept in a volume between
|
||||||
|
# runs and retried: a failed download from the mirror used to fail
|
||||||
|
# the gate as if a CRITICAL vulnerability had been found.
|
||||||
|
trivy() { docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
|
||||||
|
-v dtf-trivy-cache:/root/.cache/trivy "$image" "$@"; }
|
||||||
|
for attempt in 1 2 3; do
|
||||||
|
trivy image --download-db-only --no-progress && break
|
||||||
|
if [ "$attempt" -eq 3 ]; then
|
||||||
|
echo "::error::The vulnerability database could not be downloaded; the release images were not scanned."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Database download failed (attempt $attempt); retrying in 30 s."
|
||||||
|
sleep 30
|
||||||
|
done
|
||||||
|
# Findings exit 5; any other failure means the scan did not run.
|
||||||
|
found=0; broken=0
|
||||||
for target in \
|
for target in \
|
||||||
"gitea.blyzer.com.br/blyzer/dtf-api:${{ gitea.sha }}" \
|
"gitea.blyzer.com.br/blyzer/dtf-api:${{ gitea.sha }}" \
|
||||||
"gitea.blyzer.com.br/blyzer/dtf-web:${{ gitea.sha }}"; do
|
"gitea.blyzer.com.br/blyzer/dtf-web:${{ gitea.sha }}"; do
|
||||||
echo "--- $target (HIGH, reported)"
|
echo "--- $target (HIGH, reported)"
|
||||||
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock "$image" \
|
trivy image --skip-db-update --image-src docker --scanners vuln --severity HIGH --no-progress \
|
||||||
image --image-src docker --scanners vuln --severity HIGH --no-progress \
|
|
||||||
--format table --exit-code 0 "$target" ||
|
--format table --exit-code 0 "$target" ||
|
||||||
echo "::warning::Could not scan $target for HIGH findings"
|
echo "::warning::Could not scan $target for HIGH findings"
|
||||||
echo "--- $target (CRITICAL, blocking)"
|
echo "--- $target (CRITICAL, blocking)"
|
||||||
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock "$image" \
|
set +e
|
||||||
image --image-src docker --scanners vuln --severity CRITICAL --no-progress \
|
trivy image --skip-db-update --image-src docker --scanners vuln --severity CRITICAL --no-progress \
|
||||||
--format table --exit-code 1 "$target" || failed=1
|
--format table --exit-code 5 "$target"
|
||||||
|
verdict=$?
|
||||||
|
set -e
|
||||||
|
if [ "$verdict" -eq 5 ]; then found=1
|
||||||
|
elif [ "$verdict" -ne 0 ]; then broken=1; echo "::error::The CRITICAL scan of $target did not run (exit $verdict)."
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
if [ "$failed" -ne 0 ]; then
|
if [ "$found" -ne 0 ]; then
|
||||||
echo "::error::A CRITICAL vulnerability was found in a release image."
|
echo "::error::A CRITICAL vulnerability was found in a release image."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
if [ "$broken" -ne 0 ]; then
|
||||||
|
echo "::error::A release image could not be scanned; nothing is published unscanned."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Publish validated images
|
- name: Publish validated images
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -17,9 +17,16 @@ def require_runtime():
|
|||||||
checkout until their audited implementations are added.
|
checkout until their audited implementations are added.
|
||||||
"""
|
"""
|
||||||
environment = os.environ.get('APP_ENV', 'local')
|
environment = os.environ.get('APP_ENV', 'local')
|
||||||
for name in ('FREIGHT', 'WHATSAPP'):
|
if os.environ.get('WHATSAPP_ADAPTER') != 'fake':
|
||||||
if os.environ.get(f'{name}_ADAPTER') != 'fake':
|
raise RuntimeError('WHATSAPP must use the currently supported fake adapter')
|
||||||
raise RuntimeError(f'{name} must use the currently supported fake adapter')
|
freight = os.environ.get('FREIGHT_ADAPTER')
|
||||||
|
if freight == 'jadlog':
|
||||||
|
from .jadlog import required_settings as jadlog_settings
|
||||||
|
for name in jadlog_settings():
|
||||||
|
if not os.environ.get(name):
|
||||||
|
raise RuntimeError(f'{name} is required for the Jadlog adapter')
|
||||||
|
elif freight != 'fake':
|
||||||
|
raise RuntimeError('FREIGHT must use the fake or jadlog adapter')
|
||||||
tiny = os.environ.get('TINY_ADAPTER')
|
tiny = os.environ.get('TINY_ADAPTER')
|
||||||
if tiny == 'tiny':
|
if tiny == 'tiny':
|
||||||
from .tiny import required_settings
|
from .tiny import required_settings
|
||||||
@@ -106,6 +113,11 @@ class FakePayment:
|
|||||||
|
|
||||||
def create(self, quote_id: str, total_cents: int, customer: dict, method: dict | None = None) -> dict:
|
def create(self, quote_id: str, total_cents: int, customer: dict, method: dict | None = None) -> dict:
|
||||||
kind = (method or {}).get('type', 'pix')
|
kind = (method or {}).get('type', 'pix')
|
||||||
|
if kind == 'pix':
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
expires = (datetime.now(timezone.utc) + timedelta(minutes=30)).isoformat(timespec='milliseconds')
|
||||||
|
return {'provider': 'fake', 'id': f"local-{quote_id}-pix-{(method or {}).get('attempt', 1)}",
|
||||||
|
'status': 'pending', 'total_cents': total_cents, 'expires_at': expires}
|
||||||
return {'provider': 'fake', 'id': f'local-{quote_id}-{kind}',
|
return {'provider': 'fake', 'id': f'local-{quote_id}-{kind}',
|
||||||
'status': 'pending', 'total_cents': total_cents}
|
'status': 'pending', 'total_cents': total_cents}
|
||||||
|
|
||||||
@@ -143,10 +155,12 @@ class FakePayment:
|
|||||||
'status': 'paid', 'total_cents': total_cents}
|
'status': 'paid', 'total_cents': total_cents}
|
||||||
|
|
||||||
class FreightAdapter(Protocol):
|
class FreightAdapter(Protocol):
|
||||||
def quote(self, service: str, postal_code: str) -> dict: ...
|
def quote(self, service: str, postal_code: str, metres=None, declared_cents: int = 0) -> dict: ...
|
||||||
|
|
||||||
class FakeFreight:
|
class FakeFreight:
|
||||||
def quote(self, service: str, postal_code: str) -> dict:
|
name = 'fake'
|
||||||
|
|
||||||
|
def quote(self, service: str, postal_code: str, metres=None, declared_cents: int = 0) -> dict:
|
||||||
if service == 'pickup':
|
if service == 'pickup':
|
||||||
return {'provider': 'fake', 'service': 'pickup', 'total_cents': 0, 'postal_code': ''}
|
return {'provider': 'fake', 'service': 'pickup', 'total_cents': 0, 'postal_code': ''}
|
||||||
if service != 'mock-standard' or len(postal_code) != 8 or not postal_code.isascii() or not postal_code.isdigit():
|
if service != 'mock-standard' or len(postal_code) != 8 or not postal_code.isascii() or not postal_code.isdigit():
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
from typing import Literal
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
||||||
from psycopg.errors import UniqueViolation
|
from psycopg.errors import UniqueViolation
|
||||||
from psycopg.types.json import Jsonb
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
@@ -10,8 +12,8 @@ from ..core import db
|
|||||||
from ..artwork import submit_files
|
from ..artwork import submit_files
|
||||||
from ..core.auth import (DUMMY_PASSWORD_HASH, audit, client_ip, new_session, owner,
|
from ..core.auth import (DUMMY_PASSWORD_HASH, audit, client_ip, new_session, owner,
|
||||||
login_failed, password_hash, password_matches, session_row, throttle, transfer_guest)
|
login_failed, password_hash, password_matches, session_row, throttle, transfer_guest)
|
||||||
from ..core.models import ArtworkSubmission, Login, Register
|
from ..core.models import ArtworkSubmission, EmailChange, Login, PasswordChange, ProfileUpdate, Register
|
||||||
from ..runtime import STATES, file_rows, owned_order, storage
|
from ..runtime import STATES, file_rows, order_code, owned_order, storage
|
||||||
from ..scanning import require_clean
|
from ..scanning import require_clean
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -36,12 +38,12 @@ def register(body: Register, request: Request, response: Response):
|
|||||||
try:
|
try:
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
if previous and c.execute('SELECT id FROM dtf_local.accounts WHERE id=%s', (previous['owner'],)).fetchone():
|
if previous and c.execute('SELECT id FROM dtf_local.accounts WHERE id=%s', (previous['owner'],)).fetchone():
|
||||||
raise HTTPException(409, 'Sign out before registering another account')
|
raise HTTPException(409, 'Saia da conta atual antes de criar outra.')
|
||||||
c.execute('INSERT INTO dtf_local.accounts(id,email,password_hash,profile) VALUES(%s,%s,%s,%s)', (identity,email,encoded,Jsonb(profile)))
|
c.execute('INSERT INTO dtf_local.accounts(id,email,password_hash,profile) VALUES(%s,%s,%s,%s)', (identity,email,encoded,Jsonb(profile)))
|
||||||
if previous: transfer_guest(c, previous, identity)
|
if previous: transfer_guest(c, previous, identity)
|
||||||
new_session(c, response, identity)
|
new_session(c, response, identity)
|
||||||
except UniqueViolation:
|
except UniqueViolation:
|
||||||
raise HTTPException(409, 'An account already exists; sign in')
|
raise HTTPException(409, 'Já existe uma conta com este e-mail. Entre com a sua senha.')
|
||||||
audit('account_registered', account=str(identity))
|
audit('account_registered', account=str(identity))
|
||||||
return {'customer': profile}
|
return {'customer': profile}
|
||||||
|
|
||||||
@@ -57,7 +59,7 @@ def login(body: Login, request: Request, response: Response):
|
|||||||
if not account or not matches:
|
if not account or not matches:
|
||||||
login_failed(email)
|
login_failed(email)
|
||||||
audit('customer_login_failed', ip=client_ip(request))
|
audit('customer_login_failed', ip=client_ip(request))
|
||||||
raise HTTPException(401, 'Invalid email or password')
|
raise HTTPException(401, 'E-mail ou senha inválidos.')
|
||||||
previous = current(request)
|
previous = current(request)
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
if not stored.startswith('scrypt-v2$'):
|
if not stored.startswith('scrypt-v2$'):
|
||||||
@@ -86,21 +88,129 @@ def me(identity=Depends(owner)):
|
|||||||
row = c.execute('SELECT profile FROM dtf_local.accounts WHERE id=%s', (identity,)).fetchone()
|
row = c.execute('SELECT profile FROM dtf_local.accounts WHERE id=%s', (identity,)).fetchone()
|
||||||
return {'customer': row['profile'] if row else None}
|
return {'customer': row['profile'] if row else None}
|
||||||
|
|
||||||
@router.get('/api/customer/orders')
|
def account_row(c, identity, lock=False):
|
||||||
def orders(identity=Depends(owner)):
|
row = c.execute('SELECT * FROM dtf_local.accounts WHERE id=%s' + (' FOR UPDATE' if lock else ''), (identity,)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(401, 'Entre na sua conta.')
|
||||||
|
return row
|
||||||
|
|
||||||
|
@router.post('/api/account/profile')
|
||||||
|
def update_profile(body: ProfileUpdate, identity=Depends(owner)):
|
||||||
|
"""WhatsApp and the saved delivery address. The CNPJ is the account's and
|
||||||
|
does not change; the e-mail is the login and changes on its own route."""
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
rows = c.execute('SELECT id,number,state,version,snapshot,created_at,updated_at FROM dtf_local.orders WHERE owner=%s ORDER BY created_at DESC', (identity,)).fetchall()
|
row = account_row(c, identity, lock=True)
|
||||||
quotes = c.execute('''SELECT q.id,q.approved,q.approved_at,q.created_at FROM dtf_local.quotes q
|
profile = {**row['profile'], 'zap': body.zap,
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id WHERE q.owner=%s AND o.id IS NULL ORDER BY q.created_at DESC''', (identity,)).fetchall()
|
'address': body.address.model_dump() if body.address else None}
|
||||||
return {'orders': rows, 'quotes': quotes, 'states': STATES}
|
c.execute('UPDATE dtf_local.accounts SET profile=%s WHERE id=%s', (Jsonb(profile), identity))
|
||||||
|
audit('account_profile_updated', account=str(identity))
|
||||||
|
return {'customer': profile}
|
||||||
|
|
||||||
|
@router.post('/api/account/email')
|
||||||
|
def change_email(body: EmailChange, request: Request, identity=Depends(owner)):
|
||||||
|
with db.connect() as c:
|
||||||
|
row = account_row(c, identity)
|
||||||
|
throttle(row['email'], request)
|
||||||
|
if not password_matches(body.password, row['password_hash']):
|
||||||
|
login_failed(row['email'])
|
||||||
|
raise HTTPException(401, 'Senha incorreta.')
|
||||||
|
profile = {**row['profile'], 'mail': body.email}
|
||||||
|
try:
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('UPDATE dtf_local.accounts SET email=%s, profile=%s WHERE id=%s', (body.email, Jsonb(profile), identity))
|
||||||
|
except UniqueViolation:
|
||||||
|
raise HTTPException(409, 'Este e-mail já é de outra conta.')
|
||||||
|
audit('account_email_changed', account=str(identity))
|
||||||
|
return {'customer': profile}
|
||||||
|
|
||||||
|
@router.post('/api/account/password')
|
||||||
|
def change_password(body: PasswordChange, request: Request, identity=Depends(owner)):
|
||||||
|
with db.connect() as c:
|
||||||
|
row = account_row(c, identity)
|
||||||
|
throttle(row['email'], request)
|
||||||
|
if not password_matches(body.current, row['password_hash']):
|
||||||
|
login_failed(row['email'])
|
||||||
|
raise HTTPException(401, 'Senha atual incorreta.')
|
||||||
|
session = current(request)
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('UPDATE dtf_local.accounts SET password_hash=%s WHERE id=%s', (password_hash(body.new), identity))
|
||||||
|
# Other devices signed in with the old password are signed out.
|
||||||
|
c.execute('DELETE FROM dtf_local.sessions WHERE owner=%s AND id<>%s', (identity, session['id']))
|
||||||
|
audit('account_password_changed', account=str(identity))
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
# The order list's groups, as the customer filters them.
|
||||||
|
GROUPS = {'pay': 'Aguardando pagamento', 'prod': 'Em produção', 'cor': 'Correção', 'fin': 'Finalizados'}
|
||||||
|
ENTRIES = '''WITH live_quote AS (
|
||||||
|
-- The cart waiting for payment: the newest unpaid quote whose files still
|
||||||
|
-- exist. An older one was replaced when the cart changed, and one whose
|
||||||
|
-- files are gone can no longer be paid.
|
||||||
|
SELECT q.id, q.created_at, q.approved FROM dtf_local.quotes q
|
||||||
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
|
WHERE q.owner=%(me)s AND o.id IS NULL AND q.discarded_at IS NULL
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM jsonb_array_elements(q.draft->'items') i
|
||||||
|
CROSS JOIN LATERAL jsonb_array_elements_text(i->'uploads') u(uid)
|
||||||
|
JOIN dtf_local.uploads up ON up.id=u.uid::uuid
|
||||||
|
WHERE up.purged_at IS NOT NULL OR up.expires_at<=now())
|
||||||
|
ORDER BY q.created_at DESC LIMIT 1
|
||||||
|
), entries AS (
|
||||||
|
SELECT 'order' AS kind, o.id, o.number, CASE WHEN o.cancelled_at IS NOT NULL THEN 'cancelled' ELSE o.state END,
|
||||||
|
CASE WHEN o.cancelled_at IS NOT NULL OR o.state='fin' THEN 'fin' WHEN o.state='cor' THEN 'cor' ELSE 'prod' END AS grp,
|
||||||
|
(o.snapshot->>'total_cents')::bigint AS total_cents, o.created_at, o.snapshot->'items' AS items
|
||||||
|
FROM dtf_local.orders o WHERE o.owner=%(me)s
|
||||||
|
UNION ALL
|
||||||
|
SELECT 'quote', q.id, NULL, CASE WHEN q.approved IS NULL THEN 'review' ELSE 'pay' END, 'pay',
|
||||||
|
(q.approved->>'total_cents')::bigint, q.created_at, COALESCE(q.approved->'items','[]'::jsonb)
|
||||||
|
FROM live_quote q
|
||||||
|
)'''
|
||||||
|
|
||||||
|
@router.get('/api/customer/orders')
|
||||||
|
def orders(identity=Depends(owner), status: Literal['all','pay','prod','cor','fin'] = 'all',
|
||||||
|
number: int | None = Query(None, ge=1), code: str | None = Query(None, max_length=12, pattern=r'^[0-9A-Fa-f]+$'),
|
||||||
|
days: int = Query(0, ge=0, le=3650),
|
||||||
|
page: int = Query(1, ge=1, le=10000), size: int = Query(10, ge=1, le=50)):
|
||||||
|
"""The customer's orders and the cart waiting for payment, newest first,
|
||||||
|
filtered and a page at a time, with how many there are in each group."""
|
||||||
|
params = {'me': identity, 'grp': None if status == 'all' else status, 'number': number,
|
||||||
|
'code': code.lower() + '%' if code else None,
|
||||||
|
'since': datetime.now(timezone.utc) - timedelta(days=days) if days else None,
|
||||||
|
'size': size, 'offset': (page - 1) * size}
|
||||||
|
where = '''WHERE (%(grp)s::text IS NULL OR grp=%(grp)s) AND (%(number)s::int IS NULL OR number=%(number)s)
|
||||||
|
AND (%(code)s::text IS NULL OR (kind='order' AND replace(id::text,'-','') LIKE %(code)s))
|
||||||
|
AND (%(since)s::timestamptz IS NULL OR created_at>=%(since)s)'''
|
||||||
|
with db.connect() as c:
|
||||||
|
counts = {r['grp']: r['n'] for r in c.execute(ENTRIES + ' SELECT grp, count(*) AS n FROM entries GROUP BY grp', params).fetchall()}
|
||||||
|
total = c.execute(ENTRIES + ' SELECT count(*) AS n FROM entries ' + where, params).fetchone()['n']
|
||||||
|
rows = c.execute(ENTRIES + ' SELECT * FROM entries ' + where +
|
||||||
|
' ORDER BY created_at DESC LIMIT %(size)s OFFSET %(offset)s', params).fetchall()
|
||||||
|
items = []
|
||||||
|
for r in rows:
|
||||||
|
entry = {'kind': r['kind'], 'id': r['id'], 'number': r['number'], 'state': r['state'],
|
||||||
|
'code': order_code(r['id']) if r['kind'] == 'order' else None,
|
||||||
|
'group': r['grp'], 'total_cents': r['total_cents'], 'created_at': r['created_at'],
|
||||||
|
'items': [{'mode': i['mode'], 'billed_metres': i.get('billed_metres')} for i in r['items']]}
|
||||||
|
if r['kind'] == 'quote':
|
||||||
|
# A PIX code still open is paid on its own page.
|
||||||
|
pix = c.execute('''SELECT 1 FROM dtf_local.payment_intents WHERE quote_id=%s AND method='pix'
|
||||||
|
AND status='pending' AND COALESCE((response->>'expires_at')::timestamptz > now(), false)''',
|
||||||
|
(r['id'],)).fetchone()
|
||||||
|
entry['pay_with'] = 'pix' if pix else 'page'
|
||||||
|
items.append(entry)
|
||||||
|
return {'items': items, 'total': total, 'page': page, 'size': size,
|
||||||
|
'counts': {g: counts.get(g, 0) for g in GROUPS}, 'groups': GROUPS, 'states': STATES}
|
||||||
|
|
||||||
@router.get('/api/customer/orders/{oid}')
|
@router.get('/api/customer/orders/{oid}')
|
||||||
def detail(oid: UUID, identity=Depends(owner)):
|
def detail(oid: UUID, identity=Depends(owner)):
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
row = owned_order(c, oid, identity)
|
row = owned_order(c, oid, identity)
|
||||||
history = c.execute('SELECT from_state,to_state,reason,created_at FROM dtf_local.movements WHERE order_id=%s ORDER BY id', (oid,)).fetchall()
|
# A move back undoes an operator's mistake and its reason is internal;
|
||||||
return {'id': row['id'], 'number': row['number'], 'state': row['state'], 'version': row['version'],
|
# only a correction's reason is written for the customer.
|
||||||
'snapshot': row['snapshot'], 'history': history, 'files': file_rows(c,oid)}
|
history = c.execute('''SELECT from_state,to_state,CASE WHEN to_state='cor' THEN reason ELSE '' END AS reason,
|
||||||
|
created_at FROM dtf_local.movements WHERE order_id=%s AND NOT back ORDER BY id''', (oid,)).fetchall()
|
||||||
|
return {'id': row['id'], 'number': row['number'], 'code': order_code(row['id']),
|
||||||
|
'state': row['state'], 'version': row['version'],
|
||||||
|
'created_at': row['created_at'], 'snapshot': row['snapshot'], 'history': history,
|
||||||
|
'files': file_rows(c,oid)}
|
||||||
|
|
||||||
@router.post('/api/customer/orders/{oid}/corrections')
|
@router.post('/api/customer/orders/{oid}/corrections')
|
||||||
def correction(oid: UUID, body: ArtworkSubmission, identity=Depends(owner)):
|
def correction(oid: UUID, body: ArtworkSubmission, identity=Depends(owner)):
|
||||||
@@ -113,7 +223,7 @@ def customer_download(oid: UUID, fid: UUID, identity=Depends(owner)):
|
|||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
owned_order(c,oid,identity)
|
owned_order(c,oid,identity)
|
||||||
row = c.execute('SELECT u.* FROM dtf_local.order_files f JOIN dtf_local.uploads u ON u.id=f.upload_id WHERE f.id=%s AND f.order_id=%s AND f.active', (fid,oid)).fetchone()
|
row = c.execute('SELECT u.* FROM dtf_local.order_files f JOIN dtf_local.uploads u ON u.id=f.upload_id WHERE f.id=%s AND f.order_id=%s AND f.active', (fid,oid)).fetchone()
|
||||||
if not row: raise HTTPException(404, 'Active file not found')
|
if not row: raise HTTPException(404, 'Arquivo não encontrado.')
|
||||||
if row['expires_at'] <= datetime.now(timezone.utc): raise HTTPException(410, 'File expired')
|
if row['expires_at'] <= datetime.now(timezone.utc): raise HTTPException(410, 'Este arquivo não está mais disponível: os arquivos ficam guardados por 30 dias.')
|
||||||
require_clean(row)
|
require_clean(row)
|
||||||
return {'url':storage.download(row['object_key'],row['name']), 'name':row['name']}
|
return {'url':storage.download(row['object_key'],row['name']), 'name':row['name']}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
"""Health, session bootstrap and freight quoting."""
|
"""Health, session bootstrap, freight quoting and the address of a CEP."""
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
import httpx
|
||||||
from fastapi import APIRouter, HTTPException, Request, Response
|
from fastapi import APIRouter, HTTPException, Request, Response
|
||||||
|
|
||||||
from ..core import db
|
from ..core import db
|
||||||
from ..core.auth import client_ip, owner, new_session, rate_limit
|
from ..core.auth import client_ip, owner, new_session, rate_limit
|
||||||
from ..core.limits import upload_limit_bytes
|
from ..core.limits import upload_limit_bytes
|
||||||
from ..core.models import Freight
|
from ..core.models import FreightEstimate
|
||||||
from ..runtime import ENVIRONMENT, GUEST_SESSION_LIMIT, PART_BYTES, freight, payment, storage
|
from ..runtime import (ENVIRONMENT, GUEST_SESSION_LIMIT, PART_BYTES, freight, payment,
|
||||||
|
require_delivery_available, storage)
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@@ -36,11 +39,34 @@ def session(request: Request, response: Response):
|
|||||||
return {'environment': ENVIRONMENT, 'cart_scope': str(session_id), 'part_bytes': PART_BYTES,
|
return {'environment': ENVIRONMENT, 'cart_scope': str(session_id), 'part_bytes': PART_BYTES,
|
||||||
'max_upload_bytes': upload_limit_bytes(), 'payment_provider': payment.name,
|
'max_upload_bytes': upload_limit_bytes(), 'payment_provider': payment.name,
|
||||||
# Public by design: Mercado Pago's card form needs it in the browser.
|
# Public by design: Mercado Pago's card form needs it in the browser.
|
||||||
'payment_public_key': os.environ.get('MP_PUBLIC_KEY', '') if payment.name == 'mercadopago' else ''}
|
'payment_public_key': os.environ.get('MP_PUBLIC_KEY', '') if payment.name == 'mercadopago' else '',
|
||||||
|
# The delivery service the cart quotes, or none: pickup only.
|
||||||
|
'freight_service': 'jadlog' if freight.name == 'jadlog' else 'mock-standard' if ENVIRONMENT == 'local' else None}
|
||||||
|
|
||||||
@router.post('/api/freight')
|
@router.post('/api/freight')
|
||||||
def quote_freight(body: Freight):
|
def quote_freight(body: FreightEstimate):
|
||||||
|
require_delivery_available(body.service)
|
||||||
try:
|
try:
|
||||||
return freight.quote(body.service, body.postal_code)
|
return freight.quote(body.service, body.postal_code, body.metres, body.declared_cents)
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise HTTPException(422, str(exc))
|
raise HTTPException(422, str(exc))
|
||||||
|
|
||||||
|
# The address of a CEP, so the cart fills it in. Looked up here rather than in
|
||||||
|
# the browser, which keeps the Site's CSP to its own origin.
|
||||||
|
VIACEP = 'https://viacep.com.br/ws/{}/json/'
|
||||||
|
CEP_LIMIT = 120
|
||||||
|
|
||||||
|
@router.get('/api/cep/{cep}')
|
||||||
|
def cep_address(cep: str, request: Request):
|
||||||
|
if not re.fullmatch(r'[0-9]{8}', cep):
|
||||||
|
raise HTTPException(422, 'CEP must have eight digits')
|
||||||
|
rate_limit('cep-lookup', client_ip(request), CEP_LIMIT, 900)
|
||||||
|
try:
|
||||||
|
response = httpx.get(VIACEP.format(cep), timeout=5)
|
||||||
|
data = response.json() if response.status_code == 200 else {}
|
||||||
|
except (httpx.HTTPError, ValueError):
|
||||||
|
raise HTTPException(503, 'Consulta de CEP indisponível')
|
||||||
|
if not data or data.get('erro'):
|
||||||
|
raise HTTPException(404, 'CEP não encontrado')
|
||||||
|
return {'street': data.get('logradouro') or '', 'district': data.get('bairro') or '',
|
||||||
|
'city': data.get('localidade') or '', 'state': data.get('uf') or ''}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Kanban: sign-in, the board, commercial review and card movement."""
|
"""Kanban: sign-in, the board, commercial review and card movement."""
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
@@ -8,21 +9,25 @@ from uuid import UUID
|
|||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
from psycopg.types.json import Jsonb
|
|
||||||
|
|
||||||
from ..core import db
|
from ..core import db
|
||||||
from ..core.auth import (COOKIE_SECURE, DUMMY_PASSWORD_HASH, audit, client_ip, operator,
|
from ..core.auth import (COOKIE_SECURE, DUMMY_PASSWORD_HASH, audit, client_ip, operator,
|
||||||
login_failed, password_matches, throttle)
|
login_failed, password_matches, throttle)
|
||||||
from ..core.models import Move, OperatorLogin, Resolution, Review
|
from ..core.models import Move, OperatorLogin, Resolution, Review, StrictModel
|
||||||
from ..core.pricing import price
|
|
||||||
from ..printjobs import queue as queue_print_files
|
from ..printjobs import queue as queue_print_files
|
||||||
|
from .. import cnpj, payments, quote_review
|
||||||
from .. import tiny
|
from .. import tiny
|
||||||
from ..runtime import (BOARD_FINISHED_LIMIT, BOARD_QUOTE_LIMIT, STATES, TRANSITIONS,
|
from ..runtime import (BACK, BOARD_FINISHED_LIMIT, BOARD_QUOTE_LIMIT, ENVIRONMENT, PUBLIC_ORIGIN, STATES,
|
||||||
enqueue, freight, quote_view, storage, upload_row)
|
TRANSITIONS, payment, enqueue, freight, quote_view, storage)
|
||||||
from ..scanning import require_clean
|
from ..scanning import require_clean
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
# A quote that can still become an order: waiting for review, or approved within
|
||||||
|
# the payment window. Expired ones leave the Kanban; they can never be paid.
|
||||||
|
LIVE_QUOTE = (f"(q.discarded_at IS NULL AND (q.approved_at IS NULL OR "
|
||||||
|
f"q.approved_at > now() - interval '{int(payments.QUOTE_VALID_HOURS)} hours'))")
|
||||||
|
|
||||||
@router.post('/api/operator/login')
|
@router.post('/api/operator/login')
|
||||||
def operator_login(body: OperatorLogin, request: Request, response: Response):
|
def operator_login(body: OperatorLogin, request: Request, response: Response):
|
||||||
email = body.email
|
email = body.email
|
||||||
@@ -30,14 +35,14 @@ def operator_login(body: OperatorLogin, request: Request, response: Response):
|
|||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
account = c.execute('SELECT * FROM dtf_local.operators WHERE email=%s', (email,)).fetchone()
|
account = c.execute('SELECT * FROM dtf_local.operators WHERE email=%s', (email,)).fetchone()
|
||||||
if not c.execute('SELECT 1 FROM dtf_local.operators WHERE active LIMIT 1').fetchone():
|
if not c.execute('SELECT 1 FROM dtf_local.operators WHERE active LIMIT 1').fetchone():
|
||||||
raise HTTPException(503, 'No Kanban operator account is configured')
|
raise HTTPException(503, 'Nenhuma conta de operador configurada.')
|
||||||
# Comparable password work whether or not the account exists or is active.
|
# Comparable password work whether or not the account exists or is active.
|
||||||
stored = account['password_hash'] if account else DUMMY_PASSWORD_HASH
|
stored = account['password_hash'] if account else DUMMY_PASSWORD_HASH
|
||||||
matches = password_matches(body.password, stored)
|
matches = password_matches(body.password, stored)
|
||||||
if not account or not account['active'] or not matches:
|
if not account or not account['active'] or not matches:
|
||||||
login_failed('operator:'+email)
|
login_failed('operator:'+email)
|
||||||
audit('operator_login_failed', ip=client_ip(request), operator=email)
|
audit('operator_login_failed', ip=client_ip(request), operator=email)
|
||||||
raise HTTPException(401, 'Invalid operator login')
|
raise HTTPException(401, 'E-mail ou senha inválidos.')
|
||||||
token = secrets.token_urlsafe(32)
|
token = secrets.token_urlsafe(32)
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
previous = hashlib.sha256(request.cookies.get('dtf_operator','').encode()).hexdigest()
|
previous = hashlib.sha256(request.cookies.get('dtf_operator','').encode()).hexdigest()
|
||||||
@@ -60,6 +65,29 @@ def operator_logout(request: Request, response: Response):
|
|||||||
audit('operator_logout')
|
audit('operator_logout')
|
||||||
return {'ok': True}
|
return {'ok': True}
|
||||||
|
|
||||||
|
def freight_status():
|
||||||
|
"""What delivery the Site offers, and the package rule it prices with."""
|
||||||
|
if freight.name != 'jadlog':
|
||||||
|
return {'provider': freight.name}
|
||||||
|
cubed = freight.cubed_kg()
|
||||||
|
return {'provider': 'jadlog', 'base_kg': str(freight.base_kg), 'per_metre_kg': str(freight.per_metre_kg),
|
||||||
|
'production_days': freight.production_days,
|
||||||
|
'box_cm': [str(side) for side in freight.box_cm] if freight.box_cm else None,
|
||||||
|
'cubage_kg_m3': str(freight.cubage_kg_m3) if freight.cubage_kg_m3 else None,
|
||||||
|
'cubed_kg': str(cubed) if cubed is not None else None,
|
||||||
|
'metres_per_box': str(freight.metres_per_box) if freight.metres_per_box else None}
|
||||||
|
|
||||||
|
|
||||||
|
def backup_status(c):
|
||||||
|
"""The latest database backup and the latest run, which may have failed."""
|
||||||
|
ok = c.execute('''SELECT finished_at,bytes FROM dtf_local.backups WHERE status='ok'
|
||||||
|
ORDER BY finished_at DESC LIMIT 1''').fetchone()
|
||||||
|
last = c.execute('SELECT finished_at,status,detail FROM dtf_local.backups ORDER BY finished_at DESC LIMIT 1').fetchone()
|
||||||
|
return {'last_ok': ok['finished_at'].isoformat() if ok else None, 'bytes': ok['bytes'] if ok else None,
|
||||||
|
'failed': last['detail'] if last and last['status'] == 'failed' else None,
|
||||||
|
'failed_at': last['finished_at'].isoformat() if last and last['status'] == 'failed' else None}
|
||||||
|
|
||||||
|
|
||||||
@router.get('/api/operator/board')
|
@router.get('/api/operator/board')
|
||||||
def board(user=Depends(operator)):
|
def board(user=Depends(operator)):
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
@@ -70,55 +98,259 @@ def board(user=Depends(operator)):
|
|||||||
finished = c.execute("SELECT * FROM dtf_local.orders WHERE state='fin' ORDER BY created_at DESC LIMIT %s",
|
finished = c.execute("SELECT * FROM dtf_local.orders WHERE state='fin' ORDER BY created_at DESC LIMIT %s",
|
||||||
(BOARD_FINISHED_LIMIT,)).fetchall()
|
(BOARD_FINISHED_LIMIT,)).fetchall()
|
||||||
finished_total = c.execute("SELECT count(*) AS n FROM dtf_local.orders WHERE state='fin'").fetchone()['n']
|
finished_total = c.execute("SELECT count(*) AS n FROM dtf_local.orders WHERE state='fin'").fetchone()['n']
|
||||||
pending = c.execute('''SELECT q.* FROM dtf_local.quotes q
|
pending = c.execute(f'''SELECT q.* FROM dtf_local.quotes q
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
WHERE o.id IS NULL AND q.approved IS NULL
|
WHERE o.id IS NULL AND q.approved IS NULL AND {LIVE_QUOTE}
|
||||||
ORDER BY q.created_at DESC,q.id DESC LIMIT %s''', (BOARD_QUOTE_LIMIT,)).fetchall()
|
ORDER BY q.created_at DESC,q.id DESC LIMIT %s''', (BOARD_QUOTE_LIMIT,)).fetchall()
|
||||||
approved = c.execute('''SELECT q.* FROM dtf_local.quotes q
|
approved = c.execute(f'''SELECT q.* FROM dtf_local.quotes q
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
WHERE o.id IS NULL AND q.approved IS NOT NULL
|
WHERE o.id IS NULL AND q.approved IS NOT NULL AND {LIVE_QUOTE}
|
||||||
ORDER BY q.created_at DESC,q.id DESC LIMIT 20''').fetchall()
|
ORDER BY q.created_at DESC,q.id DESC LIMIT 20''').fetchall()
|
||||||
pending_total = c.execute('''SELECT count(*) AS n FROM dtf_local.quotes q
|
pending_total = c.execute(f'''SELECT count(*) AS n FROM dtf_local.quotes q
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
WHERE o.id IS NULL AND q.approved IS NULL''').fetchone()['n']
|
WHERE o.id IS NULL AND q.approved IS NULL AND {LIVE_QUOTE}''').fetchone()['n']
|
||||||
approved_total = c.execute('''SELECT count(*) AS n FROM dtf_local.quotes q
|
approved_total = c.execute(f'''SELECT count(*) AS n FROM dtf_local.quotes q
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
WHERE o.id IS NULL AND q.approved IS NOT NULL''').fetchone()['n']
|
WHERE o.id IS NULL AND q.approved IS NOT NULL AND {LIVE_QUOTE}''').fetchone()['n']
|
||||||
orders = active + list(reversed(finished))
|
orders = with_print_files(c, active + list(reversed(finished)))
|
||||||
|
# A paid notification that did not become an order is money received
|
||||||
|
# for nothing the factory will make. The board carries the count; the
|
||||||
|
# Pagamentos tab pages through them until someone records a resolution.
|
||||||
|
issues_total = c.execute('''SELECT count(*) AS n FROM dtf_local.payment_events
|
||||||
|
WHERE (outcome LIKE 'refused%' OR outcome LIKE 'attention%') AND resolved_at IS NULL''').fetchone()['n']
|
||||||
|
backup = backup_status(c)
|
||||||
|
quotes = [quote_view(c, q) for q in pending + approved]
|
||||||
|
return {'states': STATES, 'transitions': TRANSITIONS, 'back': BACK,
|
||||||
|
'orders': orders, 'payment_issues_total': issues_total, 'tiny': tiny_status(), 'operator': user,
|
||||||
|
'providers': {'payment': payment.name, 'freight': freight_status(), 'backup': backup},
|
||||||
|
'environment': ENVIRONMENT, 'site_origin': PUBLIC_ORIGIN,
|
||||||
|
'finished_shown': len(finished), 'finished_total': finished_total,
|
||||||
|
'quotes': quotes, 'thumbnails': thumbnails(c, orders, quotes),
|
||||||
|
'names': company_names(c, orders, quotes),
|
||||||
|
'pending_total': pending_total, 'approved_total': approved_total}
|
||||||
|
|
||||||
|
|
||||||
|
def uploads_of(rows):
|
||||||
|
"""Every upload id an order (its snapshot) or a quote (its draft) refers to."""
|
||||||
|
ids = set()
|
||||||
|
for row in rows:
|
||||||
|
content = row.get('snapshot') or row.get('draft') or {}
|
||||||
|
for item in content.get('items') or []:
|
||||||
|
ids.update(str(uid) for uid in item.get('uploads') or [])
|
||||||
|
return ids
|
||||||
|
|
||||||
|
|
||||||
|
def thumbnails(c, *groups):
|
||||||
|
"""The uploads among these orders and quotes that have an artwork picture,
|
||||||
|
so the Kanban asks only for pictures that exist."""
|
||||||
|
ids = set().union(*(uploads_of(rows) for rows in groups))
|
||||||
|
if not ids:
|
||||||
|
return []
|
||||||
|
rows = c.execute('SELECT upload_id FROM dtf_local.upload_thumbnails WHERE upload_id=ANY(%s::uuid[])',
|
||||||
|
(list(ids),)).fetchall()
|
||||||
|
return [str(r['upload_id']) for r in rows]
|
||||||
|
|
||||||
|
|
||||||
|
def company_names(c, *groups):
|
||||||
|
"""The registry's company names for these orders' and quotes' CNPJs (app/cnpj.py)."""
|
||||||
|
return cnpj.names(c, [((row.get('snapshot') or row.get('draft') or {}).get('customer') or {}).get('cnpj')
|
||||||
|
for rows in groups for row in rows])
|
||||||
|
|
||||||
|
|
||||||
|
# An item's final counts while its file is still there and scanned clean: the
|
||||||
|
# same rule that lets an order into the print queue.
|
||||||
|
FINAL_OK = """f.kind='final' AND f.active AND u.expires_at>now() AND u.purged_at IS NULL AND u.scan_state='clean'"""
|
||||||
|
|
||||||
|
|
||||||
|
def with_print_files(c, orders):
|
||||||
|
"""Each order with its generated print files, the items whose final file is
|
||||||
|
approved, and the customer's corrections waiting to be approved as finals."""
|
||||||
|
ids = [o['id'] for o in orders]
|
||||||
generated = c.execute('''SELECT p.order_id,p.item_index,p.status,p.upload_id,p.detail,u.name
|
generated = c.execute('''SELECT p.order_id,p.item_index,p.status,p.upload_id,p.detail,u.name
|
||||||
FROM dtf_local.print_files p LEFT JOIN dtf_local.uploads u ON u.id=p.upload_id
|
FROM dtf_local.print_files p LEFT JOIN dtf_local.uploads u ON u.id=p.upload_id
|
||||||
WHERE p.order_id=ANY(%s) ORDER BY p.item_index''', ([o['id'] for o in orders],)).fetchall()
|
WHERE p.order_id=ANY(%s) ORDER BY p.item_index''', (ids,)).fetchall()
|
||||||
|
finals = c.execute(f'''SELECT DISTINCT f.order_id,f.item_index FROM dtf_local.order_files f
|
||||||
|
JOIN dtf_local.uploads u ON u.id=f.upload_id WHERE f.order_id=ANY(%s) AND {FINAL_OK}''', (ids,)).fetchall()
|
||||||
|
# Expired ones too: once corrected, a generated file is no longer the artwork.
|
||||||
|
fixes = c.execute('''SELECT f.order_id,f.item_index,f.upload_id,u.name,
|
||||||
|
(u.expires_at<=now() OR u.purged_at IS NOT NULL) AS expired FROM dtf_local.order_files f
|
||||||
|
JOIN dtf_local.uploads u ON u.id=f.upload_id
|
||||||
|
WHERE f.order_id=ANY(%s) AND f.kind='correction' AND f.active ORDER BY f.item_index''', (ids,)).fetchall()
|
||||||
|
# Why each order was last sent to correction, for the notice to the customer.
|
||||||
|
reasons = {row['order_id']: row['reason'] for row in c.execute('''SELECT DISTINCT ON (order_id) order_id,reason
|
||||||
|
FROM dtf_local.movements WHERE order_id=ANY(%s) AND to_state='cor' ORDER BY order_id,id DESC''', (ids,)).fetchall()}
|
||||||
for order in orders:
|
for order in orders:
|
||||||
|
order['correction_reason'] = reasons.get(order['id']) if order['state'] == 'cor' else None
|
||||||
order['print_files'] = [row for row in generated if row['order_id'] == order['id']]
|
order['print_files'] = [row for row in generated if row['order_id'] == order['id']]
|
||||||
# A paid notification that did not become an order is money received
|
order['final_items'] = sorted(row['item_index'] for row in finals if row['order_id'] == order['id'])
|
||||||
# for nothing the factory will make. It stays on the board until a
|
order['corrections'] = [{k: row[k] for k in ('item_index', 'upload_id', 'name', 'expired')}
|
||||||
# person records what was done about it.
|
for row in fixes if row['order_id'] == order['id']]
|
||||||
refused = c.execute('''SELECT id,provider,event_id,reference,status,amount_cents,received_at,outcome
|
return orders
|
||||||
FROM dtf_local.payment_events WHERE (outcome LIKE 'refused%' OR outcome LIKE 'attention%') AND resolved_at IS NULL
|
|
||||||
ORDER BY received_at LIMIT 100''').fetchall()
|
|
||||||
return {'states': STATES, 'transitions': TRANSITIONS,
|
def cursor_pair(first, second):
|
||||||
'orders': orders, 'payment_issues': refused, 'tiny': tiny_status(),
|
if (first is None) != (second is None):
|
||||||
'finished_shown': len(finished), 'finished_total': finished_total,
|
raise HTTPException(422, 'Both cursor fields are required')
|
||||||
'quotes': [quote_view(c, q) for q in pending + approved],
|
return first is not None
|
||||||
'pending_total': pending_total, 'approved_total': approved_total,
|
|
||||||
'events': c.execute('SELECT * FROM dtf_local.outbox ORDER BY id DESC LIMIT 100').fetchall()}
|
|
||||||
|
@router.get('/api/operator/orders/finished')
|
||||||
|
def finished_page(before_created_at: datetime | None = None, before_id: UUID | None = None,
|
||||||
|
limit: int = Query(default=50, ge=1, le=100), user=Depends(operator)):
|
||||||
|
"""Older finished orders, newest first, beyond the board's recent window."""
|
||||||
|
paged = cursor_pair(before_created_at, before_id)
|
||||||
|
with db.connect() as c:
|
||||||
|
rows = c.execute('''SELECT * FROM dtf_local.orders WHERE state='fin'
|
||||||
|
''' + ('AND (created_at,id)<(%s,%s) ' if paged else '') + '''
|
||||||
|
ORDER BY created_at DESC,id DESC LIMIT %s''',
|
||||||
|
((before_created_at, before_id) if paged else ()) + (limit + 1,)).fetchall()
|
||||||
|
orders = with_print_files(c, rows[:limit])
|
||||||
|
return {'orders': orders, 'has_more': len(rows) > limit, 'thumbnails': thumbnails(c, orders),
|
||||||
|
'names': company_names(c, orders)}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/payment-events')
|
||||||
|
def payment_events(state: Literal['open','resolved','all'] = 'open',
|
||||||
|
offset: int = Query(default=0, ge=0),
|
||||||
|
limit: int = Query(default=20, ge=1, le=100), user=Depends(operator)):
|
||||||
|
"""Payments that needed a person, newest first: open ones to act on, resolved ones as history."""
|
||||||
|
where = {'open': 'AND resolved_at IS NULL', 'resolved': 'AND resolved_at IS NOT NULL', 'all': ''}[state]
|
||||||
|
with db.connect() as c:
|
||||||
|
rows = c.execute('''SELECT id,provider,event_id,reference,status,amount_cents,received_at,outcome,
|
||||||
|
resolved_at,resolved_by,resolution
|
||||||
|
FROM dtf_local.payment_events WHERE (outcome LIKE 'refused%%' OR outcome LIKE 'attention%%') ''' + where + '''
|
||||||
|
ORDER BY received_at DESC,id DESC LIMIT %s OFFSET %s''', (limit, offset)).fetchall()
|
||||||
|
total = c.execute('''SELECT count(*) AS n FROM dtf_local.payment_events
|
||||||
|
WHERE (outcome LIKE 'refused%' OR outcome LIKE 'attention%') ''' + where).fetchone()['n']
|
||||||
|
return {'issues': rows, 'total': total}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/events')
|
||||||
|
def events(provider: Literal['tiny','whatsapp'] | None = None,
|
||||||
|
status: Literal['delivered','queued','failing'] | None = None,
|
||||||
|
event: Literal['payment_approved','production_started','correction_needed','ready'] | None = None,
|
||||||
|
order: int | None = Query(default=None, ge=1), offset: int = Query(default=0, ge=0),
|
||||||
|
limit: int = Query(default=20, ge=1, le=100), user=Depends(operator)):
|
||||||
|
"""The integration send log, newest first, filtered, by page with a total."""
|
||||||
|
clauses, params = [], []
|
||||||
|
if provider:
|
||||||
|
clauses.append('provider=%s'); params.append(provider)
|
||||||
|
if status == 'delivered':
|
||||||
|
clauses.append('delivered_at IS NOT NULL')
|
||||||
|
elif status == 'queued':
|
||||||
|
clauses.append('delivered_at IS NULL AND last_error IS NULL')
|
||||||
|
elif status == 'failing':
|
||||||
|
clauses.append('delivered_at IS NULL AND last_error IS NOT NULL')
|
||||||
|
if event:
|
||||||
|
clauses.append("payload->>'event'=%s"); params.append(event)
|
||||||
|
if order:
|
||||||
|
clauses.append("payload->>'number'=%s"); params.append(str(order))
|
||||||
|
where = ('WHERE ' + ' AND '.join(clauses)) if clauses else ''
|
||||||
|
with db.connect() as c:
|
||||||
|
rows = c.execute(f'SELECT * FROM dtf_local.outbox {where} ORDER BY id DESC LIMIT %s OFFSET %s',
|
||||||
|
(*params, limit, offset)).fetchall()
|
||||||
|
total = c.execute(f'SELECT count(*) AS n FROM dtf_local.outbox {where}', params).fetchone()['n']
|
||||||
|
return {'events': rows, 'total': total}
|
||||||
|
|
||||||
|
MODES = {'textil': {'file', 'avulsa'}, 'uv': {'uvfile', 'uv'},
|
||||||
|
'folha': {'file', 'uvfile'}, 'avulsa': {'avulsa', 'uv'}}
|
||||||
|
|
||||||
|
|
||||||
|
def like(text):
|
||||||
|
"""A LIKE pattern that matches the text anywhere, its own % and _ literal."""
|
||||||
|
return '%' + re.sub(r'([\\%_])', r'\\\1', text) + '%'
|
||||||
|
|
||||||
|
|
||||||
|
def numeric(q):
|
||||||
|
"""The digits of a query made of digits and punctuation only ("#1042",
|
||||||
|
"11.222.333/0001-81", "(16) 99999-0000"); empty when it has letters."""
|
||||||
|
return '' if re.search(r'[^\W\d_]', q) else re.sub(r'\D', '', q)
|
||||||
|
|
||||||
|
|
||||||
|
def customer_match(column, q):
|
||||||
|
"""SQL and parameters matching a customer (e-mail, company name, CNPJ or
|
||||||
|
WhatsApp) in an order snapshot or a quote draft. CNPJ and phone match by
|
||||||
|
their digits, and only when nothing but digits and punctuation was typed:
|
||||||
|
the digits of an e-mail address are not a phone number."""
|
||||||
|
text, digits = q.strip().lower(), numeric(q)
|
||||||
|
cnpj = f"regexp_replace(coalesce({column}->'customer'->>'cnpj',''),'\\D','','g')"
|
||||||
|
sql = (f"lower({column}->'customer'->>'mail') LIKE %s OR {cnpj} IN (SELECT n.cnpj FROM dtf_local.cnpj_names n"
|
||||||
|
" WHERE lower(n.legal_name) LIKE %s OR lower(n.trade_name) LIKE %s)")
|
||||||
|
params = [like(text)] * 3
|
||||||
|
if len(digits) >= 3:
|
||||||
|
sql += (f" OR regexp_replace(coalesce({column}->'customer'->>'cnpj',''),'\\D','','g') LIKE %s"
|
||||||
|
f" OR regexp_replace(coalesce({column}->'customer'->>'zap',''),'\\D','','g') LIKE %s")
|
||||||
|
params += [like(digits), like(digits)]
|
||||||
|
return '(' + sql + ')', params
|
||||||
|
|
||||||
|
|
||||||
@router.get('/api/operator/quotes')
|
@router.get('/api/operator/quotes')
|
||||||
def quote_page(kind: Literal['pending','approved'], before_created_at: datetime | None = None,
|
def quote_page(kind: Literal['pending','approved'], before_created_at: datetime | None = None,
|
||||||
before_id: UUID | None = None, limit: int = Query(default=50, ge=1, le=100),
|
before_id: UUID | None = None, offset: int = Query(default=0, ge=0),
|
||||||
user=Depends(operator)):
|
limit: int = Query(default=50, ge=1, le=100),
|
||||||
|
q: str | None = Query(default=None, max_length=100),
|
||||||
|
product: Literal['textil','uv'] | None = None, layout: Literal['folha','avulsa'] | None = None,
|
||||||
|
flag: Literal['ressalva'] | None = None, user=Depends(operator)):
|
||||||
|
"""Unpaid quotes, newest first: by cursor, or by page (offset) with a total;
|
||||||
|
filtered by customer, product, layout and an accepted resolution warning."""
|
||||||
if (before_created_at is None) != (before_id is None):
|
if (before_created_at is None) != (before_id is None):
|
||||||
raise HTTPException(422, 'Both quote cursor fields are required')
|
raise HTTPException(422, 'Both quote cursor fields are required')
|
||||||
approved_filter = 'q.approved IS NULL' if kind == 'pending' else 'q.approved IS NOT NULL'
|
clauses = ['o.id IS NULL', LIVE_QUOTE, 'q.approved IS NULL' if kind == 'pending' else 'q.approved IS NOT NULL']
|
||||||
|
params = []
|
||||||
|
if q and q.strip():
|
||||||
|
sql, values = customer_match('q.draft', q)
|
||||||
|
clauses.append(sql); params += values
|
||||||
|
if product or layout:
|
||||||
|
modes = set.intersection(*(MODES[k] for k in (product, layout) if k))
|
||||||
|
clauses.append("EXISTS (SELECT 1 FROM jsonb_array_elements(q.draft->'items') i WHERE i->>'mode'=ANY(%s))")
|
||||||
|
params.append(sorted(modes))
|
||||||
|
if flag:
|
||||||
|
clauses.append("EXISTS (SELECT 1 FROM jsonb_array_elements(q.draft->'items') i WHERE i->>'quality_status'='warning')")
|
||||||
|
where = ' AND '.join(clauses)
|
||||||
cursor = 'AND (q.created_at,q.id)<(%s,%s)' if before_created_at else ''
|
cursor = 'AND (q.created_at,q.id)<(%s,%s)' if before_created_at else ''
|
||||||
params = ((before_created_at,before_id) if before_created_at else ()) + (limit+1,)
|
skip = 0 if before_created_at else offset
|
||||||
|
page = params + ([before_created_at, before_id] if before_created_at else []) + [limit+1, skip]
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
rows = c.execute(f'''SELECT q.* FROM dtf_local.quotes q
|
rows = c.execute(f'''SELECT q.* FROM dtf_local.quotes q
|
||||||
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
WHERE o.id IS NULL AND {approved_filter} {cursor}
|
WHERE {where} {cursor}
|
||||||
ORDER BY q.created_at DESC,q.id DESC LIMIT %s''', params).fetchall()
|
ORDER BY q.created_at DESC,q.id DESC LIMIT %s OFFSET %s''', page).fetchall()
|
||||||
return {'quotes':[quote_view(c,row) for row in rows[:limit]],
|
total = c.execute(f'''SELECT count(*) AS n FROM dtf_local.quotes q
|
||||||
'has_more':len(rows)>limit}
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
|
WHERE {where}''', params).fetchone()['n']
|
||||||
|
quotes = [quote_view(c,row) for row in rows[:limit]]
|
||||||
|
return {'quotes': quotes, 'has_more': len(rows)>limit, 'total': total,
|
||||||
|
'thumbnails': thumbnails(c, quotes), 'names': company_names(c, quotes)}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/search')
|
||||||
|
def search(q: str = Query(min_length=2, max_length=100), user=Depends(operator)):
|
||||||
|
"""One search over orders (any stage) and unpaid quotes: customer e-mail,
|
||||||
|
CNPJ or WhatsApp, the delivery recipient, or the order number."""
|
||||||
|
match, params = customer_match('snapshot', q)
|
||||||
|
digits = numeric(q)
|
||||||
|
sql = match + " OR lower(coalesce(snapshot->'destination'->>'recipient','')) LIKE %s"
|
||||||
|
params.append(like(q.strip().lower()))
|
||||||
|
if digits and len(digits) <= 9:
|
||||||
|
sql += ' OR number=%s'
|
||||||
|
params.append(int(digits))
|
||||||
|
# The code a customer reads out (the id's first characters, see order_code).
|
||||||
|
code = q.strip().lstrip('#').lower()
|
||||||
|
if 6 <= len(code) <= 12 and re.fullmatch(r'[0-9a-f]+', code):
|
||||||
|
sql += " OR replace(id::text,'-','') LIKE %s"
|
||||||
|
params.append(code + '%')
|
||||||
|
quote_match, quote_params = customer_match('q.draft', q)
|
||||||
|
with db.connect() as c:
|
||||||
|
orders = c.execute(f'''SELECT * FROM dtf_local.orders WHERE {sql}
|
||||||
|
ORDER BY created_at DESC LIMIT 8''', params).fetchall()
|
||||||
|
rows = c.execute(f'''SELECT q.* FROM dtf_local.quotes q
|
||||||
|
LEFT JOIN dtf_local.orders o ON o.quote_id=q.id
|
||||||
|
WHERE o.id IS NULL AND {LIVE_QUOTE} AND {quote_match}
|
||||||
|
ORDER BY q.created_at DESC LIMIT 8''', quote_params).fetchall()
|
||||||
|
orders = with_print_files(c, orders)
|
||||||
|
quotes = [quote_view(c, row) for row in rows]
|
||||||
|
return {'orders': orders, 'quotes': quotes, 'thumbnails': thumbnails(c, orders, quotes),
|
||||||
|
'names': company_names(c, orders, quotes)}
|
||||||
|
|
||||||
@router.post('/api/operator/quotes/{uid}/approve')
|
@router.post('/api/operator/quotes/{uid}/approve')
|
||||||
def approve(uid: UUID, body: Review, user=Depends(operator)):
|
def approve(uid: UUID, body: Review, user=Depends(operator)):
|
||||||
@@ -126,31 +358,123 @@ def approve(uid: UUID, body: Review, user=Depends(operator)):
|
|||||||
row = c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s FOR UPDATE', (uid,)).fetchone()
|
row = c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s FOR UPDATE', (uid,)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, 'Quote not found')
|
raise HTTPException(404, 'Quote not found')
|
||||||
if row['approved']:
|
return quote_review.approve(c, row, body.items, user)
|
||||||
raise HTTPException(409, 'Approved quotes are immutable; request a new quote')
|
|
||||||
draft = row['draft']
|
@router.post('/api/operator/quotes/{uid}/test-order')
|
||||||
if any(item.get('production', {}).get('version') != 2 for item in draft['items']):
|
def test_order(uid: UUID, user=Depends(operator)):
|
||||||
raise HTTPException(409, 'Quote uses an obsolete production layout; customer must request a new quote')
|
"""An order for an approved quote without payment, to try the Kanban,
|
||||||
if len(body.items) != len(draft['items']):
|
files and print flow in production. Marked TEST on the board, never sent
|
||||||
raise HTTPException(422, 'Review must cover every item')
|
to Tiny or WhatsApp, and audited."""
|
||||||
items = []
|
with db.connect() as c:
|
||||||
for item, original in zip(body.items, draft['items']):
|
try:
|
||||||
if item.mode != original['mode'] or list(map(str,item.uploads)) != original['uploads']:
|
quote = payments.approved_quote(c, uid)
|
||||||
raise HTTPException(422, 'Product mode and attached files cannot change during review')
|
except payments.PaymentRefused as refusal:
|
||||||
if item.production.model_dump(mode='json') != original['production'] or item.quality_status != original['quality_status'] or item.quality_acknowledged != original['quality_acknowledged']:
|
raise HTTPException(404 if 'not found' in str(refusal) else 409, str(refusal))
|
||||||
raise HTTPException(422, 'Production instructions and customer acknowledgement cannot change during commercial review')
|
receipt = {'provider': payments.TEST_PROVIDER, 'id': f'teste-{uid}', 'status': 'paid',
|
||||||
for upload_id in item.uploads:
|
'total_cents': quote['approved']['total_cents'], 'operator': user}
|
||||||
require_clean(upload_row(c, upload_id, row['owner']))
|
order, created = payments.create_order(c, quote, receipt)
|
||||||
items.append({**price(item.mode, str(item.metres), item.grade),
|
if created:
|
||||||
'uploads': original['uploads'], 'production': original['production'],
|
audit('test_order_created', order=str(order['id']), operator=user)
|
||||||
'quality_status': original['quality_status'],
|
return order
|
||||||
'quality_acknowledged': original['quality_acknowledged']})
|
|
||||||
quoted_freight = freight.quote(**draft['freight'])
|
|
||||||
approved = {'customer': draft['customer'], 'items': items, 'freight': quoted_freight,
|
@router.post('/api/operator/quotes/{uid}/discard')
|
||||||
'destination': draft.get('destination'),
|
def discard_quote(uid: UUID, user=Depends(operator)):
|
||||||
'total_cents': sum(i['total_cents'] for i in items)+quoted_freight['total_cents']}
|
"""An unpaid quote leaves the Kanban and can no longer be paid. Its files
|
||||||
c.execute('UPDATE dtf_local.quotes SET approved=%s, reviewed_by=%s, approved_at=now() WHERE id=%s', (Jsonb(approved),user,uid))
|
keep their normal expiry; a payment that still arrives for it is refused
|
||||||
return approved
|
and listed under Pagamentos."""
|
||||||
|
with db.connect() as c:
|
||||||
|
# Locked like a payment locks it, so a discard and a payment never cross.
|
||||||
|
row = c.execute('SELECT id FROM dtf_local.quotes WHERE id=%s FOR UPDATE', (uid,)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(404, 'Quote not found')
|
||||||
|
if c.execute('SELECT 1 FROM dtf_local.orders WHERE quote_id=%s', (uid,)).fetchone():
|
||||||
|
raise HTTPException(409, 'Esta cotação já virou pedido.')
|
||||||
|
c.execute('UPDATE dtf_local.quotes SET discarded_at=now(), discarded_by=%s WHERE id=%s AND discarded_at IS NULL',
|
||||||
|
(user, uid))
|
||||||
|
audit('quote_discarded', quote=str(uid), operator=user)
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('/api/operator/orders/{uid}/delete')
|
||||||
|
def delete_order(uid: UUID, user=Depends(operator)):
|
||||||
|
"""Deletes a test order, or a paid order already cancelled, with its
|
||||||
|
history and files' records. A paid order in progress must be cancelled
|
||||||
|
first, with its reason. A test order's quote is unpaid again; a cancelled
|
||||||
|
one's is discarded, so it neither returns to Cotações nor can be paid twice.
|
||||||
|
The payment itself stays recorded, and the audit log keeps who deleted what."""
|
||||||
|
with db.connect() as c:
|
||||||
|
row = c.execute('SELECT * FROM dtf_local.orders WHERE id=%s FOR UPDATE', (uid,)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(404, 'Order not found')
|
||||||
|
test = payments.is_test(row)
|
||||||
|
if not test and not row.get('cancelled_at'):
|
||||||
|
raise HTTPException(409, 'Cancele o pedido antes de excluir. Só pedidos cancelados ou de teste podem ser excluídos.')
|
||||||
|
for table in ('movements', 'order_files', 'print_files'):
|
||||||
|
c.execute(f'DELETE FROM dtf_local.{table} WHERE order_id=%s', (uid,))
|
||||||
|
c.execute('DELETE FROM dtf_local.outbox WHERE event_key LIKE %s', (f'{uid}:%',))
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (uid,))
|
||||||
|
if not test:
|
||||||
|
c.execute('UPDATE dtf_local.quotes SET discarded_at=COALESCE(discarded_at,now()), discarded_by=COALESCE(discarded_by,%s) WHERE id=%s',
|
||||||
|
(user, row['quote_id']))
|
||||||
|
audit('order_deleted', order=row['number'], test=test, operator=user,
|
||||||
|
reason=row.get('cancel_reason') or '')
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('/api/operator/orders/{uid}/cancel')
|
||||||
|
def cancel_order(uid: UUID, body: Resolution, user=Depends(operator)):
|
||||||
|
"""A paid order leaves the board but keeps its payment and history. Nothing
|
||||||
|
is refunded here (that is done at the provider) and Tiny is not told."""
|
||||||
|
with db.connect() as c:
|
||||||
|
row = c.execute('''UPDATE dtf_local.orders SET cancelled_at=now(), cancelled_by=%s, cancel_reason=%s,
|
||||||
|
version=version+1, updated_at=now() WHERE id=%s AND cancelled_at IS NULL RETURNING number''',
|
||||||
|
(user, body.note, uid)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(404, 'Pedido não encontrado ou já cancelado.')
|
||||||
|
audit('order_cancelled', order=row['number'], operator=user)
|
||||||
|
return {'ok': True}
|
||||||
|
|
||||||
|
|
||||||
|
class QueuePlace(StrictModel):
|
||||||
|
"""Where a dragged card lands: between two orders of its column, either
|
||||||
|
of them absent at the top or the bottom."""
|
||||||
|
after: UUID | None = None
|
||||||
|
before: UUID | None = None
|
||||||
|
|
||||||
|
|
||||||
|
QUEUE_KEY = 'COALESCE(queue_key, extract(epoch FROM created_at))'
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('/api/operator/orders/{uid}/queue')
|
||||||
|
def queue_place(uid: UUID, body: QueuePlace, user=Depends(operator)):
|
||||||
|
"""An order's place in its column: by default its payment time; dragged,
|
||||||
|
halfway between the orders it was dropped between. It neither changes the
|
||||||
|
order's stage nor counts as activity (the stalled clock keeps running)."""
|
||||||
|
if not body.after and not body.before:
|
||||||
|
raise HTTPException(422, 'Informe entre quais pedidos ele fica.')
|
||||||
|
with db.connect() as c:
|
||||||
|
row = c.execute('SELECT state FROM dtf_local.orders WHERE id=%s FOR UPDATE', (uid,)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(404, 'Order not found')
|
||||||
|
keys = {}
|
||||||
|
for side in ('after', 'before'):
|
||||||
|
other = getattr(body, side)
|
||||||
|
if other:
|
||||||
|
near = c.execute(f'SELECT state, {QUEUE_KEY} AS key FROM dtf_local.orders WHERE id=%s', (other,)).fetchone()
|
||||||
|
if not near or near['state'] != row['state']:
|
||||||
|
raise HTTPException(409, 'Os pedidos mudaram de etapa. Clique em Atualizar.')
|
||||||
|
keys[side] = float(near['key'])
|
||||||
|
if 'after' in keys and 'before' in keys:
|
||||||
|
key = (keys['after'] + keys['before']) / 2
|
||||||
|
elif 'after' in keys:
|
||||||
|
key = keys['after'] + 60
|
||||||
|
else:
|
||||||
|
key = keys['before'] - 60
|
||||||
|
c.execute('UPDATE dtf_local.orders SET queue_key=%s WHERE id=%s', (key, uid))
|
||||||
|
audit('order_queue_moved', order=str(uid), operator=user)
|
||||||
|
return {'ok': True, 'queue_key': key}
|
||||||
|
|
||||||
|
|
||||||
@router.post('/api/operator/orders/{uid}/move')
|
@router.post('/api/operator/orders/{uid}/move')
|
||||||
def move(uid: UUID, body: Move, user=Depends(operator)):
|
def move(uid: UUID, body: Move, user=Depends(operator)):
|
||||||
@@ -159,28 +483,44 @@ def move(uid: UUID, body: Move, user=Depends(operator)):
|
|||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, 'Order not found')
|
raise HTTPException(404, 'Order not found')
|
||||||
if body.version != row['version']:
|
if body.version != row['version']:
|
||||||
raise HTTPException(409, 'Order changed; refresh the board')
|
raise HTTPException(409, 'O pedido mudou. Clique em Atualizar.')
|
||||||
|
if row.get('cancelled_at'):
|
||||||
|
raise HTTPException(409, 'Pedido cancelado.')
|
||||||
if body.state == row['state']:
|
if body.state == row['state']:
|
||||||
return row
|
return row
|
||||||
if body.state not in TRANSITIONS[row['state']]:
|
back = BACK.get(row['state']) == body.state
|
||||||
raise HTTPException(409, 'Move is not allowed from this state')
|
if body.state not in TRANSITIONS[row['state']] and not back:
|
||||||
|
raise HTTPException(409, f"Não dá para ir de {STATES[row['state']]} para {STATES[body.state]}.")
|
||||||
if body.state == 'cor' and not body.reason.strip():
|
if body.state == 'cor' and not body.reason.strip():
|
||||||
raise HTTPException(422, 'Correction requires a reason')
|
raise HTTPException(422, 'Informe o motivo da correção.')
|
||||||
|
if back and not body.reason.strip():
|
||||||
|
raise HTTPException(422, 'Informe por que o pedido está voltando de etapa.')
|
||||||
if body.state in ('fil','imp'):
|
if body.state in ('fil','imp'):
|
||||||
coverage = c.execute('SELECT DISTINCT f.item_index FROM dtf_local.order_files f JOIN dtf_local.uploads u ON u.id=f.upload_id WHERE f.order_id=%s AND f.kind=\'final\' AND f.active AND u.expires_at>now() AND u.purged_at IS NULL AND u.scan_state=\'clean\'', (uid,)).fetchall()
|
coverage = c.execute(f'''SELECT DISTINCT f.item_index FROM dtf_local.order_files f
|
||||||
|
JOIN dtf_local.uploads u ON u.id=f.upload_id WHERE f.order_id=%s AND {FINAL_OK}''', (uid,)).fetchall()
|
||||||
if {r['item_index'] for r in coverage} != set(range(len(row['snapshot']['items']))):
|
if {r['item_index'] for r in coverage} != set(range(len(row['snapshot']['items']))):
|
||||||
raise HTTPException(409, 'Approve a complete final-file set for every item before queueing')
|
raise HTTPException(409, 'Aprove os arquivos finais de todos os itens antes de colocar na fila.')
|
||||||
if body.state == 'cor':
|
if body.state == 'cor':
|
||||||
c.execute("UPDATE dtf_local.order_files SET active=false WHERE order_id=%s AND kind='final'", (uid,))
|
c.execute("UPDATE dtf_local.order_files SET active=false WHERE order_id=%s AND kind='final'", (uid,))
|
||||||
c.execute('INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason) VALUES(%s,%s,%s,%s,%s)',
|
c.execute('INSERT INTO dtf_local.movements(order_id,from_state,to_state,operator,reason,back) VALUES(%s,%s,%s,%s,%s,%s)',
|
||||||
(uid,row['state'],body.state,user,body.reason))
|
(uid,row['state'],body.state,user,body.reason,back))
|
||||||
changed = c.execute('UPDATE dtf_local.orders SET state=%s, version=version+1, updated_at=now() WHERE id=%s RETURNING *', (body.state,uid)).fetchone()
|
changed = c.execute('UPDATE dtf_local.orders SET state=%s, version=version+1, updated_at=now(), queue_key=NULL WHERE id=%s RETURNING *', (body.state,uid)).fetchone()
|
||||||
events = {'imp':'production_started','cor':'correction_needed','fin':'ready'}
|
events = {'imp':'production_started','cor':'correction_needed','fin':'ready'}
|
||||||
if body.state in events:
|
if body.state in events and not back and not payments.is_test(row):
|
||||||
for provider in ('tiny','whatsapp'):
|
# "Production started" and "ready" reach the customer once per order,
|
||||||
enqueue(c, f'{uid}:{changed["version"]}:{provider}', provider,
|
# even if a mistaken move is undone and made again. Each correction
|
||||||
{'order_id':str(uid), 'number':row['number'], 'event':events[body.state], 'reason':body.reason,
|
# is a new request, so it keeps one message per movement.
|
||||||
'customer_path': f'/portal.html?order={uid}'})
|
once = body.state in ('imp','fin')
|
||||||
|
event = {'order_id':str(uid), 'number':row['number'], 'event':events[body.state], 'reason':body.reason,
|
||||||
|
'customer_path': f'/portal.html?order={uid}'}
|
||||||
|
# Tiny needs the order (customer, pickup or delivery) and, once the
|
||||||
|
# sale reached it, its id, to set the situação without a search.
|
||||||
|
sale = c.execute("SELECT receipt FROM dtf_local.outbox WHERE event_key=%s AND delivered_at IS NOT NULL",
|
||||||
|
(f'{uid}:paid:tiny',)).fetchone()
|
||||||
|
tiny = {**event, 'order': row['snapshot'], 'tiny_id': ((sale or {}).get('receipt') or {}).get('tiny_id')}
|
||||||
|
for provider, payload in (('tiny', tiny), ('whatsapp', event)):
|
||||||
|
key = f'{uid}:{events[body.state]}:{provider}' if once else f'{uid}:{changed["version"]}:{provider}'
|
||||||
|
enqueue(c, key, provider, payload)
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
@router.post('/api/operator/orders/{uid}/print-files')
|
@router.post('/api/operator/orders/{uid}/print-files')
|
||||||
@@ -224,13 +564,55 @@ def tiny_connect(user=Depends(operator)):
|
|||||||
audit('tiny_connect_started', operator=user)
|
audit('tiny_connect_started', operator=user)
|
||||||
return {'url': tiny.TinyAuth().authorize_url(user)}
|
return {'url': tiny.TinyAuth().authorize_url(user)}
|
||||||
|
|
||||||
@router.get('/api/operator/tiny/callback')
|
@router.post('/api/operator/tiny/test')
|
||||||
def tiny_callback(code: str = Query(max_length=4096), state: str = Query(max_length=128)):
|
def tiny_test(user=Depends(operator)):
|
||||||
"""Tiny's redirect back. Cross-site, so the operator cookie is absent: the
|
"""Read one order and one contact from Tiny. Creates nothing."""
|
||||||
single-use state an operator created is what authorises it."""
|
|
||||||
if not tiny.configured():
|
if not tiny.configured():
|
||||||
raise HTTPException(503, 'Tiny application is not configured')
|
raise HTTPException(503, 'Tiny application is not configured')
|
||||||
try:
|
try:
|
||||||
|
results = tiny.check()
|
||||||
|
except tiny.TinyNotConnected as exc:
|
||||||
|
raise HTTPException(409, str(exc))
|
||||||
|
audit('tiny_tested', operator=user, ok=all(v == 'ok' for v in results.values()))
|
||||||
|
return {'ok': all(v == 'ok' for v in results.values()), 'results': results}
|
||||||
|
|
||||||
|
@router.post('/api/operator/mercadopago/check')
|
||||||
|
def mercadopago_check(user=Depends(operator)):
|
||||||
|
"""The Mercado Pago account behind the configured token; reads only."""
|
||||||
|
if payment.name != 'mercadopago':
|
||||||
|
raise HTTPException(409, 'Mercado Pago não está configurado')
|
||||||
|
from ..mercadopago_probe import check
|
||||||
|
audit('mercadopago_check', operator=user)
|
||||||
|
result = check(payment.access_token)
|
||||||
|
# Whether Mercado Pago's notifications reach this server and pass the
|
||||||
|
# signature: every accepted one is recorded, a refused one is audited.
|
||||||
|
with db.connect() as c:
|
||||||
|
received = c.execute('''SELECT count(*) AS n, max(received_at) AS last FROM dtf_local.payment_events
|
||||||
|
WHERE provider='mercadopago' AND received_at > now()-interval '24 hours' ''').fetchone()
|
||||||
|
last = c.execute('''SELECT received_at,status,outcome FROM dtf_local.payment_events
|
||||||
|
WHERE provider='mercadopago' ORDER BY received_at DESC LIMIT 1''').fetchone()
|
||||||
|
refused = c.execute('''SELECT count(*) AS n FROM dtf_local.security_events
|
||||||
|
WHERE event='payment_webhook_rejected' AND created_at > now()-interval '24 hours' ''').fetchone()
|
||||||
|
result['webhooks'] = {'accepted_24h': received['n'], 'refused_24h': refused['n'], 'last': last}
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/tiny/callback')
|
||||||
|
def tiny_callback(code: str = Query('', max_length=4096), state: str = Query('', max_length=128),
|
||||||
|
error: str = Query('', max_length=128)):
|
||||||
|
"""Tiny's redirect back. Cross-site, so the operator cookie is absent: the
|
||||||
|
single-use state an operator created is what authorises it. Tiny reports a
|
||||||
|
refusal (the operator declined, or offline access is not allowed for this
|
||||||
|
application) with `error` instead of a code."""
|
||||||
|
if not tiny.configured():
|
||||||
|
raise HTTPException(503, 'Tiny application is not configured')
|
||||||
|
try:
|
||||||
|
if error == 'invalid_scope':
|
||||||
|
retry = tiny.TinyAuth().without_offline(state)
|
||||||
|
audit('tiny_offline_refused')
|
||||||
|
return RedirectResponse(retry, status_code=303)
|
||||||
|
if error or not code:
|
||||||
|
raise tiny.TinyError(f'Tiny returned {error or "no code"}')
|
||||||
who = tiny.TinyAuth().complete(code, state)
|
who = tiny.TinyAuth().complete(code, state)
|
||||||
except tiny.TinyError:
|
except tiny.TinyError:
|
||||||
audit('tiny_connect_failed')
|
audit('tiny_connect_failed')
|
||||||
@@ -243,6 +625,101 @@ def history(uid: UUID, user=Depends(operator)):
|
|||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
return c.execute('SELECT * FROM dtf_local.movements WHERE order_id=%s ORDER BY id', (uid,)).fetchall()
|
return c.execute('SELECT * FROM dtf_local.movements WHERE order_id=%s ORDER BY id', (uid,)).fetchall()
|
||||||
|
|
||||||
|
@router.get('/api/operator/uploads/{uid}/thumbnail')
|
||||||
|
def thumbnail(uid: UUID, user=Depends(operator)):
|
||||||
|
"""The browser-made picture of an artwork. Cached by the operator's browser:
|
||||||
|
the board is redrawn on every refresh."""
|
||||||
|
with db.connect() as c:
|
||||||
|
row = c.execute('SELECT mime,data FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (uid,)).fetchone()
|
||||||
|
if not row:
|
||||||
|
raise HTTPException(404, 'No picture for this upload')
|
||||||
|
return Response(bytes(row['data']), media_type=row['mime'],
|
||||||
|
headers={'Cache-Control': 'private, max-age=86400', 'Content-Disposition': 'inline'})
|
||||||
|
|
||||||
|
# Real work only: an operator's test order and a cancelled one are not production.
|
||||||
|
REAL_ORDER = "o.cancelled_at IS NULL AND COALESCE(o.payment->>'provider','') <> 'teste'"
|
||||||
|
LOCAL_DAY = "(o.created_at AT TIME ZONE 'America/Sao_Paulo')::date"
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/stats')
|
||||||
|
def production_stats(days: int = Query(default=30, ge=7, le=365), user=Depends(operator)):
|
||||||
|
"""Production numbers from what the board already records: orders paid in
|
||||||
|
the period, how long each stage holds an order, and what is waiting now."""
|
||||||
|
# From the start of the first local day of the period.
|
||||||
|
since = ("o.created_at >= (((now() AT TIME ZONE 'America/Sao_Paulo')::date - make_interval(days => %s - 1))"
|
||||||
|
" AT TIME ZONE 'America/Sao_Paulo')")
|
||||||
|
metres = "(SELECT COALESCE(sum((i->>'billed_metres')::numeric),0) FROM jsonb_array_elements(o.snapshot->'items') i)"
|
||||||
|
with db.connect() as c:
|
||||||
|
total = c.execute(f'''SELECT count(*) AS orders, COALESCE(sum((o.snapshot->>'total_cents')::bigint),0) AS cents,
|
||||||
|
COALESCE(sum({metres}),0) AS metres,
|
||||||
|
count(*) FILTER (WHERE EXISTS (SELECT 1 FROM dtf_local.movements m WHERE m.order_id=o.id
|
||||||
|
AND m.to_state='cor' AND NOT m.back)) AS corrected
|
||||||
|
FROM dtf_local.orders o WHERE {REAL_ORDER} AND {since}''', (days,)).fetchone()
|
||||||
|
daily = {row['day']: row for row in c.execute(f'''SELECT {LOCAL_DAY} AS day, count(*) AS orders,
|
||||||
|
COALESCE(sum({metres}),0) AS metres FROM dtf_local.orders o WHERE {REAL_ORDER} AND {since}
|
||||||
|
GROUP BY 1''', (days,)).fetchall()}
|
||||||
|
# Each order's stages as spans: from entering a stage to the next move.
|
||||||
|
spans = f'''WITH events AS (
|
||||||
|
SELECT o.id, 'rec' AS state, o.created_at AS at FROM dtf_local.orders o WHERE {REAL_ORDER} AND {since}
|
||||||
|
UNION ALL
|
||||||
|
SELECT m.order_id, m.to_state, m.created_at FROM dtf_local.movements m
|
||||||
|
JOIN dtf_local.orders o ON o.id=m.order_id WHERE {REAL_ORDER} AND {since})
|
||||||
|
SELECT id, state, at, LEAD(at) OVER (PARTITION BY id ORDER BY at) AS until FROM events'''
|
||||||
|
stages = c.execute(f'''SELECT state, count(*) AS orders,
|
||||||
|
percentile_cont(0.5) WITHIN GROUP (ORDER BY extract(epoch FROM until-at)) / 3600 AS median_hours
|
||||||
|
FROM ({spans}) s WHERE until IS NOT NULL GROUP BY state''', (days, days)).fetchall()
|
||||||
|
lead = c.execute(f'''SELECT count(*) AS orders, percentile_cont(0.5) WITHIN GROUP (ORDER BY extract(epoch FROM
|
||||||
|
f.at - o.created_at)) / 3600 AS median_hours FROM dtf_local.orders o
|
||||||
|
JOIN LATERAL (SELECT min(created_at) AS at FROM dtf_local.movements m WHERE m.order_id=o.id AND m.to_state='fin') f
|
||||||
|
ON f.at IS NOT NULL
|
||||||
|
WHERE {REAL_ORDER} AND {since}''', (days,)).fetchone()
|
||||||
|
# What waits now, however old, and since when in its stage.
|
||||||
|
waiting = c.execute(f'''SELECT o.state, count(*) AS orders, max(extract(epoch FROM now() - COALESCE(
|
||||||
|
(SELECT max(m.created_at) FROM dtf_local.movements m WHERE m.order_id=o.id AND m.to_state=o.state),
|
||||||
|
o.created_at))) / 3600 AS oldest_hours
|
||||||
|
FROM dtf_local.orders o WHERE {REAL_ORDER} AND o.state<>'fin' GROUP BY o.state''').fetchall()
|
||||||
|
today = datetime.now(timezone(timedelta(hours=-3))).date()
|
||||||
|
series = []
|
||||||
|
for offset in range(days - 1, -1, -1):
|
||||||
|
day = today - timedelta(days=offset)
|
||||||
|
row = daily.get(day)
|
||||||
|
series.append({'day': day.isoformat(), 'orders': row['orders'] if row else 0,
|
||||||
|
'metres': float(row['metres']) if row else 0.0})
|
||||||
|
hours = lambda value: round(float(value), 1) if value is not None else None
|
||||||
|
return {'days': days, 'orders': total['orders'], 'cents': int(total['cents']), 'metres': float(total['metres']),
|
||||||
|
'corrected': total['corrected'], 'daily': series,
|
||||||
|
'lead': {'orders': lead['orders'], 'median_hours': hours(lead['median_hours'])},
|
||||||
|
'stages': {row['state']: {'orders': row['orders'], 'median_hours': hours(row['median_hours'])} for row in stages},
|
||||||
|
'waiting': {row['state']: {'orders': row['orders'], 'oldest_hours': hours(row['oldest_hours'])} for row in waiting}}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/api/operator/queue/finals')
|
||||||
|
def queue_finals(user=Depends(operator)):
|
||||||
|
"""The approved final files of every order in the print queue, in queue
|
||||||
|
order, each named after its order so a folder of them stays readable. The
|
||||||
|
browser downloads them straight from storage: a zip of multi-GB sheets
|
||||||
|
would go through the server."""
|
||||||
|
with db.connect() as c:
|
||||||
|
rows = c.execute(f'''SELECT o.number,f.item_index,u.object_key,u.name FROM dtf_local.orders o
|
||||||
|
JOIN dtf_local.order_files f ON f.order_id=o.id JOIN dtf_local.uploads u ON u.id=f.upload_id
|
||||||
|
WHERE o.state='fil' AND o.cancelled_at IS NULL AND {FINAL_OK}
|
||||||
|
ORDER BY COALESCE(o.queue_key, extract(epoch FROM o.created_at)),o.id,f.item_index,f.created_at''').fetchall()
|
||||||
|
files, names = [], set()
|
||||||
|
for row in rows:
|
||||||
|
prefix = f"pedido-{row['number']}-item-{row['item_index'] + 1}"
|
||||||
|
name = row['name'] if row['name'].startswith(prefix) else f"{prefix}-{row['name']}"
|
||||||
|
stem, dot, ext = name.rpartition('.')
|
||||||
|
copy = 2
|
||||||
|
while name in names:
|
||||||
|
name = f'{stem} ({copy}).{ext}' if dot else f'{ext} ({copy})'
|
||||||
|
copy += 1
|
||||||
|
names.add(name)
|
||||||
|
files.append({'number': row['number'], 'item': row['item_index'] + 1, 'name': name,
|
||||||
|
'url': storage.download(row['object_key'], name)})
|
||||||
|
audit('queue_finals_downloaded', operator=user, files=len(files))
|
||||||
|
return {'files': files}
|
||||||
|
|
||||||
|
|
||||||
@router.get('/api/operator/uploads/{uid}/download')
|
@router.get('/api/operator/uploads/{uid}/download')
|
||||||
def download(uid: UUID, user=Depends(operator)):
|
def download(uid: UUID, user=Depends(operator)):
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ and an unverified delivery is recorded and refused rather than retried.
|
|||||||
"""
|
"""
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||||
from psycopg.types.json import Jsonb
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
from .. import payments
|
from .. import payment_messages as messages, payments
|
||||||
from ..core import db
|
from ..core import db
|
||||||
from ..core.auth import audit, client_ip, owner, rate_limit
|
from ..core.auth import audit, client_ip, owner, rate_limit
|
||||||
from ..core.models import PaymentIntent
|
from ..core.models import PaymentIntent
|
||||||
@@ -21,6 +22,8 @@ router = APIRouter()
|
|||||||
# Generous: a provider legitimately retries, and a signature check is cheap.
|
# Generous: a provider legitimately retries, and a signature check is cheap.
|
||||||
# This exists so an unsigned flood cannot keep the database busy.
|
# This exists so an unsigned flood cannot keep the database busy.
|
||||||
WEBHOOK_LIMIT = 600
|
WEBHOOK_LIMIT = 600
|
||||||
|
# How long a card waiting for the bank's confirmation holds off a new attempt.
|
||||||
|
CHALLENGE_MINUTES = 10
|
||||||
|
|
||||||
|
|
||||||
@router.post('/api/payments/webhook')
|
@router.post('/api/payments/webhook')
|
||||||
@@ -30,9 +33,20 @@ async def webhook(request: Request):
|
|||||||
|
|
||||||
query = dict(request.query_params)
|
query = dict(request.query_params)
|
||||||
if not payment.verify(request.headers, body, query):
|
if not payment.verify(request.headers, body, query):
|
||||||
audit('payment_webhook_rejected', ip=client_ip(request), reason='signature')
|
# What arrived, never what it carried: which query parameters (Mercado
|
||||||
|
# Pago's current webhook sends data.id/type, its old IPN topic/id and
|
||||||
|
# often no signature), whether the signature and request id headers came,
|
||||||
|
# and how old the signature's timestamp is.
|
||||||
|
audit('payment_webhook_rejected', ip=client_ip(request), reason='signature',
|
||||||
|
params=','.join(sorted(query))[:200], source=query.get('source_news', '')[:20],
|
||||||
|
signed='x-signature' in request.headers, request_id='x-request-id' in request.headers,
|
||||||
|
age=signature_age(request.headers.get('x-signature', '')))
|
||||||
raise HTTPException(403, 'Invalid signature')
|
raise HTTPException(403, 'Invalid signature')
|
||||||
|
|
||||||
|
if getattr(payment, 'last_signature', 'documentado') not in (None, 'documentado') or getattr(payment, 'secret_was_trimmed', False):
|
||||||
|
# Genuine, but not quite as documented: say how, so it can be settled.
|
||||||
|
audit('payment_webhook_signature', variant=payment.last_signature,
|
||||||
|
secret_trimmed=getattr(payment, 'secret_was_trimmed', False))
|
||||||
event = payment.parse(body, query)
|
event = payment.parse(body, query)
|
||||||
if event is None:
|
if event is None:
|
||||||
# Verified, so genuinely from the provider, but not about a payment.
|
# Verified, so genuinely from the provider, but not about a payment.
|
||||||
@@ -58,6 +72,25 @@ def event_provider():
|
|||||||
return payment.name
|
return payment.name
|
||||||
|
|
||||||
|
|
||||||
|
def signature_age(signature):
|
||||||
|
"""Seconds since the signature's ts (ts=<unix>,v1=<hex>), or '' without one."""
|
||||||
|
import time
|
||||||
|
parts = dict(part.strip().split('=', 1) for part in signature.split(',') if '=' in part)
|
||||||
|
ts = parts.get('ts', '')
|
||||||
|
return int(time.time() - int(ts[:10])) if ts[:10].isdigit() else ''
|
||||||
|
|
||||||
|
|
||||||
|
def provider_reason(response):
|
||||||
|
"""A short, loggable reason from a refused provider call."""
|
||||||
|
try:
|
||||||
|
data = response.json()
|
||||||
|
except ValueError:
|
||||||
|
return f'HTTP {response.status_code}'
|
||||||
|
causes = '; '.join(f"{c.get('code')}: {c.get('description')}" for c in data.get('cause') or []
|
||||||
|
if isinstance(c, dict))
|
||||||
|
return (causes or data.get('message') or data.get('error') or f'HTTP {response.status_code}')[:300]
|
||||||
|
|
||||||
|
|
||||||
@router.post('/api/payments/intent')
|
@router.post('/api/payments/intent')
|
||||||
def intent(body: PaymentIntent, session_id=Depends(owner)):
|
def intent(body: PaymentIntent, session_id=Depends(owner)):
|
||||||
"""Start paying an approved quote: a PIX code, or a card token from the
|
"""Start paying an approved quote: a PIX code, or a card token from the
|
||||||
@@ -68,27 +101,71 @@ def intent(body: PaymentIntent, session_id=Depends(owner)):
|
|||||||
try:
|
try:
|
||||||
quote = payments.approved_quote(c, body.quote_id, session_id)
|
quote = payments.approved_quote(c, body.quote_id, session_id)
|
||||||
except payments.PaymentRefused as refusal:
|
except payments.PaymentRefused as refusal:
|
||||||
raise HTTPException(404 if 'not found' in str(refusal) else 409, str(refusal))
|
raise HTTPException(404 if 'not found' in str(refusal) else 409, messages.quote_refusal(refusal))
|
||||||
if c.execute('SELECT 1 FROM dtf_local.orders WHERE quote_id=%s', (body.quote_id,)).fetchone():
|
if c.execute('SELECT 1 FROM dtf_local.orders WHERE quote_id=%s', (body.quote_id,)).fetchone():
|
||||||
raise HTTPException(409, 'Quote is already paid')
|
raise HTTPException(409, 'Este pedido já foi pago.')
|
||||||
|
# Never charge for files that are gone: an unpaid cart's files are
|
||||||
|
# removed after a while. A payment under way keeps them a day longer,
|
||||||
|
# time enough for the provider's notice to become the order.
|
||||||
|
uploads = payments.quote_uploads(quote['approved'])
|
||||||
|
live = c.execute('''SELECT count(*) AS n FROM dtf_local.uploads WHERE id=ANY(%s)
|
||||||
|
AND purged_at IS NULL AND expires_at>now()''', (uploads,)).fetchone()['n']
|
||||||
|
if live != len(set(uploads)):
|
||||||
|
raise HTTPException(410, 'Os arquivos deste pedido expiraram porque ele não foi pago a tempo. '
|
||||||
|
'Monte o pedido de novo para pagar.')
|
||||||
|
payments.hold_uploads(c, uploads, '1 day')
|
||||||
# Never a second charge: an approved payment is waiting for its
|
# Never a second charge: an approved payment is waiting for its
|
||||||
# notification to become the order, and a card in review may still be.
|
# notification to become the order, and a card in review may still be.
|
||||||
|
# A card waiting for the bank's confirmation (3-D Secure) blocks only
|
||||||
|
# for CHALLENGE_MINUTES: a customer who gave up on it must still be able
|
||||||
|
# to pay, and an unanswered challenge is not charged.
|
||||||
if c.execute('''SELECT 1 FROM dtf_local.payment_intents WHERE quote_id=%s
|
if c.execute('''SELECT 1 FROM dtf_local.payment_intents WHERE quote_id=%s
|
||||||
AND (status='approved' OR (method='card' AND status='pending'))''', (body.quote_id,)).fetchone():
|
AND (status='approved' OR (method='card' AND status='pending'
|
||||||
raise HTTPException(409, 'A payment for this quote is already approved or in review')
|
AND NOT (jsonb_typeof(response->'challenge')='object'
|
||||||
|
AND created_at < now() - make_interval(mins => %s))))''',
|
||||||
|
(body.quote_id, CHALLENGE_MINUTES)).fetchone():
|
||||||
|
raise HTTPException(409, 'Já existe um pagamento aprovado ou em análise para este pedido. '
|
||||||
|
'Aguarde a confirmação nesta página.')
|
||||||
|
method = body.method.model_dump()
|
||||||
if body.method.type == 'pix':
|
if body.method.type == 'pix':
|
||||||
existing = c.execute('''SELECT * FROM dtf_local.payment_intents WHERE quote_id=%s AND method='pix'
|
# One open PIX per quote: the same code until it expires, and a new
|
||||||
|
# one only after that, when the old code can no longer be paid.
|
||||||
|
# Serialised per quote, so two clicks never open two codes.
|
||||||
|
c.execute('SELECT pg_advisory_xact_lock(hashtext(%s))', ('pix:'+str(body.quote_id),))
|
||||||
|
existing = c.execute('''SELECT *, COALESCE((response->>'expires_at')::timestamptz <= now(), false) AS expired
|
||||||
|
FROM dtf_local.payment_intents WHERE quote_id=%s AND method='pix'
|
||||||
AND status='pending' ORDER BY created_at DESC LIMIT 1''', (body.quote_id,)).fetchone()
|
AND status='pending' ORDER BY created_at DESC LIMIT 1''', (body.quote_id,)).fetchone()
|
||||||
if existing:
|
if existing and not existing['expired']:
|
||||||
return existing['response']
|
return existing['response']
|
||||||
|
if existing:
|
||||||
|
c.execute("UPDATE dtf_local.payment_intents SET status='expired', updated_at=now() WHERE id=%s",
|
||||||
|
(existing['id'],))
|
||||||
|
method['attempt'] = c.execute('''SELECT count(*) AS n FROM dtf_local.payment_intents
|
||||||
|
WHERE quote_id=%s AND method='pix' ''', (body.quote_id,)).fetchone()['n'] + 1
|
||||||
try:
|
try:
|
||||||
created = payment.create(str(body.quote_id), quote['approved']['total_cents'],
|
created = payment.create(str(body.quote_id), quote['approved']['total_cents'],
|
||||||
quote['approved']['customer'], body.method.model_dump())
|
quote['approved']['customer'], method)
|
||||||
except ValueError as exc:
|
except ValueError:
|
||||||
raise HTTPException(422, str(exc))
|
raise HTTPException(422, 'Esta forma de pagamento não é aceita. Pague com cartão de crédito ou PIX.')
|
||||||
except Exception:
|
except httpx.HTTPStatusError as exc:
|
||||||
audit('payment_intent_failed', quote=str(body.quote_id))
|
# Mercado Pago's own reason (status, message and cause codes) goes to
|
||||||
raise HTTPException(502, 'Payment provider unavailable; try again')
|
# the log; it never contains card data, only what was refused.
|
||||||
|
reason = provider_reason(exc.response)
|
||||||
|
audit('payment_intent_refused', quote=str(body.quote_id), method=body.method.type,
|
||||||
|
status=exc.response.status_code, reason=reason)
|
||||||
|
if exc.response.status_code < 500:
|
||||||
|
try:
|
||||||
|
data = exc.response.json()
|
||||||
|
except ValueError:
|
||||||
|
data = {}
|
||||||
|
raise HTTPException(422, messages.refusal(data))
|
||||||
|
raise HTTPException(502, messages.UNAVAILABLE)
|
||||||
|
except Exception as exc:
|
||||||
|
audit('payment_intent_failed', quote=str(body.quote_id), error=type(exc).__name__)
|
||||||
|
raise HTTPException(502, messages.UNAVAILABLE)
|
||||||
|
if created.get('status') == 'rejected':
|
||||||
|
# What to tell the customer; the code stays in status_detail for the team.
|
||||||
|
created['message'] = messages.rejection(created.get('status_detail'))
|
||||||
c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,
|
c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,
|
||||||
status,amount_cents,response) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)
|
status,amount_cents,response) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)
|
||||||
ON CONFLICT(provider,provider_payment_id) DO NOTHING''',
|
ON CONFLICT(provider,provider_payment_id) DO NOTHING''',
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Quotes: the customer's cart, and the operator-reviewed version of it."""
|
"""Quotes: the customer's cart, approved at once when it can be (app/quote_review.py)."""
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@@ -7,10 +7,14 @@ from uuid import UUID, uuid4
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from psycopg.types.json import Jsonb
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from .. import payments
|
||||||
from ..core import db
|
from ..core import db
|
||||||
from ..core.auth import owner
|
from ..core.auth import owner
|
||||||
from ..core.models import QuoteRequest
|
from ..core.models import QuoteRequest
|
||||||
from ..runtime import freight, quote_view, upload_row
|
from ..core.pricing import price
|
||||||
|
from .. import quote_review
|
||||||
|
from ..grade_check import Files, mismatch
|
||||||
|
from ..runtime import freight, payment, quote_view, require_delivery_available, storage, upload_row
|
||||||
from ..scanning import require_clean
|
from ..scanning import require_clean
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -19,32 +23,58 @@ router = APIRouter()
|
|||||||
def create_quote(body: QuoteRequest, session_id=Depends(owner)):
|
def create_quote(body: QuoteRequest, session_id=Depends(owner)):
|
||||||
for item in body.items:
|
for item in body.items:
|
||||||
if abs(item.metres*100-item.production.height_cm) > Decimal('0.02'):
|
if abs(item.metres*100-item.production.height_cm) > Decimal('0.02'):
|
||||||
raise HTTPException(422, 'Quoted metres do not match the submitted layout height')
|
raise HTTPException(422, 'A metragem não confere com a montagem enviada. Monte o pedido de novo.')
|
||||||
|
require_delivery_available(body.freight.service)
|
||||||
draft = body.model_dump(mode='json', exclude={'request_key'})
|
draft = body.model_dump(mode='json', exclude={'request_key'})
|
||||||
digest = hashlib.sha256(json.dumps(draft, sort_keys=True).encode()).hexdigest()
|
digest = hashlib.sha256(json.dumps(draft, sort_keys=True).encode()).hexdigest()
|
||||||
|
reason = quote_review.review_reason(draft)
|
||||||
|
if reason is not None:
|
||||||
|
# Waits for review: check the delivery now, priced at approval.
|
||||||
try:
|
try:
|
||||||
freight.quote(body.freight.service, body.freight.postal_code)
|
priced = [price(item.mode, str(item.metres), item.grade) for item in body.items]
|
||||||
|
freight.quote(body.freight.service, body.freight.postal_code,
|
||||||
|
sum(Decimal(i['billed_metres']) for i in priced), sum(i['total_cents'] for i in priced))
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise HTTPException(422, str(exc))
|
raise HTTPException(422, str(exc))
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
|
rows = {}
|
||||||
for item in body.items:
|
for item in body.items:
|
||||||
for uid in item.uploads:
|
for uid in item.uploads:
|
||||||
row = upload_row(c, uid, session_id)
|
row = upload_row(c, uid, session_id)
|
||||||
if not row['complete']:
|
if not row['complete']:
|
||||||
raise HTTPException(409, 'Complete every upload before requesting a quote')
|
raise HTTPException(409, 'Espere o envio de todos os arquivos terminar antes de continuar.')
|
||||||
require_clean(row)
|
require_clean(row)
|
||||||
|
rows[str(uid)] = row
|
||||||
|
# The grade sets the price and came from the browser: before a cart is
|
||||||
|
# approved at checkout, the server works it out from the files.
|
||||||
|
note = mismatch(Files(storage), draft['items'], rows) if reason is None else None
|
||||||
|
reason = reason or note
|
||||||
uid = uuid4()
|
uid = uuid4()
|
||||||
c.execute('INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft) VALUES(%s,%s,%s,%s,%s) ON CONFLICT(owner,request_key) DO NOTHING',
|
c.execute('INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,review_note) VALUES(%s,%s,%s,%s,%s,%s) ON CONFLICT(owner,request_key) DO NOTHING',
|
||||||
(uid, session_id, body.request_key, digest, Jsonb(draft)))
|
(uid, session_id, body.request_key, digest, Jsonb(draft), note))
|
||||||
row = c.execute('SELECT * FROM dtf_local.quotes WHERE owner=%s AND request_key=%s', (session_id, body.request_key)).fetchone()
|
row = c.execute('SELECT * FROM dtf_local.quotes WHERE owner=%s AND request_key=%s FOR UPDATE', (session_id, body.request_key)).fetchone()
|
||||||
if row['request_hash'] != digest:
|
if row['request_hash'] != digest:
|
||||||
raise HTTPException(409, 'Request key already used for a different cart')
|
raise HTTPException(409, 'O carrinho mudou durante o envio. Tente de novo.')
|
||||||
return {'id': row['id'], 'status': 'pending_review'}
|
if row['id'] == uid and reason is None:
|
||||||
|
quote_review.approve(c, row, body.items, quote_review.AUTO)
|
||||||
|
return {'id': row['id'], 'status': 'approved'}
|
||||||
|
if row['id'] == uid:
|
||||||
|
# An operator reviews it first; the files wait for that review.
|
||||||
|
payments.hold_uploads(c, payments.quote_uploads(draft), payments.REVIEW_HOLD)
|
||||||
|
return {'id': row['id'], 'status': 'approved' if row['approved'] else 'pending_review'}
|
||||||
|
|
||||||
@router.get('/api/quotes/{uid}')
|
@router.get('/api/quotes/{uid}')
|
||||||
def get_quote(uid: UUID, session_id=Depends(owner)):
|
def get_quote(uid: UUID, session_id=Depends(owner)):
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
row = c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s AND owner=%s', (uid,session_id)).fetchone()
|
row = c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s AND owner=%s', (uid,session_id)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, 'Quote not found')
|
raise HTTPException(404, 'Pedido não encontrado.')
|
||||||
return quote_view(c, row)
|
view = quote_view(c, row)
|
||||||
|
# The payment page asks every few seconds while it waits for the order. An
|
||||||
|
# approved card with no order yet is checked with the provider now, rather
|
||||||
|
# than waiting for its notification.
|
||||||
|
if view['status'] == 'approved' and (view['payment'] or {}).get('status') == 'approved':
|
||||||
|
if payments.reconcile(db.connect, payment, quote_id=uid, limit=3):
|
||||||
|
with db.connect() as c:
|
||||||
|
view = quote_view(c, c.execute('SELECT * FROM dtf_local.quotes WHERE id=%s', (uid,)).fetchone())
|
||||||
|
return view
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ import os
|
|||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||||
|
from starlette.concurrency import run_in_threadpool
|
||||||
|
|
||||||
from ..core import db
|
from ..core import db
|
||||||
from ..core.auth import audit, owner, rate_limit
|
from ..core.auth import audit, owner, rate_limit
|
||||||
from ..core.limits import upload_limit_bytes
|
from ..core.limits import upload_limit_bytes
|
||||||
from ..core.models import UploadStart
|
from ..core.models import UploadStart
|
||||||
|
from ..payments import UNPAID_HOLD
|
||||||
from ..runtime import PART_BYTES, storage, upload_row
|
from ..runtime import PART_BYTES, storage, upload_row
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -21,7 +23,7 @@ router = APIRouter()
|
|||||||
@router.post('/api/uploads')
|
@router.post('/api/uploads')
|
||||||
def begin_upload(body: UploadStart, session_id=Depends(owner)):
|
def begin_upload(body: UploadStart, session_id=Depends(owner)):
|
||||||
if body.size > upload_limit_bytes():
|
if body.size > upload_limit_bytes():
|
||||||
raise HTTPException(413, 'File exceeds the malware scan limit; select a smaller file')
|
raise HTTPException(413, 'O arquivo passa do tamanho máximo aceito. Escolha um arquivo menor.')
|
||||||
uid = uuid4()
|
uid = uuid4()
|
||||||
key = f'originals/{uid}'
|
key = f'originals/{uid}'
|
||||||
rate_limit('upload-start', str(session_id), 60, 900)
|
rate_limit('upload-start', str(session_id), 60, 900)
|
||||||
@@ -37,7 +39,7 @@ def begin_upload(body: UploadStart, session_id=Depends(owner)):
|
|||||||
usage['owned']+body.size > int(os.environ.get('OWNER_UPLOAD_QUOTA_BYTES','10737418240')) or
|
usage['owned']+body.size > int(os.environ.get('OWNER_UPLOAD_QUOTA_BYTES','10737418240')) or
|
||||||
usage['pending'] >= int(os.environ.get('MAX_PENDING_UPLOADS','10'))):
|
usage['pending'] >= int(os.environ.get('MAX_PENDING_UPLOADS','10'))):
|
||||||
audit('upload_quota_rejected')
|
audit('upload_quota_rejected')
|
||||||
raise HTTPException(429, 'Local storage quota or pending upload limit reached')
|
raise HTTPException(429, 'Limite de armazenamento ou de envios pendentes atingido. Tente de novo mais tarde.')
|
||||||
multipart = storage.begin(key)
|
multipart = storage.begin(key)
|
||||||
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,expires_at)
|
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,expires_at)
|
||||||
VALUES(%s,%s,%s,%s,%s,%s,now()+interval '1 hour')''',
|
VALUES(%s,%s,%s,%s,%s,%s,now()+interval '1 hour')''',
|
||||||
@@ -58,7 +60,10 @@ def part_url(uid: UUID, part: int, session_id=Depends(owner)):
|
|||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
row = upload_row(c, uid, session_id)
|
row = upload_row(c, uid, session_id)
|
||||||
if row['complete'] or not 1 <= part <= math.ceil(row['size'] / PART_BYTES):
|
if row['complete'] or not 1 <= part <= math.ceil(row['size'] / PART_BYTES):
|
||||||
raise HTTPException(409, 'Invalid part or completed upload')
|
raise HTTPException(409, 'O envio deste arquivo já terminou ou foi interrompido. Escolha o arquivo de novo.')
|
||||||
|
# The reservation lease is an hour; a multi-GB upload on a slow line
|
||||||
|
# takes longer, so each part it asks for keeps it alive.
|
||||||
|
c.execute("UPDATE dtf_local.uploads SET expires_at=GREATEST(expires_at,now()+interval '1 hour') WHERE id=%s", (uid,))
|
||||||
size = min(PART_BYTES, row['size']-(part-1)*PART_BYTES)
|
size = min(PART_BYTES, row['size']-(part-1)*PART_BYTES)
|
||||||
return {'url': storage.part_url(row['object_key'], row['multipart_id'], part, size)}
|
return {'url': storage.part_url(row['object_key'], row['multipart_id'], part, size)}
|
||||||
|
|
||||||
@@ -79,21 +84,59 @@ def complete_upload(uid: UUID, session_id=Depends(owner)):
|
|||||||
expected = math.ceil(row['size'] / PART_BYTES)
|
expected = math.ceil(row['size'] / PART_BYTES)
|
||||||
if [p['PartNumber'] for p in parts] != list(range(1, expected+1)) or any(
|
if [p['PartNumber'] for p in parts] != list(range(1, expected+1)) or any(
|
||||||
p['Size'] != min(PART_BYTES, row['size'] - i*PART_BYTES) for i,p in enumerate(parts)):
|
p['Size'] != min(PART_BYTES, row['size'] - i*PART_BYTES) for i,p in enumerate(parts)):
|
||||||
raise HTTPException(409, 'Parts are missing or their sizes do not match')
|
raise HTTPException(409, 'O envio do arquivo não chegou inteiro. Escolha o arquivo de novo.')
|
||||||
storage.complete(row['object_key'], row['multipart_id'], parts)
|
storage.complete(row['object_key'], row['multipart_id'], parts)
|
||||||
existing_size = storage.size(row['object_key'])
|
existing_size = storage.size(row['object_key'])
|
||||||
if existing_size != row['size']:
|
if existing_size != row['size']:
|
||||||
raise HTTPException(409, 'Stored size differs from declared size')
|
raise HTTPException(409, 'O envio do arquivo não chegou inteiro. Escolha o arquivo de novo.')
|
||||||
c.execute("UPDATE dtf_local.uploads SET complete=true,expires_at=now()+interval '30 days' WHERE id=%s", (uid,))
|
# Held while the cart is unpaid: an abandoned cart's files go after
|
||||||
|
# UNPAID_HOLD; a paid order keeps them for its 30 days (app/payments.py).
|
||||||
|
c.execute('UPDATE dtf_local.uploads SET complete=true,expires_at=now()+%s::interval WHERE id=%s',
|
||||||
|
(UNPAID_HOLD, uid))
|
||||||
return {'id': uid, 'complete': True}
|
return {'id': uid, 'complete': True}
|
||||||
|
|
||||||
|
# The browser's small picture of the artwork, for the Kanban. Only an image, and
|
||||||
|
# only a small one: the type is read from the bytes, never from the header.
|
||||||
|
THUMBNAIL_BYTES = 300_000
|
||||||
|
|
||||||
|
def thumbnail_type(data):
|
||||||
|
if data[:4] == b'RIFF' and data[8:12] == b'WEBP':
|
||||||
|
return 'image/webp'
|
||||||
|
if data[:3] == b'\xff\xd8\xff':
|
||||||
|
return 'image/jpeg'
|
||||||
|
if data[:8] == b'\x89PNG\r\n\x1a\n':
|
||||||
|
return 'image/png'
|
||||||
|
return None
|
||||||
|
|
||||||
|
def store_thumbnail(uid, session_id, mime, data):
|
||||||
|
rate_limit('upload-thumbnail', str(session_id), 120, 900)
|
||||||
|
with db.connect() as c:
|
||||||
|
upload_row(c, uid, session_id)
|
||||||
|
c.execute('''INSERT INTO dtf_local.upload_thumbnails(upload_id,mime,data) VALUES(%s,%s,%s)
|
||||||
|
ON CONFLICT(upload_id) DO UPDATE SET mime=EXCLUDED.mime, data=EXCLUDED.data, created_at=now()''',
|
||||||
|
(uid, mime, data))
|
||||||
|
|
||||||
|
@router.put('/api/uploads/{uid}/thumbnail')
|
||||||
|
async def put_thumbnail(uid: UUID, request: Request, session_id=Depends(owner)):
|
||||||
|
if int(request.headers.get('content-length') or 0) > THUMBNAIL_BYTES:
|
||||||
|
raise HTTPException(413, 'Thumbnail too large')
|
||||||
|
data = await request.body()
|
||||||
|
if len(data) > THUMBNAIL_BYTES:
|
||||||
|
raise HTTPException(413, 'Thumbnail too large')
|
||||||
|
mime = thumbnail_type(data)
|
||||||
|
if not mime:
|
||||||
|
raise HTTPException(415, 'Thumbnail must be a WebP, JPEG or PNG image')
|
||||||
|
await run_in_threadpool(store_thumbnail, uid, session_id, mime, data)
|
||||||
|
return {'id': uid}
|
||||||
|
|
||||||
@router.delete('/api/uploads/{uid}')
|
@router.delete('/api/uploads/{uid}')
|
||||||
def cancel_upload(uid: UUID, session_id=Depends(owner)):
|
def cancel_upload(uid: UUID, session_id=Depends(owner)):
|
||||||
with db.connect() as c:
|
with db.connect() as c:
|
||||||
row=upload_row(c,uid,session_id,lock=True)
|
row=upload_row(c,uid,session_id,lock=True)
|
||||||
if row['complete']:
|
if row['complete']:
|
||||||
raise HTTPException(409, 'Completed upload cannot be cancelled')
|
raise HTTPException(409, 'Este arquivo já foi enviado e não pode ser cancelado.')
|
||||||
storage.discard(row['object_key'],row['multipart_id'],False)
|
storage.discard(row['object_key'],row['multipart_id'],False)
|
||||||
c.execute('UPDATE dtf_local.uploads SET purged_at=now() WHERE id=%s',(uid,))
|
c.execute('UPDATE dtf_local.uploads SET purged_at=now() WHERE id=%s',(uid,))
|
||||||
|
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=%s',(uid,))
|
||||||
audit('upload_cancelled', upload=str(uid))
|
audit('upload_cancelled', upload=str(uid))
|
||||||
return {'id':uid,'cancelled':True}
|
return {'id':uid,'cancelled':True}
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ async def safe_headers(request, call_next):
|
|||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
if response.status_code in (401,403,429) or response.status_code>=500:
|
if response.status_code in (401,403,429) or response.status_code>=500:
|
||||||
audit('http_security_event', method=request.method, status=response.status_code, ip=client_ip(request))
|
audit('http_security_event', method=request.method, status=response.status_code, ip=client_ip(request))
|
||||||
response.headers['Cache-Control'] = 'no-store'
|
# Nothing is cached unless a route says so (the Kanban's artwork pictures).
|
||||||
|
response.headers.setdefault('Cache-Control', 'no-store')
|
||||||
response.headers['X-Content-Type-Options'] = 'nosniff'
|
response.headers['X-Content-Type-Options'] = 'nosniff'
|
||||||
response.headers['Referrer-Policy'] = 'no-referrer'
|
response.headers['Referrer-Policy'] = 'no-referrer'
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ from uuid import UUID, uuid4
|
|||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
from .printjobs import generated_identity
|
|
||||||
from .runtime import upload_row
|
from .runtime import upload_row
|
||||||
from .scanning import require_clean
|
from .scanning import require_clean
|
||||||
|
|
||||||
@@ -26,38 +25,57 @@ def generated_owner(c, order, ref, kind):
|
|||||||
if not generated:
|
if not generated:
|
||||||
return None
|
return None
|
||||||
if kind != 'final' or generated['item_index'] != ref.item_index:
|
if kind != 'final' or generated['item_index'] != ref.item_index:
|
||||||
raise HTTPException(422, 'A generated print file can only be the final file of its own item')
|
raise HTTPException(422, 'O arquivo gerado só pode ser o final do próprio item.')
|
||||||
if c.execute("SELECT 1 FROM dtf_local.order_files WHERE order_id=%s AND kind='correction' LIMIT 1",
|
if c.execute("SELECT 1 FROM dtf_local.order_files WHERE order_id=%s AND kind='correction' LIMIT 1",
|
||||||
(order['id'],)).fetchone():
|
(order['id'],)).fetchone():
|
||||||
raise HTTPException(409, 'A customer correction replaced the artwork this file was generated from')
|
raise HTTPException(409, 'O cliente enviou uma correção depois que este arquivo foi gerado. Use a correção ou envie outro arquivo.')
|
||||||
return generated_identity(order['id'])
|
# A large sheet's print file is its original, owned by the customer.
|
||||||
|
return c.execute('SELECT owner FROM dtf_local.uploads WHERE id=%s', (ref.upload_id,)).fetchone()['owner']
|
||||||
|
|
||||||
|
|
||||||
|
def correction_owner(c, order, ref, kind):
|
||||||
|
"""The owner of a customer correction approved as it is, or None if it is not one.
|
||||||
|
|
||||||
|
A corrected file is often ready to print: the operator approves it as the
|
||||||
|
final of the item it corrects, without downloading and sending it back.
|
||||||
|
"""
|
||||||
|
if kind != 'final':
|
||||||
|
return None
|
||||||
|
row = c.execute('''SELECT u.owner FROM dtf_local.order_files f JOIN dtf_local.uploads u ON u.id=f.upload_id
|
||||||
|
WHERE f.order_id=%s AND f.upload_id=%s AND f.kind='correction' AND f.active AND f.item_index=%s''',
|
||||||
|
(order['id'], ref.upload_id, ref.item_index)).fetchone()
|
||||||
|
return row and row['owner']
|
||||||
|
|
||||||
|
|
||||||
def submit_files(c, order, body, identity, kind, actor):
|
def submit_files(c, order, body, identity, kind, actor):
|
||||||
if order['version'] != body.version:
|
if order['version'] != body.version:
|
||||||
raise HTTPException(409, 'Order changed. Refresh before submitting files.')
|
raise HTTPException(409, 'O pedido mudou. Atualize a página e envie de novo.')
|
||||||
if kind == 'final' and order['state'] not in ('rec','tra','cor'):
|
if kind == 'final' and order['state'] not in ('rec','tra','cor'):
|
||||||
raise HTTPException(409, 'Final files can only change during artwork review')
|
raise HTTPException(409, 'O arquivo final só pode mudar antes da fila de impressão.')
|
||||||
if kind == 'correction' and order['state'] != 'cor':
|
if kind == 'correction' and order['state'] != 'cor':
|
||||||
raise HTTPException(409, 'This order is not awaiting artwork correction')
|
raise HTTPException(409, 'Este pedido não está esperando correção.')
|
||||||
if len({f.upload_id for f in body.files}) != len(body.files):
|
if len({f.upload_id for f in body.files}) != len(body.files):
|
||||||
raise HTTPException(422, 'Each uploaded file must appear once')
|
raise HTTPException(422, 'O mesmo arquivo foi enviado duas vezes.')
|
||||||
count = len(order['snapshot']['items'])
|
count = len(order['snapshot']['items'])
|
||||||
if any(f.item_index >= count for f in body.files):
|
if any(f.item_index >= count for f in body.files):
|
||||||
raise HTTPException(422, 'Invalid order item')
|
raise HTTPException(422, 'Item do pedido inválido.')
|
||||||
if kind == 'final' and {f.item_index for f in body.files} != set(range(count)):
|
if kind == 'final' and {f.item_index for f in body.files} != set(range(count)):
|
||||||
raise HTTPException(422, 'Final-file set must cover every order item')
|
raise HTTPException(422, 'Envie um arquivo final para cada item do pedido.')
|
||||||
original_ids = [UUID(uid) for item in order['snapshot']['items'] for uid in item['uploads']]
|
original_ids = [UUID(uid) for item in order['snapshot']['items'] for uid in item['uploads']]
|
||||||
first = c.execute('SELECT min(created_at) AS first FROM dtf_local.uploads WHERE id=ANY(%s)', (original_ids,)).fetchone()['first']
|
first = c.execute('SELECT min(created_at) AS first FROM dtf_local.uploads WHERE id=ANY(%s)', (original_ids,)).fetchone()['first']
|
||||||
expiry = first + timedelta(days=30)
|
expiry = first + timedelta(days=30)
|
||||||
if expiry <= datetime.now(timezone.utc):
|
if expiry <= datetime.now(timezone.utc):
|
||||||
raise HTTPException(410, 'Order artwork retention has expired')
|
raise HTTPException(410, 'Os arquivos deste pedido expiraram (30 dias).')
|
||||||
for ref in body.files:
|
for ref in body.files:
|
||||||
upload = upload_row(c, ref.upload_id, generated_owner(c, order, ref, kind) or identity, lock=True)
|
corrected = correction_owner(c, order, ref, kind)
|
||||||
|
upload = upload_row(c, ref.upload_id, corrected or generated_owner(c, order, ref, kind) or identity, lock=True)
|
||||||
if not upload['complete']:
|
if not upload['complete']:
|
||||||
raise HTTPException(409, 'Complete all uploads first')
|
raise HTTPException(409, 'Espere o envio de todos os arquivos terminar.')
|
||||||
require_clean(upload)
|
require_clean(upload)
|
||||||
if c.execute('SELECT id FROM dtf_local.order_files WHERE upload_id=%s', (ref.upload_id,)).fetchone():
|
# Attached once, except a correction approved as its own final.
|
||||||
raise HTTPException(409, 'File is already attached. Upload a new revision.')
|
if c.execute('SELECT id FROM dtf_local.order_files WHERE upload_id=%s'+(" AND kind<>'correction'" if corrected else ''),
|
||||||
|
(ref.upload_id,)).fetchone():
|
||||||
|
raise HTTPException(409, 'Este arquivo já está no pedido. Envie uma nova versão.')
|
||||||
# A new customer correction supersedes every final prepared from earlier
|
# A new customer correction supersedes every final prepared from earlier
|
||||||
# artwork, including finals uploaded while this order was in correction.
|
# artwork, including finals uploaded while this order was in correction.
|
||||||
if kind == 'correction':
|
if kind == 'correction':
|
||||||
@@ -67,9 +85,13 @@ def submit_files(c, order, body, identity, kind, actor):
|
|||||||
for ref in body.files:
|
for ref in body.files:
|
||||||
c.execute('INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) VALUES(%s,%s,%s,%s,%s,%s,%s)',
|
c.execute('INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) VALUES(%s,%s,%s,%s,%s,%s,%s)',
|
||||||
(uuid4(),order['id'],ref.upload_id,ref.item_index,kind,body.note,actor))
|
(uuid4(),order['id'],ref.upload_id,ref.item_index,kind,body.note,actor))
|
||||||
c.execute('UPDATE dtf_local.uploads SET expires_at=LEAST(expires_at,%s) WHERE id=%s', (expiry,ref.upload_id))
|
# The order's window, whatever the upload's own hold was.
|
||||||
|
c.execute('UPDATE dtf_local.uploads SET expires_at=%s WHERE id=%s', (expiry,ref.upload_id))
|
||||||
c.execute('UPDATE dtf_local.orders SET version=version+1,updated_at=now() WHERE id=%s', (order['id'],))
|
c.execute('UPDATE dtf_local.orders SET version=version+1,updated_at=now() WHERE id=%s', (order['id'],))
|
||||||
if kind == 'final':
|
if kind == 'final':
|
||||||
# Artwork approval, not commercial quote approval, starts original cleanup.
|
# Artwork approval, not commercial quote approval, starts original cleanup.
|
||||||
c.execute("UPDATE dtf_local.uploads SET expires_at=LEAST(expires_at,now()+interval '7 days') WHERE id=ANY(%s)", (original_ids,))
|
# An original approved as its own print file is kept as the final.
|
||||||
|
finals = [ref.upload_id for ref in body.files]
|
||||||
|
c.execute("UPDATE dtf_local.uploads SET expires_at=LEAST(expires_at,now()+interval '7 days') WHERE id=ANY(%s) AND NOT id=ANY(%s)",
|
||||||
|
(original_ids, finals))
|
||||||
return {'ok': True, 'version': order['version']+1, 'expires_at': expiry}
|
return {'ok': True, 'version': order['version']+1, 'expires_at': expiry}
|
||||||
|
|||||||
100
app/cnpj.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
"""The company name behind a customer's CNPJ, from the public registry.
|
||||||
|
|
||||||
|
The Site asks for e-mail, CNPJ and WhatsApp only, so the Kanban showed an
|
||||||
|
e-mail where a name belongs, and a new Tiny contact was named after it. The
|
||||||
|
Receita's CNPJ registry is public, and BrasilAPI serves it without a key.
|
||||||
|
Names are looked up by the worker, never while a customer waits, and kept in
|
||||||
|
dtf_local.cnpj_names; a lookup that fails is tried again a day later.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
API = os.environ.get('CNPJ_LOOKUP_URL', 'https://brasilapi.com.br/api/cnpj/v1/')
|
||||||
|
# A few per minute of the worker's time is plenty: customers repeat.
|
||||||
|
PER_RUN = 5
|
||||||
|
|
||||||
|
|
||||||
|
def digits(value):
|
||||||
|
return ''.join(ch for ch in str(value or '') if ch.isdigit())
|
||||||
|
|
||||||
|
|
||||||
|
def fetch(cnpj, transport=None):
|
||||||
|
"""The registry's names for one CNPJ. An unknown CNPJ has none, and is not asked again."""
|
||||||
|
with httpx.Client(timeout=8, transport=transport) as http:
|
||||||
|
response = http.get(API + cnpj, headers={'Accept': 'application/json', 'User-Agent': 'dtf-sistema'})
|
||||||
|
if response.status_code == 404:
|
||||||
|
return {'legal_name': None, 'trade_name': None}
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
clean = lambda value: ' '.join(str(value).split()) or None if value else None
|
||||||
|
return {'legal_name': clean(data.get('razao_social')), 'trade_name': clean(data.get('nome_fantasia'))}
|
||||||
|
|
||||||
|
|
||||||
|
def pending(c, limit):
|
||||||
|
"""CNPJs of orders and recent quotes never looked up, or whose lookup failed a day ago."""
|
||||||
|
return [row['cnpj'] for row in c.execute('''WITH seen AS (
|
||||||
|
SELECT regexp_replace(snapshot->'customer'->>'cnpj', '\\D', '', 'g') AS cnpj, created_at FROM dtf_local.orders
|
||||||
|
UNION ALL
|
||||||
|
SELECT regexp_replace(draft->'customer'->>'cnpj', '\\D', '', 'g'), created_at FROM dtf_local.quotes
|
||||||
|
WHERE created_at > now() - interval '3 days')
|
||||||
|
SELECT s.cnpj FROM seen s LEFT JOIN dtf_local.cnpj_names n ON n.cnpj=s.cnpj
|
||||||
|
WHERE length(s.cnpj)=14 AND (n.cnpj IS NULL OR (n.fetched_at IS NULL AND n.failed_at < now() - interval '1 day'))
|
||||||
|
GROUP BY s.cnpj ORDER BY max(s.created_at) DESC LIMIT %s''', (limit,)).fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
def store(c, cnpj, found):
|
||||||
|
c.execute('''INSERT INTO dtf_local.cnpj_names(cnpj,legal_name,trade_name,fetched_at) VALUES(%s,%s,%s,now())
|
||||||
|
ON CONFLICT (cnpj) DO UPDATE SET legal_name=EXCLUDED.legal_name, trade_name=EXCLUDED.trade_name,
|
||||||
|
fetched_at=now(), failed_at=NULL''', (cnpj, found['legal_name'], found['trade_name']))
|
||||||
|
|
||||||
|
|
||||||
|
def failed(c, cnpj):
|
||||||
|
c.execute('''INSERT INTO dtf_local.cnpj_names(cnpj,failed_at) VALUES(%s,now())
|
||||||
|
ON CONFLICT (cnpj) DO UPDATE SET failed_at=now()''', (cnpj,))
|
||||||
|
|
||||||
|
|
||||||
|
def resolve(connect, limit=PER_RUN, transport=None):
|
||||||
|
"""Look up the next few CNPJs; returns how many were found."""
|
||||||
|
with connect() as c:
|
||||||
|
cnpjs = pending(c, limit)
|
||||||
|
found = 0
|
||||||
|
for cnpj in cnpjs:
|
||||||
|
try:
|
||||||
|
names = fetch(cnpj, transport)
|
||||||
|
except (httpx.HTTPError, ValueError):
|
||||||
|
with connect() as c:
|
||||||
|
failed(c, cnpj)
|
||||||
|
continue
|
||||||
|
with connect() as c:
|
||||||
|
store(c, cnpj, names)
|
||||||
|
found += 1
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def names(c, cnpjs):
|
||||||
|
"""{cnpj: {'legal': ..., 'trade': ...}} for the CNPJs already found."""
|
||||||
|
wanted = sorted({digits(value) for value in cnpjs} - {''})
|
||||||
|
if not wanted:
|
||||||
|
return {}
|
||||||
|
rows = c.execute('''SELECT cnpj,legal_name,trade_name FROM dtf_local.cnpj_names
|
||||||
|
WHERE cnpj=ANY(%s) AND (legal_name IS NOT NULL OR trade_name IS NOT NULL)''', (wanted,)).fetchall()
|
||||||
|
return {row['cnpj']: {'legal': row['legal_name'], 'trade': row['trade_name']} for row in rows}
|
||||||
|
|
||||||
|
|
||||||
|
def company(cnpj, connect=None, transport=None):
|
||||||
|
"""One CNPJ's names for a Tiny contact: stored, or looked up now. None if unavailable."""
|
||||||
|
if connect is None:
|
||||||
|
from .core.db import connect
|
||||||
|
cnpj = digits(cnpj)
|
||||||
|
with connect() as c:
|
||||||
|
known = names(c, [cnpj]).get(cnpj)
|
||||||
|
if known:
|
||||||
|
return known
|
||||||
|
try:
|
||||||
|
found = fetch(cnpj, transport)
|
||||||
|
except (httpx.HTTPError, ValueError):
|
||||||
|
return None
|
||||||
|
with connect() as c:
|
||||||
|
store(c, cnpj, found)
|
||||||
|
return {'legal': found['legal_name'], 'trade': found['trade_name']} if any(found.values()) else None
|
||||||
@@ -45,7 +45,7 @@ def session_row(request):
|
|||||||
try:
|
try:
|
||||||
sid = UUID(request.cookies.get('dtf_session', ''))
|
sid = UUID(request.cookies.get('dtf_session', ''))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise HTTPException(401, 'Start a local session first')
|
raise HTTPException(401, 'Sessão não iniciada. Recarregue a página.')
|
||||||
with connect() as c:
|
with connect() as c:
|
||||||
row = c.execute('SELECT * FROM dtf_local.sessions WHERE id=%s AND expires_at>now()', (sid,)).fetchone()
|
row = c.execute('SELECT * FROM dtf_local.sessions WHERE id=%s AND expires_at>now()', (sid,)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
@@ -92,7 +92,7 @@ def rate_limit(scope, identity, limit, seconds=900):
|
|||||||
RETURNING attempts""", (key,seconds,seconds)).fetchone()
|
RETURNING attempts""", (key,seconds,seconds)).fetchone()
|
||||||
if row['attempts'] > limit:
|
if row['attempts'] > limit:
|
||||||
audit('rate_limit', scope=scope)
|
audit('rate_limit', scope=scope)
|
||||||
raise HTTPException(429, 'Too many requests. Try again later.', headers={'Retry-After':str(seconds)})
|
raise HTTPException(429, 'Muitas tentativas. Tente de novo mais tarde.', headers={'Retry-After':str(seconds)})
|
||||||
|
|
||||||
ACCOUNT_FAILURES = 10
|
ACCOUNT_FAILURES = 10
|
||||||
|
|
||||||
@@ -108,7 +108,7 @@ def throttle(email, request):
|
|||||||
WHERE key=%s AND started_at >= now()-interval '900 seconds'""", (key,)).fetchone()
|
WHERE key=%s AND started_at >= now()-interval '900 seconds'""", (key,)).fetchone()
|
||||||
if row and row['attempts'] >= ACCOUNT_FAILURES:
|
if row and row['attempts'] >= ACCOUNT_FAILURES:
|
||||||
audit('rate_limit', scope='auth-account')
|
audit('rate_limit', scope='auth-account')
|
||||||
raise HTTPException(429, 'Too many requests. Try again later.', headers={'Retry-After': '900'})
|
raise HTTPException(429, 'Muitas tentativas. Tente de novo mais tarde.', headers={'Retry-After': '900'})
|
||||||
|
|
||||||
def login_failed(email):
|
def login_failed(email):
|
||||||
"""Count a failed sign-in (or registration attempt) against the account."""
|
"""Count a failed sign-in (or registration attempt) against the account."""
|
||||||
@@ -117,10 +117,10 @@ def login_failed(email):
|
|||||||
def operator(request: Request):
|
def operator(request: Request):
|
||||||
token = request.cookies.get('dtf_operator', '')
|
token = request.cookies.get('dtf_operator', '')
|
||||||
if not token or len(token)>128:
|
if not token or len(token)>128:
|
||||||
raise HTTPException(401, 'Sign in to the local Kanban')
|
raise HTTPException(401, 'Entre no Kanban.')
|
||||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||||
with connect() as c:
|
with connect() as c:
|
||||||
row = c.execute('SELECT username FROM dtf_local.operator_sessions WHERE token_hash=%s AND expires_at>now()', (digest,)).fetchone()
|
row = c.execute('SELECT username FROM dtf_local.operator_sessions WHERE token_hash=%s AND expires_at>now()', (digest,)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(401, 'Operator session expired')
|
raise HTTPException(401, 'Sua sessão expirou. Entre de novo.')
|
||||||
return row['username']
|
return row['username']
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
"""Limits shared by upload admission and the malware scanner."""
|
"""Limits shared by upload admission and the malware scanner."""
|
||||||
import os
|
import os
|
||||||
|
|
||||||
CLAMAV_STREAM_MAX_BYTES = 128 * 1024 * 1024 # infra/clamd.conf
|
CLAMAV_STREAM_MAX_BYTES = 2000 * 1024 * 1024 # infra/clamd.conf StreamMaxLength
|
||||||
|
|
||||||
|
|
||||||
def scan_limit_bytes():
|
def scan_limit_bytes():
|
||||||
return min(CLAMAV_STREAM_MAX_BYTES, int(os.environ.get('SCAN_MAX_BYTES', '134217728')))
|
"""The largest file ClamAV scans; above it the format check releases it."""
|
||||||
|
return min(CLAMAV_STREAM_MAX_BYTES, int(os.environ.get('SCAN_MAX_BYTES', str(CLAMAV_STREAM_MAX_BYTES))))
|
||||||
|
|
||||||
|
|
||||||
def upload_limit_bytes():
|
def upload_limit_bytes():
|
||||||
transport = int(os.environ.get('MAX_UPLOAD_BYTES', '5368709120'))
|
return int(os.environ.get('MAX_UPLOAD_BYTES', '5368709120'))
|
||||||
return min(transport, scan_limit_bytes())
|
|
||||||
|
|||||||
@@ -41,9 +41,15 @@ class Customer(StrictModel):
|
|||||||
return value.strip()
|
return value.strip()
|
||||||
|
|
||||||
class Freight(StrictModel):
|
class Freight(StrictModel):
|
||||||
service: Literal['pickup','mock-standard'] = 'pickup'
|
service: Literal['pickup','mock-standard','jadlog'] = 'pickup'
|
||||||
postal_code: str = Field(default='', max_length=8)
|
postal_code: str = Field(default='', max_length=8)
|
||||||
|
|
||||||
|
class FreightEstimate(Freight):
|
||||||
|
"""The cart's estimate. The charged freight is quoted again by the server
|
||||||
|
from the approved items, never from these numbers."""
|
||||||
|
metres: Decimal | None = Field(default=None, gt=0, le=12000)
|
||||||
|
declared_cents: int = Field(default=0, ge=0, le=100_000_000, strict=True)
|
||||||
|
|
||||||
UF = Literal['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB',
|
UF = Literal['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB',
|
||||||
'PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO']
|
'PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO']
|
||||||
|
|
||||||
@@ -186,11 +192,20 @@ class PaymentMethod(StrictModel):
|
|||||||
payment_method_id: str | None = Field(default=None, max_length=40, pattern=r'^[a-z_]+$')
|
payment_method_id: str | None = Field(default=None, max_length=40, pattern=r'^[a-z_]+$')
|
||||||
installments: int = Field(default=1, ge=1, le=12, strict=True)
|
installments: int = Field(default=1, ge=1, le=12, strict=True)
|
||||||
issuer_id: str | None = Field(default=None, max_length=40)
|
issuer_id: str | None = Field(default=None, max_length=40)
|
||||||
|
# The cardholder's document from the card form: for a card, the payer is
|
||||||
|
# the cardholder, not necessarily the company on the invoice.
|
||||||
|
payer_document_type: Literal['CPF', 'CNPJ'] | None = None
|
||||||
|
payer_document: str | None = Field(default=None, pattern=r'^[0-9]{11,14}$')
|
||||||
|
|
||||||
@model_validator(mode='after')
|
@model_validator(mode='after')
|
||||||
def card_needs_token(self):
|
def card_needs_token(self):
|
||||||
if self.type == 'card' and not (self.token and self.payment_method_id):
|
if self.type == 'card' and not (self.token and self.payment_method_id):
|
||||||
raise ValueError('Card payment requires the provider token and method')
|
raise ValueError('Card payment requires the provider token and method')
|
||||||
|
# The store takes credit cards and PIX only. Mercado Pago's debit
|
||||||
|
# methods are debvisa, debmaster, debelo...; the adapter still knows
|
||||||
|
# how to pay one, should debit be offered again.
|
||||||
|
if self.type == 'card' and self.payment_method_id.startswith('deb'):
|
||||||
|
raise ValueError('Só aceitamos cartão de crédito ou PIX.')
|
||||||
return self
|
return self
|
||||||
|
|
||||||
class PaymentIntent(StrictModel):
|
class PaymentIntent(StrictModel):
|
||||||
@@ -209,6 +224,35 @@ class Register(StrictModel):
|
|||||||
customer: Customer
|
customer: Customer
|
||||||
password: str = Field(min_length=12, max_length=128)
|
password: str = Field(min_length=12, max_length=128)
|
||||||
|
|
||||||
|
class ProfileUpdate(StrictModel):
|
||||||
|
"""What a customer may change about their account; the CNPJ is not among it."""
|
||||||
|
zap: str
|
||||||
|
address: Destination | None = None
|
||||||
|
|
||||||
|
@field_validator('zap')
|
||||||
|
@classmethod
|
||||||
|
def phone_valid(cls, value):
|
||||||
|
digits = re.sub(r'\D', '', value)
|
||||||
|
if len(digits) not in (10,11) or not digits.isascii():
|
||||||
|
raise ValueError('Invalid phone')
|
||||||
|
return digits
|
||||||
|
|
||||||
|
class EmailChange(StrictModel):
|
||||||
|
email: str = Field(max_length=254)
|
||||||
|
password: str = Field(min_length=1, max_length=128)
|
||||||
|
|
||||||
|
@field_validator('email')
|
||||||
|
@classmethod
|
||||||
|
def email_valid(cls, value):
|
||||||
|
value = value.strip().lower()
|
||||||
|
if not re.fullmatch(r'[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}', value):
|
||||||
|
raise ValueError('Invalid email')
|
||||||
|
return value
|
||||||
|
|
||||||
|
class PasswordChange(StrictModel):
|
||||||
|
current: str = Field(min_length=1, max_length=128)
|
||||||
|
new: str = Field(min_length=12, max_length=128)
|
||||||
|
|
||||||
class Login(StrictModel):
|
class Login(StrictModel):
|
||||||
email: str = Field(min_length=3, max_length=254)
|
email: str = Field(min_length=3, max_length=254)
|
||||||
password: str = Field(min_length=1, max_length=128)
|
password: str = Field(min_length=1, max_length=128)
|
||||||
|
|||||||
189
app/grade_check.py
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
"""The grade (the resolution discount) recomputed from the uploaded files.
|
||||||
|
|
||||||
|
The Site grades the art in the customer's browser and sends the grade with the
|
||||||
|
cart, and the price depends on it. A customer who edits the page could claim a
|
||||||
|
better grade, so before a cart is approved at checkout the server reads the
|
||||||
|
files it already has and works the grade out again, by the Site's own rules:
|
||||||
|
|
||||||
|
- a finished sheet: its pixel width over the sheet width is its DPI, and the
|
||||||
|
item's grade comes from the worst sheet (web/site-quality.js);
|
||||||
|
- artworks: each one's DPI along the width it is printed at, rotation
|
||||||
|
included; the item's grade is the average of the artworks' grades;
|
||||||
|
- a PDF: the area-weighted DPI of the images drawn on its page, 300 when it
|
||||||
|
is only vectors (web/site-pdf.js, dpiDasImagens and resumoDpi).
|
||||||
|
|
||||||
|
Images are measured from their first bytes, never decoded. A PDF is opened
|
||||||
|
only up to PDF_MAX_BYTES, the size the Site itself analyses. A grade that
|
||||||
|
cannot be recomputed is None; the caller decides what that means.
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import struct
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
DPI_IDEAL = 300
|
||||||
|
# The Site analyses PDFs up to 150 MB (GRANDE_BYTES); above that it does not
|
||||||
|
# grade them, so neither does this.
|
||||||
|
PDF_MAX_BYTES = 150 * 1048576
|
||||||
|
HEAD_BYTES = 1048576
|
||||||
|
# Rounding in the browser and here can differ by a point on the same file.
|
||||||
|
TOLERANCE = 2
|
||||||
|
|
||||||
|
|
||||||
|
def js_round(value):
|
||||||
|
"""Math.round, which rounds halves up; Python's round() does not."""
|
||||||
|
return math.floor(value + 0.5)
|
||||||
|
|
||||||
|
|
||||||
|
def grade_of(dpi):
|
||||||
|
return max(6, min(100, js_round(dpi / DPI_IDEAL * 100)))
|
||||||
|
|
||||||
|
|
||||||
|
def image_size(head):
|
||||||
|
"""(width, height) in pixels from a PNG, JPEG or WebP's first bytes."""
|
||||||
|
if head[:8] == b'\x89PNG\r\n\x1a\n' and head[12:16] == b'IHDR':
|
||||||
|
return struct.unpack('>II', head[16:24])
|
||||||
|
if head[:2] == b'\xff\xd8':
|
||||||
|
i = 2
|
||||||
|
while i + 9 < len(head):
|
||||||
|
if head[i] != 0xFF:
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
marker = head[i + 1]
|
||||||
|
if marker in (0xD8, 0x01) or 0xD0 <= marker <= 0xD7 or marker == 0xFF:
|
||||||
|
i += 1 if marker == 0xFF else 2
|
||||||
|
continue
|
||||||
|
length = struct.unpack('>H', head[i + 2:i + 4])[0]
|
||||||
|
if 0xC0 <= marker <= 0xCF and marker not in (0xC4, 0xC8, 0xCC):
|
||||||
|
h, w = struct.unpack('>HH', head[i + 5:i + 9])
|
||||||
|
return w, h
|
||||||
|
i += 2 + length
|
||||||
|
return None
|
||||||
|
if head[:4] == b'RIFF' and head[8:12] == b'WEBP':
|
||||||
|
chunk = head[12:16]
|
||||||
|
if chunk == b'VP8X':
|
||||||
|
return (int.from_bytes(head[24:27], 'little') + 1, int.from_bytes(head[27:30], 'little') + 1)
|
||||||
|
if chunk == b'VP8L' and head[20] == 0x2F:
|
||||||
|
bits = int.from_bytes(head[21:25], 'little')
|
||||||
|
return (bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1
|
||||||
|
if chunk == b'VP8 ' and head[23:26] == b'\x9d\x01\x2a':
|
||||||
|
w, h = struct.unpack('<HH', head[26:30])
|
||||||
|
return w & 0x3FFF, h & 0x3FFF
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _multiply(a, b):
|
||||||
|
return [a[0] * b[0] + a[2] * b[1], a[1] * b[0] + a[3] * b[1],
|
||||||
|
a[0] * b[2] + a[2] * b[3], a[1] * b[2] + a[3] * b[3],
|
||||||
|
a[0] * b[4] + a[2] * b[5] + a[4], a[1] * b[4] + a[3] * b[5] + a[5]]
|
||||||
|
|
||||||
|
|
||||||
|
def pdf_dpi(path):
|
||||||
|
"""The page's area-weighted image DPI, 300 for vectors only, None if the
|
||||||
|
file is not a one-page PDF this can read."""
|
||||||
|
import pikepdf
|
||||||
|
try:
|
||||||
|
with pikepdf.open(path) as pdf:
|
||||||
|
if len(pdf.pages) != 1:
|
||||||
|
return None
|
||||||
|
page = pdf.pages[0]
|
||||||
|
unit = float(page.obj.get('/UserUnit', 1))
|
||||||
|
found = []
|
||||||
|
|
||||||
|
def walk(resources, instructions, ctm, depth):
|
||||||
|
stack = []
|
||||||
|
xobjects = resources.get('/XObject', {}) if resources is not None else {}
|
||||||
|
for operands, operator in instructions:
|
||||||
|
op = str(operator)
|
||||||
|
if op == 'q':
|
||||||
|
stack.append(ctm)
|
||||||
|
elif op == 'Q':
|
||||||
|
ctm = stack.pop() if stack else [1, 0, 0, 1, 0, 0]
|
||||||
|
elif op == 'cm':
|
||||||
|
ctm = _multiply(ctm, [float(v) for v in operands])
|
||||||
|
elif op == 'Do' and operands:
|
||||||
|
xobject = xobjects.get(str(operands[0]))
|
||||||
|
if xobject is None:
|
||||||
|
continue
|
||||||
|
subtype = xobject.get('/Subtype')
|
||||||
|
if subtype == '/Image':
|
||||||
|
width_pt = math.hypot(ctm[0], ctm[1])
|
||||||
|
if width_pt >= 1:
|
||||||
|
found.append((int(xobject.get('/Width', 0)) / (width_pt * unit / 72),
|
||||||
|
abs(ctm[0] * ctm[3] - ctm[1] * ctm[2])))
|
||||||
|
elif subtype == '/Form' and depth < 8:
|
||||||
|
matrix = [float(v) for v in xobject.get('/Matrix', [1, 0, 0, 1, 0, 0])]
|
||||||
|
walk(xobject.get('/Resources', resources),
|
||||||
|
pikepdf.parse_content_stream(xobject), _multiply(ctm, matrix), depth + 1)
|
||||||
|
|
||||||
|
walk(page.obj.get('/Resources'), pikepdf.parse_content_stream(page), [1, 0, 0, 1, 0, 0], 0)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
found = [(dpi, area) for dpi, area in found if dpi > 0]
|
||||||
|
if not found:
|
||||||
|
return DPI_IDEAL
|
||||||
|
total = sum(area for _, area in found) or 1
|
||||||
|
return js_round(sum(dpi * area for dpi, area in found) / total)
|
||||||
|
|
||||||
|
|
||||||
|
class Files:
|
||||||
|
"""The uploaded files, read from storage as little as needed."""
|
||||||
|
|
||||||
|
def __init__(self, storage):
|
||||||
|
self.storage = storage
|
||||||
|
|
||||||
|
def head(self, row):
|
||||||
|
return self.storage.client.get_object(Bucket=self.storage.bucket, Key=row['object_key'],
|
||||||
|
Range=f'bytes=0-{HEAD_BYTES - 1}')['Body'].read()
|
||||||
|
|
||||||
|
def pdf_dpi(self, row):
|
||||||
|
if row['size'] > PDF_MAX_BYTES:
|
||||||
|
return None
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||||
|
self.storage.client.download_fileobj(self.storage.bucket, row['object_key'], handle)
|
||||||
|
handle.flush()
|
||||||
|
return pdf_dpi(handle.name)
|
||||||
|
|
||||||
|
|
||||||
|
def source_dpi(files, row, source):
|
||||||
|
"""One source's DPI across the width it is printed at, or None."""
|
||||||
|
name = row['name'].lower()
|
||||||
|
width_in = float(source['width_cm']) / 2.54
|
||||||
|
if name.endswith('.pdf'):
|
||||||
|
return files.pdf_dpi(row) if source['kind'] == 'sheet' else None
|
||||||
|
if not name.endswith(('.png', '.jpg', '.jpeg', '.webp')):
|
||||||
|
return None
|
||||||
|
size = image_size(files.head(row))
|
||||||
|
if not size or not all(size):
|
||||||
|
return None
|
||||||
|
across = size[1] if source['rotation_degrees'] in (90, 270) else size[0]
|
||||||
|
return across / width_in
|
||||||
|
|
||||||
|
|
||||||
|
def item_grade(files, item, rows):
|
||||||
|
"""The grade the Site gives this item, recomputed; None if it cannot be."""
|
||||||
|
sources = item['production']['sources']
|
||||||
|
dpis = []
|
||||||
|
for source in sources:
|
||||||
|
row = rows.get(str(source['upload_id']))
|
||||||
|
dpi = source_dpi(files, row, source) if row else None
|
||||||
|
if dpi is None:
|
||||||
|
return None
|
||||||
|
dpis.append(dpi)
|
||||||
|
if all(source['kind'] == 'sheet' for source in sources):
|
||||||
|
# A sheet's DPI is shown and compared as a whole number.
|
||||||
|
return grade_of(min(js_round(d) for d in dpis))
|
||||||
|
return js_round(sum(grade_of(d) for d in dpis) / len(dpis))
|
||||||
|
|
||||||
|
|
||||||
|
def mismatch(files, items, rows):
|
||||||
|
"""Why a cart's grades cannot be approved at checkout, or None."""
|
||||||
|
for item in items:
|
||||||
|
if item['grade'] == 0:
|
||||||
|
continue # full price: nothing to verify
|
||||||
|
verified = item_grade(files, item, rows)
|
||||||
|
if verified is None:
|
||||||
|
return 'Nota não conferida no servidor'
|
||||||
|
if item['grade'] > verified + TOLERANCE:
|
||||||
|
return f"Nota {item['grade']} maior que a do arquivo ({verified})"
|
||||||
|
return None
|
||||||
167
app/jadlog.py
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
"""Jadlog freight quotes (Embarcador API, "Simulador de Frete").
|
||||||
|
|
||||||
|
Written from Jadlog's API manual v2.3 (August 2025) and exercised only
|
||||||
|
against a fake HTTP transport until app.jadlog_probe has run on the client's
|
||||||
|
account. A quote is read-only: it creates no shipment and costs nothing, so
|
||||||
|
unlike Tiny it can be tried on the real account as often as needed.
|
||||||
|
|
||||||
|
The client's contract: origin CEP 14402-310, service .PACKAGE (modalidade 3),
|
||||||
|
home delivery. Jadlog prices by weight in kg and expects the larger of the
|
||||||
|
real and the cubed weight. The package weight comes from the client (a base
|
||||||
|
plus a weight per billed metre) and has no default: FREIGHT_ADAPTER=jadlog
|
||||||
|
refuses to start without it, so a guessed weight never prices a customer's
|
||||||
|
freight. The cubed weight is the box's volume times the contract's factor
|
||||||
|
(JADLOG_CAIXA_CM, JADLOG_CUBAGEM_KG_M3); set both or neither, and with neither
|
||||||
|
only the real weight is sent. A box holds JADLOG_METROS_POR_CAIXA metres of
|
||||||
|
film; a longer order ships in as many boxes as it needs, each with its own
|
||||||
|
packaging weight and volume.
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from decimal import ROUND_HALF_UP, Decimal
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
QUOTE_URL = 'https://www.jadlog.com.br/embarcador/api/frete/valor'
|
||||||
|
PACKAGE = 3
|
||||||
|
ORIGIN = '14402310'
|
||||||
|
SERVICE = 'jadlog'
|
||||||
|
WEIGHT_SETTINGS = ('JADLOG_PESO_BASE_KG', 'JADLOG_PESO_POR_METRO_KG')
|
||||||
|
GRAMS = Decimal('0.001')
|
||||||
|
|
||||||
|
|
||||||
|
def box_cm(text):
|
||||||
|
"""The box's three sides in cm from "56x14x14" ("56 × 14 × 14", "56,5x14x14")."""
|
||||||
|
sides = [Decimal(side.strip().replace(',', '.')) for side in re.split(r'[x×*]', text.lower())]
|
||||||
|
if len(sides) != 3 or any(side <= 0 for side in sides):
|
||||||
|
raise RuntimeError('JADLOG_CAIXA_CM takes three sides in cm, such as 56x14x14')
|
||||||
|
return sides
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def digits(value):
|
||||||
|
return ''.join(ch for ch in str(value or '') if ch.isdigit())
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogQuotes:
|
||||||
|
def __init__(self, token=None, cnpj=None, conta=None, contrato=None, origin=None,
|
||||||
|
modalidade=None, transport=None):
|
||||||
|
self.token = (token or os.environ.get('JADLOG_TOKEN', '')).strip()
|
||||||
|
self.cnpj = digits(cnpj or os.environ.get('JADLOG_CNPJ'))
|
||||||
|
# Sent as configured: Jadlog issues it as 000000-0 and documents six
|
||||||
|
# characters, so which form it accepts is only known on the account.
|
||||||
|
self.conta = (conta if conta is not None else os.environ.get('JADLOG_CONTA', '')).strip()
|
||||||
|
# Only when Jadlog issued one; the manual says to send null otherwise.
|
||||||
|
self.contrato = (contrato if contrato is not None else os.environ.get('JADLOG_CONTRATO', '')).strip() or None
|
||||||
|
self.origin = digits(origin or os.environ.get('JADLOG_ORIGEM_CEP') or ORIGIN)
|
||||||
|
self.modalidade = int(modalidade or os.environ.get('JADLOG_MODALIDADE') or PACKAGE)
|
||||||
|
if not self.token or len(self.cnpj) != 14:
|
||||||
|
raise RuntimeError('Jadlog needs JADLOG_TOKEN and a 14-digit JADLOG_CNPJ')
|
||||||
|
self.http = httpx.Client(timeout=20, transport=transport)
|
||||||
|
|
||||||
|
def item(self, postal_code, weight_kg, declared_cents):
|
||||||
|
return {'cepori': self.origin, 'cepdes': digits(postal_code), 'frap': 'N',
|
||||||
|
'peso': float(weight_kg), 'cnpj': self.cnpj, 'conta': self.conta,
|
||||||
|
'contrato': self.contrato, 'modalidade': self.modalidade,
|
||||||
|
'tpentrega': 'D', 'tpseguro': 'N',
|
||||||
|
'vldeclarado': declared_cents / 100, 'vlcoleta': 0}
|
||||||
|
|
||||||
|
def quote(self, postal_code, weight_kg, declared_cents):
|
||||||
|
"""Price in centavos and delivery days for one package to one CEP."""
|
||||||
|
response = self.http.post(QUOTE_URL, json={'frete': [self.item(postal_code, weight_kg, declared_cents)]},
|
||||||
|
headers={'Authorization': self.token})
|
||||||
|
if response.status_code == 401:
|
||||||
|
raise JadlogError('Jadlog refused the token (HTTP 401)')
|
||||||
|
try:
|
||||||
|
data = response.json()
|
||||||
|
except ValueError:
|
||||||
|
raise JadlogError(f'HTTP {response.status_code}: the response is not JSON') from None
|
||||||
|
items = data.get('frete') or []
|
||||||
|
# The manual names the group "erro" in its tables and "error" in its
|
||||||
|
# examples, at the top level and per item.
|
||||||
|
problem = data.get('error') or data.get('erro')
|
||||||
|
if not problem and items:
|
||||||
|
problem = items[0].get('erro') or items[0].get('error')
|
||||||
|
if problem:
|
||||||
|
raise JadlogError(problem.get('descricao') or str(problem) if isinstance(problem, dict) else str(problem))
|
||||||
|
if response.status_code >= 400 or not items or items[0].get('vltotal') is None:
|
||||||
|
raise JadlogError(f'HTTP {response.status_code}: no freight value in the response')
|
||||||
|
total = Decimal(str(items[0]['vltotal']))
|
||||||
|
return {'total_cents': int((total * 100).quantize(Decimal('1'), ROUND_HALF_UP)),
|
||||||
|
'days': items[0].get('prazo'), 'raw': items[0]}
|
||||||
|
|
||||||
|
|
||||||
|
def required_settings():
|
||||||
|
return ('JADLOG_TOKEN', 'JADLOG_CNPJ') + WEIGHT_SETTINGS
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogFreight:
|
||||||
|
"""The Site's delivery option: Jadlog's price for the order's package."""
|
||||||
|
name = 'jadlog'
|
||||||
|
|
||||||
|
def __init__(self, quotes=None):
|
||||||
|
self.quotes = quotes or JadlogQuotes()
|
||||||
|
self.base_kg = Decimal(os.environ['JADLOG_PESO_BASE_KG'])
|
||||||
|
self.per_metre_kg = Decimal(os.environ['JADLOG_PESO_POR_METRO_KG'])
|
||||||
|
# Jadlog's time counts from collection; the order is printed first.
|
||||||
|
self.production_days = int(os.environ.get('FREIGHT_PRODUCTION_DAYS') or 0)
|
||||||
|
if self.base_kg < 0 or self.per_metre_kg <= 0 or self.production_days < 0:
|
||||||
|
raise RuntimeError('Jadlog package weight and production days must be positive')
|
||||||
|
# A box without its factor (or a factor without a box) would price by a guess.
|
||||||
|
box = (os.environ.get('JADLOG_CAIXA_CM') or '').strip()
|
||||||
|
factor = (os.environ.get('JADLOG_CUBAGEM_KG_M3') or '').strip()
|
||||||
|
if bool(box) != bool(factor):
|
||||||
|
raise RuntimeError('Set JADLOG_CAIXA_CM and JADLOG_CUBAGEM_KG_M3 together, or neither')
|
||||||
|
self.box_cm = box_cm(box) if box else None
|
||||||
|
self.cubage_kg_m3 = Decimal(factor.replace(',', '.')) if factor else None
|
||||||
|
if self.cubage_kg_m3 is not None and self.cubage_kg_m3 <= 0:
|
||||||
|
raise RuntimeError('JADLOG_CUBAGEM_KG_M3 must be positive')
|
||||||
|
per_box = (os.environ.get('JADLOG_METROS_POR_CAIXA') or '').strip()
|
||||||
|
self.metres_per_box = Decimal(per_box.replace(',', '.')) if per_box else None
|
||||||
|
if self.metres_per_box is not None and self.metres_per_box <= 0:
|
||||||
|
raise RuntimeError('JADLOG_METROS_POR_CAIXA must be positive')
|
||||||
|
|
||||||
|
def boxes(self, metres=None):
|
||||||
|
"""How many boxes the order fills; one when the capacity is not set."""
|
||||||
|
if metres is None or self.metres_per_box is None:
|
||||||
|
return 1
|
||||||
|
return max(1, math.ceil(Decimal(str(metres)) / self.metres_per_box))
|
||||||
|
|
||||||
|
def real_kg(self, metres):
|
||||||
|
return (self.boxes(metres) * self.base_kg + self.per_metre_kg * Decimal(str(metres))).quantize(GRAMS)
|
||||||
|
|
||||||
|
def cubed_kg(self, metres=None):
|
||||||
|
"""The boxes' volume in m³ times the contract's factor (one box without a
|
||||||
|
length); None without a box."""
|
||||||
|
if not self.box_cm:
|
||||||
|
return None
|
||||||
|
a, b, c = self.box_cm
|
||||||
|
return (self.boxes(metres) * a * b * c / Decimal(1_000_000) * self.cubage_kg_m3).quantize(GRAMS)
|
||||||
|
|
||||||
|
def weight_kg(self, metres):
|
||||||
|
"""What Jadlog charges by: the larger of the real and the cubed weight."""
|
||||||
|
real, cubed = self.real_kg(metres), self.cubed_kg(metres)
|
||||||
|
return max(real, cubed) if cubed is not None else real
|
||||||
|
|
||||||
|
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': ''}
|
||||||
|
if service != SERVICE or len(postal_code) != 8 or not postal_code.isascii() or not postal_code.isdigit():
|
||||||
|
raise ValueError('Select pickup or Jadlog delivery with an eight-digit CEP')
|
||||||
|
if metres is None or Decimal(str(metres)) <= 0:
|
||||||
|
raise ValueError('The order length is required to quote freight')
|
||||||
|
weight, real = self.weight_kg(metres), self.real_kg(metres)
|
||||||
|
try:
|
||||||
|
result = self.quotes.quote(postal_code, weight, int(declared_cents))
|
||||||
|
except (JadlogError, httpx.HTTPError) as error:
|
||||||
|
raise ValueError(f'Não foi possível cotar o frete na Jadlog agora: {error}') from None
|
||||||
|
days = result['days']
|
||||||
|
return {'provider': self.name, 'service': SERVICE, 'postal_code': postal_code,
|
||||||
|
'total_cents': result['total_cents'], 'weight_kg': str(weight), 'real_kg': str(real),
|
||||||
|
'boxes': self.boxes(metres),
|
||||||
|
'days': None if days is None else int(days) + self.production_days,
|
||||||
|
'description': 'Jadlog .PACKAGE'}
|
||||||
80
app/jadlog_probe.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
"""Read-only price check against the client's real Jadlog account, run by hand.
|
||||||
|
A quote creates no shipment and costs nothing.
|
||||||
|
|
||||||
|
python -m app.jadlog_probe [--cep 01310100 ...] [--peso 0.5 ...] [--valor 100]
|
||||||
|
|
||||||
|
Without --cep and --peso it prices a few test weights to a few regions. It
|
||||||
|
answers whether the token, CNPJ and account are accepted, whether a contract
|
||||||
|
number is required (Jadlog names it in the error), and what the contract
|
||||||
|
prices and delivery times are. 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 JADLOG_TOKEN, JADLOG_CNPJ and JADLOG_CONTA in the stack:
|
||||||
|
|
||||||
|
python -m app.jadlog_probe
|
||||||
|
|
||||||
|
Locally, with the credentials in jadlog.env (ignored by git):
|
||||||
|
|
||||||
|
docker compose -f compose.local.yaml -f compose.providers.yaml run --rm --no-deps --build \\
|
||||||
|
--env-from-file jadlog.env worker python -m app.jadlog_probe
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from .core.secrets import load as load_secret_files
|
||||||
|
from .jadlog import JadlogError, JadlogQuotes
|
||||||
|
|
||||||
|
DESTINATIONS = {'14402310': 'Franca (origem)', '01310100': 'São Paulo', '20040002': 'Rio de Janeiro',
|
||||||
|
'80010000': 'Curitiba', '50030230': 'Recife', '69005010': 'Manaus'}
|
||||||
|
WEIGHTS = ['0.3', '0.5', '1', '2', '5']
|
||||||
|
|
||||||
|
|
||||||
|
def money(cents):
|
||||||
|
return f'R$ {cents / 100:.2f}'.replace('.', ',')
|
||||||
|
|
||||||
|
|
||||||
|
def run(quotes, ceps, weights, declared_cents, out):
|
||||||
|
header = quotes.token
|
||||||
|
for weight in weights:
|
||||||
|
for cep in ceps:
|
||||||
|
try:
|
||||||
|
result = quotes.quote(cep, Decimal(weight), declared_cents)
|
||||||
|
except JadlogError as error:
|
||||||
|
# The manual shows the header as the bare token; some accounts
|
||||||
|
# are issued one that expects the Bearer scheme.
|
||||||
|
if '401' in str(error) and not header.lower().startswith('bearer '):
|
||||||
|
quotes.token = 'Bearer ' + header
|
||||||
|
header = quotes.token
|
||||||
|
try:
|
||||||
|
result = quotes.quote(cep, Decimal(weight), declared_cents)
|
||||||
|
except JadlogError as retry:
|
||||||
|
out(f'ERRO {cep} {weight} kg: {retry} (also with "Bearer ")')
|
||||||
|
return 1
|
||||||
|
out('Note: the token only works with the "Bearer " prefix.')
|
||||||
|
else:
|
||||||
|
out(f'ERRO {cep} {weight} kg: {error}')
|
||||||
|
return 1
|
||||||
|
days = result['days']
|
||||||
|
out(f"{weight} kg\t{cep}\t{DESTINATIONS.get(cep, '')}\t{money(result['total_cents'])}\t"
|
||||||
|
f"{days if days is not None else '?'} dia(s)")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m app.jadlog_probe')
|
||||||
|
parser.add_argument('--cep', action='append', help='destination CEP (repeatable)')
|
||||||
|
parser.add_argument('--peso', action='append', help='weight in kg (repeatable)')
|
||||||
|
parser.add_argument('--valor', default='100', help='declared value in reais (default 100)')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
load_secret_files()
|
||||||
|
quotes = JadlogQuotes()
|
||||||
|
declared = int(Decimal(args.valor) * 100)
|
||||||
|
print(f'Origem {quotes.origin}, modalidade {quotes.modalidade}, contrato {quotes.contrato or "-"}, '
|
||||||
|
f'valor declarado {money(declared)}')
|
||||||
|
return run(quotes, args.cep or list(DESTINATIONS), args.peso or WEIGHTS, declared, print)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -21,6 +21,7 @@ import hmac
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
from typing import Mapping
|
from typing import Mapping
|
||||||
|
|
||||||
@@ -35,6 +36,9 @@ MAX_SIGNATURE_AGE = 30 * 60
|
|||||||
STATUSES = {'approved': 'approved', 'pending': 'pending', 'in_process': 'pending',
|
STATUSES = {'approved': 'approved', 'pending': 'pending', 'in_process': 'pending',
|
||||||
'authorized': 'pending', 'in_mediation': 'pending', 'rejected': 'rejected',
|
'authorized': 'pending', 'in_mediation': 'pending', 'rejected': 'rejected',
|
||||||
'cancelled': 'cancelled', 'refunded': 'refunded', 'charged_back': 'refunded'}
|
'cancelled': 'cancelled', 'refunded': 'refunded', 'charged_back': 'refunded'}
|
||||||
|
# A PIX code stops working after this; Mercado Pago then cancels the payment.
|
||||||
|
PIX_MINUTES = 30
|
||||||
|
BRASILIA = timezone(timedelta(hours=-3))
|
||||||
|
|
||||||
|
|
||||||
class MercadoPagoPayment:
|
class MercadoPagoPayment:
|
||||||
@@ -43,7 +47,13 @@ class MercadoPagoPayment:
|
|||||||
def __init__(self, access_token=None, webhook_secret=None, notification_url=None,
|
def __init__(self, access_token=None, webhook_secret=None, notification_url=None,
|
||||||
transport=None, clock=time.time):
|
transport=None, clock=time.time):
|
||||||
self.access_token = access_token or os.environ.get('MP_ACCESS_TOKEN', '')
|
self.access_token = access_token or os.environ.get('MP_ACCESS_TOKEN', '')
|
||||||
self.webhook_secret = (webhook_secret or os.environ.get('MP_WEBHOOK_SECRET', '')).encode()
|
# Trimmed: a space or line break pasted with the secret into the stack
|
||||||
|
# changes every HMAC, and Mercado Pago's own secret has none.
|
||||||
|
raw_secret = webhook_secret or os.environ.get('MP_WEBHOOK_SECRET', '')
|
||||||
|
self.webhook_secret = raw_secret.strip().encode()
|
||||||
|
self.secret_was_trimmed = raw_secret != raw_secret.strip()
|
||||||
|
# Which signed text matched the last notification verified (see verify).
|
||||||
|
self.last_signature = None
|
||||||
self.notification_url = notification_url or os.environ.get('MP_NOTIFICATION_URL', '')
|
self.notification_url = notification_url or os.environ.get('MP_NOTIFICATION_URL', '')
|
||||||
if not self.access_token or not self.webhook_secret:
|
if not self.access_token or not self.webhook_secret:
|
||||||
raise RuntimeError('Mercado Pago needs MP_ACCESS_TOKEN and MP_WEBHOOK_SECRET')
|
raise RuntimeError('Mercado Pago needs MP_ACCESS_TOKEN and MP_WEBHOOK_SECRET')
|
||||||
@@ -70,31 +80,51 @@ class MercadoPagoPayment:
|
|||||||
'identification': {'type': 'CNPJ', 'number': customer['cnpj']}}}
|
'identification': {'type': 'CNPJ', 'number': customer['cnpj']}}}
|
||||||
if self.notification_url:
|
if self.notification_url:
|
||||||
body['notification_url'] = self.notification_url
|
body['notification_url'] = self.notification_url
|
||||||
|
expires_at = None
|
||||||
if method['type'] == 'pix':
|
if method['type'] == 'pix':
|
||||||
body['payment_method_id'] = 'pix'
|
body['payment_method_id'] = 'pix'
|
||||||
|
expires_at = (datetime.now(BRASILIA) + timedelta(minutes=PIX_MINUTES)).isoformat(timespec='milliseconds')
|
||||||
|
body['date_of_expiration'] = expires_at
|
||||||
elif method['type'] == 'card':
|
elif method['type'] == 'card':
|
||||||
body.update(token=method['token'], payment_method_id=method['payment_method_id'],
|
body.update(token=method['token'], payment_method_id=method['payment_method_id'],
|
||||||
installments=int(method.get('installments', 1)))
|
installments=int(method.get('installments', 1)))
|
||||||
if method.get('issuer_id'):
|
# 3-D Secure (the bank's confirmation) only for debit, which needs
|
||||||
body['issuer_id'] = method['issuer_id']
|
# it; asking it of every card was refused as a rule (10113).
|
||||||
|
if method['payment_method_id'].startswith('deb'):
|
||||||
|
body['three_d_secure_mode'] = 'optional'
|
||||||
|
if method.get('payer_document_type') and method.get('payer_document'):
|
||||||
|
body['payer']['identification'] = {'type': method['payer_document_type'],
|
||||||
|
'number': method['payer_document']}
|
||||||
|
# No issuer_id: Mercado Pago takes the issuer from the card number.
|
||||||
|
# The form's suggestion was refused for its own test cards
|
||||||
|
# (10111, "the issuer does not have the BIN configured").
|
||||||
else:
|
else:
|
||||||
raise ValueError('Unsupported payment method')
|
raise ValueError('Unsupported payment method')
|
||||||
# A PIX retry must return the same code. A card retry after a decline
|
# A PIX retry must return the same code; a new one, after the last
|
||||||
# is a new attempt with a new token, so the token is part of the key;
|
# expired, is the next attempt. A card retry after a decline is a new
|
||||||
# the intent route refuses new attempts once one is approved or in review.
|
# attempt with a new token, so the token is part of the key; the intent
|
||||||
key = f'dtf-quote-{quote_id}-pix' if method['type'] == 'pix' else \
|
# route refuses new attempts once one is approved or in review.
|
||||||
|
key = f"dtf-quote-{quote_id}-pix-{int(method.get('attempt', 1))}" if method['type'] == 'pix' else \
|
||||||
f"dtf-quote-{quote_id}-card-{hashlib.sha256(method['token'].encode()).hexdigest()[:24]}"
|
f"dtf-quote-{quote_id}-card-{hashlib.sha256(method['token'].encode()).hexdigest()[:24]}"
|
||||||
response = self.http.post('/v1/payments', json=body, headers={'X-Idempotency-Key': key})
|
response = self.http.post('/v1/payments', json=body, headers={'X-Idempotency-Key': key})
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
payment = response.json()
|
payment = response.json()
|
||||||
transaction = (payment.get('point_of_interaction') or {}).get('transaction_data') or {}
|
transaction = (payment.get('point_of_interaction') or {}).get('transaction_data') or {}
|
||||||
|
# A payment waiting for 3-D Secure carries the bank's challenge page,
|
||||||
|
# which the Site shows in a frame by posting `creq` to that address.
|
||||||
|
three_ds = payment.get('three_ds_info') or {}
|
||||||
|
challenge = ({'url': three_ds['external_resource_url'], 'creq': three_ds['creq']}
|
||||||
|
if payment.get('status_detail') == 'pending_challenge'
|
||||||
|
and three_ds.get('external_resource_url') and three_ds.get('creq') else None)
|
||||||
return {'provider': self.name, 'id': str(payment['id']),
|
return {'provider': self.name, 'id': str(payment['id']),
|
||||||
'status': STATUSES.get(payment.get('status'), 'pending'),
|
'status': STATUSES.get(payment.get('status'), 'pending'),
|
||||||
'status_detail': payment.get('status_detail'),
|
'status_detail': payment.get('status_detail'),
|
||||||
'total_cents': total_cents,
|
'total_cents': total_cents,
|
||||||
'pix_qr_code': transaction.get('qr_code'),
|
'pix_qr_code': transaction.get('qr_code'),
|
||||||
'pix_qr_code_base64': transaction.get('qr_code_base64'),
|
'pix_qr_code_base64': transaction.get('qr_code_base64'),
|
||||||
'ticket_url': transaction.get('ticket_url')}
|
'ticket_url': transaction.get('ticket_url'),
|
||||||
|
'expires_at': payment.get('date_of_expiration') or expires_at,
|
||||||
|
'challenge': challenge}
|
||||||
|
|
||||||
def lookup(self, payment_id: str) -> dict:
|
def lookup(self, payment_id: str) -> dict:
|
||||||
response = self.http.get(f'/v1/payments/{payment_id}')
|
response = self.http.get(f'/v1/payments/{payment_id}')
|
||||||
@@ -104,6 +134,12 @@ class MercadoPagoPayment:
|
|||||||
# Webhooks ------------------------------------------------------------
|
# Webhooks ------------------------------------------------------------
|
||||||
|
|
||||||
def verify(self, headers: Mapping[str, str], body: bytes, query: Mapping[str, str] | None = None) -> bool:
|
def verify(self, headers: Mapping[str, str], body: bytes, query: Mapping[str, str] | None = None) -> bool:
|
||||||
|
"""The notification is Mercado Pago's when its x-signature is the HMAC of
|
||||||
|
the documented text (id, request-id, ts) under our secret. A few close
|
||||||
|
variants of that text are also tried, each still needing the secret, so
|
||||||
|
a format that differs from the documentation is accepted and named in
|
||||||
|
self.last_signature rather than silently refused."""
|
||||||
|
self.last_signature = None
|
||||||
signature = headers.get('x-signature') or ''
|
signature = headers.get('x-signature') or ''
|
||||||
parts = dict(part.strip().split('=', 1) for part in signature.split(',') if '=' in part)
|
parts = dict(part.strip().split('=', 1) for part in signature.split(',') if '=' in part)
|
||||||
ts, supplied = parts.get('ts', ''), parts.get('v1', '')
|
ts, supplied = parts.get('ts', ''), parts.get('v1', '')
|
||||||
@@ -111,18 +147,28 @@ class MercadoPagoPayment:
|
|||||||
return False
|
return False
|
||||||
if abs(self.clock() - int(ts[:10])) > MAX_SIGNATURE_AGE:
|
if abs(self.clock() - int(ts[:10])) > MAX_SIGNATURE_AGE:
|
||||||
return False
|
return False
|
||||||
data_id = (query or {}).get('data.id', '')
|
|
||||||
if data_id.isalnum():
|
|
||||||
data_id = data_id.lower()
|
|
||||||
manifest = ''
|
|
||||||
if data_id:
|
|
||||||
manifest += f'id:{data_id};'
|
|
||||||
request_id = headers.get('x-request-id') or ''
|
request_id = headers.get('x-request-id') or ''
|
||||||
if request_id:
|
query_id = (query or {}).get('data.id', '')
|
||||||
|
try:
|
||||||
|
body_id = str(((json.loads(body) or {}).get('data') or {}).get('id') or '')
|
||||||
|
except (ValueError, AttributeError):
|
||||||
|
body_id = ''
|
||||||
|
|
||||||
|
def text(data_id, with_request=True):
|
||||||
|
manifest = f'id:{data_id};' if data_id else ''
|
||||||
|
if with_request and request_id:
|
||||||
manifest += f'request-id:{request_id};'
|
manifest += f'request-id:{request_id};'
|
||||||
manifest += f'ts:{ts};'
|
return manifest + f'ts:{ts};'
|
||||||
|
|
||||||
|
lowered = query_id.lower() if query_id.isalnum() else query_id
|
||||||
|
variants = [('documentado', text(lowered)), ('sem request-id', text(lowered, False)),
|
||||||
|
('id sem minúsculas', text(query_id)), ('id do corpo', text(body_id))]
|
||||||
|
for name, manifest in variants:
|
||||||
expected = hmac.new(self.webhook_secret, manifest.encode(), hashlib.sha256).hexdigest()
|
expected = hmac.new(self.webhook_secret, manifest.encode(), hashlib.sha256).hexdigest()
|
||||||
return hmac.compare_digest(expected, supplied.lower())
|
if hmac.compare_digest(expected, supplied.lower()):
|
||||||
|
self.last_signature = name
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def parse(self, body: bytes, query: Mapping[str, str] | None = None) -> PaymentEvent | None:
|
def parse(self, body: bytes, query: Mapping[str, str] | None = None) -> PaymentEvent | None:
|
||||||
try:
|
try:
|
||||||
@@ -134,7 +180,15 @@ class MercadoPagoPayment:
|
|||||||
payment_id = str((query or {}).get('data.id') or (data.get('data') or {}).get('id') or '')
|
payment_id = str((query or {}).get('data.id') or (data.get('data') or {}).get('id') or '')
|
||||||
if not payment_id.isdigit():
|
if not payment_id.isdigit():
|
||||||
return None
|
return None
|
||||||
|
try:
|
||||||
payment = self.lookup(payment_id)
|
payment = self.lookup(payment_id)
|
||||||
|
except httpx.HTTPStatusError as error:
|
||||||
|
# Signed by Mercado Pago but about no payment of ours, such as the
|
||||||
|
# panel's "Simular notificação". Anything else is raised so that a
|
||||||
|
# real notification is retried.
|
||||||
|
if error.response.status_code == 404:
|
||||||
|
return None
|
||||||
|
raise
|
||||||
return event_from_payment(payment, notification_id=str(data.get('id', '')))
|
return event_from_payment(payment, notification_id=str(data.get('id', '')))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
65
app/mercadopago_probe.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
"""Read-only look at the Mercado Pago account behind MP_ACCESS_TOKEN.
|
||||||
|
|
||||||
|
python -m app.mercadopago_probe [--bin 503143] [--valor 50]
|
||||||
|
|
||||||
|
Run from the api container's console (Portainer > Containers > api >
|
||||||
|
Console), or with "Verificar conta" on the Kanban's Integrations tab. It creates nothing and charges nothing: it asks Mercado Pago which
|
||||||
|
account the token belongs to, which card methods the account accepts, and how
|
||||||
|
it classifies a card number's first digits (type, issuer, instalments). That
|
||||||
|
is what payment errors such as 10111 (issuer) and 10113 (method excluded by a
|
||||||
|
rule) depend on.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from .core.secrets import load as load_secret_files
|
||||||
|
|
||||||
|
API = 'https://api.mercadopago.com'
|
||||||
|
|
||||||
|
|
||||||
|
def check(token, bin_='548083', amount='50', transport=None):
|
||||||
|
"""The account, its card methods and how a card's first digits are read."""
|
||||||
|
http = httpx.Client(base_url=API, timeout=15, transport=transport,
|
||||||
|
headers={'Authorization': f'Bearer {token}'})
|
||||||
|
result = {'token': 'test' if token.startswith('TEST-') else 'production'}
|
||||||
|
me = http.get('/users/me')
|
||||||
|
if me.status_code == 200:
|
||||||
|
user = me.json()
|
||||||
|
tags = user.get('tags') or []
|
||||||
|
result['account'] = {'id': user.get('id'), 'nickname': user.get('nickname'),
|
||||||
|
'site': user.get('site_id'), 'test_user': 'test_user' in tags, 'tags': tags}
|
||||||
|
else:
|
||||||
|
result['account'] = {'error': f'HTTP {me.status_code}'}
|
||||||
|
methods = http.get('/v1/payment_methods')
|
||||||
|
result['cards'] = ([{'id': m.get('id'), 'type': m.get('payment_type_id'), 'status': m.get('status')}
|
||||||
|
for m in methods.json() if m.get('payment_type_id') in ('credit_card', 'debit_card')]
|
||||||
|
if methods.status_code == 200 else {'error': f'HTTP {methods.status_code}'})
|
||||||
|
options = http.get('/v1/payment_methods/installments', params={'bin': bin_, 'amount': amount})
|
||||||
|
result['bin'] = ([{'method': o.get('payment_method_id'), 'type': o.get('payment_type_id'),
|
||||||
|
'issuer': (o.get('issuer') or {}).get('name'),
|
||||||
|
'installments': [c.get('installments') for c in o.get('payer_costs') or []]}
|
||||||
|
for o in options.json()]
|
||||||
|
if options.status_code == 200 else {'error': f'HTTP {options.status_code} {options.text[:200]}'})
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m app.mercadopago_probe')
|
||||||
|
parser.add_argument('--bin', default='548083', help="the card's first six digits")
|
||||||
|
parser.add_argument('--valor', default='50', help='amount in reais for the instalment lookup')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
load_secret_files()
|
||||||
|
token = os.environ.get('MP_ACCESS_TOKEN', '')
|
||||||
|
if not token:
|
||||||
|
print('MP_ACCESS_TOKEN is not set in this container.')
|
||||||
|
return 1
|
||||||
|
import json
|
||||||
|
print(json.dumps(check(token, args.bin, args.valor), indent=2, ensure_ascii=False))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
94
app/payment_messages.py
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
"""What to tell a customer when a payment does not go through.
|
||||||
|
|
||||||
|
Mercado Pago answers with codes: a refused card has a status_detail
|
||||||
|
("cc_rejected_insufficient_amount"), a request it cannot process has cause
|
||||||
|
codes (324, "invalid identification number"). Shown as they come, they read
|
||||||
|
like a crash. Each becomes one plain sentence that says what happened and
|
||||||
|
what the customer can do; the code itself goes to the log and, for the team,
|
||||||
|
stays on the Kanban.
|
||||||
|
"""
|
||||||
|
|
||||||
|
TRY_OTHER = 'Use outro cartão ou pague com PIX.'
|
||||||
|
|
||||||
|
# Mercado Pago's status_detail for a card payment it refused.
|
||||||
|
REJECTIONS = {
|
||||||
|
'cc_rejected_bad_filled_card_number': 'O número do cartão está incorreto. Confira e tente de novo.',
|
||||||
|
'cc_rejected_bad_filled_date': 'A data de validade do cartão está incorreta. Confira e tente de novo.',
|
||||||
|
'cc_rejected_bad_filled_security_code': 'O código de segurança (CVV) está incorreto. Confira e tente de novo.',
|
||||||
|
'cc_rejected_bad_filled_other': 'Algum dado do cartão está incorreto. Confira o nome, o CPF e a validade e tente de novo.',
|
||||||
|
'cc_rejected_insufficient_amount': 'O cartão não tem limite suficiente para este valor. ' + TRY_OTHER,
|
||||||
|
'cc_amount_rate_limit_exceeded': 'O cartão atingiu o limite de valor permitido. ' + TRY_OTHER,
|
||||||
|
'cc_rejected_call_for_authorize': 'O banco precisa autorizar este pagamento. Ligue para o banco emissor e '
|
||||||
|
'tente de novo, ou use outro cartão.',
|
||||||
|
'cc_rejected_card_disabled': 'O cartão está bloqueado ou ainda não foi ativado. Fale com o banco emissor '
|
||||||
|
'ou use outro cartão.',
|
||||||
|
'cc_rejected_invalid_installments': 'O cartão não aceita esse número de parcelas. Escolha outro número de parcelas.',
|
||||||
|
'cc_rejected_duplicated_payment': 'Um pagamento igual acabou de ser feito com este cartão. Se precisar pagar '
|
||||||
|
'de novo, use outro cartão ou PIX.',
|
||||||
|
'cc_rejected_max_attempts': 'O cartão atingiu o limite de tentativas. ' + TRY_OTHER,
|
||||||
|
'cc_rejected_high_risk': 'O pagamento foi recusado pela análise de segurança. ' + TRY_OTHER,
|
||||||
|
'rejected_high_risk': 'O pagamento foi recusado pela análise de segurança. ' + TRY_OTHER,
|
||||||
|
'cc_rejected_blacklist': 'Não foi possível pagar com este cartão. ' + TRY_OTHER,
|
||||||
|
'cc_rejected_card_type_not_allowed': 'Este tipo de cartão não é aceito. Use um cartão de crédito ou pague com PIX.',
|
||||||
|
'cc_rejected_3ds_challenge': 'O banco não confirmou o pagamento. Tente de novo ou use outro cartão.',
|
||||||
|
'cc_rejected_3ds_mandatory': 'O banco não confirmou o pagamento. Tente de novo ou use outro cartão.',
|
||||||
|
'cc_rejected_card_error': 'Não foi possível processar o cartão. Tente de novo ou use outro cartão.',
|
||||||
|
'cc_rejected_other_reason': 'O banco emissor recusou o pagamento. ' + TRY_OTHER,
|
||||||
|
'rejected_insufficient_data': 'Faltaram dados para processar o pagamento. Confira os dados e tente de novo.',
|
||||||
|
'rejected_by_regulations': 'Não foi possível pagar com este cartão. ' + TRY_OTHER,
|
||||||
|
}
|
||||||
|
REJECTED = 'O pagamento foi recusado. Confira os dados ou ' + TRY_OTHER[0].lower() + TRY_OTHER[1:]
|
||||||
|
|
||||||
|
# Mercado Pago's cause codes for a request it refused before charging.
|
||||||
|
CAUSES = {
|
||||||
|
'205': 'Informe o número do cartão.',
|
||||||
|
'208': 'Informe o mês de validade do cartão.',
|
||||||
|
'209': 'Informe o ano de validade do cartão.',
|
||||||
|
'212': 'Informe o CPF ou CNPJ do titular do cartão.',
|
||||||
|
'213': 'Informe o CPF ou CNPJ do titular do cartão.',
|
||||||
|
'214': 'Informe o CPF ou CNPJ do titular do cartão.',
|
||||||
|
'221': 'Informe o nome do titular como está no cartão.',
|
||||||
|
'224': 'Informe o código de segurança (CVV).',
|
||||||
|
'E301': 'O número do cartão está incorreto. Confira e tente de novo.',
|
||||||
|
'E302': 'O código de segurança (CVV) está incorreto. Confira e tente de novo.',
|
||||||
|
'316': 'O nome do titular está incorreto. Digite como está no cartão.',
|
||||||
|
'322': 'O CPF ou CNPJ do titular está incorreto. Confira e tente de novo.',
|
||||||
|
'323': 'O CPF ou CNPJ do titular está incorreto. Confira e tente de novo.',
|
||||||
|
'324': 'O CPF ou CNPJ do titular está incorreto. Confira e tente de novo.',
|
||||||
|
'325': 'A data de validade do cartão está incorreta. Confira e tente de novo.',
|
||||||
|
'326': 'A data de validade do cartão está incorreta. Confira e tente de novo.',
|
||||||
|
'2067': 'O CPF ou CNPJ do titular está incorreto. Confira e tente de novo.',
|
||||||
|
'3034': 'O número do cartão está incorreto. Confira e tente de novo.',
|
||||||
|
'4033': 'O cartão não aceita esse número de parcelas. Escolha outro número de parcelas.',
|
||||||
|
}
|
||||||
|
NOT_PROCESSED = 'Não foi possível processar o pagamento com este cartão. Confira os dados ou ' + \
|
||||||
|
TRY_OTHER[0].lower() + TRY_OTHER[1:]
|
||||||
|
UNAVAILABLE = 'O Mercado Pago não respondeu agora. Tente de novo em alguns instantes.'
|
||||||
|
|
||||||
|
|
||||||
|
def rejection(status_detail):
|
||||||
|
"""The sentence for a refused card payment."""
|
||||||
|
return REJECTIONS.get(status_detail or '', REJECTED)
|
||||||
|
|
||||||
|
|
||||||
|
def refusal(data):
|
||||||
|
"""The sentence for a request Mercado Pago refused (its JSON error body)."""
|
||||||
|
for cause in (data or {}).get('cause') or []:
|
||||||
|
if isinstance(cause, dict) and str(cause.get('code')) in CAUSES:
|
||||||
|
return CAUSES[str(cause.get('code'))]
|
||||||
|
return NOT_PROCESSED
|
||||||
|
|
||||||
|
|
||||||
|
# The reasons a quote cannot be paid (app.payments.PaymentRefused), as the customer reads them.
|
||||||
|
QUOTE_REFUSALS = {
|
||||||
|
'quote not found': 'Não encontramos este pedido. Monte o pedido de novo.',
|
||||||
|
'quote was never reviewed': 'Este pedido ainda está sendo conferido pela equipe. Você poderá pagar assim que for aprovado.',
|
||||||
|
'quote was discarded': 'Esta cotação foi cancelada. Monte o pedido de novo para pagar.',
|
||||||
|
'quote uses an obsolete production layout; request a new quote':
|
||||||
|
'Este pedido foi montado numa versão anterior do Site. Monte o pedido de novo para pagar.',
|
||||||
|
'quote expired before payment': 'O prazo de 24 horas para pagar este pedido acabou. Monte o pedido de novo para pagar.',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def quote_refusal(reason):
|
||||||
|
return QUOTE_REFUSALS.get(str(reason), 'Não foi possível pagar este pedido. Monte o pedido de novo.')
|
||||||
@@ -6,9 +6,11 @@ or a second WhatsApp message. Every delivery is recorded under the provider's
|
|||||||
own event id and applied inside one transaction, so a duplicate is a no-op and a
|
own event id and applied inside one transaction, so a duplicate is a no-op and a
|
||||||
crash mid-way is retried rather than half-applied.
|
crash mid-way is retried rather than half-applied.
|
||||||
|
|
||||||
Order creation lives here rather than in a route because two paths reach it: the
|
Order creation lives here rather than in a route because the paths that reach
|
||||||
webhook, and the local development checkout. They must agree.
|
it must agree: the webhook, reconcile() (the provider asked directly) and the
|
||||||
|
local development checkout.
|
||||||
"""
|
"""
|
||||||
|
import logging
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
@@ -34,6 +36,8 @@ def approved_quote(c, quote_id, owner=None):
|
|||||||
raise PaymentRefused('quote not found')
|
raise PaymentRefused('quote not found')
|
||||||
if not row['approved']:
|
if not row['approved']:
|
||||||
raise PaymentRefused('quote was never reviewed')
|
raise PaymentRefused('quote was never reviewed')
|
||||||
|
if row.get('discarded_at'):
|
||||||
|
raise PaymentRefused('quote was discarded')
|
||||||
if any(item.get('production', {}).get('version') != 2 for item in row['approved']['items']):
|
if any(item.get('production', {}).get('version') != 2 for item in row['approved']['items']):
|
||||||
raise PaymentRefused('quote uses an obsolete production layout; request a new quote')
|
raise PaymentRefused('quote uses an obsolete production layout; request a new quote')
|
||||||
if row['approved_at'] < datetime.now(timezone.utc) - timedelta(hours=QUOTE_VALID_HOURS):
|
if row['approved_at'] < datetime.now(timezone.utc) - timedelta(hours=QUOTE_VALID_HOURS):
|
||||||
@@ -41,6 +45,34 @@ def approved_quote(c, quote_id, owner=None):
|
|||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
TEST_PROVIDER = 'teste'
|
||||||
|
|
||||||
|
|
||||||
|
def is_test(order):
|
||||||
|
"""An operator's test order: it goes through production and notifies no one."""
|
||||||
|
return (order.get('payment') or {}).get('provider') == TEST_PROVIDER
|
||||||
|
|
||||||
|
|
||||||
|
# How long a file is kept while nothing has been paid for it. A cart's files
|
||||||
|
# are uploaded before payment, so the price and the security check are done on
|
||||||
|
# the file itself; a cart that is never paid must not keep them for 30 days.
|
||||||
|
UNPAID_HOLD = '2 days'
|
||||||
|
# A quote waiting for an operator's review keeps its files this long.
|
||||||
|
REVIEW_HOLD = '7 days'
|
||||||
|
# Once paid, the order keeps its originals for this long from the upload.
|
||||||
|
ORDER_RETENTION = '30 days'
|
||||||
|
|
||||||
|
|
||||||
|
def quote_uploads(quote_or_draft):
|
||||||
|
return [uid for item in quote_or_draft['items'] for uid in item['uploads']]
|
||||||
|
|
||||||
|
|
||||||
|
def hold_uploads(c, upload_ids, interval):
|
||||||
|
"""Keep these files at least `interval` from now; never shortens a hold."""
|
||||||
|
c.execute('''UPDATE dtf_local.uploads SET expires_at=GREATEST(expires_at, now()+%s::interval)
|
||||||
|
WHERE id=ANY(%s) AND purged_at IS NULL''', (interval, [str(u) for u in upload_ids]))
|
||||||
|
|
||||||
|
|
||||||
def create_order(c, quote, payment):
|
def create_order(c, quote, payment):
|
||||||
"""Create the order for a reviewed quote, or return the one already there.
|
"""Create the order for a reviewed quote, or return the one already there.
|
||||||
|
|
||||||
@@ -60,7 +92,12 @@ def create_order(c, quote, payment):
|
|||||||
order = c.execute(
|
order = c.execute(
|
||||||
'INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s) RETURNING *',
|
'INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s) RETURNING *',
|
||||||
(uuid4(), quote['id'], quote['owner'], Jsonb(approved), Jsonb(payment))).fetchone()
|
(uuid4(), quote['id'], quote['owner'], Jsonb(approved), Jsonb(payment))).fetchone()
|
||||||
|
# Paid: the files are kept for the order's retention, counted from upload.
|
||||||
|
c.execute('''UPDATE dtf_local.uploads SET expires_at=GREATEST(expires_at, created_at+%s::interval)
|
||||||
|
WHERE id=ANY(%s) AND purged_at IS NULL''', (ORDER_RETENTION, quote_uploads(approved)))
|
||||||
queue_print_files(c, order['id'], len(approved['items']))
|
queue_print_files(c, order['id'], len(approved['items']))
|
||||||
|
if is_test(order):
|
||||||
|
return order, True
|
||||||
for provider in ('tiny', 'whatsapp'):
|
for provider in ('tiny', 'whatsapp'):
|
||||||
enqueue(c, f"{order['id']}:paid:{provider}", provider,
|
enqueue(c, f"{order['id']}:paid:{provider}", provider,
|
||||||
{'order_id': str(order['id']), 'number': order['number'],
|
{'order_id': str(order['id']), 'number': order['number'],
|
||||||
@@ -123,3 +160,39 @@ def apply(c, event):
|
|||||||
|
|
||||||
order, created = create_order(c, quote, {'provider': event.raw.get('provider', 'webhook'), **event.raw})
|
order, created = create_order(c, quote, {'provider': event.raw.get('provider', 'webhook'), **event.raw})
|
||||||
return f"order {order['number']}" + ('' if created else ' (already existed)')
|
return f"order {order['number']}" + ('' if created else ' (already existed)')
|
||||||
|
|
||||||
|
|
||||||
|
def reconcile(connect, provider, quote_id=None, limit=20):
|
||||||
|
"""Ask the provider about the payments of quotes that have no order yet and
|
||||||
|
apply what it says, exactly as a notification would. A notification can be
|
||||||
|
refused, lost or late; the provider's API is the authority either way, and
|
||||||
|
the event id (payment id and status) makes a webhook and this the same
|
||||||
|
delivery, applied once. Returns the outcomes of what was new."""
|
||||||
|
if not hasattr(provider, 'lookup'):
|
||||||
|
return []
|
||||||
|
from .mercadopago import event_from_payment
|
||||||
|
with connect() as c:
|
||||||
|
rows = c.execute('''SELECT i.provider_payment_id FROM dtf_local.payment_intents i
|
||||||
|
LEFT JOIN dtf_local.orders o ON o.quote_id=i.quote_id
|
||||||
|
WHERE o.id IS NULL AND i.provider=%s AND i.status IN ('pending','approved')
|
||||||
|
AND i.created_at > now() - make_interval(hours => %s)''' +
|
||||||
|
(' AND i.quote_id=%s' if quote_id else '') + ' ORDER BY i.created_at DESC LIMIT %s',
|
||||||
|
(provider.name, QUOTE_VALID_HOURS + 1, *([quote_id] if quote_id else []), limit)).fetchall()
|
||||||
|
outcomes = []
|
||||||
|
for row in rows:
|
||||||
|
# Asked outside any transaction: no lock is held while the provider answers.
|
||||||
|
try:
|
||||||
|
event = event_from_payment(provider.lookup(row['provider_payment_id']))
|
||||||
|
except Exception:
|
||||||
|
logging.exception('Payment reconciliation lookup failed')
|
||||||
|
continue
|
||||||
|
with connect() as c:
|
||||||
|
stored = record(c, provider.name, event)
|
||||||
|
if stored is None:
|
||||||
|
continue
|
||||||
|
outcome = apply(c, event)
|
||||||
|
c.execute('UPDATE dtf_local.payment_events SET processed_at=now(), outcome=%s WHERE id=%s',
|
||||||
|
(outcome, stored['id']))
|
||||||
|
audit('payment_reconciled', payment_event=event.event_id, status=event.status, outcome=outcome)
|
||||||
|
outcomes.append(outcome)
|
||||||
|
return outcomes
|
||||||
|
|||||||
@@ -46,7 +46,8 @@ Image.MAX_IMAGE_PIXELS = None
|
|||||||
|
|
||||||
|
|
||||||
class Unsupported(Exception):
|
class Unsupported(Exception):
|
||||||
"""This item cannot be generated automatically; the reason is for the operator."""
|
"""This item cannot be generated automatically. The reason is shown to the
|
||||||
|
operator on the Kanban, so it is written in Portuguese."""
|
||||||
|
|
||||||
|
|
||||||
class Name(str):
|
class Name(str):
|
||||||
@@ -153,14 +154,14 @@ class SourceImage:
|
|||||||
self.image = Image.open(path)
|
self.image = Image.open(path)
|
||||||
self.format = self.image.format
|
self.format = self.image.format
|
||||||
except Image.DecompressionBombError as exc:
|
except Image.DecompressionBombError as exc:
|
||||||
raise Unsupported(f'"{name}" is too large to generate automatically') from exc
|
raise Unsupported(f'"{name}" é grande demais para gerar automaticamente') from exc
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise Unsupported(f'"{name}" is not an image this generator can read') from exc
|
raise Unsupported(f'"{name}" não é uma imagem que o gerador consiga ler') from exc
|
||||||
if self.format not in SUPPORTED_FORMATS:
|
if self.format not in SUPPORTED_FORMATS:
|
||||||
raise Unsupported(f'"{name}" is {self.format or "an unknown format"}; '
|
raise Unsupported(f'"{name}" está em {self.format or "formato desconhecido"}; '
|
||||||
'only JPEG, PNG, WebP and TIFF are generated automatically')
|
'só JPEG, PNG, WebP, TIFF e PDF são gerados automaticamente')
|
||||||
if getattr(self.image, 'n_frames', 1) > 1 and self.format != 'TIFF':
|
if getattr(self.image, 'n_frames', 1) > 1 and self.format != 'TIFF':
|
||||||
raise Unsupported(f'"{name}" is animated or has several frames')
|
raise Unsupported(f'"{name}" é animado ou tem vários quadros')
|
||||||
# Browsers draw a photo upright according to its EXIF orientation, and
|
# Browsers draw a photo upright according to its EXIF orientation, and
|
||||||
# the Site measured it that way, so the print must too.
|
# the Site measured it that way, so the print must too.
|
||||||
try:
|
try:
|
||||||
@@ -181,8 +182,8 @@ class SourceImage:
|
|||||||
return self._embed_jpeg(pdf)
|
return self._embed_jpeg(pdf)
|
||||||
width, height = self.image.size
|
width, height = self.image.size
|
||||||
if width * height > MAX_DECODED_PIXELS:
|
if width * height > MAX_DECODED_PIXELS:
|
||||||
raise Unsupported(f'"{self.name}" has {width} x {height} px, more than the '
|
raise Unsupported(f'"{self.name}" tem {width} × {height} px, mais do que o '
|
||||||
'generator decodes; prepare this item by hand')
|
'gerador processa; prepare este item à mão')
|
||||||
return self._embed_pixels(pdf)
|
return self._embed_pixels(pdf)
|
||||||
|
|
||||||
def _colorspace(self, pdf, mode):
|
def _colorspace(self, pdf, mode):
|
||||||
@@ -235,7 +236,7 @@ class SourceImage:
|
|||||||
elif mode in ('P', 'PA', 'RGB', 'RGBA', 'RGBa'):
|
elif mode in ('P', 'PA', 'RGB', 'RGBA', 'RGBa'):
|
||||||
color_mode = 'RGB'
|
color_mode = 'RGB'
|
||||||
else:
|
else:
|
||||||
raise Unsupported(f'"{self.name}" uses the {mode} colour mode, which is not generated automatically')
|
raise Unsupported(f'"{self.name}" usa o modo de cor {mode}, que não é gerado automaticamente')
|
||||||
if has_alpha:
|
if has_alpha:
|
||||||
image = image.convert('RGBA' if color_mode == 'RGB' else 'LA')
|
image = image.convert('RGBA' if color_mode == 'RGB' else 'LA')
|
||||||
width, height = image.size
|
width, height = image.size
|
||||||
@@ -304,29 +305,29 @@ class PdfPage:
|
|||||||
try:
|
try:
|
||||||
self.pdf = pikepdf.open(path)
|
self.pdf = pikepdf.open(path)
|
||||||
except pikepdf.PasswordError as exc:
|
except pikepdf.PasswordError as exc:
|
||||||
raise Unsupported(f'"{name}" is password-protected') from exc
|
raise Unsupported(f'"{name}" está protegido por senha') from exc
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise Unsupported(f'"{name}" is not a PDF this generator can read') from exc
|
raise Unsupported(f'"{name}" não é um PDF que o gerador consiga ler') from exc
|
||||||
try:
|
try:
|
||||||
pages = len(self.pdf.pages)
|
pages = len(self.pdf.pages)
|
||||||
if pages != 1:
|
if pages != 1:
|
||||||
raise Unsupported(f'"{name}" has {pages} pages; only single-page PDFs are generated automatically')
|
raise Unsupported(f'"{name}" tem {pages} páginas; só PDFs de uma página são gerados automaticamente')
|
||||||
self.page = self.pdf.pages[0]
|
self.page = self.pdf.pages[0]
|
||||||
self.rotation = int(self.page.rotation) % 360
|
self.rotation = int(self.page.rotation) % 360
|
||||||
if self.rotation not in (0, 90, 180, 270):
|
if self.rotation not in (0, 90, 180, 270):
|
||||||
raise Unsupported(f'"{name}" has an unsupported page rotation')
|
raise Unsupported(f'"{name}" tem uma rotação de página não suportada')
|
||||||
box = [float(v) for v in self.page.cropbox]
|
box = [float(v) for v in self.page.cropbox]
|
||||||
except Unsupported:
|
except Unsupported:
|
||||||
self.pdf.close()
|
self.pdf.close()
|
||||||
raise
|
raise
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.pdf.close()
|
self.pdf.close()
|
||||||
raise Unsupported(f'"{name}" has a page this generator cannot read') from exc
|
raise Unsupported(f'"{name}" tem uma página que o gerador não consegue ler') from exc
|
||||||
self.x0, self.y0 = min(box[0], box[2]), min(box[1], box[3])
|
self.x0, self.y0 = min(box[0], box[2]), min(box[1], box[3])
|
||||||
self.w, self.h = abs(box[2] - box[0]), abs(box[3] - box[1])
|
self.w, self.h = abs(box[2] - box[0]), abs(box[3] - box[1])
|
||||||
if self.w <= 0 or self.h <= 0:
|
if self.w <= 0 or self.h <= 0:
|
||||||
self.pdf.close()
|
self.pdf.close()
|
||||||
raise Unsupported(f'"{name}" has an empty page')
|
raise Unsupported(f'"{name}" tem uma página vazia')
|
||||||
# As displayed, which is what the proportions are checked against.
|
# As displayed, which is what the proportions are checked against.
|
||||||
self.size = (self.h, self.w) if self.rotation in (90, 270) else (self.w, self.h)
|
self.size = (self.h, self.w) if self.rotation in (90, 270) else (self.w, self.h)
|
||||||
|
|
||||||
@@ -373,7 +374,7 @@ def check_layout(item, sizes, vector=()):
|
|||||||
height = Decimal(str(production['height_cm']))
|
height = Decimal(str(production['height_cm']))
|
||||||
billed = Decimal(str(item['billed_metres'])) * 100
|
billed = Decimal(str(item['billed_metres'])) * 100
|
||||||
if height > billed + HEIGHT_TOLERANCE_CM:
|
if height > billed + HEIGHT_TOLERANCE_CM:
|
||||||
raise Unsupported(f'layout is {height} cm long but only {billed} cm were billed')
|
raise Unsupported(f'a montagem tem {height} cm, mas só {billed} cm foram cobrados')
|
||||||
lowest = None
|
lowest = None
|
||||||
for placement in production['placements']:
|
for placement in production['placements']:
|
||||||
width_px, height_px = sizes[placement['source_index']]
|
width_px, height_px = sizes[placement['source_index']]
|
||||||
@@ -384,8 +385,8 @@ def check_layout(item, sizes, vector=()):
|
|||||||
drift = abs((width_px / height_px) / (width_cm / length_cm) - 1)
|
drift = abs((width_px / height_px) / (width_cm / length_cm) - 1)
|
||||||
if drift > ASPECT_TOLERANCE:
|
if drift > ASPECT_TOLERANCE:
|
||||||
source = production['sources'][placement['source_index']]
|
source = production['sources'][placement['source_index']]
|
||||||
raise Unsupported(f'file {placement["source_index"] + 1} has proportions that do not match '
|
raise Unsupported(f'o arquivo {placement["source_index"] + 1} tem proporções diferentes '
|
||||||
f'the quoted {source["width_cm"]} x {source["length_cm"]} cm')
|
f'das cotadas ({source["width_cm"]} × {source["length_cm"]} cm)')
|
||||||
if placement['source_index'] in vector:
|
if placement['source_index'] in vector:
|
||||||
continue
|
continue
|
||||||
dpi = width_px / (width_cm / 2.54)
|
dpi = width_px / (width_cm / 2.54)
|
||||||
@@ -405,7 +406,7 @@ def render(item, files, out, title):
|
|||||||
"""
|
"""
|
||||||
production = item['production']
|
production = item['production']
|
||||||
if production.get('version') != 2:
|
if production.get('version') != 2:
|
||||||
raise Unsupported('item uses an obsolete production layout')
|
raise Unsupported('o item usa uma montagem antiga')
|
||||||
sources = []
|
sources = []
|
||||||
try:
|
try:
|
||||||
for index in range(len(production['sources'])):
|
for index in range(len(production['sources'])):
|
||||||
|
|||||||
@@ -9,6 +9,12 @@ The result is an ordinary upload row owned by an identity derived from the
|
|||||||
order, already marked clean: its only inputs are artwork that passed the
|
order, already marked clean: its only inputs are artwork that passed the
|
||||||
malware scan, and the bytes are written here. The operator still decides
|
malware scan, and the bytes are written here. The operator still decides
|
||||||
whether it becomes the final file; generation never approves anything.
|
whether it becomes the final file; generation never approves anything.
|
||||||
|
|
||||||
|
Sheets of several GB are the normal order, and decoding one would take more
|
||||||
|
memory than the worker has. A source above LARGE_SOURCE_BYTES is never
|
||||||
|
opened: a finished sheet placed whole on the film is already its own print
|
||||||
|
file, so the original becomes the print file; any other layout is prepared
|
||||||
|
by hand from the original.
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@@ -25,6 +31,9 @@ from .printfile import Unsupported, render
|
|||||||
|
|
||||||
CLAIM_TIMEOUT = timedelta(minutes=15)
|
CLAIM_TIMEOUT = timedelta(minutes=15)
|
||||||
MAX_ATTEMPTS = 3
|
MAX_ATTEMPTS = 3
|
||||||
|
LARGE_SOURCE_BYTES = int(os.environ.get('PRINT_DECODE_MAX_BYTES', str(300 * 1024 * 1024)))
|
||||||
|
# Formats the operator can import as they are.
|
||||||
|
PRINTABLE_ORIGINAL = ('.png', '.jpg', '.jpeg', '.tif', '.tiff', '.pdf')
|
||||||
|
|
||||||
|
|
||||||
def generated_identity(order_id):
|
def generated_identity(order_id):
|
||||||
@@ -60,13 +69,26 @@ def render_one(storage):
|
|||||||
c.execute('''UPDATE dtf_local.print_files SET status='rendering', claimed_at=now(),
|
c.execute('''UPDATE dtf_local.print_files SET status='rendering', claimed_at=now(),
|
||||||
attempts=attempts+1 WHERE id=%s''', (job['id'],))
|
attempts=attempts+1 WHERE id=%s''', (job['id'],))
|
||||||
item = job['snapshot']['items'][job['item_index']]
|
item = job['snapshot']['items'][job['item_index']]
|
||||||
uploads = c.execute('''SELECT id,name,object_key,scan_state,purged_at,expires_at,
|
uploads = c.execute('''SELECT id,name,size,object_key,scan_state,purged_at,expires_at,
|
||||||
(expires_at<=now()) AS expired FROM dtf_local.uploads WHERE id=ANY(%s)''',
|
(expires_at<=now()) AS expired FROM dtf_local.uploads WHERE id=ANY(%s)''',
|
||||||
([UUID(u) for u in item['uploads']],)).fetchall()
|
([UUID(u) for u in item['uploads']],)).fetchall()
|
||||||
# Every generated file shares its order's artwork retention deadline.
|
# Every generated file shares its order's artwork retention deadline.
|
||||||
expiry = c.execute('SELECT min(created_at)+interval \'30 days\' AS e FROM dtf_local.uploads WHERE id=ANY(%s)',
|
expiry = c.execute('SELECT min(created_at)+interval \'30 days\' AS e FROM dtf_local.uploads WHERE id=ANY(%s)',
|
||||||
([UUID(u) for u in item['uploads']],)).fetchone()['e']
|
([UUID(u) for u in item['uploads']],)).fetchone()['e']
|
||||||
by_id = {str(row['id']): row for row in uploads}
|
by_id = {str(row['id']): row for row in uploads}
|
||||||
|
if any(row['size'] > LARGE_SOURCE_BYTES for row in uploads):
|
||||||
|
original = whole_sheet(item, by_id)
|
||||||
|
if original:
|
||||||
|
with connect() as c:
|
||||||
|
c.execute('''UPDATE dtf_local.print_files SET status='ready', upload_id=%s, detail=%s,
|
||||||
|
finished_at=now(), claimed_at=NULL WHERE id=%s''',
|
||||||
|
(original['id'], Jsonb({'source': 'original', 'name': original['name']}), job['id']))
|
||||||
|
audit('print_file_original', order=str(job['order_id']), item=job['item_index'])
|
||||||
|
else:
|
||||||
|
finish(job, 'manual', {'reason': f'arquivo acima de {LARGE_SOURCE_BYTES // 1048576} MB: '
|
||||||
|
'monte a folha a partir do original'})
|
||||||
|
audit('print_file_manual', order=str(job['order_id']), item=job['item_index'])
|
||||||
|
return True
|
||||||
try:
|
try:
|
||||||
result = produce(storage, job, item, by_id)
|
result = produce(storage, job, item, by_id)
|
||||||
except Unsupported as reason:
|
except Unsupported as reason:
|
||||||
@@ -96,14 +118,33 @@ def render_one(storage):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def whole_sheet(item, uploads):
|
||||||
|
"""The original, when the item is one finished sheet placed whole, once,
|
||||||
|
unrotated and unmirrored, across the film: then it is the print file."""
|
||||||
|
spec = item.get('production') or {}
|
||||||
|
sources, placements = spec.get('sources') or [], spec.get('placements') or []
|
||||||
|
if len(item['uploads']) != 1 or len(sources) != 1 or len(placements) != 1:
|
||||||
|
return None
|
||||||
|
source, place = sources[0], placements[0]
|
||||||
|
row = uploads.get(item['uploads'][0])
|
||||||
|
if (not row or row['scan_state'] != 'clean' or row['purged_at'] or row['expired']
|
||||||
|
or source.get('kind') != 'sheet' or int(source.get('copies', 1)) != 1
|
||||||
|
or not row['name'].lower().endswith(PRINTABLE_ORIGINAL)):
|
||||||
|
return None
|
||||||
|
if (float(place['x_cm']) != 0 or float(place['y_cm']) != 0 or int(place['rotation_degrees']) != 0
|
||||||
|
or place['mirrored'] or abs(float(source['width_cm']) - float(spec['film_width_cm'])) > 0.5):
|
||||||
|
return None
|
||||||
|
return row
|
||||||
|
|
||||||
|
|
||||||
def produce(storage, job, item, uploads):
|
def produce(storage, job, item, uploads):
|
||||||
"""Fetch the item's artwork and render it. Returns (pdf path, name, size, evidence)."""
|
"""Fetch the item's artwork and render it. Returns (pdf path, name, size, evidence)."""
|
||||||
for upload_id in item['uploads']:
|
for upload_id in item['uploads']:
|
||||||
row = uploads.get(upload_id)
|
row = uploads.get(upload_id)
|
||||||
if not row or row['scan_state'] != 'clean':
|
if not row or row['scan_state'] != 'clean':
|
||||||
raise Unsupported('an original file is missing or not cleared by the malware scan')
|
raise Unsupported('um arquivo original está ausente ou não foi liberado pelo antivírus')
|
||||||
if row['purged_at'] or row['expired']:
|
if row['purged_at'] or row['expired']:
|
||||||
raise Unsupported('an original file has passed its retention period')
|
raise Unsupported('um arquivo original passou do prazo de guarda')
|
||||||
number = job['number']
|
number = job['number']
|
||||||
name = f'pedido-{number}-item-{job["item_index"] + 1}.pdf'
|
name = f'pedido-{number}-item-{job["item_index"] + 1}.pdf'
|
||||||
with tempfile.TemporaryDirectory(prefix='print-') as scratch:
|
with tempfile.TemporaryDirectory(prefix='print-') as scratch:
|
||||||
|
|||||||
89
app/quote_review.py
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
"""Quote approval: automatic at checkout, by an operator for the exceptions.
|
||||||
|
|
||||||
|
A quote the customer can pay is priced here from the server's own ladders,
|
||||||
|
whether an operator approved it on the Kanban or the Site approved it the
|
||||||
|
moment it was created. The Site is a shop: a customer who can price the order
|
||||||
|
should be able to pay for it straight away, at any hour, so only orders a
|
||||||
|
person must look at before charging wait for the Kanban (review_reason).
|
||||||
|
|
||||||
|
The grade (and so the discount) is worked out in the customer's browser and
|
||||||
|
checked against the files before an automatic approval (app/grade_check.py);
|
||||||
|
a grade the files do not support waits for review. The layout is still the
|
||||||
|
browser's (roadmap 3.9): the API checks its geometry. Operators see every
|
||||||
|
order's grade and artwork at Arte recebida.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from . import payments
|
||||||
|
from .core.pricing import price
|
||||||
|
from .runtime import freight, upload_row
|
||||||
|
from .scanning import require_clean
|
||||||
|
|
||||||
|
AUTO = 'auto'
|
||||||
|
|
||||||
|
|
||||||
|
def auto_approve_enabled():
|
||||||
|
return os.environ.get('QUOTE_AUTO_APPROVE', 'true').lower() == 'true'
|
||||||
|
|
||||||
|
|
||||||
|
def max_auto_metres():
|
||||||
|
return Decimal(os.environ.get('QUOTE_AUTO_MAX_METRES', '50'))
|
||||||
|
|
||||||
|
|
||||||
|
def review_reason(draft):
|
||||||
|
"""Why this quote needs a person before it can be paid, or None."""
|
||||||
|
if not auto_approve_enabled():
|
||||||
|
return 'Aprovação automática desligada'
|
||||||
|
total = sum(Decimal(str(item['metres'])) for item in draft['items'])
|
||||||
|
if total > max_auto_metres():
|
||||||
|
return f'Pedido acima de {max_auto_metres():g} m'
|
||||||
|
for item in draft['items']:
|
||||||
|
if item.get('production', {}).get('version') != 2:
|
||||||
|
return 'Montagem antiga'
|
||||||
|
# The Site grades only the art it could analyse; anything else is
|
||||||
|
# priced at the full rate. A discount on it did not come from the Site.
|
||||||
|
if item['quality_status'] == 'unverified' and item['grade'] != 0:
|
||||||
|
return 'Nota informada sem análise da arte'
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def approve(c, row, items, reviewer):
|
||||||
|
"""Price the reviewed items and bind them to the quote; returns the approval.
|
||||||
|
`row` must be locked by the caller."""
|
||||||
|
draft = row['draft']
|
||||||
|
if row['approved']:
|
||||||
|
raise HTTPException(409, 'Esta cotação já foi aprovada e não pode mudar.')
|
||||||
|
if any(item.get('production', {}).get('version') != 2 for item in draft['items']):
|
||||||
|
raise HTTPException(409, 'A cotação usa uma montagem antiga: o cliente precisa montar o pedido de novo.')
|
||||||
|
if len(items) != len(draft['items']):
|
||||||
|
raise HTTPException(422, 'Confira todos os itens da cotação.')
|
||||||
|
priced = []
|
||||||
|
for item, original in zip(items, draft['items']):
|
||||||
|
if item.mode != original['mode'] or list(map(str, item.uploads)) != original['uploads']:
|
||||||
|
raise HTTPException(422, 'O produto e os arquivos não podem mudar na revisão.')
|
||||||
|
if item.production.model_dump(mode='json') != original['production'] or item.quality_status != original['quality_status'] or item.quality_acknowledged != original['quality_acknowledged']:
|
||||||
|
raise HTTPException(422, 'A montagem e a confirmação do cliente não podem mudar na revisão.')
|
||||||
|
for upload_id in item.uploads:
|
||||||
|
require_clean(upload_row(c, upload_id, row['owner']))
|
||||||
|
priced.append({**price(item.mode, str(item.metres), item.grade),
|
||||||
|
'uploads': original['uploads'], 'production': original['production'],
|
||||||
|
'quality_status': original['quality_status'],
|
||||||
|
'quality_acknowledged': original['quality_acknowledged']})
|
||||||
|
metres = sum(Decimal(i['billed_metres']) for i in priced)
|
||||||
|
try:
|
||||||
|
quoted_freight = freight.quote(draft['freight']['service'], draft['freight'].get('postal_code', ''),
|
||||||
|
metres, sum(i['total_cents'] for i in priced))
|
||||||
|
except ValueError as exc:
|
||||||
|
raise HTTPException(422, str(exc))
|
||||||
|
approved = {'customer': draft['customer'], 'items': priced, 'freight': quoted_freight,
|
||||||
|
'destination': draft.get('destination'),
|
||||||
|
'total_cents': sum(i['total_cents'] for i in priced) + quoted_freight['total_cents']}
|
||||||
|
c.execute('UPDATE dtf_local.quotes SET approved=%s, reviewed_by=%s, approved_at=now() WHERE id=%s',
|
||||||
|
(Jsonb(approved), reviewer, row['id']))
|
||||||
|
# Approved and payable now: the files wait for the payment, not for ever.
|
||||||
|
payments.hold_uploads(c, payments.quote_uploads(approved), payments.UNPAID_HOLD)
|
||||||
|
return approved
|
||||||
@@ -25,6 +25,10 @@ if os.environ.get('PAYMENT_ADAPTER') == 'mercadopago':
|
|||||||
payment = MercadoPagoPayment()
|
payment = MercadoPagoPayment()
|
||||||
else:
|
else:
|
||||||
payment = FakePayment()
|
payment = FakePayment()
|
||||||
|
if os.environ.get('FREIGHT_ADAPTER') == 'jadlog':
|
||||||
|
from .jadlog import JadlogFreight
|
||||||
|
freight = JadlogFreight()
|
||||||
|
else:
|
||||||
freight = FakeFreight()
|
freight = FakeFreight()
|
||||||
ENVIRONMENT = os.environ.get('APP_ENV', 'local')
|
ENVIRONMENT = os.environ.get('APP_ENV', 'local')
|
||||||
PUBLIC_ORIGIN = os.environ.get('PUBLIC_ORIGIN', 'http://localhost')
|
PUBLIC_ORIGIN = os.environ.get('PUBLIC_ORIGIN', 'http://localhost')
|
||||||
@@ -46,25 +50,56 @@ STATES = {'rec': 'Arte recebida', 'tra': 'Arte tratada', 'fil': 'Fila de impress
|
|||||||
'imp': 'Imprimindo', 'cor': 'Correção', 'fin': 'Finalizado'}
|
'imp': 'Imprimindo', 'cor': 'Correção', 'fin': 'Finalizado'}
|
||||||
TRANSITIONS = {'rec': ['tra','cor'], 'tra': ['fil','cor'], 'fil': ['imp','cor'],
|
TRANSITIONS = {'rec': ['tra','cor'], 'tra': ['fil','cor'], 'fil': ['imp','cor'],
|
||||||
'imp': ['fin','cor'], 'cor': ['rec','tra'], 'fin': []}
|
'imp': ['fin','cor'], 'cor': ['rec','tra'], 'fin': []}
|
||||||
|
# Undoing a move made by mistake: one stage back, with an internal reason. It
|
||||||
|
# is not a correction: the customer is not told and approved finals stay.
|
||||||
|
BACK = {'tra': 'rec', 'fil': 'tra', 'imp': 'fil', 'fin': 'imp'}
|
||||||
|
|
||||||
|
|
||||||
|
def require_delivery_available(service):
|
||||||
|
"""Outside the local stack, a simulated freight price must never reach a
|
||||||
|
customer: until a real freight provider exists, only pickup is offered."""
|
||||||
|
if service != 'pickup' and ENVIRONMENT != 'local' and getattr(freight, 'name', '') == 'fake':
|
||||||
|
raise HTTPException(503, 'A entrega ainda não está disponível. Escolha a retirada em Franca.')
|
||||||
|
|
||||||
|
|
||||||
def upload_row(c, upload_id, session_id, lock=False):
|
def upload_row(c, upload_id, session_id, lock=False):
|
||||||
row = c.execute('SELECT * FROM dtf_local.uploads WHERE id=%s AND owner=%s' +
|
row = c.execute('SELECT * FROM dtf_local.uploads WHERE id=%s AND owner=%s' +
|
||||||
(' FOR UPDATE' if lock else ''), (upload_id, session_id)).fetchone()
|
(' FOR UPDATE' if lock else ''), (upload_id, session_id)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise HTTPException(404, 'Upload not found')
|
raise HTTPException(404, 'Arquivo não encontrado. Escolha o arquivo de novo.')
|
||||||
days = 30 if row['complete'] else 1
|
days = 30 if row['complete'] else 1
|
||||||
if row['purged_at'] or row['expires_at'] <= datetime.now(timezone.utc) or row['created_at'] < datetime.now(timezone.utc) - timedelta(days=days):
|
if row['purged_at'] or row['expires_at'] <= datetime.now(timezone.utc) or row['created_at'] < datetime.now(timezone.utc) - timedelta(days=days):
|
||||||
raise HTTPException(410, 'Upload expired; select the file again')
|
raise HTTPException(410, 'Este arquivo expirou. Escolha o arquivo de novo.')
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
def order_code(order_id):
|
||||||
|
"""What a customer calls an order: the first 8 characters of its id, in
|
||||||
|
capitals ("F6B9CDBC"). It is stable and says nothing about how many orders
|
||||||
|
the store has, unlike the sequential number the operators work with."""
|
||||||
|
return str(order_id).replace('-', '')[:8].upper()
|
||||||
|
|
||||||
|
|
||||||
def quote_view(c, row):
|
def quote_view(c, row):
|
||||||
|
from .quote_review import review_reason # it imports this module
|
||||||
order = c.execute('SELECT id,number,state FROM dtf_local.orders WHERE quote_id=%s', (row['id'],)).fetchone()
|
order = c.execute('SELECT id,number,state FROM dtf_local.orders WHERE quote_id=%s', (row['id'],)).fetchone()
|
||||||
|
# The latest payment attempt, so the payment page can tell a refused card
|
||||||
|
# (the notification updates it) from one still waiting.
|
||||||
|
# Its PIX expiry and time let the Kanban tell "tried to pay" from "never tried".
|
||||||
|
attempt = c.execute('''SELECT method,status,response->>'status_detail' AS status_detail,
|
||||||
|
response->>'expires_at' AS expires_at, created_at
|
||||||
|
FROM dtf_local.payment_intents WHERE quote_id=%s ORDER BY created_at DESC LIMIT 1''', (row['id'],)).fetchone()
|
||||||
|
if attempt and attempt['status'] == 'rejected':
|
||||||
|
from .payment_messages import rejection
|
||||||
|
attempt = {**attempt, 'message': rejection(attempt['status_detail'])}
|
||||||
expired = row['approved_at'] and row['approved_at'] < datetime.now(timezone.utc)-timedelta(hours=24)
|
expired = row['approved_at'] and row['approved_at'] < datetime.now(timezone.utc)-timedelta(hours=24)
|
||||||
return {'id': row['id'], 'created_at': row['created_at'],
|
return {'id': row['id'], 'created_at': row['created_at'],
|
||||||
'draft': row['draft'], 'approved': row['approved'],
|
'draft': row['draft'], 'approved': row['approved'],
|
||||||
'status': 'paid' if order else 'expired' if expired else 'approved' if row['approved'] else 'pending_review',
|
'status': 'paid' if order else 'discarded' if row.get('discarded_at') else 'expired' if expired
|
||||||
|
else 'approved' if row['approved'] else 'pending_review',
|
||||||
|
'auto_approved': row.get('reviewed_by') == 'auto',
|
||||||
|
'review_reason': None if row['approved'] else (row.get('review_note') or review_reason(row['draft'])),
|
||||||
|
'payment': attempt,
|
||||||
'order': order}
|
'order': order}
|
||||||
|
|
||||||
|
|
||||||
@@ -75,7 +110,7 @@ def enqueue(c, event_key, provider, payload):
|
|||||||
|
|
||||||
def owned_order(c, oid, identity, lock=False):
|
def owned_order(c, oid, identity, lock=False):
|
||||||
row = c.execute('SELECT * FROM dtf_local.orders WHERE id=%s AND owner=%s'+(' FOR UPDATE' if lock else ''), (oid,identity)).fetchone()
|
row = c.execute('SELECT * FROM dtf_local.orders WHERE id=%s AND owner=%s'+(' FOR UPDATE' if lock else ''), (oid,identity)).fetchone()
|
||||||
if not row: raise HTTPException(404, 'Order not found')
|
if not row: raise HTTPException(404, 'Pedido não encontrado.')
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,13 @@
|
|||||||
"""Local ClamAV boundary. Unknown/error/over-limit results NEVER release artwork."""
|
"""Releasing artwork: ClamAV up to its size limit, a format check above it.
|
||||||
|
|
||||||
|
Unknown or error results NEVER release artwork. ClamAV scans files up to
|
||||||
|
scan_limit_bytes() (2 GB). Sheets of several GB are the normal order and
|
||||||
|
ClamAV cannot take them, so a larger file is released only if its first bytes
|
||||||
|
are those of the format its name claims (a PNG that really is a PNG, not a
|
||||||
|
program renamed .png). That is the check the client chose for large files; it
|
||||||
|
does not look for malware inside a valid file.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
@@ -30,10 +39,9 @@ class ClamAV:
|
|||||||
return self.command(b'VERSION').decode('utf-8','replace')
|
return self.command(b'VERSION').decode('utf-8','replace')
|
||||||
|
|
||||||
def scan(self, stream, size):
|
def scan(self, stream, size):
|
||||||
if size > scan_limit_bytes():
|
|
||||||
return 'rejected', 'File exceeds the malware scan limit'
|
|
||||||
with socket.create_connection(('scanner',3310),timeout=10) as sock:
|
with socket.create_connection(('scanner',3310),timeout=10) as sock:
|
||||||
sock.settimeout(150)
|
# A 2 GB file takes minutes to stream and scan.
|
||||||
|
sock.settimeout(900)
|
||||||
sock.sendall(b'zINSTREAM\0')
|
sock.sendall(b'zINSTREAM\0')
|
||||||
sent=0
|
sent=0
|
||||||
for chunk in stream.iter_chunks(chunk_size=65536):
|
for chunk in stream.iter_chunks(chunk_size=65536):
|
||||||
@@ -52,6 +60,37 @@ class ClamAV:
|
|||||||
if result.endswith(b' FOUND'):return 'rejected','Malware or unsafe scan condition detected'
|
if result.endswith(b' FOUND'):return 'rejected','Malware or unsafe scan condition detected'
|
||||||
return 'error','Scanner could not verify this file'
|
return 'error','Scanner could not verify this file'
|
||||||
|
|
||||||
|
# What each accepted extension must start with. AI files are PDF or PostScript;
|
||||||
|
# CDR and WebP are RIFF containers with their own form type.
|
||||||
|
TIFF = (b'II*\x00', b'MM\x00*', b'II+\x00', b'MM\x00+')
|
||||||
|
SIGNATURES = {
|
||||||
|
'png': (b'\x89PNG\r\n\x1a\n',),
|
||||||
|
'jpg': (b'\xff\xd8\xff',), 'jpeg': (b'\xff\xd8\xff',),
|
||||||
|
'tif': TIFF, 'tiff': TIFF,
|
||||||
|
'pdf': (b'%PDF-',), 'ai': (b'%PDF-', b'%!PS'),
|
||||||
|
'psd': (b'8BPS',), 'psb': (b'8BPS',),
|
||||||
|
}
|
||||||
|
RIFF_FORMS = {'cdr': re.compile(rb'^RIFF....CDR', re.S), 'webp': re.compile(rb'^RIFF....WEBP', re.S)}
|
||||||
|
HEAD_BYTES = 64
|
||||||
|
|
||||||
|
|
||||||
|
def format_matches(name, head):
|
||||||
|
"""Whether a file's first bytes are those of the format its name claims."""
|
||||||
|
ext = name.rsplit('.', 1)[-1].lower() if '.' in name else ''
|
||||||
|
if ext in RIFF_FORMS:
|
||||||
|
return bool(RIFF_FORMS[ext].match(head))
|
||||||
|
return any(head.startswith(sig) for sig in SIGNATURES.get(ext, ()))
|
||||||
|
|
||||||
|
|
||||||
|
def check_large(storage, row):
|
||||||
|
"""Release decision for a file above the antivirus limit."""
|
||||||
|
head = storage.client.get_object(Bucket=storage.bucket, Key=row['object_key'],
|
||||||
|
Range=f'bytes=0-{HEAD_BYTES - 1}')['Body'].read()
|
||||||
|
if format_matches(row['name'], head):
|
||||||
|
return 'clean', 'Acima do limite do antivírus; formato do arquivo conferido'
|
||||||
|
return 'rejected', 'O conteúdo do arquivo não corresponde ao formato do nome'
|
||||||
|
|
||||||
|
|
||||||
def scan_one(storage, scanner=None):
|
def scan_one(storage, scanner=None):
|
||||||
scanner=scanner or ClamAV()
|
scanner=scanner or ClamAV()
|
||||||
with connect() as c:
|
with connect() as c:
|
||||||
@@ -60,6 +99,9 @@ def scan_one(storage, scanner=None):
|
|||||||
ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 1''').fetchone()
|
ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 1''').fetchone()
|
||||||
if not row:return False
|
if not row:return False
|
||||||
try:
|
try:
|
||||||
|
if row['size'] > scan_limit_bytes():
|
||||||
|
state,reason=check_large(storage,row)
|
||||||
|
else:
|
||||||
stream=storage.client.get_object(Bucket=storage.bucket,Key=row['object_key'])['Body']
|
stream=storage.client.get_object(Bucket=storage.bucket,Key=row['object_key'])['Body']
|
||||||
try:state,reason=scanner.scan(stream,row['size'])
|
try:state,reason=scanner.scan(stream,row['size'])
|
||||||
finally:stream.close()
|
finally:stream.close()
|
||||||
|
|||||||
@@ -107,6 +107,13 @@ CREATE TABLE IF NOT EXISTS dtf_local.provider_tokens (
|
|||||||
connected_by text NOT NULL, connected_at timestamptz NOT NULL DEFAULT now(),
|
connected_by text NOT NULL, connected_at timestamptz NOT NULL DEFAULT now(),
|
||||||
updated_at timestamptz NOT NULL DEFAULT now()
|
updated_at timestamptz NOT NULL DEFAULT now()
|
||||||
);
|
);
|
||||||
|
-- Renewal health, cleared by the next successful renewal or connection.
|
||||||
|
-- refused_at: the provider rejected the refresh token, so only a new
|
||||||
|
-- connection helps. offline: the grant is not bound to a login session.
|
||||||
|
ALTER TABLE dtf_local.provider_tokens ADD COLUMN IF NOT EXISTS refresh_failed_at timestamptz;
|
||||||
|
ALTER TABLE dtf_local.provider_tokens ADD COLUMN IF NOT EXISTS refresh_error text;
|
||||||
|
ALTER TABLE dtf_local.provider_tokens ADD COLUMN IF NOT EXISTS refused_at timestamptz;
|
||||||
|
ALTER TABLE dtf_local.provider_tokens ADD COLUMN IF NOT EXISTS offline boolean NOT NULL DEFAULT false;
|
||||||
-- Single-use states for an operator-started OAuth connection. They protect the
|
-- Single-use states for an operator-started OAuth connection. They protect the
|
||||||
-- callback, which arrives cross-site without the operator's cookie.
|
-- callback, which arrives cross-site without the operator's cookie.
|
||||||
CREATE TABLE IF NOT EXISTS dtf_local.oauth_states (
|
CREATE TABLE IF NOT EXISTS dtf_local.oauth_states (
|
||||||
@@ -127,6 +134,18 @@ CREATE TABLE IF NOT EXISTS dtf_local.print_files (
|
|||||||
UNIQUE(order_id,item_index)
|
UNIQUE(order_id,item_index)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Each run of the off-server database backup (ops/db_backup.py), which the
|
||||||
|
-- Kanban shows so a backup that stopped working does not go unnoticed.
|
||||||
|
CREATE TABLE IF NOT EXISTS dtf_local.backups (
|
||||||
|
id uuid PRIMARY KEY, started_at timestamptz NOT NULL, finished_at timestamptz NOT NULL,
|
||||||
|
status text NOT NULL CHECK(status IN ('ok','failed')), object_key text, bytes bigint, detail text
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS backups_finished ON dtf_local.backups(finished_at DESC);
|
||||||
|
|
||||||
|
-- Why a quote waits for review, when the reason came from its files (the grade
|
||||||
|
-- the server recomputed) and cannot be worked out again from the draft alone.
|
||||||
|
ALTER TABLE dtf_local.quotes ADD COLUMN IF NOT EXISTS review_note text;
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS uploads_owner ON dtf_local.uploads(owner);
|
CREATE INDEX IF NOT EXISTS uploads_owner ON dtf_local.uploads(owner);
|
||||||
|
|
||||||
-- Indexes follow the queries the application actually issues. Only these; every
|
-- Indexes follow the queries the application actually issues. Only these; every
|
||||||
@@ -173,6 +192,9 @@ CREATE INDEX IF NOT EXISTS payment_events_unprocessed ON dtf_local.payment_event
|
|||||||
CREATE INDEX IF NOT EXISTS print_files_open ON dtf_local.print_files(created_at)
|
CREATE INDEX IF NOT EXISTS print_files_open ON dtf_local.print_files(created_at)
|
||||||
WHERE status IN ('pending','rendering');
|
WHERE status IN ('pending','rendering');
|
||||||
|
|
||||||
|
-- A movement that undid an earlier one (a mistaken move), shown as such.
|
||||||
|
ALTER TABLE dtf_local.movements ADD COLUMN IF NOT EXISTS back boolean NOT NULL DEFAULT false;
|
||||||
|
|
||||||
-- The Kanban lists payment events a person must act on (money without an
|
-- The Kanban lists payment events a person must act on (money without an
|
||||||
-- order, or a reversed payment on an existing order) until resolved.
|
-- order, or a reversed payment on an existing order) until resolved.
|
||||||
ALTER TABLE dtf_local.payment_events ADD COLUMN IF NOT EXISTS resolved_at timestamptz;
|
ALTER TABLE dtf_local.payment_events ADD COLUMN IF NOT EXISTS resolved_at timestamptz;
|
||||||
@@ -183,3 +205,31 @@ CREATE INDEX IF NOT EXISTS payment_events_open_issues ON dtf_local.payment_event
|
|||||||
|
|
||||||
-- Payment intents look up by quote (customer retry) and by provider id (webhook).
|
-- Payment intents look up by quote (customer retry) and by provider id (webhook).
|
||||||
CREATE INDEX IF NOT EXISTS payment_intents_quote ON dtf_local.payment_intents(quote_id, created_at DESC);
|
CREATE INDEX IF NOT EXISTS payment_intents_quote ON dtf_local.payment_intents(quote_id, created_at DESC);
|
||||||
|
|
||||||
|
-- A small picture of each artwork (about 480 px), made by the customer's
|
||||||
|
-- browser from the file it already read, so the Kanban shows what an order is
|
||||||
|
-- without opening the original. It goes when the file's bytes go.
|
||||||
|
CREATE TABLE IF NOT EXISTS dtf_local.upload_thumbnails (
|
||||||
|
upload_id uuid PRIMARY KEY REFERENCES dtf_local.uploads(id),
|
||||||
|
mime text NOT NULL, data bytea NOT NULL, created_at timestamptz NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- An operator may discard an unpaid quote (it leaves the Kanban and can no
|
||||||
|
-- longer be paid) and cancel a paid order (it leaves the board but keeps its
|
||||||
|
-- payment and history; a refund is made at the provider).
|
||||||
|
ALTER TABLE dtf_local.quotes ADD COLUMN IF NOT EXISTS discarded_at timestamptz;
|
||||||
|
ALTER TABLE dtf_local.quotes ADD COLUMN IF NOT EXISTS discarded_by text;
|
||||||
|
ALTER TABLE dtf_local.orders ADD COLUMN IF NOT EXISTS cancelled_at timestamptz;
|
||||||
|
ALTER TABLE dtf_local.orders ADD COLUMN IF NOT EXISTS cancelled_by text;
|
||||||
|
ALTER TABLE dtf_local.orders ADD COLUMN IF NOT EXISTS cancel_reason text;
|
||||||
|
|
||||||
|
-- An order's place in its column's queue, when an operator dragged it up or
|
||||||
|
-- down. Empty means "by payment time"; it is cleared when the order moves on.
|
||||||
|
ALTER TABLE dtf_local.orders ADD COLUMN IF NOT EXISTS queue_key double precision;
|
||||||
|
|
||||||
|
-- The company name behind each customer's CNPJ, from the public registry
|
||||||
|
-- (app/cnpj.py). failed_at alone: the lookup failed and is retried later.
|
||||||
|
CREATE TABLE IF NOT EXISTS dtf_local.cnpj_names (
|
||||||
|
cnpj text PRIMARY KEY, legal_name text, trade_name text,
|
||||||
|
fetched_at timestamptz, failed_at timestamptz
|
||||||
|
);
|
||||||
|
|||||||
362
app/tiny.py
@@ -13,12 +13,21 @@ workers never spend the same one.
|
|||||||
|
|
||||||
Orders are created by contact and product id: the customer's contact is found
|
Orders are created by contact and product id: the customer's contact is found
|
||||||
by CNPJ or created, and each product mode maps to a product that must already
|
by CNPJ or created, and each product mode maps to a product that must already
|
||||||
exist in Tiny (PRODUCT_SETTINGS).
|
exist in Tiny (PRODUCT_SETTINGS), sold by the metre. A mode can instead be
|
||||||
|
sold as the client's own SKUs per length (SKU_SETTINGS): the order is then a
|
||||||
|
sum, 1,30 m = 1 × "1 metro" + 1 × "30 cm", at the price the customer paid.
|
||||||
|
|
||||||
Idempotency: the outbox may deliver the same event more than once. Every order
|
Idempotency: the outbox may deliver the same event more than once. Every order
|
||||||
carries numeroOrdemCompra = "DTF-<order number>", and before creating one the
|
carries numeroOrdemCompra = "DTF-<order number>", and before creating one the
|
||||||
customer's recent orders are searched for that number, so a second delivery
|
customer's recent orders are searched for that number, so a second delivery
|
||||||
finds the first order instead of creating another.
|
finds the first order instead of creating another.
|
||||||
|
|
||||||
|
Customer notices: the client already sends WhatsApp messages from Tiny's
|
||||||
|
order situação (Tiny webhook -> their middleware -> n8n). With
|
||||||
|
TINY_STATUS_UPDATES on, a paid order is set to "Aprovada" and a finished
|
||||||
|
pickup order to "Pronto para envio", so those notices reach Site customers
|
||||||
|
from the same number and templates; the system's own WhatsApp sender stays
|
||||||
|
off. Pickup orders carry the client's "retirar pessoalmente" forma de envio.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import secrets
|
import secrets
|
||||||
@@ -35,13 +44,39 @@ AUTH = 'https://accounts.tiny.com.br/realms/tiny/protocol/openid-connect'
|
|||||||
# _FILE as a path to a secret file, and one product mode is called "file".
|
# _FILE as a path to a secret file, and one product mode is called "file".
|
||||||
PRODUCT_SETTINGS = {'file': 'TINY_PRODUCT_TEXTIL_FOLHA', 'avulsa': 'TINY_PRODUCT_TEXTIL_AVULSA',
|
PRODUCT_SETTINGS = {'file': 'TINY_PRODUCT_TEXTIL_FOLHA', 'avulsa': 'TINY_PRODUCT_TEXTIL_AVULSA',
|
||||||
'uvfile': 'TINY_PRODUCT_UV_FOLHA', 'uv': 'TINY_PRODUCT_UV_AVULSA'}
|
'uvfile': 'TINY_PRODUCT_UV_FOLHA', 'uv': 'TINY_PRODUCT_UV_AVULSA'}
|
||||||
|
# The common code prefix of a mode's SKUs ("DTFIMP.TX" for DTFIMP.TX.100CM,
|
||||||
|
# the metre, and DTFIMP.TX.0010CM to DTFIMP.TX.0090CM). When set, it replaces
|
||||||
|
# the mode's product id.
|
||||||
|
SKU_SETTINGS = {'file': 'TINY_SKU_TEXTIL_FOLHA', 'avulsa': 'TINY_SKU_TEXTIL_AVULSA',
|
||||||
|
'uvfile': 'TINY_SKU_UV_FOLHA', 'uv': 'TINY_SKU_UV_AVULSA'}
|
||||||
|
# Or a single SKU per 10 cm, as the client sells UV ("DTFIMP.UV.010CM"):
|
||||||
|
# 2,30 m is 23 of it.
|
||||||
|
STEP_SETTINGS = {'file': 'TINY_SKU10_TEXTIL_FOLHA', 'avulsa': 'TINY_SKU10_TEXTIL_AVULSA',
|
||||||
|
'uvfile': 'TINY_SKU10_UV_FOLHA', 'uv': 'TINY_SKU10_UV_AVULSA'}
|
||||||
PRODUCTS = {'file': 'DTF Têxtil 57 cm · folha montada',
|
PRODUCTS = {'file': 'DTF Têxtil 57 cm · folha montada',
|
||||||
'avulsa': 'DTF Têxtil 57 cm · artes avulsas',
|
'avulsa': 'DTF Têxtil 57 cm · artes avulsas',
|
||||||
'uvfile': 'DTF UV 28,5 cm · folha montada',
|
'uvfile': 'DTF UV 28,5 cm · folha montada',
|
||||||
'uv': 'DTF UV 28,5 cm · artes avulsas'}
|
'uv': 'DTF UV 28,5 cm · artes avulsas'}
|
||||||
# How far back to look for an order a previous delivery may already have made.
|
# How far back to look for an order a previous delivery may already have made.
|
||||||
SEARCH_DAYS = 7
|
SEARCH_DAYS = 7
|
||||||
|
# A "ready" event can come after corrections; it normally knows the Tiny id.
|
||||||
|
READY_SEARCH_DAYS = 45
|
||||||
|
# Tiny v3 order situações (PUT /pedidos/{id}/situacao).
|
||||||
|
ABERTA, APROVADA, PRONTO_ENVIO, ENVIADA, ENTREGUE, CANCELADA, NAO_ENTREGUE = 0, 3, 7, 5, 6, 2, 9
|
||||||
|
SITUACOES = {0: 'Aberta', 3: 'Aprovada', 4: 'Preparando envio', 1: 'Faturada', 7: 'Pronto para envio',
|
||||||
|
5: 'Enviada', 6: 'Entregue', 2: 'Cancelada', 8: 'Dados incompletos', 9: 'Não entregue'}
|
||||||
|
# The client's customer notices (Tiny webhook -> middleware -> n8n -> WhatsApp)
|
||||||
|
# react to these situações. Off until go-live: while n8n still sends the
|
||||||
|
# designer message for DTFIMP products on "Aprovado", setting it would reach
|
||||||
|
# Site customers.
|
||||||
|
STATUS_UPDATES_SETTING = 'TINY_STATUS_UPDATES'
|
||||||
|
# The id of the client's custom "retirar pessoalmente" forma de envio (v2 code
|
||||||
|
# X), which the pickup notice is keyed on. Find it with the console tool.
|
||||||
|
PICKUP_SETTING = 'TINY_FORMA_ENVIO_RETIRADA'
|
||||||
STATE_MINUTES = 10
|
STATE_MINUTES = 10
|
||||||
|
# Renewal runs about every four hours against a one-day refresh token, so
|
||||||
|
# less than this left means renewals have been failing for hours.
|
||||||
|
EXPIRY_WARNING = timedelta(hours=12)
|
||||||
# Brazil has had no daylight saving since 2019; the order date is the local day.
|
# Brazil has had no daylight saving since 2019; the order date is the local day.
|
||||||
BRASILIA = timezone(timedelta(hours=-3))
|
BRASILIA = timezone(timedelta(hours=-3))
|
||||||
|
|
||||||
@@ -68,7 +103,69 @@ def configured():
|
|||||||
|
|
||||||
|
|
||||||
def required_settings():
|
def required_settings():
|
||||||
return ['TINY_CLIENT_ID', 'TINY_CLIENT_SECRET', 'TINY_REDIRECT_URI'] + list(PRODUCT_SETTINGS.values())
|
"""The OAuth application, and a product id for each mode not sold by SKU."""
|
||||||
|
return ['TINY_CLIENT_ID', 'TINY_CLIENT_SECRET', 'TINY_REDIRECT_URI'] + [
|
||||||
|
setting for mode, setting in PRODUCT_SETTINGS.items() if not (sku_prefix(mode) or step_sku(mode))]
|
||||||
|
|
||||||
|
|
||||||
|
def sku_prefix(mode):
|
||||||
|
return os.environ.get(SKU_SETTINGS[mode], '').strip()
|
||||||
|
|
||||||
|
|
||||||
|
def step_sku(mode):
|
||||||
|
return os.environ.get(STEP_SETTINGS[mode], '').strip()
|
||||||
|
|
||||||
|
|
||||||
|
def mode_codes(mode):
|
||||||
|
"""Every SKU code a mode sold by SKU uses, or [] for a mode sold by product id."""
|
||||||
|
if step_sku(mode):
|
||||||
|
return [step_sku(mode)]
|
||||||
|
return sku_codes(sku_prefix(mode)) if sku_prefix(mode) else []
|
||||||
|
|
||||||
|
|
||||||
|
def sku_codes(prefix):
|
||||||
|
"""The metre SKU and the nine of 10 to 90 cm, as the client's catalogue names them."""
|
||||||
|
return [f'{prefix}.100CM'] + [f'{prefix}.{cm:04d}CM' for cm in range(10, 100, 10)]
|
||||||
|
|
||||||
|
|
||||||
|
def tenths(item):
|
||||||
|
"""The billed length in 10 cm steps; the Site bills in them."""
|
||||||
|
billed = Decimal(item['billed_metres'])
|
||||||
|
steps = billed * 10
|
||||||
|
if steps != steps.to_integral_value():
|
||||||
|
raise TinyError(f'{billed} m is not a whole number of 10 cm steps')
|
||||||
|
return int(steps), int(billed * int(item['unit_cents']))
|
||||||
|
|
||||||
|
|
||||||
|
def step_lines(item, code):
|
||||||
|
"""(code, quantity, cents each) for a single 10 cm SKU, adding up to the
|
||||||
|
item's total; a rest that does not divide goes on one line of its own."""
|
||||||
|
steps, total = tenths(item)
|
||||||
|
each, rest = divmod(total, steps)
|
||||||
|
if not rest:
|
||||||
|
return [(code, steps, each)]
|
||||||
|
return [(code, steps - 1, each), (code, 1, each + rest)] if steps > 1 else [(code, 1, total)]
|
||||||
|
|
||||||
|
|
||||||
|
def sku_lines(item, prefix):
|
||||||
|
"""(code, quantity, cents each) adding up to the item: whole metres as the
|
||||||
|
metre SKU, the rest as one 10 cm SKU. The Site bills in 10 cm steps, so
|
||||||
|
the rest always has a SKU; the lines carry the price the customer paid."""
|
||||||
|
billed = Decimal(item['billed_metres'])
|
||||||
|
centimetres = billed * 100
|
||||||
|
if centimetres != centimetres.to_integral_value() or int(centimetres) % 10:
|
||||||
|
raise TinyError(f'{billed} m is not a whole number of 10 cm steps')
|
||||||
|
metres, rest = divmod(int(centimetres), 100)
|
||||||
|
unit = int(item['unit_cents'])
|
||||||
|
lines = [(f'{prefix}.100CM', metres, unit)] if metres else []
|
||||||
|
if rest:
|
||||||
|
# What is left of the item's total, so the order adds up to the cent.
|
||||||
|
lines.append((f'{prefix}.{rest:04d}CM', 1, int(billed * unit) - metres * unit))
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
|
def status_updates():
|
||||||
|
return os.environ.get(STATUS_UPDATES_SETTING, '').lower() == 'true'
|
||||||
|
|
||||||
|
|
||||||
# OAuth -------------------------------------------------------------------
|
# OAuth -------------------------------------------------------------------
|
||||||
@@ -86,44 +183,78 @@ class TinyAuth:
|
|||||||
self.http = httpx.Client(timeout=20, transport=transport)
|
self.http = httpx.Client(timeout=20, transport=transport)
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
|
|
||||||
def authorize_url(self, operator):
|
def authorize_url(self, operator, offline=True):
|
||||||
"""Start a connection. The state is single-use, short-lived, and only an
|
"""Start a connection. The state is single-use, short-lived, and only an
|
||||||
authenticated operator can create one, which is what protects the
|
authenticated operator can create one, which is what protects the
|
||||||
callback: Tiny's redirect back is cross-site, so the operator's
|
callback: Tiny's redirect back is cross-site, so the operator's
|
||||||
SameSite=Strict cookie does not travel with it."""
|
SameSite=Strict cookie does not travel with it.
|
||||||
|
|
||||||
|
offline_access asks for a grant that is not bound to a login session,
|
||||||
|
so the connection lasts as long as it keeps being renewed. Tiny's
|
||||||
|
documented refresh token otherwise lasts one day."""
|
||||||
state = secrets.token_urlsafe(32)
|
state = secrets.token_urlsafe(32)
|
||||||
with self.connect() as c:
|
with self.connect() as c:
|
||||||
c.execute("DELETE FROM dtf_local.oauth_states WHERE expires_at<now()")
|
c.execute("DELETE FROM dtf_local.oauth_states WHERE expires_at<now()")
|
||||||
c.execute('''INSERT INTO dtf_local.oauth_states(state,provider,operator,expires_at)
|
c.execute('''INSERT INTO dtf_local.oauth_states(state,provider,operator,expires_at)
|
||||||
VALUES(%s,'tiny',%s,now()+%s)''', (state, operator, timedelta(minutes=STATE_MINUTES)))
|
VALUES(%s,'tiny',%s,now()+%s)''', (state, operator, timedelta(minutes=STATE_MINUTES)))
|
||||||
return f'{AUTH}/auth?' + urlencode({'response_type': 'code', 'client_id': self.client_id,
|
return f'{AUTH}/auth?' + urlencode({'response_type': 'code', 'client_id': self.client_id,
|
||||||
'redirect_uri': self.redirect_uri, 'scope': 'openid',
|
'redirect_uri': self.redirect_uri,
|
||||||
|
'scope': 'openid offline_access' if offline else 'openid',
|
||||||
'state': state})
|
'state': state})
|
||||||
|
|
||||||
def complete(self, code, state):
|
def _claim(self, c, state):
|
||||||
"""Exchange the authorisation code; returns the operator who started it."""
|
|
||||||
with self.connect() as c:
|
|
||||||
row = c.execute('''DELETE FROM dtf_local.oauth_states WHERE state=%s AND provider='tiny'
|
row = c.execute('''DELETE FROM dtf_local.oauth_states WHERE state=%s AND provider='tiny'
|
||||||
AND expires_at>now() RETURNING operator''', (state,)).fetchone()
|
AND expires_at>now() RETURNING operator''', (state,)).fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
raise TinyError('Unknown or expired authorisation state')
|
raise TinyError('Unknown or expired authorisation state')
|
||||||
|
return row['operator']
|
||||||
|
|
||||||
|
def complete(self, code, state):
|
||||||
|
"""Exchange the authorisation code; returns the operator who started it."""
|
||||||
|
with self.connect() as c:
|
||||||
|
operator = self._claim(c, state)
|
||||||
tokens = self._token({'grant_type': 'authorization_code', 'code': code,
|
tokens = self._token({'grant_type': 'authorization_code', 'code': code,
|
||||||
'redirect_uri': self.redirect_uri})
|
'redirect_uri': self.redirect_uri})
|
||||||
self._store(c, tokens, row['operator'])
|
self._store(c, tokens, operator)
|
||||||
return row['operator']
|
return operator
|
||||||
|
|
||||||
|
def without_offline(self, state):
|
||||||
|
"""Tiny refused the offline_access scope for this application: spend the
|
||||||
|
operator's state and start again with a session-bound grant."""
|
||||||
|
with self.connect() as c:
|
||||||
|
operator = self._claim(c, state)
|
||||||
|
return self.authorize_url(operator, offline=False)
|
||||||
|
|
||||||
def status(self):
|
def status(self):
|
||||||
with self.connect() as c:
|
with self.connect() as c:
|
||||||
row = c.execute('''SELECT connected_by,connected_at,refresh_expires_at
|
row = c.execute('''SELECT connected_by,connected_at,refresh_expires_at,updated_at,offline,
|
||||||
FROM dtf_local.provider_tokens WHERE provider='tiny' ''').fetchone()
|
refresh_failed_at,refused_at FROM dtf_local.provider_tokens WHERE provider='tiny' ''').fetchone()
|
||||||
if not row:
|
if not row:
|
||||||
return {'connected': False}
|
return {'connected': False}
|
||||||
expired = row['refresh_expires_at'] and row['refresh_expires_at'] <= datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
return {'connected': not expired, 'connected_by': row['connected_by'],
|
expires = row['refresh_expires_at']
|
||||||
'connected_at': row['connected_at'], 'expires_at': row['refresh_expires_at']}
|
expired = bool(expires and expires <= now)
|
||||||
|
if row['refused_at']:
|
||||||
|
problem = 'refused'
|
||||||
|
elif expired:
|
||||||
|
problem = 'expired'
|
||||||
|
elif row['refresh_failed_at']:
|
||||||
|
problem = 'renewal-failing'
|
||||||
|
elif expires and expires - now < EXPIRY_WARNING:
|
||||||
|
problem = 'expiring'
|
||||||
|
else:
|
||||||
|
problem = None
|
||||||
|
return {'connected': not expired and not row['refused_at'], 'problem': problem,
|
||||||
|
'connected_by': row['connected_by'], 'connected_at': row['connected_at'],
|
||||||
|
'renewed_at': row['updated_at'], 'expires_at': expires, 'offline': row['offline'],
|
||||||
|
'failed_at': row['refresh_failed_at']}
|
||||||
|
|
||||||
def access_token(self):
|
def access_token(self):
|
||||||
"""A valid access token, refreshing (and rotating) under a row lock."""
|
"""A valid access token, refreshing (and rotating) under a row lock.
|
||||||
|
|
||||||
|
A failed renewal is recorded after the lock is released, so the Kanban
|
||||||
|
can show it. A refused refresh token is never tried again: only a new
|
||||||
|
connection helps, and retrying it would only repeat the refusal."""
|
||||||
with self.connect() as c:
|
with self.connect() as c:
|
||||||
row = c.execute('''SELECT * FROM dtf_local.provider_tokens WHERE provider='tiny'
|
row = c.execute('''SELECT * FROM dtf_local.provider_tokens WHERE provider='tiny'
|
||||||
FOR UPDATE''').fetchone()
|
FOR UPDATE''').fetchone()
|
||||||
@@ -132,11 +263,25 @@ class TinyAuth:
|
|||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
if row['access_expires_at'] > now + timedelta(seconds=60):
|
if row['access_expires_at'] > now + timedelta(seconds=60):
|
||||||
return row['access_token']
|
return row['access_token']
|
||||||
|
if row['refused_at']:
|
||||||
|
raise TinyNotConnected('Tiny refused the stored refresh token; connect again on the Kanban')
|
||||||
if row['refresh_expires_at'] and row['refresh_expires_at'] <= now:
|
if row['refresh_expires_at'] and row['refresh_expires_at'] <= now:
|
||||||
raise TinyNotConnected('The Tiny connection expired; connect again on the Kanban')
|
raise TinyNotConnected('The Tiny connection expired; connect again on the Kanban')
|
||||||
|
try:
|
||||||
tokens = self._token({'grant_type': 'refresh_token', 'refresh_token': row['refresh_token']})
|
tokens = self._token({'grant_type': 'refresh_token', 'refresh_token': row['refresh_token']})
|
||||||
|
except (TinyError, httpx.HTTPError, ValueError) as exc:
|
||||||
|
failure = exc
|
||||||
|
else:
|
||||||
self._store(c, tokens, row['connected_by'], refreshed=True)
|
self._store(c, tokens, row['connected_by'], refreshed=True)
|
||||||
return tokens['access_token']
|
return tokens['access_token']
|
||||||
|
# Only against the token that failed: another worker may have renewed since.
|
||||||
|
with self.connect() as c:
|
||||||
|
c.execute('''UPDATE dtf_local.provider_tokens SET refresh_failed_at=now(), refresh_error=%s,
|
||||||
|
refused_at=CASE WHEN %s THEN now() ELSE refused_at END
|
||||||
|
WHERE provider='tiny' AND refresh_token=%s''',
|
||||||
|
(str(failure)[:300] or type(failure).__name__, isinstance(failure, TinyNotConnected),
|
||||||
|
row['refresh_token']))
|
||||||
|
raise failure
|
||||||
|
|
||||||
def _token(self, form):
|
def _token(self, form):
|
||||||
response = self.http.post(f'{AUTH}/token', data={**form, 'client_id': self.client_id,
|
response = self.http.post(f'{AUTH}/token', data={**form, 'client_id': self.client_id,
|
||||||
@@ -152,18 +297,21 @@ class TinyAuth:
|
|||||||
def _store(self, c, tokens, operator, refreshed=False):
|
def _store(self, c, tokens, operator, refreshed=False):
|
||||||
now = datetime.now(timezone.utc)
|
now = datetime.now(timezone.utc)
|
||||||
access_expires = now + timedelta(seconds=int(tokens.get('expires_in', 300)))
|
access_expires = now + timedelta(seconds=int(tokens.get('expires_in', 300)))
|
||||||
|
# An offline grant reports refresh_expires_in 0: no fixed end.
|
||||||
refresh_in = tokens.get('refresh_expires_in')
|
refresh_in = tokens.get('refresh_expires_in')
|
||||||
refresh_expires = now + timedelta(seconds=int(refresh_in)) if refresh_in else None
|
refresh_expires = now + timedelta(seconds=int(refresh_in)) if refresh_in else None
|
||||||
|
offline = 'offline_access' in str(tokens.get('scope') or '').split()
|
||||||
c.execute('''INSERT INTO dtf_local.provider_tokens(provider,access_token,refresh_token,
|
c.execute('''INSERT INTO dtf_local.provider_tokens(provider,access_token,refresh_token,
|
||||||
access_expires_at,refresh_expires_at,connected_by,connected_at,updated_at)
|
access_expires_at,refresh_expires_at,connected_by,connected_at,updated_at,offline)
|
||||||
VALUES('tiny',%s,%s,%s,%s,%s,now(),now())
|
VALUES('tiny',%s,%s,%s,%s,%s,now(),now(),%s)
|
||||||
ON CONFLICT(provider) DO UPDATE SET access_token=EXCLUDED.access_token,
|
ON CONFLICT(provider) DO UPDATE SET access_token=EXCLUDED.access_token,
|
||||||
refresh_token=EXCLUDED.refresh_token, access_expires_at=EXCLUDED.access_expires_at,
|
refresh_token=EXCLUDED.refresh_token, access_expires_at=EXCLUDED.access_expires_at,
|
||||||
refresh_expires_at=EXCLUDED.refresh_expires_at, updated_at=now(),
|
refresh_expires_at=EXCLUDED.refresh_expires_at, updated_at=now(), offline=EXCLUDED.offline,
|
||||||
|
refresh_failed_at=NULL, refresh_error=NULL, refused_at=NULL,
|
||||||
connected_by=CASE WHEN %s THEN dtf_local.provider_tokens.connected_by ELSE EXCLUDED.connected_by END,
|
connected_by=CASE WHEN %s THEN dtf_local.provider_tokens.connected_by ELSE EXCLUDED.connected_by END,
|
||||||
connected_at=CASE WHEN %s THEN dtf_local.provider_tokens.connected_at ELSE now() END''',
|
connected_at=CASE WHEN %s THEN dtf_local.provider_tokens.connected_at ELSE now() END''',
|
||||||
(tokens['access_token'], tokens['refresh_token'], access_expires, refresh_expires,
|
(tokens['access_token'], tokens['refresh_token'], access_expires, refresh_expires,
|
||||||
operator, refreshed, refreshed))
|
operator, offline, refreshed, refreshed))
|
||||||
|
|
||||||
|
|
||||||
# Orders ------------------------------------------------------------------
|
# Orders ------------------------------------------------------------------
|
||||||
@@ -172,12 +320,17 @@ def money(cents):
|
|||||||
return float(Decimal(cents) / 100)
|
return float(Decimal(cents) / 100)
|
||||||
|
|
||||||
|
|
||||||
def contact_payload(order):
|
def contact_payload(order, company=None):
|
||||||
|
"""A new contact, named after the company's razão social when the
|
||||||
|
registry gave it (app/cnpj.py), else the recipient or the e-mail."""
|
||||||
customer = order['customer']
|
customer = order['customer']
|
||||||
destination = order.get('destination')
|
destination = order.get('destination')
|
||||||
contact = {'nome': (destination or {}).get('recipient') or customer['mail'],
|
company = company or {}
|
||||||
|
contact = {'nome': company.get('legal') or (destination or {}).get('recipient') or customer['mail'],
|
||||||
'tipoPessoa': 'J', 'cpfCnpj': customer['cnpj'], 'email': customer['mail'],
|
'tipoPessoa': 'J', 'cpfCnpj': customer['cnpj'], 'email': customer['mail'],
|
||||||
'celular': customer['zap'], 'situacao': 'A'}
|
'celular': customer['zap'], 'situacao': 'A'}
|
||||||
|
if company.get('trade'):
|
||||||
|
contact['fantasia'] = company['trade']
|
||||||
if destination:
|
if destination:
|
||||||
contact['endereco'] = address(destination)
|
contact['endereco'] = address(destination)
|
||||||
return contact
|
return contact
|
||||||
@@ -190,11 +343,24 @@ def address(destination):
|
|||||||
'uf': destination['state'], 'pais': 'Brasil'}
|
'uf': destination['state'], 'pais': 'Brasil'}
|
||||||
|
|
||||||
|
|
||||||
def order_payload(payload, contact_id, today=None):
|
def order_payload(payload, contact_id, today=None, product_id=None):
|
||||||
"""The v3 'pedido' for a paid order's approved snapshot."""
|
"""The v3 'pedido' for a paid order's approved snapshot. product_id finds a
|
||||||
|
SKU's product id by its code, for the modes sold by SKU."""
|
||||||
order = payload['order']
|
order = payload['order']
|
||||||
items = []
|
items = []
|
||||||
for item in order['items']:
|
for item in order['items']:
|
||||||
|
note = PRODUCTS[item['mode']] + f" · nota {item['grade']}"
|
||||||
|
prefix, step = sku_prefix(item['mode']), step_sku(item['mode'])
|
||||||
|
if step or prefix:
|
||||||
|
if product_id is None:
|
||||||
|
raise TinyError(f"{SKU_SETTINGS[item['mode']]} needs the SKUs looked up in Tiny")
|
||||||
|
lines = step_lines(item, step) if step else sku_lines(item, prefix)
|
||||||
|
for index, (code, quantity, cents) in enumerate(lines):
|
||||||
|
line = {'produto': {'id': product_id(code)}, 'quantidade': quantity, 'valorUnitario': money(cents)}
|
||||||
|
if index == 0:
|
||||||
|
line['infoAdicional'] = note + f" · {item['billed_metres']} m"
|
||||||
|
items.append(line)
|
||||||
|
continue
|
||||||
setting = PRODUCT_SETTINGS[item['mode']]
|
setting = PRODUCT_SETTINGS[item['mode']]
|
||||||
product = os.environ.get(setting, '')
|
product = os.environ.get(setting, '')
|
||||||
if not product.isdigit():
|
if not product.isdigit():
|
||||||
@@ -202,7 +368,7 @@ def order_payload(payload, contact_id, today=None):
|
|||||||
items.append({'produto': {'id': int(product)},
|
items.append({'produto': {'id': int(product)},
|
||||||
'quantidade': float(Decimal(item['billed_metres'])),
|
'quantidade': float(Decimal(item['billed_metres'])),
|
||||||
'valorUnitario': money(item['unit_cents']),
|
'valorUnitario': money(item['unit_cents']),
|
||||||
'infoAdicional': PRODUCTS[item['mode']] + f" · nota {item['grade']}"})
|
'infoAdicional': note})
|
||||||
freight = order['freight']
|
freight = order['freight']
|
||||||
pickup = freight.get('service') == 'pickup'
|
pickup = freight.get('service') == 'pickup'
|
||||||
pedido = {'data': (today or local_today()).isoformat(),
|
pedido = {'data': (today or local_today()).isoformat(),
|
||||||
@@ -212,6 +378,9 @@ def order_payload(payload, contact_id, today=None):
|
|||||||
'valorFrete': money(freight['total_cents']),
|
'valorFrete': money(freight['total_cents']),
|
||||||
'observacoes': f"Pedido DTF #{payload['number']}" + (' · retirada em Franca' if pickup else ''),
|
'observacoes': f"Pedido DTF #{payload['number']}" + (' · retirada em Franca' if pickup else ''),
|
||||||
'observacoesInternas': f"Site DTF · pago · {payload['order_id']}"}
|
'observacoesInternas': f"Site DTF · pago · {payload['order_id']}"}
|
||||||
|
pickup_method = os.environ.get(PICKUP_SETTING, '')
|
||||||
|
if pickup and pickup_method.isdigit():
|
||||||
|
pedido['transportador'] = {'formaEnvio': {'id': int(pickup_method)}}
|
||||||
destination = order.get('destination')
|
destination = order.get('destination')
|
||||||
if destination:
|
if destination:
|
||||||
pedido['enderecoEntrega'] = {**address(destination), 'enderecoNro': destination['number'],
|
pedido['enderecoEntrega'] = {**address(destination), 'enderecoNro': destination['number'],
|
||||||
@@ -223,14 +392,76 @@ def order_payload(payload, contact_id, today=None):
|
|||||||
return pedido
|
return pedido
|
||||||
|
|
||||||
|
|
||||||
|
def configured_product(orders, mode):
|
||||||
|
"""The Tiny product a mode's setting points at, or a reason it cannot be used."""
|
||||||
|
setting = PRODUCT_SETTINGS[mode]
|
||||||
|
value = os.environ.get(setting, '')
|
||||||
|
if not value.isdigit():
|
||||||
|
return None, f'{setting} sem id'
|
||||||
|
found = orders.request('GET', f'/produtos/{value}')
|
||||||
|
if found.get('situacao') != 'A':
|
||||||
|
return found, f"produto {value} não está ativo no Tiny ({found.get('situacao')})"
|
||||||
|
return found, 'ok'
|
||||||
|
|
||||||
|
|
||||||
|
def configured_skus(orders, mode):
|
||||||
|
"""Whether every SKU of a mode sold by SKU is an active product in Tiny."""
|
||||||
|
missing = []
|
||||||
|
for code in mode_codes(mode):
|
||||||
|
try:
|
||||||
|
orders.product_id(code)
|
||||||
|
except TinyError:
|
||||||
|
missing.append(code)
|
||||||
|
return 'ok' if not missing else 'SKU não encontrado entre os produtos ativos: ' + ', '.join(missing)
|
||||||
|
|
||||||
|
|
||||||
|
def configured_pickup(orders):
|
||||||
|
"""The forma de envio the pickup setting points at, or why it cannot be used."""
|
||||||
|
value = os.environ.get(PICKUP_SETTING, '')
|
||||||
|
if not value.isdigit():
|
||||||
|
return None, f'{PICKUP_SETTING} sem id'
|
||||||
|
return orders.request('GET', f'/formas-envio/{value}'), 'ok'
|
||||||
|
|
||||||
|
|
||||||
|
def check(auth=None, transport=None, orders=None):
|
||||||
|
"""Read-only proof that the connection and permissions work: one order and
|
||||||
|
one contact listed, each configured product found active and the pickup
|
||||||
|
forma de envio found, nothing created. Raises TinyNotConnected when there is no usable connection;
|
||||||
|
otherwise reports each read separately."""
|
||||||
|
orders = orders or TinyOrders(auth=auth or TinyAuth(), transport=transport)
|
||||||
|
|
||||||
|
def listed(path):
|
||||||
|
orders.request('GET', path, params={'limit': 1})
|
||||||
|
return 'ok'
|
||||||
|
|
||||||
|
reads = [('pedidos', lambda: listed('/pedidos')), ('contatos', lambda: listed('/contatos'))]
|
||||||
|
reads += [(PRODUCTS[mode], lambda mode=mode: configured_skus(orders, mode) if mode_codes(mode)
|
||||||
|
else configured_product(orders, mode)[1]) for mode in PRODUCT_SETTINGS]
|
||||||
|
reads.append(('forma de envio de retirada', lambda: configured_pickup(orders)[1]))
|
||||||
|
results = {}
|
||||||
|
for name, read in reads:
|
||||||
|
try:
|
||||||
|
results[name] = read()
|
||||||
|
except TinyNotConnected:
|
||||||
|
raise
|
||||||
|
except TinyError as exc:
|
||||||
|
results[name] = str(exc)[:200]
|
||||||
|
except httpx.HTTPError:
|
||||||
|
results[name] = 'sem resposta do Tiny'
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
class TinyOrders:
|
class TinyOrders:
|
||||||
def __init__(self, auth=None, transport=None, today=None):
|
def __init__(self, auth=None, transport=None, today=None, names=None):
|
||||||
missing = [name for name in required_settings() if not os.environ.get(name)]
|
missing = [name for name in required_settings() if not os.environ.get(name)]
|
||||||
if auth is None and missing:
|
if auth is None and missing:
|
||||||
raise RuntimeError('Tiny needs ' + ', '.join(missing))
|
raise RuntimeError('Tiny needs ' + ', '.join(missing))
|
||||||
self.auth = auth or TinyAuth()
|
self.auth = auth or TinyAuth()
|
||||||
self.http = httpx.Client(base_url=API, transport=transport, timeout=30)
|
self.http = httpx.Client(base_url=API, transport=transport, timeout=30)
|
||||||
self.today = today
|
self.today = today
|
||||||
|
self.skus = {}
|
||||||
|
# The company's names by CNPJ, for new contacts (app.cnpj.company).
|
||||||
|
self.names = names
|
||||||
|
|
||||||
def request(self, method, path, **kwargs):
|
def request(self, method, path, **kwargs):
|
||||||
headers = {'Authorization': f'Bearer {self.auth.access_token()}'}
|
headers = {'Authorization': f'Bearer {self.auth.access_token()}'}
|
||||||
@@ -241,18 +472,37 @@ class TinyOrders:
|
|||||||
raise TinyError(f'{method} {path}: {response.status_code} {response.text[:300]}')
|
raise TinyError(f'{method} {path}: {response.status_code} {response.text[:300]}')
|
||||||
return response.json() if response.content else {}
|
return response.json() if response.content else {}
|
||||||
|
|
||||||
|
def product_id(self, code):
|
||||||
|
"""The id of the active product with this SKU code, remembered once found."""
|
||||||
|
if code not in self.skus:
|
||||||
|
found = self.request('GET', '/produtos', params={'codigo': code, 'situacao': 'A', 'limit': 10})
|
||||||
|
# The filter may match more than the exact code: DTFIMP.TX.010CM is
|
||||||
|
# a different product from DTFIMP.TX.0010CM.
|
||||||
|
match = [p for p in found.get('itens') or []
|
||||||
|
if str(p.get('sku') or p.get('codigo') or '') == code and p.get('situacao', 'A') == 'A']
|
||||||
|
if not match:
|
||||||
|
raise TinyError(f'SKU {code} não encontrado entre os produtos ativos do Tiny')
|
||||||
|
self.skus[code] = int(match[0]['id'])
|
||||||
|
return self.skus[code]
|
||||||
|
|
||||||
def contact(self, order):
|
def contact(self, order):
|
||||||
cnpj = order['customer']['cnpj']
|
cnpj = order['customer']['cnpj']
|
||||||
found = self.request('GET', '/contatos', params={'cpfCnpj': cnpj, 'limit': 5})
|
found = self.request('GET', '/contatos', params={'cpfCnpj': cnpj, 'limit': 5})
|
||||||
for entry in found.get('itens') or []:
|
for entry in found.get('itens') or []:
|
||||||
if ''.join(ch for ch in str(entry.get('cpfCnpj') or '') if ch.isdigit()) == cnpj:
|
if ''.join(ch for ch in str(entry.get('cpfCnpj') or '') if ch.isdigit()) == cnpj:
|
||||||
return entry['id']
|
return entry['id']
|
||||||
return self.request('POST', '/contatos', json=contact_payload(order))['id']
|
company = None
|
||||||
|
if self.names:
|
||||||
|
try:
|
||||||
|
company = self.names(cnpj)
|
||||||
|
except Exception: # a name is a nicety; the order must still go
|
||||||
|
company = None
|
||||||
|
return self.request('POST', '/contatos', json=contact_payload(order, company))['id']
|
||||||
|
|
||||||
def find(self, payload):
|
def find(self, payload, days=SEARCH_DAYS):
|
||||||
"""An order a previous delivery already created, or None."""
|
"""An order a previous delivery already created, or None."""
|
||||||
wanted = purchase_order(payload['number'])
|
wanted = purchase_order(payload['number'])
|
||||||
since = ((self.today or local_today()) - timedelta(days=SEARCH_DAYS)).isoformat()
|
since = ((self.today or local_today()) - timedelta(days=days)).isoformat()
|
||||||
found = self.request('GET', '/pedidos', params={'cpfCnpj': payload['order']['customer']['cnpj'],
|
found = self.request('GET', '/pedidos', params={'cpfCnpj': payload['order']['customer']['cnpj'],
|
||||||
'dataInicial': since, 'limit': 100})
|
'dataInicial': since, 'limit': 100})
|
||||||
for entry in found.get('itens') or []:
|
for entry in found.get('itens') or []:
|
||||||
@@ -261,16 +511,56 @@ class TinyOrders:
|
|||||||
return detail
|
return detail
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def set_situacao(self, tiny_id, situacao):
|
||||||
|
self.request('PUT', f'/pedidos/{tiny_id}/situacao', json={'situacao': situacao})
|
||||||
|
|
||||||
def deliver(self, event_key, payload):
|
def deliver(self, event_key, payload):
|
||||||
if payload.get('event') != 'payment_approved':
|
event = payload.get('event')
|
||||||
# Production progress is not written to Tiny; only the sale is.
|
if event == 'payment_approved':
|
||||||
return {'provider': 'tiny', 'event_key': event_key, 'status': 'not-applicable',
|
return self.sale(event_key, payload)
|
||||||
'event': payload.get('event')}
|
if event == 'ready' and status_updates():
|
||||||
|
return self.ready(event_key, payload)
|
||||||
|
# Other production events have no Tiny situação and are not written.
|
||||||
|
return {'provider': 'tiny', 'event_key': event_key, 'status': 'not-applicable', 'event': event}
|
||||||
|
|
||||||
|
def sale(self, event_key, payload):
|
||||||
|
"""Create the order once; with status updates on, approve it once.
|
||||||
|
|
||||||
|
An order is created "Aberta" and then set to "Aprovada", so a retry
|
||||||
|
after a failure between the two finds it still open and finishes the
|
||||||
|
job, and an order someone has already moved on is left alone."""
|
||||||
existing = self.find(payload)
|
existing = self.find(payload)
|
||||||
if existing:
|
if existing:
|
||||||
return {'provider': 'tiny', 'event_key': event_key, 'status': 'already-created',
|
receipt = {'provider': 'tiny', 'event_key': event_key, 'status': 'already-created',
|
||||||
'tiny_id': str(existing.get('id')), 'tiny_number': str(existing.get('numeroPedido'))}
|
'tiny_id': str(existing.get('id')), 'tiny_number': str(existing.get('numeroPedido'))}
|
||||||
|
tiny_id, situacao = existing.get('id'), existing.get('situacao')
|
||||||
|
else:
|
||||||
contact_id = self.contact(payload['order'])
|
contact_id = self.contact(payload['order'])
|
||||||
created = self.request('POST', '/pedidos', json=order_payload(payload, contact_id, self.today))
|
created = self.request('POST', '/pedidos', json=order_payload(payload, contact_id, self.today,
|
||||||
return {'provider': 'tiny', 'event_key': event_key, 'status': 'created',
|
product_id=self.product_id))
|
||||||
|
receipt = {'provider': 'tiny', 'event_key': event_key, 'status': 'created',
|
||||||
'tiny_id': str(created.get('id')), 'tiny_number': str(created.get('numeroPedido'))}
|
'tiny_id': str(created.get('id')), 'tiny_number': str(created.get('numeroPedido'))}
|
||||||
|
tiny_id, situacao = created.get('id'), ABERTA
|
||||||
|
if status_updates() and situacao == ABERTA:
|
||||||
|
self.set_situacao(tiny_id, APROVADA)
|
||||||
|
receipt['situacao'] = SITUACOES[APROVADA]
|
||||||
|
return receipt
|
||||||
|
|
||||||
|
def ready(self, event_key, payload):
|
||||||
|
"""A pickup order became ready: "Pronto para envio", which the client's
|
||||||
|
notices turn into the pickup message. Shipped orders get "Enviada"
|
||||||
|
once freight exists (1.2)."""
|
||||||
|
base = {'provider': 'tiny', 'event_key': event_key}
|
||||||
|
order = payload.get('order') or {}
|
||||||
|
if (order.get('freight') or {}).get('service') != 'pickup':
|
||||||
|
return {**base, 'status': 'not-applicable', 'event': 'ready'}
|
||||||
|
tiny_id = payload.get('tiny_id')
|
||||||
|
detail = self.request('GET', f'/pedidos/{tiny_id}') if tiny_id else self.find(payload, READY_SEARCH_DAYS)
|
||||||
|
if not detail:
|
||||||
|
raise TinyError(f"{purchase_order(payload['number'])} is not in Tiny yet; the outbox will retry")
|
||||||
|
base.update(tiny_id=str(detail.get('id')), tiny_number=str(detail.get('numeroPedido')))
|
||||||
|
situacao = detail.get('situacao')
|
||||||
|
if situacao in (PRONTO_ENVIO, ENVIADA, ENTREGUE, CANCELADA, NAO_ENTREGUE):
|
||||||
|
return {**base, 'status': 'status-unchanged', 'situacao': SITUACOES.get(situacao, str(situacao))}
|
||||||
|
self.set_situacao(detail['id'], PRONTO_ENVIO)
|
||||||
|
return {**base, 'status': 'status-updated', 'situacao': SITUACOES[PRONTO_ENVIO]}
|
||||||
|
|||||||
177
app/tiny_probe.py
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
"""Supervised checks against the client's real Tiny, run by hand from a
|
||||||
|
container console (docker exec, or Portainer > Containers > worker > Console).
|
||||||
|
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 pedido --cnpj ... --email ... --celular ... [--modo file] [--metros 1.3]
|
||||||
|
show the test order; --confirmar creates it
|
||||||
|
|
||||||
|
The test order goes through TinyOrders.deliver, exactly as the worker sends a
|
||||||
|
paid order. The duplicate guard is then proven read-only first (the order must
|
||||||
|
be found by its purchase-order number) and only then by a second delivery,
|
||||||
|
which must return the existing order. If the search cannot find the order, the
|
||||||
|
second delivery is not attempted: it would create a duplicate. Cancel the test
|
||||||
|
order in Olist afterwards.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
from decimal import Decimal, InvalidOperation
|
||||||
|
|
||||||
|
from .core.pricing import TIERS
|
||||||
|
from .core.secrets import load as load_secret_files
|
||||||
|
from . import tiny
|
||||||
|
|
||||||
|
FIND_ATTEMPTS = 4
|
||||||
|
FIND_WAIT_SECONDS = 5
|
||||||
|
|
||||||
|
|
||||||
|
def test_order(cnpj, email, celular, mode, now=None, metres='1'):
|
||||||
|
number = 'TESTE-' + (now or datetime.now(tiny.BRASILIA)).strftime('%Y%m%d%H%M')
|
||||||
|
unit = TIERS[mode][0][1]
|
||||||
|
total = int(Decimal(metres) * unit)
|
||||||
|
return {'order_id': 'teste-integracao', 'number': number, 'event': 'payment_approved',
|
||||||
|
'order': {'customer': {'cnpj': cnpj, 'mail': email, 'zap': celular},
|
||||||
|
'items': [{'mode': mode, 'grade': 100, 'billed_metres': metres, 'unit_cents': unit,
|
||||||
|
'total_cents': total}],
|
||||||
|
'freight': {'service': 'pickup', 'total_cents': 0},
|
||||||
|
'destination': None, 'total_cents': total}}
|
||||||
|
|
||||||
|
|
||||||
|
def produtos(orders, termo, out):
|
||||||
|
params = {'situacao': 'A', 'limit': 100}
|
||||||
|
if termo:
|
||||||
|
params['nome'] = termo
|
||||||
|
found = orders.request('GET', '/produtos', params=params).get('itens') or []
|
||||||
|
for item in found:
|
||||||
|
preco = (item.get('precos') or {}).get('preco')
|
||||||
|
out(f"{item['id']}\t{item.get('sku') or '-'}\t{item.get('descricao')}\tR$ {preco}")
|
||||||
|
out(f'{len(found)} produto(s) ativo(s)' + (f' com "{termo}"' if termo else '') + '.')
|
||||||
|
|
||||||
|
|
||||||
|
def formas_envio(orders, termo, out):
|
||||||
|
params = {'limit': 100}
|
||||||
|
if termo:
|
||||||
|
params['nome'] = termo
|
||||||
|
found = orders.request('GET', '/formas-envio', params=params).get('itens') or []
|
||||||
|
for item in found:
|
||||||
|
out(f"{item.get('id')}\t{item.get('tipo')}\t{item.get('nome')}")
|
||||||
|
out(f'{len(found)} forma(s) de envio' + (f' com "{termo}"' if termo else '') +
|
||||||
|
'. A retirada é a do tipo 6 (Customizado) que a equipe usa para retirar pessoalmente.')
|
||||||
|
|
||||||
|
|
||||||
|
def conferir(orders, out):
|
||||||
|
ok = True
|
||||||
|
for mode, setting in tiny.PRODUCT_SETTINGS.items():
|
||||||
|
prefix, step = tiny.sku_prefix(mode), tiny.step_sku(mode)
|
||||||
|
if step:
|
||||||
|
result = tiny.configured_skus(orders, mode)
|
||||||
|
ok &= result == 'ok'
|
||||||
|
out(f'{tiny.STEP_SETTINGS[mode]} ({tiny.PRODUCTS[mode]}): {result} ({step} a cada 10 cm)')
|
||||||
|
continue
|
||||||
|
if prefix:
|
||||||
|
result = tiny.configured_skus(orders, mode)
|
||||||
|
ok &= result == 'ok'
|
||||||
|
out(f'{tiny.SKU_SETTINGS[mode]} ({tiny.PRODUCTS[mode]}): {result} '
|
||||||
|
f'({prefix}.100CM e {prefix}.0010CM a {prefix}.0090CM)')
|
||||||
|
continue
|
||||||
|
product, result = tiny.configured_product(orders, mode)
|
||||||
|
ok &= result == 'ok'
|
||||||
|
described = f"{product.get('sku') or '-'} · {product.get('descricao')}" if product else ''
|
||||||
|
out(f'{setting} ({tiny.PRODUCTS[mode]}): {result} {described}'.rstrip())
|
||||||
|
method, result = tiny.configured_pickup(orders)
|
||||||
|
ok &= result == 'ok'
|
||||||
|
out(f"{tiny.PICKUP_SETTING} (retirada): {result} {method.get('nome') if method else ''}".rstrip())
|
||||||
|
out(f"{tiny.STATUS_UPDATES_SETTING}: {'ligado' if tiny.status_updates() else 'desligado'}")
|
||||||
|
for name, result in tiny.check(orders=orders).items():
|
||||||
|
if name in ('pedidos', 'contatos'):
|
||||||
|
ok &= result == 'ok'
|
||||||
|
out(f'{name}: {result}')
|
||||||
|
out('Tudo conferido.' if ok else 'Há pendências acima.')
|
||||||
|
return ok
|
||||||
|
|
||||||
|
|
||||||
|
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'))
|
||||||
|
out('Pedido que será enviado:')
|
||||||
|
out(json.dumps(tiny.order_payload(payload, known[0]['id'] if known else 0, product_id=orders.product_id),
|
||||||
|
ensure_ascii=False, indent=2))
|
||||||
|
if not confirm:
|
||||||
|
out('Nada foi criado. Repita com --confirmar para criar este pedido no Tiny.')
|
||||||
|
return None
|
||||||
|
first = orders.deliver('teste-integracao', payload)
|
||||||
|
out(f"1º envio: {first['status']} · Tiny id {first['tiny_id']} · nº {first['tiny_number']}")
|
||||||
|
for attempt in range(FIND_ATTEMPTS):
|
||||||
|
found = orders.find(payload)
|
||||||
|
if found:
|
||||||
|
break
|
||||||
|
if attempt < FIND_ATTEMPTS - 1:
|
||||||
|
wait(FIND_WAIT_SECONDS)
|
||||||
|
else:
|
||||||
|
out(f'FALHA: a busca por {tiny.purchase_order(payload["number"])} não encontrou o pedido. '
|
||||||
|
'Um reenvio criaria um duplicado; o 2º envio não foi feito. Cancele o pedido no Olist.')
|
||||||
|
return False
|
||||||
|
second = orders.deliver('teste-integracao', payload)
|
||||||
|
out(f"2º envio: {second['status']} · Tiny id {second['tiny_id']}")
|
||||||
|
passed = second['status'] == 'already-created' and second['tiny_id'] == first['tiny_id']
|
||||||
|
out(('OK: o reenvio encontrou o mesmo pedido.' if passed else 'FALHA: o reenvio não devolveu o mesmo pedido.')
|
||||||
|
+ f" Cancele o pedido nº {first['tiny_number']} no Olist.")
|
||||||
|
return passed
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None, orders=None, out=print):
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m app.tiny_probe')
|
||||||
|
commands = parser.add_subparsers(dest='command', required=True)
|
||||||
|
listing = commands.add_parser('produtos')
|
||||||
|
listing.add_argument('termo', nargs='?', default='')
|
||||||
|
methods = commands.add_parser('formas-envio')
|
||||||
|
methods.add_argument('termo', nargs='?', default='')
|
||||||
|
commands.add_parser('conferir')
|
||||||
|
order = commands.add_parser('pedido')
|
||||||
|
order.add_argument('--cnpj', required=True)
|
||||||
|
order.add_argument('--email', required=True)
|
||||||
|
order.add_argument('--celular', required=True)
|
||||||
|
order.add_argument('--modo', choices=sorted(tiny.PRODUCT_SETTINGS), default='file')
|
||||||
|
order.add_argument('--metros', default='1', help='metragem cobrada, em passos de 10 cm (ex.: 1.3)')
|
||||||
|
order.add_argument('--confirmar', action='store_true')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
if orders is None:
|
||||||
|
load_secret_files()
|
||||||
|
orders = tiny.TinyOrders(auth=tiny.TinyAuth())
|
||||||
|
try:
|
||||||
|
if args.command == 'produtos':
|
||||||
|
produtos(orders, args.termo, out)
|
||||||
|
return 0
|
||||||
|
if args.command == 'formas-envio':
|
||||||
|
formas_envio(orders, args.termo, out)
|
||||||
|
return 0
|
||||||
|
if args.command == 'conferir':
|
||||||
|
return 0 if conferir(orders, out) else 1
|
||||||
|
cnpj = ''.join(ch for ch in args.cnpj if ch.isdigit())
|
||||||
|
if len(cnpj) != 14:
|
||||||
|
out('Informe um CNPJ com 14 dígitos.')
|
||||||
|
return 2
|
||||||
|
try:
|
||||||
|
metres = Decimal(args.metros.replace(',', '.'))
|
||||||
|
except InvalidOperation:
|
||||||
|
metres = Decimal(0)
|
||||||
|
if metres < 1 or (metres * 10) % 1:
|
||||||
|
out('Informe a metragem a partir de 1 m, em passos de 10 cm (ex.: 1.3).')
|
||||||
|
return 2
|
||||||
|
payload = test_order(cnpj, args.email, args.celular, args.modo, metres=str(metres))
|
||||||
|
return 0 if pedido(orders, payload, args.confirmar, out) is not False else 1
|
||||||
|
except tiny.TinyError as exc:
|
||||||
|
out(f'Tiny: {exc}')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -18,9 +18,13 @@ adapters = {'tiny': FakeTiny(), 'whatsapp': FakeWhatsApp()}
|
|||||||
# Not yet confirmed against the client's account; see app/tiny.py.
|
# Not yet confirmed against the client's account; see app/tiny.py.
|
||||||
if os.environ.get('TINY_ADAPTER') == 'tiny':
|
if os.environ.get('TINY_ADAPTER') == 'tiny':
|
||||||
from .tiny import TinyOrders
|
from .tiny import TinyOrders
|
||||||
adapters['tiny'] = TinyOrders()
|
from .cnpj import company
|
||||||
|
adapters['tiny'] = TinyOrders(names=company)
|
||||||
last_tick = 0.0
|
last_tick = 0.0
|
||||||
last_cleanup = 0.0
|
last_cleanup = 0.0
|
||||||
|
last_reconcile = 0.0
|
||||||
|
last_names = 0.0
|
||||||
|
names_thread = None
|
||||||
last_tiny_keepalive = 0.0
|
last_tiny_keepalive = 0.0
|
||||||
storage = LocalS3Storage()
|
storage = LocalS3Storage()
|
||||||
scan_thread = None
|
scan_thread = None
|
||||||
@@ -35,6 +39,7 @@ def cleanup():
|
|||||||
for row in rows:
|
for row in rows:
|
||||||
storage.discard(row['object_key'],row['multipart_id'],row['complete'])
|
storage.discard(row['object_key'],row['multipart_id'],row['complete'])
|
||||||
c.execute('UPDATE dtf_local.uploads SET purged_at=now() WHERE id=%s', (row['id'],))
|
c.execute('UPDATE dtf_local.uploads SET purged_at=now() WHERE id=%s', (row['id'],))
|
||||||
|
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (row['id'],))
|
||||||
c.execute('DELETE FROM dtf_local.sessions WHERE expires_at<=now()')
|
c.execute('DELETE FROM dtf_local.sessions WHERE expires_at<=now()')
|
||||||
c.execute('DELETE FROM dtf_local.operator_sessions WHERE expires_at<=now()')
|
c.execute('DELETE FROM dtf_local.operator_sessions WHERE expires_at<=now()')
|
||||||
c.execute("DELETE FROM dtf_local.login_attempts WHERE started_at<now()-interval '1 day'")
|
c.execute("DELETE FROM dtf_local.login_attempts WHERE started_at<now()-interval '1 day'")
|
||||||
@@ -74,6 +79,34 @@ def tiny_keepalive():
|
|||||||
except Exception:
|
except Exception:
|
||||||
logging.exception('Tiny connection refresh failed')
|
logging.exception('Tiny connection refresh failed')
|
||||||
|
|
||||||
|
def reconcile_payments():
|
||||||
|
"""Every minute: the payments of open quotes, asked of the provider, so an
|
||||||
|
approval becomes the order even when its notification never arrives."""
|
||||||
|
global last_reconcile
|
||||||
|
if time.monotonic()-last_reconcile < 60:
|
||||||
|
return
|
||||||
|
last_reconcile = time.monotonic()
|
||||||
|
from . import payments
|
||||||
|
from .runtime import payment
|
||||||
|
payments.reconcile(connect, payment)
|
||||||
|
|
||||||
|
def resolve_names():
|
||||||
|
"""Every minute: the company names of a few new customers' CNPJs, on a
|
||||||
|
thread of their own, so a slow registry never holds up orders."""
|
||||||
|
global last_names, names_thread
|
||||||
|
if time.monotonic()-last_names < 60 or (names_thread and names_thread.is_alive()):
|
||||||
|
return
|
||||||
|
last_names = time.monotonic()
|
||||||
|
from . import cnpj
|
||||||
|
|
||||||
|
def run():
|
||||||
|
try:
|
||||||
|
cnpj.resolve(connect)
|
||||||
|
except Exception:
|
||||||
|
logging.exception('CNPJ name lookup failed')
|
||||||
|
names_thread = threading.Thread(target=run, daemon=True)
|
||||||
|
names_thread.start()
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
global last_cleanup
|
global last_cleanup
|
||||||
while True:
|
while True:
|
||||||
@@ -83,6 +116,8 @@ def loop():
|
|||||||
cleanup()
|
cleanup()
|
||||||
last_cleanup = time.monotonic()
|
last_cleanup = time.monotonic()
|
||||||
tiny_keepalive()
|
tiny_keepalive()
|
||||||
|
reconcile_payments()
|
||||||
|
resolve_names()
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception('Local worker tick failed')
|
logging.exception('Local worker tick failed')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|||||||
@@ -36,7 +36,20 @@ x-app: &app
|
|||||||
MP_WEBHOOK_SECRET: ${MP_WEBHOOK_SECRET:-}
|
MP_WEBHOOK_SECRET: ${MP_WEBHOOK_SECRET:-}
|
||||||
MP_NOTIFICATION_URL: ${MP_NOTIFICATION_URL:-}
|
MP_NOTIFICATION_URL: ${MP_NOTIFICATION_URL:-}
|
||||||
MP_PUBLIC_KEY: ${MP_PUBLIC_KEY:-}
|
MP_PUBLIC_KEY: ${MP_PUBLIC_KEY:-}
|
||||||
FREIGHT_ADAPTER: fake
|
FREIGHT_ADAPTER: ${FREIGHT_ADAPTER:-fake}
|
||||||
|
JADLOG_TOKEN: ${JADLOG_TOKEN:-}
|
||||||
|
JADLOG_CNPJ: ${JADLOG_CNPJ:-}
|
||||||
|
JADLOG_CONTA: ${JADLOG_CONTA:-}
|
||||||
|
JADLOG_CONTRATO: ${JADLOG_CONTRATO:-}
|
||||||
|
JADLOG_PESO_BASE_KG: ${JADLOG_PESO_BASE_KG:-}
|
||||||
|
JADLOG_PESO_POR_METRO_KG: ${JADLOG_PESO_POR_METRO_KG:-}
|
||||||
|
JADLOG_CAIXA_CM: ${JADLOG_CAIXA_CM:-}
|
||||||
|
JADLOG_CUBAGEM_KG_M3: ${JADLOG_CUBAGEM_KG_M3:-}
|
||||||
|
JADLOG_METROS_POR_CAIXA: ${JADLOG_METROS_POR_CAIXA:-}
|
||||||
|
FREIGHT_PRODUCTION_DAYS: ${FREIGHT_PRODUCTION_DAYS:-0}
|
||||||
|
# Carts the Site priced are approved at checkout; larger ones wait for review.
|
||||||
|
QUOTE_AUTO_APPROVE: ${QUOTE_AUTO_APPROVE:-true}
|
||||||
|
QUOTE_AUTO_MAX_METRES: ${QUOTE_AUTO_MAX_METRES:-50}
|
||||||
TINY_ADAPTER: ${TINY_ADAPTER:-fake}
|
TINY_ADAPTER: ${TINY_ADAPTER:-fake}
|
||||||
TINY_CLIENT_ID: ${TINY_CLIENT_ID:-}
|
TINY_CLIENT_ID: ${TINY_CLIENT_ID:-}
|
||||||
TINY_CLIENT_SECRET: ${TINY_CLIENT_SECRET:-}
|
TINY_CLIENT_SECRET: ${TINY_CLIENT_SECRET:-}
|
||||||
@@ -45,6 +58,14 @@ x-app: &app
|
|||||||
TINY_PRODUCT_TEXTIL_AVULSA: ${TINY_PRODUCT_TEXTIL_AVULSA:-}
|
TINY_PRODUCT_TEXTIL_AVULSA: ${TINY_PRODUCT_TEXTIL_AVULSA:-}
|
||||||
TINY_PRODUCT_UV_FOLHA: ${TINY_PRODUCT_UV_FOLHA:-}
|
TINY_PRODUCT_UV_FOLHA: ${TINY_PRODUCT_UV_FOLHA:-}
|
||||||
TINY_PRODUCT_UV_AVULSA: ${TINY_PRODUCT_UV_AVULSA:-}
|
TINY_PRODUCT_UV_AVULSA: ${TINY_PRODUCT_UV_AVULSA:-}
|
||||||
|
TINY_SKU_TEXTIL_FOLHA: ${TINY_SKU_TEXTIL_FOLHA:-}
|
||||||
|
TINY_SKU_TEXTIL_AVULSA: ${TINY_SKU_TEXTIL_AVULSA:-}
|
||||||
|
TINY_SKU_UV_FOLHA: ${TINY_SKU_UV_FOLHA:-}
|
||||||
|
TINY_SKU_UV_AVULSA: ${TINY_SKU_UV_AVULSA:-}
|
||||||
|
TINY_SKU10_TEXTIL_FOLHA: ${TINY_SKU10_TEXTIL_FOLHA:-}
|
||||||
|
TINY_SKU10_TEXTIL_AVULSA: ${TINY_SKU10_TEXTIL_AVULSA:-}
|
||||||
|
TINY_SKU10_UV_FOLHA: ${TINY_SKU10_UV_FOLHA:-}
|
||||||
|
TINY_SKU10_UV_AVULSA: ${TINY_SKU10_UV_AVULSA:-}
|
||||||
TINY_ECOMMERCE_ID: ${TINY_ECOMMERCE_ID:-}
|
TINY_ECOMMERCE_ID: ${TINY_ECOMMERCE_ID:-}
|
||||||
WHATSAPP_ADAPTER: fake
|
WHATSAPP_ADAPTER: fake
|
||||||
STORAGE_ADAPTER: s3-local
|
STORAGE_ADAPTER: s3-local
|
||||||
@@ -54,7 +75,7 @@ x-app: &app
|
|||||||
STORAGE_QUOTA_BYTES: ${STORAGE_QUOTA_BYTES:-53687091200}
|
STORAGE_QUOTA_BYTES: ${STORAGE_QUOTA_BYTES:-53687091200}
|
||||||
OWNER_UPLOAD_QUOTA_BYTES: ${OWNER_UPLOAD_QUOTA_BYTES:-10737418240}
|
OWNER_UPLOAD_QUOTA_BYTES: ${OWNER_UPLOAD_QUOTA_BYTES:-10737418240}
|
||||||
MAX_PENDING_UPLOADS: ${MAX_PENDING_UPLOADS:-10}
|
MAX_PENDING_UPLOADS: ${MAX_PENDING_UPLOADS:-10}
|
||||||
SCAN_MAX_BYTES: ${SCAN_MAX_BYTES:-134217728}
|
SCAN_MAX_BYTES: ${SCAN_MAX_BYTES:-2097152000}
|
||||||
networks: [local]
|
networks: [local]
|
||||||
init: true
|
init: true
|
||||||
security_opt: [no-new-privileges:true]
|
security_opt: [no-new-privileges:true]
|
||||||
@@ -138,6 +159,9 @@ services:
|
|||||||
S3_APP_USER: ${S3_APP_USER:-dtf_app}
|
S3_APP_USER: ${S3_APP_USER:-dtf_app}
|
||||||
S3_APP_PASSWORD: ${S3_APP_PASSWORD:-local-app-storage-only}
|
S3_APP_PASSWORD: ${S3_APP_PASSWORD:-local-app-storage-only}
|
||||||
S3_BUCKET: ${S3_BUCKET:-dtf-local-artwork}
|
S3_BUCKET: ${S3_BUCKET:-dtf-local-artwork}
|
||||||
|
S3_BACKUP_BUCKET: dtf-local-backups
|
||||||
|
S3_BACKUP_USER: dtf_backup
|
||||||
|
S3_BACKUP_PASSWORD: local-backup-storage-only
|
||||||
networks: [local]
|
networks: [local]
|
||||||
depends_on:
|
depends_on:
|
||||||
storage: {condition: service_healthy}
|
storage: {condition: service_healthy}
|
||||||
@@ -187,6 +211,28 @@ services:
|
|||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 12
|
retries: 12
|
||||||
|
|
||||||
|
# The daily database backup, against the local backup bucket. Idle until
|
||||||
|
# BACKUP_AGE_RECIPIENT is set; tests/backup_test.py runs it with a throwaway key.
|
||||||
|
backup:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: infra/Dockerfile
|
||||||
|
command: python -m ops.db_backup serve
|
||||||
|
environment:
|
||||||
|
DATABASE_ADMIN_URL: postgresql://${POSTGRES_USER:-dtf_local}:${POSTGRES_PASSWORD:-local-database-only}@db:5432/${POSTGRES_DB:-dtf_local}
|
||||||
|
BACKUP_S3_ENDPOINT: http://storage:9000
|
||||||
|
BACKUP_BUCKET: dtf-local-backups
|
||||||
|
BACKUP_ACCESS_KEY_ID: dtf_backup
|
||||||
|
BACKUP_SECRET_ACCESS_KEY: local-backup-storage-only
|
||||||
|
BACKUP_REGION: us-east-1
|
||||||
|
BACKUP_AGE_RECIPIENT: ${BACKUP_AGE_RECIPIENT:-}
|
||||||
|
networks: [local]
|
||||||
|
read_only: true
|
||||||
|
tmpfs: [/tmp]
|
||||||
|
depends_on:
|
||||||
|
db-init: {condition: service_completed_successfully}
|
||||||
|
storage-init: {condition: service_completed_successfully}
|
||||||
|
|
||||||
site:
|
site:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
@@ -195,6 +241,7 @@ services:
|
|||||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://localhost:${STORAGE_PORT:-9000}}
|
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://localhost:${STORAGE_PORT:-9000}}
|
||||||
# Empty unless testing Mercado Pago's card form; see docs/LOCAL_SETUP.md.
|
# Empty unless testing Mercado Pago's card form; see docs/LOCAL_SETUP.md.
|
||||||
PAYMENT_CSP_SOURCES: ${PAYMENT_CSP_SOURCES:-}
|
PAYMENT_CSP_SOURCES: ${PAYMENT_CSP_SOURCES:-}
|
||||||
|
PAYMENT_CHALLENGE_SOURCES: ${PAYMENT_CHALLENGE_SOURCES:-}
|
||||||
ports:
|
ports:
|
||||||
# Published ports are host-wide even bound to loopback, so on a shared
|
# Published ports are host-wide even bound to loopback, so on a shared
|
||||||
# machine any of them can collide with something unrelated. CI overrides
|
# machine any of them can collide with something unrelated. CI overrides
|
||||||
@@ -219,6 +266,7 @@ services:
|
|||||||
WEB_INDEX: kanban.html
|
WEB_INDEX: kanban.html
|
||||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://localhost:${STORAGE_PORT:-9000}}
|
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://localhost:${STORAGE_PORT:-9000}}
|
||||||
PAYMENT_CSP_SOURCES: ""
|
PAYMENT_CSP_SOURCES: ""
|
||||||
|
PAYMENT_CHALLENGE_SOURCES: ""
|
||||||
ports: ["127.0.0.1:${KANBAN_PORT:-8081}:80"]
|
ports: ["127.0.0.1:${KANBAN_PORT:-8081}:80"]
|
||||||
networks: [local, edge]
|
networks: [local, edge]
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|||||||
@@ -13,8 +13,11 @@ LABEL org.opencontainers.image.title="DTF Portal/API" \
|
|||||||
# The base is pinned, so its OS packages are frozen at the digest's build date.
|
# The base is pinned, so its OS packages are frozen at the digest's build date.
|
||||||
# Upgrade them here or the image ships known-fixed Debian vulnerabilities, which
|
# Upgrade them here or the image ships known-fixed Debian vulnerabilities, which
|
||||||
# is what the production image was doing while the local one already did this.
|
# is what the production image was doing while the local one already did this.
|
||||||
|
# pg_dump and age are for the database backup (ops/db_backup.py). Debian 13
|
||||||
|
# ships PostgreSQL 17, the server's major version, which pg_dump must match.
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get upgrade -y \
|
&& apt-get upgrade -y \
|
||||||
|
&& apt-get install -y --no-install-recommends postgresql-client-17 age \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ LABEL org.opencontainers.image.title="DTF Site and Kanban" \
|
|||||||
org.opencontainers.image.source="DTF System repository"
|
org.opencontainers.image.source="DTF System repository"
|
||||||
ENV WEB_INDEX=index.html PUBLIC_HOST=invalid.example S3_PUBLIC_ENDPOINT=https://invalid.example
|
ENV WEB_INDEX=index.html PUBLIC_HOST=invalid.example S3_PUBLIC_ENDPOINT=https://invalid.example
|
||||||
COPY --from=policy /build/default.conf.template /etc/nginx/templates/default.conf.template
|
COPY --from=policy /build/default.conf.template /etc/nginx/templates/default.conf.template
|
||||||
COPY web/ /usr/share/nginx/html/
|
# The HTML from the policy stage, with every asset address versioned.
|
||||||
|
COPY --from=policy /build/web/ /usr/share/nginx/html/
|
||||||
|
|
||||||
# The official entrypoint renders the server configuration at startup and Nginx
|
# The official entrypoint renders the server configuration at startup and Nginx
|
||||||
# writes its PID/cache files. Keep the service non-root while granting it
|
# writes its PID/cache files. Keep the service non-root while granting it
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ server {
|
|||||||
add_header Referrer-Policy no-referrer always;
|
add_header Referrer-Policy no-referrer always;
|
||||||
add_header X-Frame-Options DENY always;
|
add_header X-Frame-Options DENY always;
|
||||||
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
||||||
add_header Content-Security-Policy "default-src 'self'; script-src 'self' @SCRIPT_HASHES@ ${PAYMENT_CSP_SOURCES}; script-src-attr 'none'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: blob: https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; connect-src 'self' ${S3_PUBLIC_ENDPOINT} https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; frame-src 'self' ${PAYMENT_CSP_SOURCES}; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'" always;
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self' @SCRIPT_HASHES@ ${PAYMENT_CSP_SOURCES}; script-src-attr 'none'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: blob: https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; connect-src 'self' ${S3_PUBLIC_ENDPOINT} https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; frame-src 'self' ${PAYMENT_CSP_SOURCES} ${PAYMENT_CHALLENGE_SOURCES}; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self' ${PAYMENT_CHALLENGE_SOURCES}" always;
|
||||||
|
|
||||||
location = /health { access_log off; return 200 'ok'; }
|
location = /health { access_log off; return 200 'ok'; }
|
||||||
location /api/ {
|
location /api/ {
|
||||||
@@ -48,6 +48,16 @@ server {
|
|||||||
}
|
}
|
||||||
# Always revalidate HTML/JS/CSS after a deployment. Without this, a browser
|
# Always revalidate HTML/JS/CSS after a deployment. Without this, a browser
|
||||||
# can pair a new Kanban page with a cached older script after a rollout.
|
# can pair a new Kanban page with a cached older script after a rollout.
|
||||||
|
# The Site's product pages and cart are addresses of the same page (web/site-pages.js).
|
||||||
|
location ~ ^/(arquivo-por-metro|artes-avulsas|uv-arquivo-por-metro|uv-artes-avulsas|carrinho|pagamento|pagamento/pix)/?$ {
|
||||||
|
expires -1;
|
||||||
|
try_files /index.html =404;
|
||||||
|
}
|
||||||
|
# The customer's area: one page, which shows the right part for each address.
|
||||||
|
location ~ ^/conta(/(entrar|pedidos|dados))?/?$ {
|
||||||
|
expires -1;
|
||||||
|
try_files /portal.html =404;
|
||||||
|
}
|
||||||
location / {
|
location / {
|
||||||
expires -1;
|
expires -1;
|
||||||
try_files $uri $uri/ =404;
|
try_files $uri $uri/ =404;
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ POSTGRES_VOLUME=TBD
|
|||||||
OPERATOR_EMAIL=TBD
|
OPERATOR_EMAIL=TBD
|
||||||
|
|
||||||
PAYMENT_ADAPTER=TBD
|
PAYMENT_ADAPTER=TBD
|
||||||
|
# With PAYMENT_ADAPTER=mercadopago. MP_ACCESS_TOKEN and MP_WEBHOOK_SECRET are
|
||||||
|
# secrets: enter them in Portainer only, never in this file.
|
||||||
|
MP_NOTIFICATION_URL=https://<SITE_DOMAIN>/api/payments/webhook
|
||||||
|
MP_PUBLIC_KEY=
|
||||||
FREIGHT_ADAPTER=TBD
|
FREIGHT_ADAPTER=TBD
|
||||||
TINY_ADAPTER=TBD
|
TINY_ADAPTER=TBD
|
||||||
# Tiny API v3 application (Configurações > Geral > Aplicativos in Tiny). The
|
# Tiny API v3 application (Configurações > Geral > Aplicativos in Tiny). The
|
||||||
@@ -37,6 +41,9 @@ TINY_PRODUCT_TEXTIL_FOLHA=TBD
|
|||||||
TINY_PRODUCT_TEXTIL_AVULSA=TBD
|
TINY_PRODUCT_TEXTIL_AVULSA=TBD
|
||||||
TINY_PRODUCT_UV_FOLHA=TBD
|
TINY_PRODUCT_UV_FOLHA=TBD
|
||||||
TINY_PRODUCT_UV_AVULSA=TBD
|
TINY_PRODUCT_UV_AVULSA=TBD
|
||||||
|
TINY_FORMA_ENVIO_RETIRADA=TBD
|
||||||
|
# true only at go-live, after the n8n DTFIMP designer branch is removed.
|
||||||
|
TINY_STATUS_UPDATES=false
|
||||||
WHATSAPP_ADAPTER=TBD
|
WHATSAPP_ADAPTER=TBD
|
||||||
STORAGE_QUOTA_BYTES=TBD
|
STORAGE_QUOTA_BYTES=TBD
|
||||||
OWNER_UPLOAD_QUOTA_BYTES=TBD
|
OWNER_UPLOAD_QUOTA_BYTES=TBD
|
||||||
@@ -44,6 +51,13 @@ MAX_PENDING_UPLOADS=10
|
|||||||
MAX_UPLOAD_BYTES=5368709120
|
MAX_UPLOAD_BYTES=5368709120
|
||||||
UPLOAD_PART_BYTES=8388608
|
UPLOAD_PART_BYTES=8388608
|
||||||
SCAN_MAX_BYTES=134217728
|
SCAN_MAX_BYTES=134217728
|
||||||
|
# Daily encrypted database backup to its own bucket (docs/BACKUP.md).
|
||||||
|
BACKUP_S3_ENDPOINT=https://<account>.r2.cloudflarestorage.com
|
||||||
|
BACKUP_BUCKET=dtf-backups
|
||||||
|
BACKUP_ACCESS_KEY_ID=TBD
|
||||||
|
BACKUP_SECRET_ACCESS_KEY=TBD
|
||||||
|
BACKUP_AGE_RECIPIENT=age1...
|
||||||
|
BACKUP_HOUR=3
|
||||||
|
|
||||||
# Names of external Portainer/Docker Swarm secrets, never their values.
|
# Names of external Portainer/Docker Swarm secrets, never their values.
|
||||||
DATABASE_URL_SECRET=TBD
|
DATABASE_URL_SECRET=TBD
|
||||||
|
|||||||
@@ -19,12 +19,47 @@ x-app-environment: &app-environment
|
|||||||
# this value is configured.
|
# this value is configured.
|
||||||
OPERATOR_EMAIL: ${OPERATOR_EMAIL:-}
|
OPERATOR_EMAIL: ${OPERATOR_EMAIL:-}
|
||||||
OPERATOR_PASSWORD: ${OPERATOR_PASSWORD:?set OPERATOR_PASSWORD}
|
OPERATOR_PASSWORD: ${OPERATOR_PASSWORD:?set OPERATOR_PASSWORD}
|
||||||
PAYMENT_ADAPTER: fake
|
# fake until Mercado Pago is configured; mercadopago requires MP_ACCESS_TOKEN
|
||||||
# Optional: without it the webhook verifies nothing and therefore accepts
|
# and MP_WEBHOOK_SECRET or the API and worker refuse to start. Test
|
||||||
# nothing, which is the correct state until a provider is connected. Set it
|
# credentials (TEST-...) until the sandbox flows have passed. The card form
|
||||||
# when the provider is configured, never to a value anyone could guess.
|
# appears only with MP_PUBLIC_KEY, and then needs PAYMENT_CSP_SOURCES too.
|
||||||
|
PAYMENT_ADAPTER: ${PAYMENT_ADAPTER:-fake}
|
||||||
|
MP_ACCESS_TOKEN: ${MP_ACCESS_TOKEN:-}
|
||||||
|
# The "assinatura secreta" from the webhook settings in Mercado Pago.
|
||||||
|
MP_WEBHOOK_SECRET: ${MP_WEBHOOK_SECRET:-}
|
||||||
|
# https://<SITE_DOMAIN>/api/payments/webhook, sent with every payment.
|
||||||
|
MP_NOTIFICATION_URL: ${MP_NOTIFICATION_URL:-}
|
||||||
|
MP_PUBLIC_KEY: ${MP_PUBLIC_KEY:-}
|
||||||
|
# The fake adapter's secret. Optional: without it the webhook verifies
|
||||||
|
# nothing and therefore accepts nothing, which is the correct state until a
|
||||||
|
# provider is connected. Never set it to a value anyone could guess.
|
||||||
PAYMENT_WEBHOOK_SECRET: ${PAYMENT_WEBHOOK_SECRET:-}
|
PAYMENT_WEBHOOK_SECRET: ${PAYMENT_WEBHOOK_SECRET:-}
|
||||||
FREIGHT_ADAPTER: fake
|
# fake offers pickup only. jadlog prices delivery with Jadlog and needs the
|
||||||
|
# credentials below and the package weight from the client; it refuses to
|
||||||
|
# start without them. The credentials alone are enough for the console
|
||||||
|
# check, python -m app.jadlog_probe, on the worker.
|
||||||
|
FREIGHT_ADAPTER: ${FREIGHT_ADAPTER:-fake}
|
||||||
|
JADLOG_TOKEN: ${JADLOG_TOKEN:-}
|
||||||
|
# The "Usuário" Jadlog issued: the CNPJ that contracts the freight.
|
||||||
|
JADLOG_CNPJ: ${JADLOG_CNPJ:-}
|
||||||
|
JADLOG_CONTA: ${JADLOG_CONTA:-}
|
||||||
|
JADLOG_CONTRATO: ${JADLOG_CONTRATO:-}
|
||||||
|
# Package weight in kg: a base plus each billed metre of film.
|
||||||
|
JADLOG_PESO_BASE_KG: ${JADLOG_PESO_BASE_KG:-}
|
||||||
|
JADLOG_PESO_POR_METRO_KG: ${JADLOG_PESO_POR_METRO_KG:-}
|
||||||
|
# The box (cm, "56x14x14") and the contract's cubage factor (kg/m³): Jadlog
|
||||||
|
# charges the larger of the real and the cubed weight. Both or neither.
|
||||||
|
JADLOG_CAIXA_CM: ${JADLOG_CAIXA_CM:-}
|
||||||
|
JADLOG_CUBAGEM_KG_M3: ${JADLOG_CUBAGEM_KG_M3:-}
|
||||||
|
# Metres of film one box holds; a longer order ships in more boxes.
|
||||||
|
JADLOG_METROS_POR_CAIXA: ${JADLOG_METROS_POR_CAIXA:-}
|
||||||
|
# Working days of production added to Jadlog's delivery time.
|
||||||
|
FREIGHT_PRODUCTION_DAYS: ${FREIGHT_PRODUCTION_DAYS:-0}
|
||||||
|
# A cart the Site priced is approved at checkout and can be paid at once;
|
||||||
|
# orders above QUOTE_AUTO_MAX_METRES, or with a grade the Site did not
|
||||||
|
# compute, wait for an operator on the Kanban (app/quote_review.py).
|
||||||
|
QUOTE_AUTO_APPROVE: ${QUOTE_AUTO_APPROVE:-true}
|
||||||
|
QUOTE_AUTO_MAX_METRES: ${QUOTE_AUTO_MAX_METRES:-50}
|
||||||
# Order creation in Tiny stays off until it has been tested against the
|
# Order creation in Tiny stays off until it has been tested against the
|
||||||
# client's account (Tiny has no sandbox). The application credentials can be
|
# client's account (Tiny has no sandbox). The application credentials can be
|
||||||
# set now: they let an operator connect Tiny from the Kanban, and the worker
|
# set now: they let an operator connect Tiny from the Kanban, and the worker
|
||||||
@@ -37,6 +72,21 @@ x-app-environment: &app-environment
|
|||||||
TINY_PRODUCT_TEXTIL_AVULSA: ${TINY_PRODUCT_TEXTIL_AVULSA:-}
|
TINY_PRODUCT_TEXTIL_AVULSA: ${TINY_PRODUCT_TEXTIL_AVULSA:-}
|
||||||
TINY_PRODUCT_UV_FOLHA: ${TINY_PRODUCT_UV_FOLHA:-}
|
TINY_PRODUCT_UV_FOLHA: ${TINY_PRODUCT_UV_FOLHA:-}
|
||||||
TINY_PRODUCT_UV_AVULSA: ${TINY_PRODUCT_UV_AVULSA:-}
|
TINY_PRODUCT_UV_AVULSA: ${TINY_PRODUCT_UV_AVULSA:-}
|
||||||
|
TINY_SKU_TEXTIL_FOLHA: ${TINY_SKU_TEXTIL_FOLHA:-}
|
||||||
|
TINY_SKU_TEXTIL_AVULSA: ${TINY_SKU_TEXTIL_AVULSA:-}
|
||||||
|
TINY_SKU_UV_FOLHA: ${TINY_SKU_UV_FOLHA:-}
|
||||||
|
TINY_SKU_UV_AVULSA: ${TINY_SKU_UV_AVULSA:-}
|
||||||
|
TINY_SKU10_TEXTIL_FOLHA: ${TINY_SKU10_TEXTIL_FOLHA:-}
|
||||||
|
TINY_SKU10_TEXTIL_AVULSA: ${TINY_SKU10_TEXTIL_AVULSA:-}
|
||||||
|
TINY_SKU10_UV_FOLHA: ${TINY_SKU10_UV_FOLHA:-}
|
||||||
|
TINY_SKU10_UV_AVULSA: ${TINY_SKU10_UV_AVULSA:-}
|
||||||
|
# The client's "retirar pessoalmente" forma de envio id, on pickup orders.
|
||||||
|
TINY_FORMA_ENVIO_RETIRADA: ${TINY_FORMA_ENVIO_RETIRADA:-}
|
||||||
|
# Sets "Aprovada" on paid orders and "Pronto para envio" on finished pickup
|
||||||
|
# orders, which the client's Tiny -> n8n notices send to customers. Turn on
|
||||||
|
# only together with TINY_ADAPTER=tiny and after n8n stops sending the
|
||||||
|
# designer message for DTFIMP products.
|
||||||
|
TINY_STATUS_UPDATES: ${TINY_STATUS_UPDATES:-false}
|
||||||
WHATSAPP_ADAPTER: fake
|
WHATSAPP_ADAPTER: fake
|
||||||
STORAGE_ADAPTER: s3-r2
|
STORAGE_ADAPTER: s3-r2
|
||||||
PUBLIC_ORIGIN: https://${SITE_DOMAIN:?set SITE_DOMAIN}
|
PUBLIC_ORIGIN: https://${SITE_DOMAIN:?set SITE_DOMAIN}
|
||||||
@@ -46,10 +96,13 @@ x-app-environment: &app-environment
|
|||||||
COOKIE_SECURE: "true"
|
COOKIE_SECURE: "true"
|
||||||
MAX_UPLOAD_BYTES: "5368709120"
|
MAX_UPLOAD_BYTES: "5368709120"
|
||||||
UPLOAD_PART_BYTES: "8388608"
|
UPLOAD_PART_BYTES: "8388608"
|
||||||
STORAGE_QUOTA_BYTES: "53687091200"
|
# Sheets of several GB are the normal order, so room for many of them.
|
||||||
OWNER_UPLOAD_QUOTA_BYTES: "10737418240"
|
STORAGE_QUOTA_BYTES: "${STORAGE_QUOTA_BYTES:-536870912000}"
|
||||||
|
OWNER_UPLOAD_QUOTA_BYTES: "${OWNER_UPLOAD_QUOTA_BYTES:-53687091200}"
|
||||||
MAX_PENDING_UPLOADS: "10"
|
MAX_PENDING_UPLOADS: "10"
|
||||||
SCAN_MAX_BYTES: "134217728"
|
# ClamAV scans up to this; larger files (up to MAX_UPLOAD_BYTES) are released
|
||||||
|
# after a file-format check instead (app/scanning.py).
|
||||||
|
SCAN_MAX_BYTES: "2097152000"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
@@ -95,7 +148,7 @@ services:
|
|||||||
image: clamav/clamav@sha256:9cb27d7660bdf66e9878c832cb433dd8aa152cfbe16f3c2c0084c80b04ae22b4
|
image: clamav/clamav@sha256:9cb27d7660bdf66e9878c832cb433dd8aa152cfbe16f3c2c0084c80b04ae22b4
|
||||||
entrypoint: [clamd, --foreground=true, --config-file=/etc/clamav/clamd.conf]
|
entrypoint: [clamd, --foreground=true, --config-file=/etc/clamav/clamd.conf]
|
||||||
configs:
|
configs:
|
||||||
- source: clamd_config
|
- source: clamd_config_2gb
|
||||||
target: /etc/clamav/clamd.conf
|
target: /etc/clamav/clamd.conf
|
||||||
mode: 0444
|
mode: 0444
|
||||||
networks: [backend]
|
networks: [backend]
|
||||||
@@ -144,6 +197,34 @@ services:
|
|||||||
replicas: 1
|
replicas: 1
|
||||||
restart_policy: {condition: on-failure, delay: 5s}
|
restart_policy: {condition: on-failure, delay: 5s}
|
||||||
|
|
||||||
|
# Daily encrypted copy of the database to a bucket of its own, off this
|
||||||
|
# server (ops/db_backup.py). Idle until the BACKUP_* settings are set. The
|
||||||
|
# bucket's lifecycle rule removes old copies; this credential never deletes.
|
||||||
|
backup:
|
||||||
|
image: ${API_IMAGE:-gitea.blyzer.com.br/blyzer/dtf-api}:${IMAGE_TAG:-latest}
|
||||||
|
command: python -m ops.db_backup serve
|
||||||
|
environment:
|
||||||
|
DATABASE_ADMIN_HOST: db
|
||||||
|
DATABASE_ADMIN_NAME: dtf
|
||||||
|
DATABASE_ADMIN_USER: dtf_admin
|
||||||
|
DATABASE_ADMIN_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
|
||||||
|
# The R2 account endpoint (the same as R2_ENDPOINT) and a bucket and API
|
||||||
|
# token for backups only, never the artwork bucket's.
|
||||||
|
BACKUP_S3_ENDPOINT: ${BACKUP_S3_ENDPOINT:-}
|
||||||
|
BACKUP_BUCKET: ${BACKUP_BUCKET:-}
|
||||||
|
BACKUP_ACCESS_KEY_ID: ${BACKUP_ACCESS_KEY_ID:-}
|
||||||
|
BACKUP_SECRET_ACCESS_KEY: ${BACKUP_SECRET_ACCESS_KEY:-}
|
||||||
|
# The public key (age1...). Its private key stays off this server.
|
||||||
|
BACKUP_AGE_RECIPIENT: ${BACKUP_AGE_RECIPIENT:-}
|
||||||
|
# Hour of day in Brasília.
|
||||||
|
BACKUP_HOUR: ${BACKUP_HOUR:-3}
|
||||||
|
networks: [backend, egress]
|
||||||
|
read_only: true
|
||||||
|
tmpfs: [/tmp]
|
||||||
|
deploy:
|
||||||
|
replicas: 1
|
||||||
|
restart_policy: {condition: on-failure, delay: 30s}
|
||||||
|
|
||||||
site:
|
site:
|
||||||
image: ${WEB_IMAGE:-gitea.blyzer.com.br/blyzer/dtf-web}:${IMAGE_TAG:-latest}
|
image: ${WEB_IMAGE:-gitea.blyzer.com.br/blyzer/dtf-web}:${IMAGE_TAG:-latest}
|
||||||
environment:
|
environment:
|
||||||
@@ -153,6 +234,10 @@ services:
|
|||||||
# Mercado Pago's card form loads from these origins; empty keeps the
|
# Mercado Pago's card form loads from these origins; empty keeps the
|
||||||
# Site at script-src 'self'. Set together with the Mercado Pago adapter.
|
# Site at script-src 'self'. Set together with the Mercado Pago adapter.
|
||||||
PAYMENT_CSP_SOURCES: ${PAYMENT_CSP_SOURCES:-}
|
PAYMENT_CSP_SOURCES: ${PAYMENT_CSP_SOURCES:-}
|
||||||
|
# The bank's confirmation page for debit and other 3-D Secure cards is
|
||||||
|
# on the issuer's own domain, so it cannot be listed: "https:" lets
|
||||||
|
# frames and form posts reach it (never scripts). Empty turns it off.
|
||||||
|
PAYMENT_CHALLENGE_SOURCES: ${PAYMENT_CHALLENGE_SOURCES:-}
|
||||||
networks: [backend]
|
networks: [backend]
|
||||||
ports:
|
ports:
|
||||||
- target: 8080
|
- target: 8080
|
||||||
@@ -176,6 +261,7 @@ services:
|
|||||||
PUBLIC_HOST: ${KANBAN_DOMAIN:?set KANBAN_DOMAIN}
|
PUBLIC_HOST: ${KANBAN_DOMAIN:?set KANBAN_DOMAIN}
|
||||||
S3_PUBLIC_ENDPOINT: ${R2_ENDPOINT:?set R2_ENDPOINT}
|
S3_PUBLIC_ENDPOINT: ${R2_ENDPOINT:?set R2_ENDPOINT}
|
||||||
PAYMENT_CSP_SOURCES: ""
|
PAYMENT_CSP_SOURCES: ""
|
||||||
|
PAYMENT_CHALLENGE_SOURCES: ""
|
||||||
networks: [backend]
|
networks: [backend]
|
||||||
ports:
|
ports:
|
||||||
- target: 8080
|
- target: 8080
|
||||||
@@ -193,7 +279,9 @@ services:
|
|||||||
restart_policy: {condition: on-failure, delay: 5s}
|
restart_policy: {condition: on-failure, delay: 5s}
|
||||||
|
|
||||||
configs:
|
configs:
|
||||||
clamd_config:
|
# Renamed whenever infra/clamd.conf changes: Swarm cannot update a deployed
|
||||||
|
# config in place, and a redeploy with new content under the old name fails.
|
||||||
|
clamd_config_2gb:
|
||||||
file: ./infra/clamd.conf
|
file: ./infra/clamd.conf
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
87
docs/BACKUP.md
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
# Database backup
|
||||||
|
|
||||||
|
The `backup` service copies the database off the server once a day
|
||||||
|
(`ops/db_backup.py`). It runs `pg_dump`, checks the archive, encrypts it with
|
||||||
|
[age](https://age-encryption.org) and uploads it to an R2 bucket used for
|
||||||
|
nothing else. Each run is recorded, and the Kanban shows the latest one under
|
||||||
|
Integrações → "Backup do banco" ("Em dia", "Verificar" or "Não configurado").
|
||||||
|
|
||||||
|
The artwork is not backed up. Originals and print files are temporary (7 to 30
|
||||||
|
days) and already stored on R2. The database holds what cannot be recreated:
|
||||||
|
orders, customers, quotes, payments, the Tiny connection and the operators.
|
||||||
|
|
||||||
|
## Why the backup cannot be read or deleted from the server
|
||||||
|
|
||||||
|
- **The server only encrypts.** It holds the public key (`age1…`). The private
|
||||||
|
key, which is the only way to open a backup, stays with the owner.
|
||||||
|
- **Its own bucket and token.** The backup token reaches the backup bucket
|
||||||
|
only, and the artwork token cannot reach it.
|
||||||
|
- **Nothing is deleted by the job.** The bucket's lifecycle rule removes old
|
||||||
|
copies. The bucket lock keeps anyone, including someone holding the token,
|
||||||
|
from deleting or overwriting a copy before its time.
|
||||||
|
|
||||||
|
## Setup (once)
|
||||||
|
|
||||||
|
1. **Key pair.** Generate it on your own computer, not on the server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --rm gitea.blyzer.com.br/blyzer/dtf-api:latest age-keygen
|
||||||
|
```
|
||||||
|
|
||||||
|
Or, with age installed (`sudo pacman -S age`, `apt install age`), just
|
||||||
|
run `age-keygen`.
|
||||||
|
|
||||||
|
Save the whole output (the `AGE-SECRET-KEY-1…` line is the private key) in
|
||||||
|
the password manager. Only the `public key: age1…` value goes to Portainer.
|
||||||
|
Without the private key, no backup can ever be restored.
|
||||||
|
2. **Cloudflare R2 → Create bucket.** Use a name such as `dtf-backups`.
|
||||||
|
3. **Settings on that bucket:**
|
||||||
|
- Object lifecycle rules: delete objects after 35 days.
|
||||||
|
- Bucket lock rules: retain for 30 days, prefix `db/`.
|
||||||
|
4. **R2 → Manage API tokens → Create API token.**
|
||||||
|
- Permission: Object Read & Write.
|
||||||
|
- Scope: the `dtf-backups` bucket only.
|
||||||
|
5. **Portainer.** Add these to the stack's environment, then redeploy:
|
||||||
|
|
||||||
|
| Variable | Value |
|
||||||
|
|---|---|
|
||||||
|
| `BACKUP_S3_ENDPOINT` | same as `R2_ENDPOINT` |
|
||||||
|
| `BACKUP_BUCKET` | `dtf-backups` |
|
||||||
|
| `BACKUP_ACCESS_KEY_ID` | the new token's Access Key ID |
|
||||||
|
| `BACKUP_SECRET_ACCESS_KEY` | the new token's Secret Access Key |
|
||||||
|
| `BACKUP_AGE_RECIPIENT` | the public key, `age1…` |
|
||||||
|
| `BACKUP_HOUR` | optional, default `3` (03:00 Brasília) |
|
||||||
|
|
||||||
|
The first backup runs as soon as the service starts. After that it runs daily,
|
||||||
|
and a failed run is retried an hour later.
|
||||||
|
|
||||||
|
## Restore test
|
||||||
|
|
||||||
|
Do this after setup, and then once a month. Open the console of the `backup`
|
||||||
|
container in Portainer (Containers → `…_backup…` → Console) and run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m ops.db_backup list
|
||||||
|
python -m ops.db_backup verify --identity -
|
||||||
|
```
|
||||||
|
|
||||||
|
`--identity -` asks for the private key to be pasted. It is kept only in the
|
||||||
|
container's memory while the command runs. `verify` restores the latest backup
|
||||||
|
into a scratch database, prints its row counts and drops it. The live database
|
||||||
|
is not touched.
|
||||||
|
|
||||||
|
## Restoring for real
|
||||||
|
|
||||||
|
From the `backup` container's console:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m ops.db_backup restore db/2026/10/dtf-20261001T060000Z.dump.age --into dtf_restored --identity -
|
||||||
|
```
|
||||||
|
|
||||||
|
This restores into a new database (or an empty one), never over the running
|
||||||
|
one. Then do one of these:
|
||||||
|
|
||||||
|
- **Check the restored data**, then point the stack at it.
|
||||||
|
- **On a new server**, deploy only the `db` service first. Restore into `dtf`
|
||||||
|
while it is still empty, then deploy the rest. `db-init` then applies the
|
||||||
|
schema and grants on top.
|
||||||
@@ -62,25 +62,35 @@ operator interfaces.
|
|||||||
4. Click the Site checkout button. Files upload directly to MinIO, remain
|
4. Click the Site checkout button. Files upload directly to MinIO, remain
|
||||||
quarantined until ClamAV returns `clean`, and then become eligible for a quote.
|
quarantined until ClamAV returns `clean`, and then become eligible for a quote.
|
||||||
The local banner displays a quote ID awaiting commercial review.
|
The local banner displays a quote ID awaiting commercial review.
|
||||||
5. Open Kanban and log in. In **Cotações**, download the original if needed,
|
5. Open Kanban and log in. Open the **Cotações** tab, pick the quote, download
|
||||||
confirm/correct total metres and grade, tick the manual confirmation, and
|
the original if needed, confirm/correct metres and grade, tick the
|
||||||
click **Aprovar cotação**. For the fixture keep 1.01 m and grade 0.
|
confirmation at the bottom and click **Aprovar cotação**. For the fixture
|
||||||
|
keep 1.01 m and grade 0.
|
||||||
6. Return to the Site and click its checkout action again. Inspect the
|
6. Return to the Site and click its checkout action again. Inspect the
|
||||||
authoritative server total, then click **Criar pedido de teste**. No real
|
authoritative server total, then click **Criar pedido de teste**. No real
|
||||||
payment occurs.
|
payment occurs.
|
||||||
7. Refresh Kanban. The paid order starts in **Arte recebida**. Move it using
|
7. Click **Atualizar** on the Kanban. The paid order appears in **Arte
|
||||||
the buttons or drag and drop to **Arte tratada**. Open **Arquivos de produção**,
|
recebida** on the **Produção** tab. Click its card to open the order panel,
|
||||||
select the manually prepared final files for every item, enter a review note,
|
then **Mover para Arte tratada** (dragging the card to a column also works).
|
||||||
tick the confirmation and click **Aprovar arquivos finais**. Use the harmless
|
In the panel's **Arquivos finais**, select the manually prepared final file
|
||||||
fixture again only for this local test. The text fixture is not an image, so
|
for every item, enter a review note, tick the confirmation and click
|
||||||
its print file shows **preparar à mão**; with a PNG or JPEG artwork the
|
**Aprovar arquivos finais**. Use the harmless fixture again only for this
|
||||||
generated PDF is preselected instead (see "Print files").
|
local test. The text fixture is not an image, so its print file shows
|
||||||
Continue through **Fila de impressão →
|
**Preparar à mão**; with a PNG, JPEG or PDF artwork the generated file is
|
||||||
Imprimindo → Finalizado**. A move to **Correção** requires a reason; it can
|
preselected instead (see "Print files"). Continue with the panel's main
|
||||||
return to **Arte recebida** or **Arte tratada**. Finalizado is terminal locally.
|
button through **Fila de impressão → Imprimindo → Finalizado**. **Pedir
|
||||||
8. Inspect **Histórico** and **Eventos locais de integração**. Worker receipts
|
correção** asks for the reason the customer will see; from **Correção** the
|
||||||
should say recorded locally. Download links expire after five minutes;
|
order returns to **Arte recebida** or **Arte tratada**. A move made by
|
||||||
request another link from Kanban when needed.
|
mistake is undone with **Voltar para …**: one stage back, with an internal
|
||||||
|
reason recorded in the history. The customer is not notified, approved
|
||||||
|
finals stay, and "produção iniciada"/"pedido pronto" are sent only once per
|
||||||
|
order even if the stage is entered again. Dragging a card only goes forward;
|
||||||
|
the columns it can be dropped on are highlighted.
|
||||||
|
8. The panel's **Histórico** lists every move. The **Pagamentos** tab lists
|
||||||
|
payments that need a person; **Integrações** shows the Tiny connection and
|
||||||
|
the send log; with the fake adapters every send reads "Registrado
|
||||||
|
(simulado)". Download links expire after five minutes; click again for a new
|
||||||
|
one. The previous Kanban is kept in git tag `ui-v1`.
|
||||||
|
|
||||||
The manual quote step is necessary to enforce the backend trust boundary while
|
The manual quote step is necessary to enforce the backend trust boundary while
|
||||||
automatic pre-flight is deferred. Browser measurements and grade are proposals.
|
automatic pre-flight is deferred. Browser measurements and grade are proposals.
|
||||||
@@ -157,6 +167,14 @@ The workflow test also checks guest-to-account migration, cross-session login,
|
|||||||
revoked sessions, ownership isolation, correction submissions, final revision
|
revoked sessions, ownership isolation, correction submissions, final revision
|
||||||
invalidation, secure downloads, and mandatory final-file approval before queueing.
|
invalidation, secure downloads, and mandatory final-file approval before queueing.
|
||||||
|
|
||||||
|
The Kanban's artwork pictures, quote filters and order/quote search have their
|
||||||
|
own check, run inside the API container because it writes fixtures to the
|
||||||
|
database (and removes them):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose -f compose.local.yaml exec -T -e SITE_BASE_URL=http://site -e SITE_HOST_HEADER=localhost api python -m tests.kanban_test
|
||||||
|
```
|
||||||
|
|
||||||
Security and malware regressions are separate so an authorized harmless EICAR
|
Security and malware regressions are separate so an authorized harmless EICAR
|
||||||
test is unmistakable:
|
test is unmistakable:
|
||||||
|
|
||||||
@@ -291,6 +309,13 @@ TINY_PRODUCT_TEXTIL_FOLHA=... # Tiny product id for each Site product
|
|||||||
TINY_PRODUCT_TEXTIL_AVULSA=...
|
TINY_PRODUCT_TEXTIL_AVULSA=...
|
||||||
TINY_PRODUCT_UV_FOLHA=...
|
TINY_PRODUCT_UV_FOLHA=...
|
||||||
TINY_PRODUCT_UV_AVULSA=...
|
TINY_PRODUCT_UV_AVULSA=...
|
||||||
|
TINY_SKU_TEXTIL_FOLHA=DTFIMP.TX # optional, instead of the product id: sell by the client's
|
||||||
|
# SKUs (<prefix>.100CM per metre + <prefix>.0010CM … .0090CM)
|
||||||
|
TINY_SKU_TEXTIL_AVULSA= # same, for the other modes, once their SKUs are confirmed
|
||||||
|
TINY_SKU_UV_FOLHA=
|
||||||
|
TINY_SKU_UV_AVULSA=
|
||||||
|
TINY_SKU10_UV_FOLHA=DTFIMP.UV.010CM # or one SKU per 10 cm: 2,30 m = 23 of it (the client's UV)
|
||||||
|
TINY_SKU10_UV_AVULSA=DTFIMP.UV.010CM
|
||||||
```
|
```
|
||||||
|
|
||||||
With the client ID and secret set, the Kanban header shows **Conectar Tiny**.
|
With the client ID and secret set, the Kanban header shows **Conectar Tiny**.
|
||||||
@@ -318,8 +343,14 @@ PAYMENT_CSP_SOURCES=https://sdk.mercadopago.com https://*.mercadopago.com https:
|
|||||||
```
|
```
|
||||||
|
|
||||||
`PAYMENT_CSP_SOURCES` is empty by default, which keeps the Site at
|
`PAYMENT_CSP_SOURCES` is empty by default, which keeps the Site at
|
||||||
`script-src 'self'`. Once a payment for a quote is approved, or a card payment
|
`script-src 'self'`. The payment page offers credit card (the default), debit
|
||||||
is in review, the API refuses any further attempt for that quote. The order is created only by the signed
|
card and PIX. Card payments ask for 3-D Secure when the issuer requires it,
|
||||||
|
which debit cards usually do: the bank's confirmation page opens in a frame on
|
||||||
|
the issuer's own domain, so it needs `PAYMENT_CHALLENGE_SOURCES=https:` (frames
|
||||||
|
and form posts only, never scripts; empty by default). Once a payment for a
|
||||||
|
quote is approved, or a card payment is in review, the API refuses any further
|
||||||
|
attempt for that quote; a card left waiting for the bank's confirmation stops
|
||||||
|
blocking after ten minutes. The order is created only by the signed
|
||||||
notification, after the payment is fetched from the Mercado Pago API and its
|
notification, after the payment is fetched from the Mercado Pago API and its
|
||||||
BRL amount matches the approved total. Mercado Pago must be able to reach the
|
BRL amount matches the approved total. Mercado Pago must be able to reach the
|
||||||
webhook, so a local run needs a public HTTPS tunnel to the Site port. A paid
|
webhook, so a local run needs a public HTTPS tunnel to the Site port. A paid
|
||||||
|
|||||||
@@ -93,6 +93,24 @@ as `dtf_prod_database_url_v1`, then place only those names in the corresponding
|
|||||||
Credential values must never be entered into Git, `portainer.env`, or Gitea
|
Credential values must never be entered into Git, `portainer.env`, or Gitea
|
||||||
workflow variables.
|
workflow variables.
|
||||||
|
|
||||||
|
**R2 CORS.** The Site uploads artwork straight from the browser to the bucket
|
||||||
|
with presigned `PUT` requests, so the bucket must allow the Site's origin.
|
||||||
|
Without it the preflight is refused (403) and the Site shows "NetworkError
|
||||||
|
when attempting to fetch resource" at checkout (found 2026-09-28). In the
|
||||||
|
Cloudflare dashboard, R2 → the bucket → Settings → CORS policy:
|
||||||
|
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"AllowedOrigins": ["https://<SITE_DOMAIN>", "https://<KANBAN_DOMAIN>"],
|
||||||
|
"AllowedMethods": ["PUT", "GET", "HEAD"],
|
||||||
|
"AllowedHeaders": ["content-type"],
|
||||||
|
"ExposeHeaders": ["ETag"],
|
||||||
|
"MaxAgeSeconds": 3600
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
## 3. Create the stack once
|
## 3. Create the stack once
|
||||||
|
|
||||||
In Portainer select **Stacks → Add stack → Git repository**:
|
In Portainer select **Stacks → Add stack → Git repository**:
|
||||||
@@ -157,6 +175,21 @@ SHA of the last known-good Gitea commit and redeploy. This rolls back Site, API,
|
|||||||
Kanban, and worker code; it does not undo database migrations or restore data.
|
Kanban, and worker code; it does not undo database migrations or restore data.
|
||||||
After recovery and a corrected release, return `IMAGE_TAG` to `latest`.
|
After recovery and a corrected release, return `IMAGE_TAG` to `latest`.
|
||||||
|
|
||||||
|
## 6. Before opening to customers
|
||||||
|
|
||||||
|
Once, right before launch, clear the test orders from the worker's console
|
||||||
|
(Containers > worker > Console):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m ops.launch_reset # only counts
|
||||||
|
python -m ops.launch_reset --confirmar APAGAR # removes orders, quotes and payments
|
||||||
|
```
|
||||||
|
|
||||||
|
It refuses if any payment was made with real money. Order numbers restart at
|
||||||
|
#1 only with the database administrator: the script prints the `psql` line to
|
||||||
|
run in the `db` container's console. Add `--contas` to remove the test
|
||||||
|
customer accounts too.
|
||||||
|
|
||||||
## Current blocker
|
## Current blocker
|
||||||
|
|
||||||
This is the final deployment shape, but it is not authorized for production
|
This is the final deployment shape, but it is not authorized for production
|
||||||
|
|||||||
254
docs/ROADMAP.md
@@ -7,16 +7,24 @@
|
|||||||
> Update the **Current step** line and the item status every time something moves.
|
> Update the **Current step** line and the item status every time something moves.
|
||||||
> Add new findings at the bottom of the relevant block rather than rewriting history.
|
> Add new findings at the bottom of the relevant block rather than rewriting history.
|
||||||
|
|
||||||
**Current step (2026-09-24, Week 2):** Client inputs for Mercado Pago and
|
**Current step (2026-09-25, last day of Week 2):** Still waiting on the client
|
||||||
freight were requested on 2026-09-24; Tiny access is already with the client.
|
for Mercado Pago credentials and webhook access (1.1) and freight data (1.2).
|
||||||
Built without them: server-side print-file generation (1.4), the delivery
|
Tiny is connected in production and reads orders, but the connected user
|
||||||
address (3.8), the Kanban's payment-issue and print-file views (1.5), and
|
cannot read contacts (403) and the client's catalogue has no per-metre UV
|
||||||
Mercado Pago and Tiny adapters written from the public API documentation and
|
product; both wait on the client (1.3). Built and deployed without them: server-side print-file
|
||||||
tested against fake transports (1.1, 1.3). None of the provider work is a
|
generation (1.4), the delivery address (3.8), the Kanban's payment-issue and
|
||||||
verified integration. The complete CI integration sequence passed locally on
|
print-file views and its redesign (1.5), the Site redesign (5.17), and Mercado
|
||||||
2026-09-24 (all API suites including the new `print_file_test`, adapter and
|
Pago and Tiny adapters written from the public API documentation and tested
|
||||||
generator unit suites, retention, runtime security, and both browser suites),
|
against fake transports. None of the provider work is a verified integration.
|
||||||
run with Docker Engine in WSL against a fresh build; pending a Gitea runner run.
|
Every change passed the full integration sequence locally (Docker Engine in
|
||||||
|
WSL) and on the Gitea runner, which publishes images on green pushes to `main`;
|
||||||
|
the user verified each release in production after redeploying in Portainer.
|
||||||
|
Operator and Site guides are in `docs/` (see Reporting).
|
||||||
|
**Left for today:** the Week 2 client report. Waiting on the client: the
|
||||||
|
Contatos permission and the product decision, then product ids and one
|
||||||
|
supervised real order (1.3); Mercado Pago credentials for the sandbox PIX and
|
||||||
|
card payments (1.1); freight data (1.2, the one Week 2 item that slips, on
|
||||||
|
client inputs).
|
||||||
|
|
||||||
**Previous step (2026-09-23):** Payment safety fixes 2.13 and 2.14 and
|
**Previous step (2026-09-23):** Payment safety fixes 2.13 and 2.14 and
|
||||||
the local order-correctness work in 3.6/3.9 have passed integration checks.
|
the local order-correctness work in 3.6/3.9 have passed integration checks.
|
||||||
@@ -239,9 +247,53 @@ From the report already sent. These are dated promises, not backlog.
|
|||||||
quote cannot be charged twice. The Site CSP gains the Mercado Pago origins
|
quote cannot be charged twice. The Site CSP gains the Mercado Pago origins
|
||||||
only through `PAYMENT_CSP_SOURCES`, empty by default. Not yet rendered
|
only through `PAYMENT_CSP_SOURCES`, empty by default. Not yet rendered
|
||||||
against a real public key; sandbox run and refund policy remain.
|
against a real public key; sandbox run and refund policy remain.
|
||||||
|
**Account setup (2026-09-28):** the client's application is Checkout
|
||||||
|
Transparente on the Payments API, webhook event "Pagamentos (legacy)" only,
|
||||||
|
URL `https://dtf.agenciacompor.com.br/api/payments/webhook`. The production
|
||||||
|
compose now takes `PAYMENT_ADAPTER` and `MP_*` from Portainer (it hard-coded
|
||||||
|
the fake adapter), so the sandbox runs on production with test credentials;
|
||||||
|
the Site has no real customers yet. A verified notification whose payment
|
||||||
|
does not exist (the panel's "Simular notificação") is acknowledged instead
|
||||||
|
of answering 500, which would have made Mercado Pago retry it.
|
||||||
- `[ ]` 1.2 — Real freight quotation. **Blocked on client inputs** (see
|
- `[ ]` 1.2 — Real freight quotation. **Blocked on client inputs** (see
|
||||||
`PRODUCTION_INPUTS.md`): source platform, credentials, origin CEP, services,
|
`PRODUCTION_INPUTS.md`): source platform, credentials, origin CEP, services,
|
||||||
packaging weight/dimensions per length, subsidy policy.
|
packaging weight/dimensions per length, subsidy policy.
|
||||||
|
**Received (2026-09-25):** Jadlog, API access (user/CNPJ, client code,
|
||||||
|
token, account), origin CEP 14402-310, service .PACKAGE (modalidade 3),
|
||||||
|
home delivery, billed by contract. Still missing: packaging weight and
|
||||||
|
dimensions per length and how freight is charged to the customer (asked
|
||||||
|
2026-09-28). `app/jadlog.py` prices one package from the manual (v2.3);
|
||||||
|
`python -m app.jadlog_probe` prices test weights to six regions, read-only,
|
||||||
|
to confirm token, account and contract on the client's account. Not yet run.
|
||||||
|
**Adapter (2026-09-29):** `FREIGHT_ADAPTER=jadlog` prices "Receber em casa"
|
||||||
|
with Jadlog from the order's billed metres (`JADLOG_PESO_BASE_KG` plus
|
||||||
|
`JADLOG_PESO_POR_METRO_KG` per metre) and value, adds
|
||||||
|
`FREIGHT_PRODUCTION_DAYS` to Jadlog's time, re-quotes the cart when it
|
||||||
|
changes, and quotes again at approval from the priced items. It refuses to
|
||||||
|
start without the credentials and the weights, which have no default. The
|
||||||
|
production stack now takes the Jadlog settings, so the probe runs from the
|
||||||
|
worker's console. Cubed weight is not computed: the client's box sizes will
|
||||||
|
tell whether it is needed.
|
||||||
|
**Packaging received (2026-10-01):** one box, 56 × 14 × 14 cm, 0.30 kg; film
|
||||||
|
1.50 kg per 20 m (0.075 kg/m); shipped with Jadlog's own insurance, which is
|
||||||
|
what `tpseguro: "N"` (normal) already quotes on the order's value. The box is
|
||||||
|
large for its weight, so the cubed weight matters: `JADLOG_CAIXA_CM` and
|
||||||
|
`JADLOG_CUBAGEM_KG_M3` make the quote send the larger of the real and the
|
||||||
|
cubed weight. The contract's factor is still unknown; until it is set the
|
||||||
|
Kanban's Frete card says the quote uses the real weight only.
|
||||||
|
**Corrected (2026-10-02):** the box is 63 × 15.5 × 15.5 cm (0.30 kg) and holds
|
||||||
|
about 60 m; UV ships in the same box at the same weight.
|
||||||
|
`JADLOG_METROS_POR_CAIXA` quotes a longer order as several boxes. Jadlog's
|
||||||
|
public .PACKAGE factor is 300 kg/m³ between states (167 within SP): 300 is
|
||||||
|
set, so in-state orders are quoted a little high, never low.
|
||||||
|
**Checked against Jadlog (2026-10-02):** `app.jadlog_probe` to Ribeirão
|
||||||
|
Preto/SP, R$ 34,27 declared: 0.47 kg R$ 13,77 · 1.68 kg R$ 17,65 · 2.52 kg
|
||||||
|
R$ 21,10 · 4.54 kg R$ 27,93. The old Olist site's R$ 17,65 is the 20 × 10 ×
|
||||||
|
28 cm box at 300 kg/m³ (1.68 kg); the client confirmed that Olist listing is
|
||||||
|
wrong and every order ships in the 63 × 15.5 × 15.5 cm box, so the new site's
|
||||||
|
4.54 kg is right. The declared value raises the price by about 0.65–0.75% of
|
||||||
|
it (R$ 500: R$ 30,93; R$ 5000: R$ 64,68): Jadlog's insurance, already in the
|
||||||
|
quote.
|
||||||
- `[~]` 1.3 — Idempotent Tiny/Olist order creation with order-number traceability.
|
- `[~]` 1.3 — Idempotent Tiny/Olist order creation with order-number traceability.
|
||||||
Confirm endpoints, tag behaviour and rate limits first.
|
Confirm endpoints, tag behaviour and rate limits first.
|
||||||
**Groundwork (2026-09-24), API v3 by decision:** OAuth2 against Tiny's
|
**Groundwork (2026-09-24), API v3 by decision:** OAuth2 against Tiny's
|
||||||
@@ -261,6 +313,85 @@ From the report already sent. These are dated promises, not backlog.
|
|||||||
real orders. Still to confirm on the client's account: plan (Construa+),
|
real orders. Still to confirm on the client's account: plan (Construa+),
|
||||||
product ids, token lifetimes, whether pickup needs a transportador, and
|
product ids, token lifetimes, whether pickup needs a transportador, and
|
||||||
rate limits.
|
rate limits.
|
||||||
|
**Supervised run (2026-09-25):** the payload and query parameters were
|
||||||
|
checked against Tiny's published v3 OpenAPI spec (`GET /pedidos` accepts
|
||||||
|
`cpfCnpj` and `dataInicial`; `GET /pedidos/{id}` returns
|
||||||
|
`numeroOrdemCompra`). "Testar conexão" on the Kanban now also reads the four
|
||||||
|
configured products and requires each to be active. `python -m
|
||||||
|
app.tiny_probe` runs from the worker console: `produtos` and `conferir` are
|
||||||
|
read-only; `pedido` shows the test order and creates it only with
|
||||||
|
`--confirmar`, through the worker's own `deliver`, then proves the duplicate
|
||||||
|
guard by search first and only then by a second delivery. Not yet run
|
||||||
|
against the client's account.
|
||||||
|
**Staying connected (2026-09-25):** Tiny documents a 4-hour access token and
|
||||||
|
a 1-day refresh token; the worker renews about every 4 hours. The connection
|
||||||
|
now asks for `offline_access` (listed by Tiny's Keycloak; if refused for this
|
||||||
|
application the callback retries once without it). Renewal failures are
|
||||||
|
stored: a refused refresh token marks the connection lost and is not retried,
|
||||||
|
a transient failure shows as a warning until the next success, and a
|
||||||
|
session grant with under 12 hours left is flagged. Previously a refused
|
||||||
|
refresh still showed "Tiny conectado". Whether Tiny grants offline access,
|
||||||
|
and whether a session grant has an undocumented maximum, is only known on
|
||||||
|
the client's account. There is no alert channel yet (no e-mail; WhatsApp
|
||||||
|
is fake): problems show on the Kanban only.
|
||||||
|
**Client account (2026-09-25):** connected in production with the
|
||||||
|
developer user the client provided. "Testar conexão": `pedidos` ok,
|
||||||
|
`contatos` 403, unchanged after reconnecting. The application's permissions
|
||||||
|
are correct (Contatos, Pedidos: read and include/edit; Produtos: read);
|
||||||
|
Olist documents that v3 calls also depend on the logged-in user's module
|
||||||
|
permissions, and that user's Cadastros menu shows only Produtos. Orders
|
||||||
|
need Contatos (search by CNPJ, create when new; delete never). **Client
|
||||||
|
to decide:** grant that user Clientes e Fornecedores, or reconnect with a
|
||||||
|
user that has it (a dedicated integration user is recommended).
|
||||||
|
**Products (2026-09-25),** from the client's product export: none of the
|
||||||
|
36 "DTF" products matches the four Site products. `951438842` "IMPRESSÃO
|
||||||
|
DTF PERSONALIZADO 57X100 (1 METRO)" (MT, R$ 19,90, active) fits Têxtil;
|
||||||
|
there is no per-metre UV product (the UV one is 27 cm, per 10 cm), and no
|
||||||
|
separate "artes avulsas" product. **Client to decide:** create four per-metre
|
||||||
|
products (recommended, copying `951438842`), or share `951438842` between
|
||||||
|
the two Têxtil modes; UV needs a product either way. The item note already
|
||||||
|
carries the Site mode and grade. No product ids are set in production yet;
|
||||||
|
both questions go to the client with the Mercado Pago credentials request.
|
||||||
|
**SKUs (2026-10-05):** the client answered that the products already exist
|
||||||
|
and an order is a sum of SKUs (1,30 m = `DTFIMP.TX.100CM` + `DTFIMP.TX.0030CM`);
|
||||||
|
the contact permission is no longer an issue (the user, 2026-09-28).
|
||||||
|
`TINY_SKU_<MODE>` (e.g. `TINY_SKU_TEXTIL_FOLHA=DTFIMP.TX`) sells a mode as
|
||||||
|
N × `<prefix>.100CM` plus one `<prefix>.00X0CM`, at the price the customer
|
||||||
|
paid (the client agreed), the last line taking what is left of the item's
|
||||||
|
total so the order adds up to the cent. Codes are looked up by `codigo`
|
||||||
|
(exact match on `sku`, active only) and remembered; "Testar conexão" and
|
||||||
|
`tiny_probe conferir` check all ten. Assumed, to confirm with the client:
|
||||||
|
above 1 m it is always N × "1 metro", not the 2–10 m SKUs. UV (only
|
||||||
|
`DTFIMP.UV.010CM` seen) and artes avulsas stay on a product id per metre
|
||||||
|
until their SKUs are confirmed. Fake transport only; whether v3's `/produtos`
|
||||||
|
filters by `codigo` is checked by `conferir` on the account.
|
||||||
|
**UV (2026-10-05):** the client sells UV only as `DTFIMP.UV.010CM`, per 10 cm.
|
||||||
|
`TINY_SKU10_<MODE>` names such a SKU: 2,30 m goes as 23 of it, at the price
|
||||||
|
paid (a rest that does not divide gets a line of its own).
|
||||||
|
**Customer notices through Tiny (2026-09-25, Week 3 head start):** the client
|
||||||
|
already sends WhatsApp notices from Tiny's order situação (Tiny webhook ->
|
||||||
|
the `api-tiny-n8n` middleware, which reads the order through API v2 -> n8n
|
||||||
|
-> WhatsApp templates): Aprovado, Pronto para envio with forma de envio `X`
|
||||||
|
(v2 "Customizada", their pickup), Enviado, Entregue. So the system's own
|
||||||
|
WhatsApp sender stays off and Tiny drives the notices. With
|
||||||
|
`TINY_STATUS_UPDATES=true` a paid order is created "Aberta" and set to
|
||||||
|
"Aprovada" (a retry finishes a half-done approval; an order already moved on
|
||||||
|
is left alone), and moving a pickup order to Finalizado sets "Pronto para
|
||||||
|
envio" by the Tiny id from the sale's receipt, searching only when it is
|
||||||
|
missing and retrying while the sale has not reached Tiny. Pickup orders
|
||||||
|
carry `TINY_FORMA_ENVIO_RETIRADA` (`tiny_probe formas-envio` lists the ids;
|
||||||
|
"Testar conexão" now checks it). Delivery orders get "Enviada" with 1.2.
|
||||||
|
Off by default: while n8n's `isDTFIMP` branch exists, "Aprovado" on a
|
||||||
|
`DTFIMP` product sends the designer message, and the Site's Têxtil product
|
||||||
|
code starts with `DTFIMP`. **Go-live together:** `TINY_ADAPTER=tiny`,
|
||||||
|
`TINY_STATUS_UPDATES=true`, n8n's `isDTFIMP`/"DTF Aprovado - Designer"
|
||||||
|
removed with "Mapear Whatsapp do Vendedor" connected to `If6`, and the
|
||||||
|
middleware's `numero_ecommerce` falling back to the purchase order so the
|
||||||
|
message shows `DTF-<n>`. **Unverified on the account:** that an API status
|
||||||
|
change fires Tiny's webhook, that Tiny accepts Aprovada -> Pronto para envio
|
||||||
|
without Faturada, and the v2 field name for the purchase order. Correção
|
||||||
|
necessária and Produção iniciada have no Tiny situação; client to decide
|
||||||
|
whether they need messages.
|
||||||
- `[~]` 1.4 — Final print-file generation (see 3.2 and 3.6: production instructions
|
- `[~]` 1.4 — Final print-file generation (see 3.2 and 3.6: production instructions
|
||||||
must survive checkout before an output engine can reproduce the approved job).
|
must survive checkout before an output engine can reproduce the approved job).
|
||||||
**Built (2026-09-24):** each paid item gets a PDF the width of the film and
|
**Built (2026-09-24):** each paid item gets a PDF the width of the film and
|
||||||
@@ -287,6 +418,17 @@ download, approve as final, queue; hand-preparation routing and retry).
|
|||||||
print-file status per item, and a panel lists payments that need a person
|
print-file status per item, and a panel lists payments that need a person
|
||||||
(money without an order, refunds after an order) until resolved. Confirm
|
(money without an order, refunds after an order) until resolved. Confirm
|
||||||
with the operation that these are the main states before closing.
|
with the operation that these are the main states before closing.
|
||||||
|
**2026-09-24:** a mistaken move can be undone one stage back (`BACK` in
|
||||||
|
`app/runtime.py`) with an internal reason, flagged in the history
|
||||||
|
(`movements.back`), without notifying the customer; "production started" and
|
||||||
|
"ready" are enqueued once per order. Previously the only way back was
|
||||||
|
Correção, which messages the customer and invalidates approved finals.
|
||||||
|
**Redesign (2026-09-24):** tabs for Produção, Cotações, Pagamentos and
|
||||||
|
Integrações; an order panel with stages, items, print files, final files and
|
||||||
|
history; numbered pagination (20 per page) on quotes, payment issues and the
|
||||||
|
integration log, which also has filters; messages dismiss themselves. The
|
||||||
|
operator guide (`docs/guia-operador-kanban.pdf`) documents the flow for the
|
||||||
|
operation to confirm.
|
||||||
- `[x]` 1.6 — **Block 0.2 + 0.3** were completed and verified on 2026-09-18.
|
- `[x]` 1.6 — **Block 0.2 + 0.3** were completed and verified on 2026-09-18.
|
||||||
|
|
||||||
`[!]` The production compose currently blocks `dev_paid` (`ENVIRONMENT != 'local'`)
|
`[!]` The production compose currently blocks `dev_paid` (`ENVIRONMENT != 'local'`)
|
||||||
@@ -482,7 +624,19 @@ The production topology has not been verified by the repository review.
|
|||||||
|
|
||||||
## Block 3 · Architecture — needs a decision before code
|
## Block 3 · Architecture — needs a decision before code
|
||||||
|
|
||||||
### `[?]` 3.1 — Manual quote approval contradicts the 24h business case `(F16)`
|
### `[~]` 3.1 — Manual quote approval contradicts the 24h business case `(F16)`
|
||||||
|
|
||||||
|
**Decided 2026-09-28 (the user: "we are an e-commerce"):** a cart the Site
|
||||||
|
priced is approved when the quote is created and can be paid at once
|
||||||
|
(`app/quote_review.py`, the same pricing the operator's approval uses). A
|
||||||
|
person reviews only orders above `QUOTE_AUTO_MAX_METRES` (50 m) and items
|
||||||
|
that claim a grade the Site could not have computed (unanalysed art with a
|
||||||
|
discount). The Kanban lists automatic approvals and the reason for each
|
||||||
|
manual one. **Still open:** the grade and layout are the browser's (3.2,
|
||||||
|
3.9), so a customer who edits the page can claim a better grade, up to the
|
||||||
|
top tier's discount; the server must compute the grade before this closes.
|
||||||
|
|
||||||
|
Previously:
|
||||||
|
|
||||||
Payment requires `quotes.approved`, set only by an authenticated operator. The
|
Payment requires `quotes.approved`, set only by an authenticated operator. The
|
||||||
meeting's premise was that the 17h30 order waiting until 5am is what costs the
|
meeting's premise was that the 17h30 order waiting until 5am is what costs the
|
||||||
@@ -509,7 +663,14 @@ the site already did.
|
|||||||
generator, with the browser as preview only. This is the single largest gap between
|
generator, with the browser as preview only. This is the single largest gap between
|
||||||
what was promised in the meeting and what exists.
|
what was promised in the meeting and what exists.
|
||||||
|
|
||||||
### `[?]` 3.3 — The 5 GB problem is unsolved `(F19)`
|
**2026-09-30:** the grade (the resolution discount) is no longer taken on trust.
|
||||||
|
Before an automatic approval the API recomputes it from the uploaded files by the
|
||||||
|
Site's rules (`app/grade_check.py`): image headers for PNG/JPG/WebP, the placed
|
||||||
|
images of a PDF up to 150 MB. A claim more than 2 points above the file's grade,
|
||||||
|
or a discount on a file the server cannot grade, waits for review with the reason
|
||||||
|
on the Kanban. The metres (the packing) are still the browser's.
|
||||||
|
|
||||||
|
### `[~]` 3.3 — The 5 GB problem is unsolved `(F19)`
|
||||||
|
|
||||||
Transport accepts 5 GiB; `SCAN_MAX_BYTES` / ClamAV `StreamMaxLength` release only
|
Transport accepts 5 GiB; `SCAN_MAX_BYTES` / ClamAV `StreamMaxLength` release only
|
||||||
≤ 128 MiB. As of 2026-09-23, customer selection and API reservation reject files
|
≤ 128 MiB. As of 2026-09-23, customer selection and API reservation reject files
|
||||||
@@ -520,6 +681,23 @@ This is exactly the risk Jorge raised in the meeting.
|
|||||||
**Decide:** raise the scan ceiling with a resource/timeout design, or define an
|
**Decide:** raise the scan ceiling with a resource/timeout design, or define an
|
||||||
explicit large-file path (staged scan, sampled scan, operator override with audit).
|
explicit large-file path (staged scan, sampled scan, operator override with audit).
|
||||||
|
|
||||||
|
**Built 2026-09-29 (sheets of several GB are the normal order, not the
|
||||||
|
exception):** files up to 5 GB. ClamAV scans up to 2 GB (`StreamMaxLength
|
||||||
|
2000M`); above that a file is released only when its first bytes match the
|
||||||
|
format its name claims (option A, the user's choice). The Site grades a sheet
|
||||||
|
over 150 MB from the pixel size in the PNG/JPEG/WebP header without decoding
|
||||||
|
it, and measures large PDFs through ranged reads; the worker never opens a
|
||||||
|
source over 300 MB: a single finished sheet placed whole becomes its own print
|
||||||
|
file, anything else goes to hand preparation. Uploads start as items enter the
|
||||||
|
cart and the lease renews with each part. Verified on the local stack: 386 MB
|
||||||
|
(ClamAV 78 s), 1.8 GB (ClamAV 6 min 18 s, scanner under 430 MB of memory, the
|
||||||
|
original as print file), 2.3 GB PNG released by the format check and a
|
||||||
|
disguised 2.3 GB file refused, and the header grade of a 200 MB file in 0.5 s.
|
||||||
|
**Open:** large PDFs get no automatic grade (the DPI of images inside is not
|
||||||
|
read without rendering); the scanner takes one file at a time, so several
|
||||||
|
multi-GB uploads queue; pieces, residue and background of a large sheet are
|
||||||
|
not checked automatically.
|
||||||
|
|
||||||
### `[ ]` 3.4 — Upload throughput `(F20)`
|
### `[ ]` 3.4 — Upload throughput `(F20)`
|
||||||
|
|
||||||
8 MiB parts, strictly sequential in `local/static/upload.js:21`, one presign
|
8 MiB parts, strictly sequential in `local/static/upload.js:21`, one presign
|
||||||
@@ -710,9 +888,31 @@ print-file evidence still need correction before this item can close.
|
|||||||
attempt counts against the source address and only failures count against
|
attempt counts against the source address and only failures count against
|
||||||
the account; registration still counts every attempt. The security suite's
|
the account; registration still counts every attempt. The security suite's
|
||||||
lockout check (ten failures, then 429) is unchanged and passes.
|
lockout check (ten failures, then 429) is unchanged and passes.
|
||||||
|
- `[x]` 5.17 — Site redesign (2026-09-24), Dropstar brand kept; the previous
|
||||||
|
look is tag `ui-v1`. The home, one page per product (`/arquivo-por-metro`,
|
||||||
|
`/artes-avulsas`, `/uv-arquivo-por-metro`, `/uv-artes-avulsas`) and
|
||||||
|
`/carrinho` have their own addresses but stay one document, so artwork held in
|
||||||
|
the browser survives moving between them (`web/site-pages.js`,
|
||||||
|
`web/site-steps.js`; nginx serves `index.html` for those paths). The product
|
||||||
|
page is built around a buy box (`web/site-compra.js`) that shows the item the
|
||||||
|
flow already priced (grade, price per metre, metres charged, total,
|
||||||
|
resolution acknowledgement and "Adicionar ao carrinho"); the duplicated
|
||||||
|
quality and preview panels are hidden. The cart has "Remover" per item,
|
||||||
|
"Esvaziar carrinho" and an 8-second undo. Fixed on the way, all already in
|
||||||
|
production before: the saved-cart note took a grid column and pushed the
|
||||||
|
order into a narrow strip, the panels outside `.w` ran edge to edge, and
|
||||||
|
"57 cm" wrapped on the ready-sheet option. The browser suite covers product
|
||||||
|
and cart addresses, reload on a product page, and remove/undo; the security
|
||||||
|
suite checks the new addresses carry the same CSP.
|
||||||
- `[ ]` 5.13 — Define production recovery: scheduled encrypted offsite database
|
- `[ ]` 5.13 — Define production recovery: scheduled encrypted offsite database
|
||||||
and object backups, a consistent snapshot boundary, Swarm data placement and
|
and object backups, a consistent snapshot boundary, Swarm data placement and
|
||||||
a restore rehearsal that opens every required live order file.
|
a restore rehearsal that opens every required live order file.
|
||||||
|
**2026-09-29:** database part built. The `backup` service dumps daily,
|
||||||
|
encrypts with age (the server holds only the public key) and uploads to a
|
||||||
|
bucket of its own, whose lifecycle rule expires copies and whose lock keeps
|
||||||
|
them from being deleted early. The Kanban shows the latest run, and
|
||||||
|
`tests/backup_test.py` restores a copy in CI. Setup and restore:
|
||||||
|
docs/BACKUP.md. Artwork is not copied: it is temporary and already on R2.
|
||||||
- `[ ]` 5.14 — Promote and verify one immutable release. **2026-09-24:** green
|
- `[ ]` 5.14 — Promote and verify one immutable release. **2026-09-24:** green
|
||||||
pushes to `main` now publish images and Portainer's pull-and-redeploy is the
|
pushes to `main` now publish images and Portainer's pull-and-redeploy is the
|
||||||
release gate; the source preflight is advisory unless enforced by variable,
|
release gate; the source preflight is advisory unless enforced by variable,
|
||||||
@@ -831,6 +1031,36 @@ print-file evidence still need correction before this item can close.
|
|||||||
|
|
||||||
### Reporting
|
### Reporting
|
||||||
|
|
||||||
|
- `[x]` Operator guide (`docs/guia-operador-kanban.pdf`) and Site guide
|
||||||
|
(`docs/guia-site-dtf.pdf`), 2026-09-24, with screenshots of a throwaway stack
|
||||||
|
and fictional orders. The payment step has no screenshot until Mercado Pago
|
||||||
|
is configured, and the guides name the provisional Kanban domain; regenerate
|
||||||
|
them when either changes. Early work toward Week 3's "orientação à operação".
|
||||||
|
- `[x]` Operator guide rebuilt from a committed source (2026-09-25):
|
||||||
|
`docs/guias/operador/` (HTML and the original screenshots, cropped as
|
||||||
|
before), printed by `docs/guias/imprimir.sh`. The source of the 09-24 PDF
|
||||||
|
was never committed. Corrected: stage moves do not update Tiny (only
|
||||||
|
"Aprovada" and, for pickup, "Pronto para envio", once enabled); WhatsApp
|
||||||
|
notices go through the client's Tiny -> n8n flow; the correction notice is
|
||||||
|
not automatic, the reason is in Minha conta; the Tiny card's renewal,
|
||||||
|
"Verificar" and "Testar conexão" checks. Found while writing it: the
|
||||||
|
customer's order history showed operators' internal reasons for moving an
|
||||||
|
order back; it now omits back moves and shows reasons only for corrections.
|
||||||
|
The Site guide's source is also not in the repository.
|
||||||
|
- `[x]` Operator guide rewritten for the Kanban of 2026-10-05 (11 pages): automatic
|
||||||
|
quote approval, the next-step box for the final file (one click for a correction
|
||||||
|
or the generated PDF), the queue and "Baixar", the WhatsApp correction notice,
|
||||||
|
cancel and delete, Números and auto-refresh. Screenshots from a throwaway stack
|
||||||
|
(compose project `dtfguide`) with four fictional companies; printed with
|
||||||
|
`docs/guias/imprimir.sh`.
|
||||||
|
- `[x]` Week-2 client report (`Relatorio-Semana-2-DTF.docx`), written 2026-09-25,
|
||||||
|
sent 2026-09-28. Before sending, the FlexiPRINT import was taken out of
|
||||||
|
"O que falta" (it is done this week) and the print-file paragraph no longer
|
||||||
|
names PSD, AI, CDR or multi-page PDFs as hand-preparation cases. **Reversed
|
||||||
|
2026-09-29:** those formats stay as built: no automatic check, the full rate
|
||||||
|
per metre, and the operator prepares them by hand. Automatic generation is
|
||||||
|
not planned; the sent report omits them from its list of exceptions. Freight is reported as Jadlog data received,
|
||||||
|
waiting on packaging weight and dimensions and the charging policy.
|
||||||
- `[x]` Week-1 client report (`Relatorio-Semana-1-DTF.docx`), corrected 2026-09-18 to
|
- `[x]` Week-1 client report (`Relatorio-Semana-1-DTF.docx`), corrected 2026-09-18 to
|
||||||
remove the inaccurate "Arquivo por metro permanece separado, com seleção explícita"
|
remove the inaccurate "Arquivo por metro permanece separado, com seleção explícita"
|
||||||
claim and the internal commit reference.
|
claim and the internal commit reference.
|
||||||
|
|||||||
BIN
docs/guia-operador-kanban.pdf
Normal file
BIN
docs/guia-site-dtf.pdf
Normal file
8
docs/guias/imprimir.sh
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Print the guide sources in docs/guias/ to the PDFs in docs/ with headless
|
||||||
|
# Chrome (A4, no browser header or footer). Needs Chrome and the DejaVu fonts.
|
||||||
|
set -eu
|
||||||
|
cd "$(dirname "$0")"
|
||||||
|
CHROME=${CHROME:-$(command -v google-chrome-stable || command -v google-chrome || command -v chromium)}
|
||||||
|
"$CHROME" --headless --disable-gpu --no-pdf-header-footer --run-all-compositor-stages-before-draw \
|
||||||
|
--print-to-pdf="$PWD/../guia-operador-kanban.pdf" "file://$PWD/operador/guia-operador-kanban.html"
|
||||||
361
docs/guias/operador/guia-operador-kanban.html
Normal file
@@ -0,0 +1,361 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!-- Source of docs/guia-operador-kanban.pdf. Print with docs/guias/imprimir.sh.
|
||||||
|
Screenshots in img/ come from a throwaway stack with fictional orders. -->
|
||||||
|
<html lang="pt-BR">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>Kanban DTF · Guia do operador</title>
|
||||||
|
<style>
|
||||||
|
@page { size: A4; margin: 0; }
|
||||||
|
:root {
|
||||||
|
--navy: #0B1320; --orange: #F08A24; --ink: #1B2331; --muted: #6B7280;
|
||||||
|
--line: #D6DCE4; --head: #EAF0F8; --note: #FFF5E8; --note-line: #F4C58E;
|
||||||
|
}
|
||||||
|
* { box-sizing: border-box; }
|
||||||
|
html, body { margin: 0; padding: 0; }
|
||||||
|
body { font-family: 'DejaVu Sans', sans-serif; color: var(--ink); font-size: 9.7pt; line-height: 1.55;
|
||||||
|
-webkit-print-color-adjust: exact; print-color-adjust: exact; }
|
||||||
|
.page { width: 210mm; height: 297mm; position: relative; padding: 26mm 18mm 20mm; overflow: hidden;
|
||||||
|
page-break-after: always; break-after: page; }
|
||||||
|
.page:last-child { page-break-after: auto; break-after: auto; }
|
||||||
|
.running { position: absolute; top: 9mm; left: 18mm; right: 18mm; font-size: 7.4pt; color: var(--muted);
|
||||||
|
border-bottom: 0.6pt solid #E3E7ED; padding-bottom: 2.4mm; }
|
||||||
|
.folio { position: absolute; bottom: 9mm; right: 18mm; font-size: 7.4pt; color: var(--muted); }
|
||||||
|
.kicker { color: var(--orange); font-weight: bold; font-size: 7.6pt; letter-spacing: 0.04em; margin: 0 0 1mm; }
|
||||||
|
h1 { font-size: 20.5pt; line-height: 1.2; margin: 0 0 2mm; color: #101826; }
|
||||||
|
h1::after { content: ""; display: block; width: 18mm; height: 1.4mm; background: var(--orange); margin-top: 3mm; }
|
||||||
|
h2 { font-size: 11.5pt; margin: 7mm 0 2.5mm; color: #101826; }
|
||||||
|
p { margin: 0 0 2.6mm; }
|
||||||
|
b { font-weight: bold; }
|
||||||
|
table { width: 100%; border-collapse: collapse; margin: 2mm 0 3mm; font-size: 8.6pt; line-height: 1.45; }
|
||||||
|
th, td { border: 0.6pt solid var(--line); padding: 2mm 2.4mm; vertical-align: top; text-align: left; }
|
||||||
|
th { background: var(--head); font-weight: bold; }
|
||||||
|
td.n { color: var(--orange); font-weight: bold; font-size: 11pt; width: 7mm; text-align: center; }
|
||||||
|
td.k { font-weight: bold; }
|
||||||
|
.note { background: var(--note); border: 0.6pt solid var(--note-line); border-radius: 2mm; padding: 3mm 3.6mm;
|
||||||
|
font-size: 8.6pt; margin: 3mm 0; }
|
||||||
|
ul { margin: 0 0 3mm; padding-left: 4.5mm; }
|
||||||
|
ul li { margin-bottom: 1.1mm; }
|
||||||
|
ul li::marker { color: var(--orange); }
|
||||||
|
ol { margin: 0 0 3mm; padding-left: 5mm; }
|
||||||
|
ol li { margin-bottom: 1.3mm; }
|
||||||
|
ol li::marker { color: var(--orange); font-weight: bold; }
|
||||||
|
.chip { display: inline-block; border: 0.6pt solid #C9D0DA; background: #F4F6F9; border-radius: 1.2mm;
|
||||||
|
padding: 0 1.6mm; font-size: 7.6pt; line-height: 1.6; white-space: nowrap; }
|
||||||
|
.shot { display: block; width: 100%; border-radius: 1.6mm; margin: 2mm 0 1mm; }
|
||||||
|
.caption { font-size: 7.2pt; color: var(--muted); margin: 0 0 3mm; }
|
||||||
|
.split { display: flex; gap: 6mm; align-items: flex-start; }
|
||||||
|
.split > div { flex: 1; }
|
||||||
|
.split > .side { flex: 0 0 79.5mm; }
|
||||||
|
.quote { font-style: normal; }
|
||||||
|
.shot.half { width: 62%; }
|
||||||
|
.shot.narrow { width: 78%; }
|
||||||
|
|
||||||
|
/* Cover */
|
||||||
|
.cover { background: var(--navy); color: #F3F5F8; padding: 0 18mm; }
|
||||||
|
.cover .block { position: absolute; left: 18mm; right: 18mm; top: 158mm; }
|
||||||
|
.cover .kicker { margin-bottom: 4mm; }
|
||||||
|
.cover .title { font-size: 27pt; font-weight: bold; line-height: 1.25; margin: 0 0 4mm; color: #fff; }
|
||||||
|
.cover .lead { font-size: 9.6pt; color: #C8CFDA; margin: 0 0 8mm; }
|
||||||
|
.cover table { font-size: 8pt; margin: 0 0 8mm; }
|
||||||
|
.cover th, .cover td { border-color: #2A3547; background: transparent; color: #E6EAF0; padding: 3mm 3.6mm; }
|
||||||
|
.cover th { color: var(--orange); }
|
||||||
|
.cover .version { font-size: 8pt; color: #A9B2C0; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!-- 1 · Capa -->
|
||||||
|
<section class="page cover">
|
||||||
|
<div class="block">
|
||||||
|
<p class="kicker">GUIA DE OPERAÇÃO</p>
|
||||||
|
<p class="title">Kanban DTF<br>Guia do operador</p>
|
||||||
|
<p class="lead">Como conferir, produzir e entregar os pedidos que chegam pelo Site DTF.</p>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:50%">Acesso</th><th>Quem usa</th></tr>
|
||||||
|
<tr><td>dtf.kanban.agenciacompor.com.br<br>e-mail e senha do operador</td>
|
||||||
|
<td>Equipe da sala de DTF e atendimento</td></tr>
|
||||||
|
</table>
|
||||||
|
<p class="version">Versão de 5 de outubro de 2026</p>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 2 · Visão geral -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">VISÃO GERAL</p>
|
||||||
|
<h1>O caminho de um pedido</h1>
|
||||||
|
<p>O cliente monta a folha e envia as artes pelo Site DTF, vê o preço e paga na hora. Daí em diante, tudo acontece
|
||||||
|
no Kanban: o pedido percorre as etapas de produção até ficar pronto para retirada ou envio.</p>
|
||||||
|
<table>
|
||||||
|
<tr><td class="n">1</td><td class="k" style="width:31mm">Cliente envia</td>
|
||||||
|
<td>Monta a folha no Site, vê a nota da arte e o preço por metro.</td></tr>
|
||||||
|
<tr><td class="n">2</td><td class="k">Site aprova a cotação</td>
|
||||||
|
<td>O Site confere a nota pelos próprios arquivos e aprova na hora. Só vão para a equipe os pedidos acima de
|
||||||
|
50 m ou com nota que não confere (aba <b>Cotações</b>).</td></tr>
|
||||||
|
<tr><td class="n">3</td><td class="k">Cliente paga</td>
|
||||||
|
<td>Pelo Mercado Pago, com cartão de crédito ou PIX, na própria página do Site.</td></tr>
|
||||||
|
<tr><td class="n">4</td><td class="k">Pedido entra na produção</td>
|
||||||
|
<td>Com o pagamento aprovado, o pedido aparece em <b>Arte recebida</b>, com o arquivo de impressão já gerado.</td></tr>
|
||||||
|
<tr><td class="n">5</td><td class="k">Equipe produz</td>
|
||||||
|
<td>Confere e aprova o arquivo final, baixa, importa no FlexiPRINT e move o card a cada etapa.</td></tr>
|
||||||
|
<tr><td class="n">6</td><td class="k">Pedido pronto</td>
|
||||||
|
<td>Em <b>Finalizado</b>, o pedido fica pronto para retirada e o cliente é avisado (página 7).</td></tr>
|
||||||
|
</table>
|
||||||
|
<h2>As cinco abas</h2>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:31mm">Aba</th><th>Para que serve</th></tr>
|
||||||
|
<tr><td class="k">Produção</td><td>O quadro com os pedidos pagos, uma coluna por etapa.</td></tr>
|
||||||
|
<tr><td class="k">Cotações</td><td>Carrinhos que ainda não foram pagos: os que esperam a equipe conferir e os aprovados
|
||||||
|
esperando o pagamento. O número ao lado indica quantos faltam revisar.</td></tr>
|
||||||
|
<tr><td class="k">Pagamentos</td><td>Pagamentos que o sistema não conseguiu ligar a um pedido. Normalmente fica vazia.</td></tr>
|
||||||
|
<tr><td class="k">Números</td><td>Pedidos, metros, valores e o tempo de cada etapa no período (página 9).</td></tr>
|
||||||
|
<tr><td class="k">Integrações</td><td>Situação do Tiny, Mercado Pago, frete, WhatsApp e cópia de segurança, e o registro do que foi enviado ao Tiny.</td></tr>
|
||||||
|
</table>
|
||||||
|
<div class="note">O quadro se atualiza sozinho, de 30 em 30 segundos. No canto superior direito, ao lado de
|
||||||
|
<b>Atualizar</b>, dá para escolher outro intervalo (de 10 segundos a 15 minutos) ou desligar. Quando chega um pedido
|
||||||
|
novo, aparece o aviso <b>Novo pedido #</b> e o título da aba mostra quantos chegaram.</div>
|
||||||
|
<div class="folio">Página 2</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 3 · Entrar e ler o quadro -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PRODUÇÃO</p>
|
||||||
|
<h1>Entrar e ler o quadro</h1>
|
||||||
|
<div class="split">
|
||||||
|
<div>
|
||||||
|
<p>Acesse <b>dtf.kanban.agenciacompor.com.br</b> e entre com seu e-mail e senha. Cada operador tem o próprio
|
||||||
|
acesso: é o nome dele que fica registrado em cada movimento do pedido.</p>
|
||||||
|
<p>Depois de várias senhas erradas seguidas, o acesso fica bloqueado por 15 minutos.</p>
|
||||||
|
</div>
|
||||||
|
<div class="side"><img class="shot" src="img/login.png" alt="Tela de entrada do Kanban"></div>
|
||||||
|
</div>
|
||||||
|
<img class="shot" src="img/quadro.png" alt="Quadro de produção">
|
||||||
|
<p class="caption">Quadro de produção. Cada coluna é uma fila: o pedido pago primeiro fica em cima.</p>
|
||||||
|
<h2>O que cada card mostra</h2>
|
||||||
|
<ul>
|
||||||
|
<li>A arte, o <b>#número</b> do pedido e há quanto tempo ele mudou de etapa.</li>
|
||||||
|
<li>O nome da empresa (do cadastro do CNPJ) e o e-mail; a nota da arte, o produto e os metros cobrados.</li>
|
||||||
|
<li><span class="chip">Retirada em Franca</span> ou a cidade da entrega.</li>
|
||||||
|
<li>O que falta: <span class="chip">Falta aprovar o final</span>, <span class="chip">Correção recebida</span>,
|
||||||
|
<span class="chip">Preparar à mão</span> ou, quando está tudo certo, <span class="chip">Final aprovado</span>.
|
||||||
|
<span class="chip">Parado</span> aparece quando o pedido fica mais de 4 h na mesma etapa.</li>
|
||||||
|
</ul>
|
||||||
|
<p>Os filtros mostram só <b>Têxtil</b>, <b>UV</b>, <b>Entrega</b>, <b>Retirada</b>, <b>Parados</b>, <b>Preparar à mão</b>
|
||||||
|
ou <b>Cancelados</b>. A busca (atalho <b>/</b>) encontra pedidos e cotações pelo nome da empresa, e-mail, CNPJ,
|
||||||
|
WhatsApp, número ou código do pedido.</p>
|
||||||
|
<div class="folio">Página 3</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 4 · Cotações -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">COTAÇÕES</p>
|
||||||
|
<h1>Cotações que esperam a equipe</h1>
|
||||||
|
<p>Quase todo carrinho é aprovado pelo próprio Site, e o cliente paga em seguida. Ficam em <b>A revisar</b> só os
|
||||||
|
pedidos acima de <b>50 m</b> e os que pedem uma nota que não confere com os arquivos. O motivo aparece na cotação.</p>
|
||||||
|
<img class="shot" src="img/cotacoes.png" alt="Aba Cotações">
|
||||||
|
<p class="caption">À esquerda, a lista com filtros e busca; à direita, a cotação selecionada.</p>
|
||||||
|
<ol>
|
||||||
|
<li>Abra a aba <b>Cotações</b> e clique na cotação em <b>A revisar</b>.</li>
|
||||||
|
<li>Confira a arte e a montagem. Se precisar, abra os arquivos em <b>Original</b> ou a lista de peças em <b>Manifesto</b>.</li>
|
||||||
|
<li>Confira <b>Metros conferidos</b> e <b>Nota conferida</b>. Os valores do cliente já vêm preenchidos; mude só se
|
||||||
|
estiverem errados. A nota define o preço do metro.</li>
|
||||||
|
<li>Marque <b>Arquivos, metragem e nota conferidos</b> e clique em <b>Aprovar cotação</b>.</li>
|
||||||
|
</ol>
|
||||||
|
<p>O cliente tem <b>24 horas</b> para pagar; depois disso a cotação vence e sai da lista. Em <b>Aprovadas, aguardando
|
||||||
|
pagamento</b>, a etiqueta mostra se o cliente já tentou pagar (por exemplo, <span class="chip">Cartão recusado</span>
|
||||||
|
ou <span class="chip">PIX gerado · expira …</span>). Uma cotação que não vai ser paga pode sair da lista em
|
||||||
|
<b>Descartar cotação</b>.</p>
|
||||||
|
<div class="folio">Página 4</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 5 · Abrir um pedido -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PRODUÇÃO</p>
|
||||||
|
<h1>Abrir um pedido</h1>
|
||||||
|
<div class="split">
|
||||||
|
<div>
|
||||||
|
<p>Clique em qualquer card para abrir o painel do pedido. O topo sempre mostra o próximo passo, e o botão
|
||||||
|
laranja é o que faz o pedido andar.</p>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:27mm">Parte</th><th>O que tem</th></tr>
|
||||||
|
<tr><td class="k">Topo</td><td>Número, etapa, código do pedido (o que o cliente vê), valor e data do pagamento,
|
||||||
|
o próximo passo e os botões. Em <b>⋯</b> ficam ações raras, como cancelar o pedido.</td></tr>
|
||||||
|
<tr><td class="k">Cliente e Entrega</td><td>Empresa, e-mail, CNPJ e <b>Abrir conversa</b> no WhatsApp;
|
||||||
|
endereço ou <b>Retirada em Franca</b>.</td></tr>
|
||||||
|
<tr><td class="k">Item</td><td>A arte e a montagem no filme, folha, peças e preço do metro. <b>PDF</b> ou
|
||||||
|
<b>Original</b> baixam os arquivos; <b>Manifesto</b> lista as peças e suas posições.</td></tr>
|
||||||
|
<tr><td class="k">Arquivos finais</td><td>As versões do arquivo que vai ser impresso, com quem aprovou e quando.</td></tr>
|
||||||
|
<tr><td class="k">Histórico</td><td>Cada movimento do pedido, com hora, operador e motivo.</td></tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div class="side"><img class="shot" src="img/painel.png" alt="Painel do pedido"></div>
|
||||||
|
</div>
|
||||||
|
<div class="folio">Página 5</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 6 · Arquivo final -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PRODUÇÃO</p>
|
||||||
|
<h1>Aprovar o arquivo final</h1>
|
||||||
|
<p>Antes de ir para a <b>Fila de impressão</b>, cada item precisa de um arquivo final aprovado. Enquanto falta, o topo
|
||||||
|
do pedido mostra <b>Próximo passo: aprovar o arquivo final</b>, com o arquivo que vai ser impresso.</p>
|
||||||
|
<img class="shot narrow" src="img/arquivo-final.png" alt="Próximo passo: aprovar o arquivo final">
|
||||||
|
<ol>
|
||||||
|
<li>Clique em <b>Baixar</b> e confira o arquivo.</li>
|
||||||
|
<li>Clique em <b>Aprovar arquivo final</b> e confirme. Pronto: o botão laranja vira <b>Mover para Fila de impressão</b>.</li>
|
||||||
|
</ol>
|
||||||
|
<p>Quer imprimir outro arquivo? Clique em <b>Enviar outro arquivo</b>, escolha o arquivo, escreva uma nota, marque
|
||||||
|
<b>Arquivos conferidos</b> e aprove. Quando o cliente manda uma correção, ela é o arquivo sugerido:</p>
|
||||||
|
<img class="shot narrow" src="img/correcao-recebida.png" alt="Correção do cliente pronta para aprovar">
|
||||||
|
<h2>Quando não há arquivo pronto</h2>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:40mm">No card ou no item</th><th>O que fazer</th></tr>
|
||||||
|
<tr><td><span class="chip">Gerando arquivo…</span></td><td>Aguarde alguns segundos: o quadro se atualiza sozinho.</td></tr>
|
||||||
|
<tr><td><span class="chip">Preparar à mão</span></td><td>O cliente enviou um formato que o gerador não lê (CDR, AI, PSD)
|
||||||
|
ou um arquivo muito grande para montar. O botão laranja vira <b>Enviar arquivo final</b>: baixe o <b>Original</b>,
|
||||||
|
prepare o arquivo no seu programa e envie por ali.</td></tr>
|
||||||
|
<tr><td><span class="chip">Falhou ao gerar</span></td><td>Clique em <b>Gerar de novo</b>. Se falhar de novo, prepare à mão.</td></tr>
|
||||||
|
</table>
|
||||||
|
<div class="folio">Página 6</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 7 · Mover pelas etapas -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PRODUÇÃO</p>
|
||||||
|
<h1>Mover o pedido pelas etapas</h1>
|
||||||
|
<p>Use o botão laranja <b>Mover para…</b> no topo do painel ou arraste o card para a coluna seguinte. Dentro da
|
||||||
|
mesma coluna, arraste o card para cima ou para baixo para mudar a prioridade na fila.</p>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:27mm">Etapa</th><th>Significa</th><th style="width:43mm">Aviso ao cliente</th></tr>
|
||||||
|
<tr><td class="k">Arte recebida</td><td>Pedido pago, arquivo gerado. Ainda não conferido.</td><td>Pedido aprovado (automático)</td></tr>
|
||||||
|
<tr><td class="k">Arte tratada</td><td>Arquivo conferido e ajustado, se preciso.</td><td>—</td></tr>
|
||||||
|
<tr><td class="k">Fila de impressão</td><td>Pronto para imprimir. Exige o arquivo final aprovado.</td><td>—</td></tr>
|
||||||
|
<tr><td class="k">Imprimindo</td><td>Na máquina.</td><td>—</td></tr>
|
||||||
|
<tr><td class="k">Finalizado</td><td>Impresso e pronto para retirada ou envio.</td><td>Pedido pronto para retirada</td></tr>
|
||||||
|
<tr><td class="k">Correção</td><td>Algo no arquivo precisa ser resolvido pelo cliente.</td><td>Pela equipe, no WhatsApp (página 8)</td></tr>
|
||||||
|
</table>
|
||||||
|
<div class="note">Os avisos por WhatsApp saem pelo número e pelas mensagens que a Dropstar já usa, a partir da situação
|
||||||
|
do pedido no Tiny. Cada aviso sai <b>uma vez só</b>, mesmo que o pedido volte uma etapa e avance de novo.</div>
|
||||||
|
<div class="split">
|
||||||
|
<div>
|
||||||
|
<h2 style="margin-top:2mm">Baixar a fila de uma vez</h2>
|
||||||
|
<p>Na coluna <b>Fila de impressão</b>, o botão <b>Baixar</b> baixa o arquivo final de todos os pedidos da fila, na
|
||||||
|
ordem da fila, com o número do pedido no nome (ex.: <span class="chip">pedido-246-item-1-…</span>). Se o navegador
|
||||||
|
perguntar, permita vários downloads.</p>
|
||||||
|
<h2>Moveu errado? Volte uma etapa</h2>
|
||||||
|
<p>Clique em <b>← Voltar para [etapa anterior]</b>, embaixo dos botões, escreva o motivo (ex.: “movi por engano”) e
|
||||||
|
confirme. O retorno fica no histórico; o cliente não vê esse retorno nem o motivo.</p>
|
||||||
|
</div>
|
||||||
|
<div class="side" style="flex-basis:58mm"><img class="shot" src="img/fila.png" alt="Coluna Fila de impressão com o botão Baixar"></div>
|
||||||
|
</div>
|
||||||
|
<div class="folio">Página 7</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 8 · Correção -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PRODUÇÃO</p>
|
||||||
|
<h1>Pedir correção ao cliente</h1>
|
||||||
|
<ol>
|
||||||
|
<li>No painel do pedido, clique em <b>Pedir correção</b>.</li>
|
||||||
|
<li>Escreva o motivo de forma clara para o cliente (ex.: “A arte escudo.png está com fundo branco. Envie com fundo transparente.”).</li>
|
||||||
|
<li>Clique em <b>Enviar para correção</b>. O pedido vai para a coluna <b>Correção</b>.</li>
|
||||||
|
<li>No topo do pedido, clique em <b>Avisar no WhatsApp</b>. Abre a conversa com o cliente com a mensagem pronta: o
|
||||||
|
código do pedido, o motivo e o link para enviar o arquivo corrigido. É só enviar.</li>
|
||||||
|
</ol>
|
||||||
|
<img class="shot" src="img/correcao.png" alt="Pedido em correção com Avisar no WhatsApp">
|
||||||
|
<p class="caption">Sem WhatsApp no pedido, use <b>Copiar mensagem</b> e envie por e-mail.</p>
|
||||||
|
<p>O cliente envia o arquivo corrigido em <b>Minha conta</b>, no Site. O card mostra
|
||||||
|
<span class="chip">Correção recebida</span>: mova o pedido para <b>Arte recebida</b> ou <b>Arte tratada</b>, confira a
|
||||||
|
correção e aprove como arquivo final (página 6).</p>
|
||||||
|
<div class="note">Correção é para problemas que só o cliente resolve (resolução, fundo, arte errada). Ajustes que a equipe
|
||||||
|
faz sozinha não precisam de correção: trate o arquivo em <b>Arte tratada</b>.</div>
|
||||||
|
<div class="folio">Página 8</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 9 · Cancelar e Números -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PEDIDOS E NÚMEROS</p>
|
||||||
|
<h1>Cancelar um pedido e ver os números</h1>
|
||||||
|
<div class="split">
|
||||||
|
<div>
|
||||||
|
<p>Em <b>⋯</b>, no topo do pedido, <b>Cancelar pedido</b> pede um motivo. O pedido sai do quadro e fica no filtro
|
||||||
|
<b>Cancelados</b>, com o pagamento e o histórico. O estorno, se houver, é feito no Mercado Pago. Um pedido cancelado
|
||||||
|
pode ser excluído de vez em <b>Excluir pedido</b>.</p>
|
||||||
|
<p>Pedidos de teste, criados pela equipe, aparecem com a etiqueta <span class="chip">TESTE</span> e são excluídos em
|
||||||
|
<b>⋯ → Excluir pedido de teste</b>.</p>
|
||||||
|
</div>
|
||||||
|
<div class="side"><img class="shot" src="img/menu.png" alt="Menu de ações do pedido"></div>
|
||||||
|
</div>
|
||||||
|
<h2>Números</h2>
|
||||||
|
<img class="shot" src="img/numeros.png" alt="Aba Números">
|
||||||
|
<p>A aba <b>Números</b> mostra, nos últimos 7, 30 ou 90 dias: pedidos pagos, metros vendidos, valor recebido, ticket
|
||||||
|
médio, quantos pedidos precisaram de correção e o tempo do pagamento até <b>Finalizado</b>. O gráfico mostra os metros
|
||||||
|
de cada dia (passe o mouse para ver os valores), e a tabela, quanto tempo cada etapa segura um pedido e o que está
|
||||||
|
esperando agora. Pedidos de teste e cancelados ficam de fora.</p>
|
||||||
|
<div class="folio">Página 9</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 10 · Pagamentos e integrações -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">PAGAMENTOS E INTEGRAÇÕES</p>
|
||||||
|
<h1>Pagamentos com problema e integrações</h1>
|
||||||
|
<p>A aba <b>Pagamentos</b> lista os pagamentos que chegaram mas não puderam virar pedido sozinhos. Exemplos:</p>
|
||||||
|
<ul>
|
||||||
|
<li>valor pago diferente do total da cotação;</li>
|
||||||
|
<li>cotação vencida antes do pagamento (passou das 24 h);</li>
|
||||||
|
<li>pagamento estornado ou cancelado depois que o pedido já existia.</li>
|
||||||
|
</ul>
|
||||||
|
<p>Resolva com o cliente ou no Mercado Pago e clique em <b>Registrar resolução</b>, descrevendo o que foi feito.
|
||||||
|
O item passa para <b>Resolvidos</b>.</p>
|
||||||
|
<img class="shot" src="img/pagamentos.png" alt="Aba Pagamentos">
|
||||||
|
<h2>Integrações</h2>
|
||||||
|
<img class="shot" src="img/integracoes.png" alt="Aba Integrações">
|
||||||
|
<p class="caption">Imagem de um ambiente sem integrações ativas. Em produção, cada cartão mostra se a integração está conectada.</p>
|
||||||
|
<ul>
|
||||||
|
<li><b>Tiny</b>: o cartão mostra quem conectou, quando a conexão foi renovada e até quando vale; o sistema renova
|
||||||
|
sozinho. Se aparecer <b>Não conectado</b> ou <b>Verificar</b>, clique em <b>Conectar Tiny</b> ou <b>Reconectar</b> e
|
||||||
|
entre com o usuário do Tiny indicado pela Dropstar. <b>Testar conexão</b> confere pedidos, contatos, os produtos
|
||||||
|
e a forma de envio de retirada, sem criar nada.</li>
|
||||||
|
<li><b>Registro de envios</b>: tudo o que o sistema mandou ao Tiny, com filtros por destino, situação, evento e
|
||||||
|
pedido. <b>Com erro</b> indica um envio que falhou; o sistema tenta de novo sozinho, mas o erro precisa de atenção.</li>
|
||||||
|
</ul>
|
||||||
|
<div class="folio">Página 10</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- 11 · Dúvidas -->
|
||||||
|
<section class="page">
|
||||||
|
<div class="running">DTF Dropstar - guia do operador</div>
|
||||||
|
<p class="kicker">DÚVIDAS FREQUENTES</p>
|
||||||
|
<h1>Mensagens e o que fazer</h1>
|
||||||
|
<table>
|
||||||
|
<tr><th style="width:62mm">Mensagem ou situação</th><th>O que fazer</th></tr>
|
||||||
|
<tr><td>“O pedido mudou. Clique em Atualizar.”</td><td>Outra pessoa mexeu no pedido ao mesmo tempo. Clique em <b>Atualizar</b> e repita a ação.</td></tr>
|
||||||
|
<tr><td>“Informe o motivo da correção.” / “Informe por que o pedido está voltando de etapa.”</td><td>Preencha o motivo antes de confirmar.</td></tr>
|
||||||
|
<tr><td>Card com <b>Parado</b></td><td>Veja se o pedido está esperando alguém ou se esqueceram de movê-lo.</td></tr>
|
||||||
|
<tr><td>Card com <b>Correção recebida</b></td><td>O cliente enviou o arquivo corrigido. Mova o pedido e aprove a correção (página 6).</td></tr>
|
||||||
|
<tr><td>Arquivo aparece como <b>expirado</b></td><td>Os arquivos ficam guardados por 30 dias. Depois disso, o cliente precisa enviar de novo.</td></tr>
|
||||||
|
<tr><td>No topo, <b>Tiny: verificar conexão</b> ou <b>Tiny não conectado</b></td><td>Abra <b>Integrações</b> e siga a mensagem do cartão do Tiny. Se pedir, clique em <b>Reconectar</b>.</td></tr>
|
||||||
|
<tr><td>O nome da empresa não aparece no card</td><td>O nome vem do cadastro do CNPJ e chega em alguns minutos. Até lá, o card mostra o e-mail.</td></tr>
|
||||||
|
<tr><td>A internet da fábrica caiu</td><td>O Site continua recebendo pedidos e pagamentos. Quando a conexão voltar, o quadro se atualiza sozinho.</td></tr>
|
||||||
|
</table>
|
||||||
|
<h2>Boas práticas</h2>
|
||||||
|
<ul>
|
||||||
|
<li>Mova o card assim que a etapa mudar: é o que avisa o cliente e mede o tempo de produção na aba <b>Números</b>.</li>
|
||||||
|
<li>Escreva motivos que outra pessoa entenda sem perguntar.</li>
|
||||||
|
<li>Use sempre o seu acesso: o histórico mostra quem fez cada movimento.</li>
|
||||||
|
<li>Clique em <b>Sair</b> ao deixar o computador.</li>
|
||||||
|
</ul>
|
||||||
|
<div class="folio">Página 11</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
docs/guias/operador/img/arquivo-final.png
Normal file
|
After Width: | Height: | Size: 44 KiB |
BIN
docs/guias/operador/img/correcao-recebida.png
Normal file
|
After Width: | Height: | Size: 46 KiB |
BIN
docs/guias/operador/img/correcao.png
Normal file
|
After Width: | Height: | Size: 86 KiB |
BIN
docs/guias/operador/img/cotacoes.png
Normal file
|
After Width: | Height: | Size: 323 KiB |
BIN
docs/guias/operador/img/fila.png
Normal file
|
After Width: | Height: | Size: 60 KiB |
BIN
docs/guias/operador/img/integracoes.png
Normal file
|
After Width: | Height: | Size: 236 KiB |
BIN
docs/guias/operador/img/login.png
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
docs/guias/operador/img/menu.png
Normal file
|
After Width: | Height: | Size: 112 KiB |
BIN
docs/guias/operador/img/numeros.png
Normal file
|
After Width: | Height: | Size: 212 KiB |
BIN
docs/guias/operador/img/pagamentos.png
Normal file
|
After Width: | Height: | Size: 87 KiB |
BIN
docs/guias/operador/img/painel.png
Normal file
|
After Width: | Height: | Size: 204 KiB |
BIN
docs/guias/operador/img/quadro.png
Normal file
|
After Width: | Height: | Size: 213 KiB |
@@ -1,8 +1,11 @@
|
|||||||
# Same pinned base as deploy/Dockerfile.api, so the integration suite exercises
|
# Same pinned base as deploy/Dockerfile.api, so the integration suite exercises
|
||||||
# the image that ships rather than a different one.
|
# the image that ships rather than a different one.
|
||||||
FROM python:3.12-slim@sha256:2f17fc044b579bab302c2e8054d3a686e2cb9a83de48e70534b94cd8ebbe06a9
|
FROM python:3.12-slim@sha256:2f17fc044b579bab302c2e8054d3a686e2cb9a83de48e70534b94cd8ebbe06a9
|
||||||
|
# pg_dump and age are for the database backup (ops/db_backup.py). Debian 13
|
||||||
|
# ships PostgreSQL 17, the server's major version, which pg_dump must match.
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get upgrade -y \
|
&& apt-get upgrade -y \
|
||||||
|
&& apt-get install -y --no-install-recommends postgresql-client-17 age \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY infra/requirements.txt infra/requirements.lock /app/infra/
|
COPY infra/requirements.txt infra/requirements.lock /app/infra/
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ RUN apk upgrade --no-cache
|
|||||||
ENV WEB_INDEX=index.html
|
ENV WEB_INDEX=index.html
|
||||||
COPY --from=policy /build/default.conf.template /etc/nginx/templates/default.conf.template
|
COPY --from=policy /build/default.conf.template /etc/nginx/templates/default.conf.template
|
||||||
ENV S3_PUBLIC_ENDPOINT=http://localhost:9000
|
ENV S3_PUBLIC_ENDPOINT=http://localhost:9000
|
||||||
COPY web/ /usr/share/nginx/html/
|
# The HTML from the policy stage, with every asset address versioned.
|
||||||
|
COPY --from=policy /build/web/ /usr/share/nginx/html/
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ TCPSocket 3310
|
|||||||
TCPAddr 0.0.0.0
|
TCPAddr 0.0.0.0
|
||||||
MaxThreads 2
|
MaxThreads 2
|
||||||
MaxQueue 8
|
MaxQueue 8
|
||||||
StreamMaxLength 128M
|
StreamMaxLength 2000M
|
||||||
MaxFileSize 128M
|
MaxFileSize 2000M
|
||||||
MaxScanSize 256M
|
MaxScanSize 4000M
|
||||||
MaxScanTime 120000
|
MaxScanTime 900000
|
||||||
|
ReadTimeout 900
|
||||||
|
CommandReadTimeout 900
|
||||||
AlertExceedsMax yes
|
AlertExceedsMax yes
|
||||||
AlertEncrypted yes
|
AlertEncrypted yes
|
||||||
ScanPDF yes
|
ScanPDF yes
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
"""Compile the gateway configuration: hash any trusted inline script for the CSP.
|
"""Compile the gateway configuration and version the Site's assets.
|
||||||
|
|
||||||
|
Inline scripts are hashed for the CSP; local scripts and stylesheets get a
|
||||||
|
content hash in their address.
|
||||||
|
|
||||||
The Site's behaviour now lives in separate files, so normally there is nothing to
|
The Site's behaviour now lives in separate files, so normally there is nothing to
|
||||||
hash and the policy is simply script-src 'self' — no allowlist to get wrong. The
|
hash and the policy is simply script-src 'self' — no allowlist to get wrong. The
|
||||||
@@ -12,6 +15,20 @@ from pathlib import Path
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
root = Path('/build')
|
root = Path('/build')
|
||||||
|
|
||||||
|
# Every local script and stylesheet is addressed by its content, so a release
|
||||||
|
# can never pair new HTML with an old cached file: the proxy in front of the
|
||||||
|
# stack caches assets for hours, and a new index.html calling an old script
|
||||||
|
# broke the Site (2026-09-28). The HTML itself is served no-cache.
|
||||||
|
def versioned(match):
|
||||||
|
attribute, path = match.group(1), match.group(2)
|
||||||
|
digest = hashlib.sha256((root / 'web' / path.lstrip('/')).read_bytes()).hexdigest()[:12]
|
||||||
|
return f'{attribute}="{path}?v={digest}"'
|
||||||
|
|
||||||
|
for html in (root / 'web').glob('*.html'):
|
||||||
|
text = re.sub(r'\b(src|href)="(/[\w./-]+\.(?:js|css))(?:\?[^"]*)?"', versioned, html.read_text())
|
||||||
|
html.write_text(text)
|
||||||
|
|
||||||
hashes = []
|
hashes = []
|
||||||
for html in (root / 'web').glob('*.html'):
|
for html in (root / 'web').glob('*.html'):
|
||||||
for attributes, script in re.findall(r'<script\b([^>]*)>(.*?)</script>', html.read_text(), re.S | re.I):
|
for attributes, script in re.findall(r'<script\b([^>]*)>(.*?)</script>', html.read_text(), re.S | re.I):
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ server {
|
|||||||
add_header Referrer-Policy no-referrer always;
|
add_header Referrer-Policy no-referrer always;
|
||||||
add_header X-Frame-Options DENY always;
|
add_header X-Frame-Options DENY always;
|
||||||
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
||||||
add_header Content-Security-Policy "default-src 'self'; script-src 'self' @SCRIPT_HASHES@ ${PAYMENT_CSP_SOURCES}; script-src-attr 'none'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: blob: https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; connect-src 'self' ${S3_PUBLIC_ENDPOINT} https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; frame-src 'self' ${PAYMENT_CSP_SOURCES}; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'" always;
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self' @SCRIPT_HASHES@ ${PAYMENT_CSP_SOURCES}; script-src-attr 'none'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: blob: https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; connect-src 'self' ${S3_PUBLIC_ENDPOINT} https://cdn.vnda.com.br ${PAYMENT_CSP_SOURCES}; frame-src 'self' ${PAYMENT_CSP_SOURCES} ${PAYMENT_CHALLENGE_SOURCES}; worker-src 'self' blob:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self' ${PAYMENT_CHALLENGE_SOURCES}" always;
|
||||||
location = /health { access_log off; return 200 'ok'; }
|
location = /health { access_log off; return 200 'ok'; }
|
||||||
location /api/ {
|
location /api/ {
|
||||||
limit_req zone=api_limit burst=100 nodelay;
|
limit_req zone=api_limit burst=100 nodelay;
|
||||||
@@ -19,6 +19,14 @@ server {
|
|||||||
proxy_set_header X-Forwarded-For $remote_addr;
|
proxy_set_header X-Forwarded-For $remote_addr;
|
||||||
client_max_body_size 2m;
|
client_max_body_size 2m;
|
||||||
}
|
}
|
||||||
|
# The Site's product pages and cart are addresses of the same page (web/site-pages.js).
|
||||||
|
location ~ ^/(arquivo-por-metro|artes-avulsas|uv-arquivo-por-metro|uv-artes-avulsas|carrinho|pagamento|pagamento/pix)/?$ {
|
||||||
|
try_files /index.html =404;
|
||||||
|
}
|
||||||
|
# The customer's area: one page, which shows the right part for each address.
|
||||||
|
location ~ ^/conta(/(entrar|pedidos|dados))?/?$ {
|
||||||
|
try_files /portal.html =404;
|
||||||
|
}
|
||||||
location / { try_files $uri $uri/ =404; }
|
location / { try_files $uri $uri/ =404; }
|
||||||
}
|
}
|
||||||
server {
|
server {
|
||||||
|
|||||||
@@ -21,4 +21,13 @@ printf '%s' "$policy" > /tmp/policy.json
|
|||||||
mc admin policy create local dtf-artwork /tmp/policy.json >/dev/null
|
mc admin policy create local dtf-artwork /tmp/policy.json >/dev/null
|
||||||
mc admin policy attach local dtf-artwork --user "$S3_APP_USER" >/dev/null
|
mc admin policy attach local dtf-artwork --user "$S3_APP_USER" >/dev/null
|
||||||
mc ilm import "local/$S3_BUCKET" < /lifecycle.json
|
mc ilm import "local/$S3_BUCKET" < /lifecycle.json
|
||||||
|
# The backup bucket and its own account, which can write and read backups but
|
||||||
|
# not delete them: the same separation as the backup token on R2.
|
||||||
|
case "$S3_BACKUP_BUCKET" in *[!a-z0-9.-]*|'') echo 'Invalid local backup bucket name' >&2; exit 1;; esac
|
||||||
|
mc mb --ignore-existing "local/$S3_BACKUP_BUCKET" >/dev/null
|
||||||
|
mc anonymous set none "local/$S3_BACKUP_BUCKET" >/dev/null
|
||||||
|
mc admin user add local "$S3_BACKUP_USER" "$S3_BACKUP_PASSWORD" >/dev/null
|
||||||
|
printf '%s' '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:ListBucket","s3:GetBucketLocation"],"Resource":["arn:aws:s3:::'"$S3_BACKUP_BUCKET"'"]},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:AbortMultipartUpload","s3:ListMultipartUploadParts"],"Resource":["arn:aws:s3:::'"$S3_BACKUP_BUCKET"'/*"]}]}' > /tmp/backup-policy.json
|
||||||
|
mc admin policy create local dtf-backup /tmp/backup-policy.json >/dev/null
|
||||||
|
mc admin policy attach local dtf-backup --user "$S3_BACKUP_USER" >/dev/null
|
||||||
echo 'Local storage runtime account provisioned.'
|
echo 'Local storage runtime account provisioned.'
|
||||||
|
|||||||
99
ops/backfill_thumbnails.py
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
"""Make the Kanban's artwork pictures for orders and quotes from before them.
|
||||||
|
|
||||||
|
The customer's browser makes each picture while uploading; older uploads have
|
||||||
|
none, so their cards show "sem prévia". Run once from the worker's console:
|
||||||
|
|
||||||
|
python -m ops.backfill_thumbnails makes the missing pictures
|
||||||
|
python -m ops.backfill_thumbnails --limite 20 at most 20 this run
|
||||||
|
|
||||||
|
Only PNG, JPEG and WebP: a PDF, CDR, AI or PSD needs a renderer the server
|
||||||
|
does not have, and keeps showing the layout drawing. Files are read from
|
||||||
|
storage one at a time, and very large images are skipped, so the worker is
|
||||||
|
never asked for more memory than a normal sheet takes. Safe to run again: it
|
||||||
|
only looks at uploads that still have no picture.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import io
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
from app.core import db
|
||||||
|
|
||||||
|
# The Site's own picture size (site-upload.js: miniatura(..., 480, 720)).
|
||||||
|
BOX = (480, 720)
|
||||||
|
# Decoding a PNG takes about 4 bytes a pixel: 100 million pixels is ~400 MB.
|
||||||
|
MAX_PIXELS = 100_000_000
|
||||||
|
MAX_BYTES = 300 * 1024 * 1024
|
||||||
|
IMAGES = ('.png', '.jpg', '.jpeg', '.webp')
|
||||||
|
|
||||||
|
|
||||||
|
def missing(c, limit):
|
||||||
|
"""Uploads an order or a live quote refers to, still readable, without a picture."""
|
||||||
|
return c.execute('''WITH used AS (
|
||||||
|
SELECT (jsonb_array_elements_text(i->'uploads'))::uuid AS id
|
||||||
|
FROM dtf_local.orders o, jsonb_array_elements(o.snapshot->'items') i
|
||||||
|
UNION
|
||||||
|
SELECT (jsonb_array_elements_text(i->'uploads'))::uuid
|
||||||
|
FROM dtf_local.quotes q, jsonb_array_elements(q.draft->'items') i
|
||||||
|
WHERE q.discarded_at IS NULL AND q.created_at > now() - interval '3 days')
|
||||||
|
SELECT u.id, u.name, u.size, u.object_key FROM dtf_local.uploads u JOIN used ON used.id=u.id
|
||||||
|
LEFT JOIN dtf_local.upload_thumbnails t ON t.upload_id=u.id
|
||||||
|
WHERE t.upload_id IS NULL AND u.complete AND u.purged_at IS NULL AND u.expires_at>now()
|
||||||
|
AND u.scan_state='clean'
|
||||||
|
ORDER BY u.created_at DESC LIMIT %s''', (limit,)).fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def picture(path):
|
||||||
|
"""A WebP no larger than BOX, or None when the image is too large to decode."""
|
||||||
|
from PIL import Image
|
||||||
|
Image.MAX_IMAGE_PIXELS = None # the size is checked here, before decoding
|
||||||
|
with Image.open(path) as image:
|
||||||
|
width, height = image.size
|
||||||
|
if width * height > MAX_PIXELS:
|
||||||
|
return None
|
||||||
|
image.draft('RGB', BOX) # JPEG decodes at a reduced size
|
||||||
|
image.thumbnail(BOX)
|
||||||
|
out = io.BytesIO()
|
||||||
|
image.convert('RGBA' if 'A' in image.getbands() or image.mode == 'P' else 'RGB').save(
|
||||||
|
out, 'WEBP', quality=80)
|
||||||
|
return out.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None, out=print, storage=None, connect=db.connect):
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m ops.backfill_thumbnails')
|
||||||
|
parser.add_argument('--limite', type=int, default=500)
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
if storage is None:
|
||||||
|
from app.runtime import storage
|
||||||
|
with connect() as c:
|
||||||
|
rows = missing(c, args.limite)
|
||||||
|
made = skipped = 0
|
||||||
|
for row in rows:
|
||||||
|
if not row['name'].lower().endswith(IMAGES) or (row['size'] or 0) > MAX_BYTES:
|
||||||
|
skipped += 1
|
||||||
|
continue
|
||||||
|
handle, path = tempfile.mkstemp(prefix='thumb-')
|
||||||
|
os.close(handle)
|
||||||
|
try:
|
||||||
|
storage.fetch(row['object_key'], path)
|
||||||
|
data = picture(path)
|
||||||
|
except Exception as exc: # one unreadable file must not stop the others
|
||||||
|
out(f"{row['name']}: não deu para ler ({type(exc).__name__})")
|
||||||
|
skipped += 1
|
||||||
|
continue
|
||||||
|
finally:
|
||||||
|
os.unlink(path)
|
||||||
|
if data is None:
|
||||||
|
skipped += 1
|
||||||
|
continue
|
||||||
|
with connect() as c:
|
||||||
|
c.execute('''INSERT INTO dtf_local.upload_thumbnails(upload_id,mime,data) VALUES(%s,'image/webp',%s)
|
||||||
|
ON CONFLICT (upload_id) DO NOTHING''', (row['id'], data))
|
||||||
|
made += 1
|
||||||
|
out(f'{made} miniatura(s) criada(s); {skipped} arquivo(s) sem miniatura possível (PDF, CDR, muito grande).')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
289
ops/db_backup.py
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
"""Daily off-server backup of the database to its own bucket.
|
||||||
|
|
||||||
|
python -m ops.db_backup serve # the stack's backup service
|
||||||
|
python -m ops.db_backup once # one backup now
|
||||||
|
python -m ops.db_backup list # what the bucket holds
|
||||||
|
python -m ops.db_backup verify --identity F # restore the latest into a scratch database, then drop it
|
||||||
|
python -m ops.db_backup restore KEY --identity F --into NAME
|
||||||
|
|
||||||
|
Each backup is `pg_dump --format=custom`, checked with `pg_restore --list`,
|
||||||
|
encrypted with age to BACKUP_AGE_RECIPIENT (a public key) and uploaded to
|
||||||
|
BACKUP_BUCKET with credentials of its own. The server holds only the public
|
||||||
|
key: it can write backups but not read them. The private key (the identity)
|
||||||
|
stays with the owner, off the server, and is needed only to restore.
|
||||||
|
|
||||||
|
Old backups are removed by the bucket's lifecycle rule, not by this job, so
|
||||||
|
its credential never needs to delete; a bucket lock keeps a stolen one from
|
||||||
|
deleting either. Every run is recorded in dtf_local.backups, which the Kanban
|
||||||
|
shows on its Integrations tab.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import boto3
|
||||||
|
import psycopg
|
||||||
|
from botocore.config import Config
|
||||||
|
from psycopg import sql
|
||||||
|
from psycopg.conninfo import conninfo_to_dict
|
||||||
|
|
||||||
|
from app.bootstrap import admin_connect
|
||||||
|
from app.core.secrets import load as load_secret_files
|
||||||
|
|
||||||
|
BRASILIA = timezone(timedelta(hours=-3))
|
||||||
|
PREFIX = 'db/'
|
||||||
|
# After a failure the next attempt comes this much later, not a day later.
|
||||||
|
RETRY = timedelta(hours=1)
|
||||||
|
# A backup older than this means the daily run has stopped working.
|
||||||
|
STALE = timedelta(hours=26)
|
||||||
|
TABLES = ('orders', 'quotes', 'uploads', 'accounts', 'movements', 'payment_intents')
|
||||||
|
|
||||||
|
|
||||||
|
def configured():
|
||||||
|
return all(os.environ.get(k) for k in
|
||||||
|
('BACKUP_S3_ENDPOINT', 'BACKUP_BUCKET', 'BACKUP_ACCESS_KEY_ID',
|
||||||
|
'BACKUP_SECRET_ACCESS_KEY', 'BACKUP_AGE_RECIPIENT'))
|
||||||
|
|
||||||
|
|
||||||
|
def bucket():
|
||||||
|
client = boto3.client('s3', endpoint_url=os.environ['BACKUP_S3_ENDPOINT'],
|
||||||
|
aws_access_key_id=os.environ['BACKUP_ACCESS_KEY_ID'],
|
||||||
|
aws_secret_access_key=os.environ['BACKUP_SECRET_ACCESS_KEY'],
|
||||||
|
region_name=os.environ.get('BACKUP_REGION', 'auto'),
|
||||||
|
config=Config(signature_version='s3v4', s3={'addressing_style': 'path'},
|
||||||
|
retries={'max_attempts': 5, 'mode': 'standard'}))
|
||||||
|
return client, os.environ['BACKUP_BUCKET']
|
||||||
|
|
||||||
|
|
||||||
|
def admin_params(database=None):
|
||||||
|
"""The administrator's connection, optionally to another database."""
|
||||||
|
if os.environ.get('DATABASE_ADMIN_HOST'):
|
||||||
|
params = {'host': os.environ['DATABASE_ADMIN_HOST'], 'user': os.environ['DATABASE_ADMIN_USER'],
|
||||||
|
'password': os.environ['DATABASE_ADMIN_PASSWORD'], 'dbname': os.environ['DATABASE_ADMIN_NAME']}
|
||||||
|
else:
|
||||||
|
params = conninfo_to_dict(os.environ['DATABASE_ADMIN_URL'])
|
||||||
|
if database:
|
||||||
|
params['dbname'] = database
|
||||||
|
return params
|
||||||
|
|
||||||
|
|
||||||
|
def libpq_env(database=None):
|
||||||
|
"""pg_dump and pg_restore read the credentials from the environment, so the
|
||||||
|
password never appears in a process list."""
|
||||||
|
names = {'host': 'PGHOST', 'port': 'PGPORT', 'user': 'PGUSER', 'password': 'PGPASSWORD', 'dbname': 'PGDATABASE'}
|
||||||
|
return {**os.environ, **{names[k]: str(v) for k, v in admin_params(database).items() if k in names}}
|
||||||
|
|
||||||
|
|
||||||
|
def run(command, **kwargs):
|
||||||
|
result = subprocess.run(command, capture_output=True, **kwargs)
|
||||||
|
if result.returncode:
|
||||||
|
detail = result.stderr.decode(errors='replace').strip().splitlines()
|
||||||
|
raise RuntimeError(f'{command[0]} failed: ' + (detail[-1] if detail else f'exit {result.returncode}'))
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def sha256(path):
|
||||||
|
digest = hashlib.sha256()
|
||||||
|
with open(path, 'rb') as stream:
|
||||||
|
for block in iter(lambda: stream.read(1 << 20), b''):
|
||||||
|
digest.update(block)
|
||||||
|
return digest.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def record(started, status, key=None, size=None, detail=None):
|
||||||
|
with admin_connect() as c:
|
||||||
|
c.execute('''INSERT INTO dtf_local.backups(id,started_at,finished_at,status,object_key,bytes,detail)
|
||||||
|
VALUES(%s,%s,now(),%s,%s,%s,%s)''',
|
||||||
|
(uuid4(), started, status, key, size, detail))
|
||||||
|
|
||||||
|
|
||||||
|
def run_once():
|
||||||
|
"""Dump, check, encrypt and upload one backup. Returns its object key."""
|
||||||
|
started = datetime.now(timezone.utc)
|
||||||
|
key = PREFIX + started.strftime('%Y/%m/dtf-%Y%m%dT%H%M%SZ') + '.dump.age'
|
||||||
|
try:
|
||||||
|
client, name = bucket()
|
||||||
|
with tempfile.TemporaryDirectory(dir=os.environ.get('BACKUP_TMP')) as work:
|
||||||
|
dump, sealed = Path(work, 'dtf.dump'), Path(work, 'dtf.dump.age')
|
||||||
|
run(['pg_dump', '--format=custom', '--compress=6', '--file', str(dump)], env=libpq_env())
|
||||||
|
# A truncated or damaged archive fails here, before it is kept.
|
||||||
|
run(['pg_restore', '--list', str(dump)])
|
||||||
|
run(['age', '--encrypt', '--recipient', os.environ['BACKUP_AGE_RECIPIENT'],
|
||||||
|
'--output', str(sealed), str(dump)])
|
||||||
|
size = sealed.stat().st_size
|
||||||
|
client.upload_file(str(sealed), name, key, ExtraArgs={'Metadata': {
|
||||||
|
'sha256': sha256(sealed), 'dump-sha256': sha256(dump), 'format': 'pg-custom+age'}})
|
||||||
|
record(started, 'ok', key, size)
|
||||||
|
print(f'Backup {key} uploaded ({size} bytes).', flush=True)
|
||||||
|
return key
|
||||||
|
except Exception as error:
|
||||||
|
# The message names a command or a provider error, never a credential.
|
||||||
|
detail = str(error)[:500]
|
||||||
|
try:
|
||||||
|
record(started, 'failed', key, None, detail)
|
||||||
|
finally:
|
||||||
|
print(f'Backup failed: {detail}', file=sys.stderr, flush=True)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def last_ok():
|
||||||
|
with admin_connect() as c:
|
||||||
|
row = c.execute("SELECT max(finished_at) FROM dtf_local.backups WHERE status='ok'").fetchone()
|
||||||
|
return row[0]
|
||||||
|
|
||||||
|
|
||||||
|
def next_run(now, hour):
|
||||||
|
"""The next `hour` o'clock in Brasília after `now`."""
|
||||||
|
local = now.astimezone(BRASILIA)
|
||||||
|
at = local.replace(hour=hour, minute=0, second=0, microsecond=0)
|
||||||
|
if at <= local:
|
||||||
|
at += timedelta(days=1)
|
||||||
|
return at.astimezone(timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
|
def serve():
|
||||||
|
if not configured():
|
||||||
|
print('Backup not configured: set BACKUP_S3_ENDPOINT, BACKUP_BUCKET, BACKUP_ACCESS_KEY_ID, '
|
||||||
|
'BACKUP_SECRET_ACCESS_KEY and BACKUP_AGE_RECIPIENT. Idle.', flush=True)
|
||||||
|
while True:
|
||||||
|
time.sleep(3600)
|
||||||
|
hour = int(os.environ.get('BACKUP_HOUR', '3'))
|
||||||
|
# Wait for the schema (db-init) before the first query.
|
||||||
|
for _ in range(60):
|
||||||
|
try:
|
||||||
|
previous = last_ok()
|
||||||
|
break
|
||||||
|
except psycopg.Error:
|
||||||
|
time.sleep(10)
|
||||||
|
else:
|
||||||
|
previous = last_ok()
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
# A fresh deployment, or one that missed a day, backs up straight away.
|
||||||
|
due = now if previous is None or now - previous > STALE else next_run(now, hour)
|
||||||
|
while True:
|
||||||
|
time.sleep(max(0, (due - datetime.now(timezone.utc)).total_seconds()))
|
||||||
|
try:
|
||||||
|
run_once()
|
||||||
|
due = next_run(datetime.now(timezone.utc), hour)
|
||||||
|
except Exception:
|
||||||
|
due = datetime.now(timezone.utc) + RETRY
|
||||||
|
|
||||||
|
|
||||||
|
def listing():
|
||||||
|
client, name = bucket()
|
||||||
|
keys = []
|
||||||
|
for page in client.get_paginator('list_objects_v2').paginate(Bucket=name, Prefix=PREFIX):
|
||||||
|
keys += [(o['Key'], o['Size'], o['LastModified']) for o in page.get('Contents', [])]
|
||||||
|
return sorted(keys)
|
||||||
|
|
||||||
|
|
||||||
|
def counts(database):
|
||||||
|
with psycopg.connect(**admin_params(database)) as c:
|
||||||
|
return {t: c.execute(sql.SQL('SELECT count(*) FROM dtf_local.{}').format(sql.Identifier(t))).fetchone()[0]
|
||||||
|
for t in TABLES}
|
||||||
|
|
||||||
|
|
||||||
|
def restore(key, identity, into):
|
||||||
|
"""Restore one backup into a database that is new or holds no DTF data."""
|
||||||
|
client, name = bucket()
|
||||||
|
with admin_connect() as c:
|
||||||
|
c.autocommit = True
|
||||||
|
if into == c.execute('SELECT current_database()').fetchone()[0]:
|
||||||
|
raise SystemExit('Refusing to restore over the database the stack is using.')
|
||||||
|
exists = c.execute('SELECT 1 FROM pg_database WHERE datname=%s', (into,)).fetchone()
|
||||||
|
if not exists:
|
||||||
|
c.execute(sql.SQL('CREATE DATABASE {}').format(sql.Identifier(into)))
|
||||||
|
if exists:
|
||||||
|
with psycopg.connect(**admin_params(into)) as c:
|
||||||
|
if c.execute("SELECT 1 FROM pg_namespace WHERE nspname='dtf_local'").fetchone():
|
||||||
|
raise SystemExit(f'{into} already holds DTF data; choose an empty or new database.')
|
||||||
|
with tempfile.TemporaryDirectory(dir=os.environ.get('BACKUP_TMP')) as work:
|
||||||
|
sealed, dump = Path(work, 'dtf.dump.age'), Path(work, 'dtf.dump')
|
||||||
|
client.download_file(name, key, str(sealed))
|
||||||
|
expected = client.head_object(Bucket=name, Key=key)['Metadata'].get('sha256')
|
||||||
|
if expected and sha256(sealed) != expected:
|
||||||
|
raise SystemExit('The downloaded backup does not match its checksum.')
|
||||||
|
run(['age', '--decrypt', '--identity', identity, '--output', str(dump), str(sealed)])
|
||||||
|
run(['pg_restore', '--exit-on-error', '--no-owner', '--no-privileges', '--dbname', into, str(dump)],
|
||||||
|
env=libpq_env(into))
|
||||||
|
return counts(into)
|
||||||
|
|
||||||
|
|
||||||
|
def drop(database):
|
||||||
|
with admin_connect() as c:
|
||||||
|
c.autocommit = True
|
||||||
|
c.execute(sql.SQL('DROP DATABASE IF EXISTS {} WITH (FORCE)').format(sql.Identifier(database)))
|
||||||
|
|
||||||
|
|
||||||
|
def verify(identity, key=None):
|
||||||
|
"""The restore test: the latest backup into a scratch database, counted, dropped."""
|
||||||
|
key = key or listing()[-1][0]
|
||||||
|
scratch = 'dtf_verify_' + uuid4().hex[:12]
|
||||||
|
try:
|
||||||
|
restored = restore(key, identity, scratch)
|
||||||
|
finally:
|
||||||
|
drop(scratch)
|
||||||
|
print(f'PASS: {key} decrypted and restored into a scratch database (now dropped). Rows: ' +
|
||||||
|
', '.join(f'{t} {n}' for t, n in restored.items()))
|
||||||
|
return restored
|
||||||
|
|
||||||
|
|
||||||
|
def identity_file(value):
|
||||||
|
"""The identity as a path, or '-' to paste it: nothing is written to disk
|
||||||
|
except a private temporary file removed when the command ends."""
|
||||||
|
if value != '-':
|
||||||
|
return value, None
|
||||||
|
print('Paste the private key (AGE-SECRET-KEY-...) and press Enter:', file=sys.stderr)
|
||||||
|
line = sys.stdin.readline().strip()
|
||||||
|
handle = tempfile.NamedTemporaryFile('w', prefix='identity-', dir=os.environ.get('BACKUP_TMP'), delete=False)
|
||||||
|
os.chmod(handle.name, 0o600)
|
||||||
|
handle.write(line + '\n')
|
||||||
|
handle.close()
|
||||||
|
return handle.name, handle.name
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
load_secret_files()
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m ops.db_backup')
|
||||||
|
sub = parser.add_subparsers(dest='command', required=True)
|
||||||
|
sub.add_parser('serve')
|
||||||
|
sub.add_parser('once')
|
||||||
|
sub.add_parser('list')
|
||||||
|
for name in ('verify', 'restore'):
|
||||||
|
p = sub.add_parser(name)
|
||||||
|
if name == 'restore':
|
||||||
|
p.add_argument('key')
|
||||||
|
p.add_argument('--into', required=True, help='a new or empty database')
|
||||||
|
else:
|
||||||
|
p.add_argument('key', nargs='?', help='default: the latest backup')
|
||||||
|
p.add_argument('--identity', required=True, help="the private key file, or '-' to paste it")
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
if args.command == 'serve':
|
||||||
|
serve()
|
||||||
|
elif args.command == 'once':
|
||||||
|
run_once()
|
||||||
|
elif args.command == 'list':
|
||||||
|
for key, size, modified in listing():
|
||||||
|
print(f'{modified:%Y-%m-%d %H:%M} UTC {size:>12} {key}')
|
||||||
|
else:
|
||||||
|
path, temporary = identity_file(args.identity)
|
||||||
|
try:
|
||||||
|
if args.command == 'verify':
|
||||||
|
verify(path, args.key)
|
||||||
|
else:
|
||||||
|
rows = restore(args.key, path, args.into)
|
||||||
|
print(f'Restored {args.key} into {args.into}. Rows: ' + ', '.join(f'{t} {n}' for t, n in rows.items()))
|
||||||
|
finally:
|
||||||
|
if temporary:
|
||||||
|
os.unlink(temporary)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
82
ops/demo_customer.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
"""A demo customer for looking around Minha conta on the local stack.
|
||||||
|
|
||||||
|
docker compose -f compose.local.yaml exec -T -e SITE_BASE_URL=http://site \
|
||||||
|
-e SITE_HOST_HEADER=localhost api python -m ops.demo_customer
|
||||||
|
|
||||||
|
Creates, once, an account with twelve paid orders in different stages, one in
|
||||||
|
correction and a cart waiting for PIX. Sign in at http://localhost:8080/conta
|
||||||
|
with DEMO_EMAIL and DEMO_PASSWORD below. Local test values only: the script
|
||||||
|
refuses to run anywhere but the local stack.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
DEMO_EMAIL = 'demo@dropstar.test'
|
||||||
|
DEMO_PASSWORD = 'demo-dropstar-local'
|
||||||
|
DEMO_CNPJ = '11222333000181'
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if os.environ.get('APP_ENV') != 'local':
|
||||||
|
sys.exit('Only on the local stack.')
|
||||||
|
from app.core import db
|
||||||
|
import tests.smoke_test as smoke
|
||||||
|
from tests.smoke_test import Client, approved_quote, item_spec, upload_bytes
|
||||||
|
# Upload links are signed for the address the browser uses (localhost:9000),
|
||||||
|
# which inside this container is the container itself: send them to the
|
||||||
|
# storage service, keeping the signed Host.
|
||||||
|
public = os.environ.get('S3_PUBLIC_ENDPOINT', '').rstrip('/')
|
||||||
|
if public and public != 'http://storage:9000':
|
||||||
|
plain = smoke.urlopen
|
||||||
|
def via_storage(request, *args, **kwargs):
|
||||||
|
if isinstance(request, smoke.Request) and request.full_url.startswith(public + '/'):
|
||||||
|
request.add_header('Host', public.split('//', 1)[1])
|
||||||
|
request.full_url = 'http://storage:9000' + request.full_url[len(public):]
|
||||||
|
return plain(request, *args, **kwargs)
|
||||||
|
smoke.urlopen = via_storage
|
||||||
|
with db.connect() as c:
|
||||||
|
if c.execute('SELECT 1 FROM dtf_local.accounts WHERE email=%s', (DEMO_EMAIL,)).fetchone():
|
||||||
|
print(f'Demo customer already exists: {DEMO_EMAIL}')
|
||||||
|
return
|
||||||
|
client = Client()
|
||||||
|
client.call('/session')
|
||||||
|
profile = {'cnpj': DEMO_CNPJ, 'zap': '16999990000', 'mail': DEMO_EMAIL}
|
||||||
|
orders = []
|
||||||
|
for i in range(12):
|
||||||
|
uid = upload_bytes(client, b'DEMO ARTWORK %d' % i)
|
||||||
|
item = item_spec('file', f'{1 + i * 0.3:.1f}', 0, uid)
|
||||||
|
quote = client.call('/quotes', {'request_key': str(uuid4()), 'customer': profile, 'items': [item],
|
||||||
|
'freight': {'service': 'pickup'}})
|
||||||
|
approved_quote(client, quote, [item])
|
||||||
|
orders.append(client.call('/orders/dev-paid', {'quote_id': quote['id']}))
|
||||||
|
# Spread them over the production stages, one waiting for a correction.
|
||||||
|
stages = {0: ['tra', 'fil', 'imp', 'fin'], 1: ['tra', 'fil', 'imp', 'fin'], 2: ['tra', 'fil', 'imp'],
|
||||||
|
3: ['tra', 'fil'], 4: ['tra'], 5: ['cor']}
|
||||||
|
for index, path in stages.items():
|
||||||
|
order = orders[index]
|
||||||
|
version = order['version']
|
||||||
|
for state in path:
|
||||||
|
if state == 'fil':
|
||||||
|
# The print queue takes approved final files, as on the Kanban.
|
||||||
|
final = upload_bytes(client, b'DEMO FINAL %d' % index, order_id=order['id'])
|
||||||
|
version = client.call('/operator/orders/' + order['id'] + '/final-files',
|
||||||
|
{'version': version, 'files': [{'item_index': 0, 'upload_id': final}],
|
||||||
|
'note': 'Arquivo final da demonstração'}, operator=True)['version']
|
||||||
|
body = {'state': state, 'version': version}
|
||||||
|
if state == 'cor':
|
||||||
|
body['reason'] = 'A arte está com fundo branco; envie em PNG com fundo transparente.'
|
||||||
|
version = client.call('/operator/orders/' + order['id'] + '/move', body, operator=True)['version']
|
||||||
|
# The cart waiting for payment, with a PIX code open.
|
||||||
|
uid = upload_bytes(client, b'DEMO ARTWORK UNPAID')
|
||||||
|
item = item_spec('file', '2.3', 0, uid)
|
||||||
|
quote = client.call('/quotes', {'request_key': str(uuid4()), 'customer': profile, 'items': [item],
|
||||||
|
'freight': {'service': 'pickup'}})
|
||||||
|
approved_quote(client, quote, [item])
|
||||||
|
client.call('/payments/intent', {'quote_id': quote['id'], 'method': {'type': 'pix'}})
|
||||||
|
client.call('/account/register', {'customer': profile, 'password': DEMO_PASSWORD})
|
||||||
|
print(f'Demo customer created: {DEMO_EMAIL}')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
83
ops/launch_reset.py
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
"""Clear the test orders before the Site opens to customers, once, by hand.
|
||||||
|
|
||||||
|
Run from the worker's console (Portainer > Containers > worker > Console):
|
||||||
|
|
||||||
|
python -m ops.launch_reset shows what would be removed
|
||||||
|
python -m ops.launch_reset --confirmar APAGAR removes it
|
||||||
|
python -m ops.launch_reset --confirmar APAGAR --contas also customer accounts
|
||||||
|
|
||||||
|
Removes every order with its moves, files and notices, every quote, payment
|
||||||
|
attempt and payment notification, and the artwork pictures. The artwork
|
||||||
|
files are marked expired, so the worker deletes them from storage within a
|
||||||
|
minute. Operators, the Tiny connection and the backups are kept.
|
||||||
|
|
||||||
|
It refuses if any payment was made with real money (Mercado Pago's
|
||||||
|
live_mode): those are customers' orders, not tests.
|
||||||
|
|
||||||
|
Order numbers start again at #1 only with the database administrator's
|
||||||
|
rights, which the worker does not have. The script prints the one command
|
||||||
|
to run in the database container's console for that.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from app.core import db
|
||||||
|
|
||||||
|
TABLES = [ # in an order the foreign keys allow
|
||||||
|
('movements', 'movimentos de etapa'),
|
||||||
|
('order_files', 'arquivos finais e correções'),
|
||||||
|
('print_files', 'arquivos de impressão gerados'),
|
||||||
|
('outbox', 'envios ao Tiny e avisos'),
|
||||||
|
('orders', 'pedidos'),
|
||||||
|
('payment_intents', 'tentativas de pagamento'),
|
||||||
|
('payment_events', 'avisos de pagamento'),
|
||||||
|
('quotes', 'cotações'),
|
||||||
|
('upload_thumbnails', 'miniaturas das artes'),
|
||||||
|
]
|
||||||
|
ACCOUNTS = [('sessions', 'sessões de clientes'), ('accounts', 'contas de clientes')]
|
||||||
|
CONFIRM = 'APAGAR'
|
||||||
|
RESTART = 'ALTER TABLE dtf_local.orders ALTER COLUMN number RESTART WITH 1;'
|
||||||
|
|
||||||
|
|
||||||
|
def counts(c, tables):
|
||||||
|
return {table: c.execute(f'SELECT count(*) AS n FROM dtf_local.{table}').fetchone()['n'] for table, _ in tables}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None, out=print, connect=db.connect):
|
||||||
|
parser = argparse.ArgumentParser(prog='python -m ops.launch_reset')
|
||||||
|
parser.add_argument('--confirmar', default='', help=f'escreva {CONFIRM} para apagar')
|
||||||
|
parser.add_argument('--contas', action='store_true', help='apagar também as contas de clientes')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
tables = TABLES + (ACCOUNTS if args.contas else [])
|
||||||
|
with connect() as c:
|
||||||
|
live = c.execute("""SELECT count(*) AS n FROM dtf_local.payment_intents
|
||||||
|
WHERE response->>'live_mode'='true'""").fetchone()['n']
|
||||||
|
if live:
|
||||||
|
out(f'Recusado: {live} pagamento(s) com dinheiro real (live_mode). Esses pedidos são de clientes.')
|
||||||
|
return 1
|
||||||
|
before = counts(c, tables)
|
||||||
|
files = c.execute('SELECT count(*) AS n FROM dtf_local.uploads WHERE purged_at IS NULL').fetchone()['n']
|
||||||
|
for table, label in tables:
|
||||||
|
out(f'{before[table]:>7} {label}')
|
||||||
|
out(f'{files:>7} arquivos de arte (o worker apaga do armazenamento)')
|
||||||
|
if args.confirmar != CONFIRM:
|
||||||
|
out(f'Nada foi apagado. Repita com --confirmar {CONFIRM} para apagar.')
|
||||||
|
return 0
|
||||||
|
for table, _ in tables:
|
||||||
|
c.execute(f'DELETE FROM dtf_local.{table}')
|
||||||
|
c.execute('UPDATE dtf_local.uploads SET expires_at=now() WHERE purged_at IS NULL')
|
||||||
|
out('Apagado.')
|
||||||
|
try:
|
||||||
|
with connect() as c:
|
||||||
|
c.execute(RESTART)
|
||||||
|
out('A numeração dos pedidos recomeça em #1.')
|
||||||
|
except Exception as exc: # the application's role cannot alter the table
|
||||||
|
if 'permission' not in str(exc).lower() and 'owner' not in str(exc).lower():
|
||||||
|
raise
|
||||||
|
out('Para a numeração recomeçar em #1, rode no console do container do banco (db):')
|
||||||
|
out(f' psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "{RESTART}"')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
@@ -115,7 +115,7 @@ try{
|
|||||||
await call('Runtime.enable');await call('Page.enable');
|
await call('Runtime.enable');await call('Page.enable');
|
||||||
await call('Emulation.setDeviceMetricsOverride',{width:1440,height:1100,deviceScaleFactor:1,mobile:false});
|
await call('Emulation.setDeviceMetricsOverride',{width:1440,height:1100,deviceScaleFactor:1,mobile:false});
|
||||||
await call('Page.navigate',{url:`http://127.0.0.1:${server.address().port}/`});
|
await call('Page.navigate',{url:`http://127.0.0.1:${server.address().port}/`});
|
||||||
await waitFor(()=>evaluate(`typeof sel==='function' && typeof AUTO!=='undefined'`),'Site scripts');
|
await waitFor(()=>evaluate(`typeof sel==='function' && typeof pintaEntrega==='function'`),'Site scripts');
|
||||||
// Observe the actual engine, without replacing its placement or rendering.
|
// Observe the actual engine, without replacing its placement or rendering.
|
||||||
await evaluate(`(()=>{
|
await evaluate(`(()=>{
|
||||||
const original=encaixar;
|
const original=encaixar;
|
||||||
@@ -149,15 +149,38 @@ try{
|
|||||||
// Navigation links must reach the chooser, never preselect a product.
|
// Navigation links must reach the chooser, never preselect a product.
|
||||||
assert.equal(await evaluate(`document.querySelectorAll('[data-modo-cta]').length`),0);
|
assert.equal(await evaluate(`document.querySelectorAll('[data-modo-cta]').length`),0);
|
||||||
await click('[data-modo="file"]');
|
await click('[data-modo="file"]');
|
||||||
|
// Opening a product paints on the next frame; on a busy runner the check
|
||||||
|
// must wait for it rather than race it.
|
||||||
|
await waitFor(()=>evaluate(`!$('tipoEnvio').hidden`),'by-metre product open');
|
||||||
// By-metre opens declaring a mounted sheet; switching is explicit and priced.
|
// By-metre opens declaring a mounted sheet; switching is explicit and priced.
|
||||||
assert.deepEqual(await evaluate(`({shown:!$('tipoEnvio').hidden,
|
assert.deepEqual(await evaluate(`({shown:!$('tipoEnvio').hidden,
|
||||||
on:[...document.querySelectorAll('#tipoEnvio .cam.on')].map(b=>b.dataset.tipo)})`),
|
on:[...document.querySelectorAll('#tipoEnvio .cam.on')].map(b=>b.dataset.tipo)})`),
|
||||||
{shown:true,on:['folha']});
|
{shown:true,on:['folha']});
|
||||||
await evaluate(`(()=>{const f=new File(['x'],'oversize.cdr');Object.defineProperty(f,'size',{value:128*1048576+1});sel([f]);})()`);
|
await evaluate(`(()=>{const f=new File(['x'],'oversize.cdr');Object.defineProperty(f,'size',{value:5*1073741824+1});sel([f]);})()`);
|
||||||
assert.equal(await evaluate(`folhas.length===0 && $('recusa').textContent.includes('128 MB')`),true,
|
assert.equal(await evaluate(`folhas.length===0 && $('recusa').textContent.includes('5 GB')`),true,
|
||||||
'files beyond the scanner limit are rejected before browser analysis');
|
'files beyond the 5 GB upload limit are rejected before browser analysis');
|
||||||
|
// A multi-GB sheet is the normal order: graded from the pixel size in its
|
||||||
|
// header, never decoded. 6732 x 35433 px across 57 cm is 3 m at 300 DPI.
|
||||||
|
await evaluate(`(()=>{const h=new Uint8Array(33);h.set([0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0,0,0,13,0x49,0x48,0x44,0x52]);
|
||||||
|
const v=new DataView(h.buffer);v.setUint32(16,6732);v.setUint32(20,35433);h.set([8,6,0,0,0],24);
|
||||||
|
const f=new File([h],'folha-grande.png');Object.defineProperty(f,'size',{value:3*1073741824});sel([f]);})()`);
|
||||||
|
await waitFor(()=>evaluate(`folhas.length===1 && !!folhas[0].an`),'large sheet graded from its header');
|
||||||
|
assert.deepEqual(await evaluate(`({grande:folhas[0].an.grande,dpi:folhas[0].an.dpi,alt:folhas[0].med.alt,preview:!!folhas[0].previewSrc})`),
|
||||||
|
{grande:true,dpi:300,alt:300,preview:false});
|
||||||
|
await evaluate(`folhas=[];pintaFolha()`);
|
||||||
await evaluate(`pickTipo('avulsa')`);
|
await evaluate(`pickTipo('avulsa')`);
|
||||||
assert.equal(await evaluate('modo'),'avulsa');
|
assert.equal(await evaluate('modo'),'avulsa');
|
||||||
|
// A large JPG artwork cannot be read in parts: it is measured from its
|
||||||
|
// header and placed as a full box, with the discount, instead of refused.
|
||||||
|
await evaluate(`(()=>{const h=new Uint8Array(20);h.set([0xFF,0xD8,0xFF,0xC0,0,17,8,0x09,0x3A,0x09,0x3A,3]);
|
||||||
|
const f=new File([h],'arte-grande.jpg');Object.defineProperty(f,'size',{value:400*1048576});sel([f]);})()`);
|
||||||
|
await waitFor(()=>evaluate(`artes.length===1 && artes[0].semPrevia===true`),'large JPG artwork measured from its header');
|
||||||
|
await fill('[data-cm]',20);
|
||||||
|
await waitFor(()=>evaluate(`!!itemAtual?.production?.sources?.length`),'large JPG artwork packed');
|
||||||
|
assert.deepEqual(await evaluate(`({dpi:Math.round(dpiDe(artes[0])),src:!!artes[0].src,
|
||||||
|
measurement:itemAtual.production.sources[0].measurement,note:$('lista').textContent.includes('Sem prévia')})`),
|
||||||
|
{dpi:300,src:false,measurement:'file',note:true});
|
||||||
|
await evaluate(`artes=[];pintaArtes()`);
|
||||||
await evaluate('sendImage()');
|
await evaluate('sendImage()');
|
||||||
await waitFor(()=>evaluate(`artes.length===1 && !!artes[0].src`),'JPG artwork model');
|
await waitFor(()=>evaluate(`artes.length===1 && !!artes[0].src`),'JPG artwork model');
|
||||||
assert.deepEqual(await evaluate(`({mode:modo,sheets:folhas.length,width:artes[0].cm,quantity:artes[0].q})`),{mode:'avulsa',sheets:0,width:0,quantity:1});
|
assert.deepEqual(await evaluate(`({mode:modo,sheets:folhas.length,width:artes[0].cm,quantity:artes[0].q})`),{mode:'avulsa',sheets:0,width:0,quantity:1});
|
||||||
@@ -284,13 +307,14 @@ try{
|
|||||||
await fill('[data-cm]',10);await fill('[data-q]',4);await packed(4);
|
await fill('[data-cm]',10);await fill('[data-q]',4);await packed(4);
|
||||||
}
|
}
|
||||||
// An older image load must not overwrite a newer edit, even before debounce.
|
// An older image load must not overwrite a newer edit, even before debounce.
|
||||||
await evaluate(`(()=>{window.realLoad=carregarImagem;window.delayed=[];
|
// The packing loads each artwork's image through imagemDaArte.
|
||||||
carregarImagem=f=>new Promise(resolve=>delayed.push(()=>realLoad(f).then(resolve)));
|
await evaluate(`(()=>{window.realLoad=imagemDaArte;window.delayed=[];
|
||||||
|
imagemDaArte=a=>new Promise(resolve=>delayed.push(()=>realLoad(a).then(resolve)));
|
||||||
})()`);
|
})()`);
|
||||||
await fill('[data-q]',5);
|
await fill('[data-q]',5);
|
||||||
await waitFor(()=>evaluate('delayed.length===1'),'delayed preview');
|
await waitFor(()=>evaluate('delayed.length===1'),'delayed preview');
|
||||||
await fill('[data-q]',7);
|
await fill('[data-q]',7);
|
||||||
await evaluate(`carregarImagem=realLoad;delayed[0]()`);
|
await evaluate(`imagemDaArte=realLoad;delayed[0]()`);
|
||||||
await packed(7);
|
await packed(7);
|
||||||
const timeoutCleanup=await evaluate(`(async()=>{
|
const timeoutCleanup=await evaluate(`(async()=>{
|
||||||
const realLoader=carregarPdfJs, realTimer=setTimeout, realWorker=temWorker;
|
const realLoader=carregarPdfJs, realTimer=setTimeout, realWorker=temWorker;
|
||||||
|
|||||||
79
tests/backup_test.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
"""The database backup against the local stack: dump, encrypt, upload, restore.
|
||||||
|
|
||||||
|
docker compose -f compose.local.yaml exec -T backup python -m tests.backup_test
|
||||||
|
|
||||||
|
Uses a throwaway age key made here, so nothing secret is kept in the repository.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from botocore.exceptions import ClientError
|
||||||
|
|
||||||
|
from app.bootstrap import admin_connect
|
||||||
|
from ops import db_backup
|
||||||
|
|
||||||
|
|
||||||
|
def check(condition, message):
|
||||||
|
if not condition:
|
||||||
|
raise AssertionError(message)
|
||||||
|
print('PASS:', message)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with tempfile.TemporaryDirectory() as work:
|
||||||
|
identity = Path(work, 'identity.txt')
|
||||||
|
subprocess.run(['age-keygen', '-o', str(identity)], check=True, capture_output=True)
|
||||||
|
public = next(line.split(': ', 1)[1] for line in identity.read_text().splitlines()
|
||||||
|
if line.startswith('# public key: '))
|
||||||
|
os.environ['BACKUP_AGE_RECIPIENT'] = public
|
||||||
|
check(db_backup.configured(), 'the local backup service is configured once a recipient is set')
|
||||||
|
|
||||||
|
live = db_backup.counts(None)
|
||||||
|
key = db_backup.run_once()
|
||||||
|
with admin_connect() as c:
|
||||||
|
row = c.execute('SELECT status,bytes FROM dtf_local.backups WHERE object_key=%s', (key,)).fetchone()
|
||||||
|
check(row is not None and row[0] == 'ok' and row[1] > 0, 'the run is recorded for the Kanban')
|
||||||
|
|
||||||
|
client, bucket = db_backup.bucket()
|
||||||
|
head = client.get_object(Bucket=bucket, Key=key, Range='bytes=0-20')['Body'].read()
|
||||||
|
check(head.startswith(b'age-encryption.org/v1'), 'what leaves the server is encrypted')
|
||||||
|
check(key in [k for k, _, _ in db_backup.listing()], 'the backup is listed in its bucket')
|
||||||
|
try:
|
||||||
|
client.delete_object(Bucket=bucket, Key=key)
|
||||||
|
deleted = True
|
||||||
|
except ClientError:
|
||||||
|
deleted = False
|
||||||
|
check(not deleted and key in [k for k, _, _ in db_backup.listing()],
|
||||||
|
'the backup credential cannot delete backups')
|
||||||
|
|
||||||
|
restored = db_backup.verify(str(identity), key)
|
||||||
|
check(restored == live, f'the restore has the same rows as the live database ({restored})')
|
||||||
|
|
||||||
|
live_name = db_backup.admin_params()['dbname']
|
||||||
|
try:
|
||||||
|
db_backup.restore(key, str(identity), live_name)
|
||||||
|
refused = False
|
||||||
|
except SystemExit:
|
||||||
|
refused = True
|
||||||
|
check(refused, 'a restore over the live database is refused')
|
||||||
|
|
||||||
|
other = Path(work, 'other.txt')
|
||||||
|
subprocess.run(['age-keygen', '-o', str(other)], check=True, capture_output=True)
|
||||||
|
try:
|
||||||
|
db_backup.verify(str(other), key)
|
||||||
|
opened = True
|
||||||
|
except RuntimeError:
|
||||||
|
opened = False
|
||||||
|
check(not opened, 'another key cannot open the backup')
|
||||||
|
|
||||||
|
brt = lambda h, m=0: datetime(2026, 9, 29, h + 3, m, tzinfo=timezone.utc)
|
||||||
|
check(db_backup.next_run(brt(2), 3) == brt(3), 'before 03:00 in Brasília the run is that day')
|
||||||
|
check(db_backup.next_run(brt(3), 3) == datetime(2026, 9, 30, 6, tzinfo=timezone.utc),
|
||||||
|
'at or after 03:00 the run is the next day')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
@@ -62,8 +62,13 @@ try{
|
|||||||
await site.call('Page.setBypassCSP',{enabled:false});
|
await site.call('Page.setBypassCSP',{enabled:false});
|
||||||
await site.call('Page.reload');
|
await site.call('Page.reload');
|
||||||
await waitFor(()=>site.eval('typeof window.dtfCheckout === "function"'),'reload after security probe');
|
await waitFor(()=>site.eval('typeof window.dtfCheckout === "function"'),'reload after security probe');
|
||||||
|
// The check above can still pass on the page being replaced; wait for the new one's cards.
|
||||||
|
await waitFor(()=>site.eval(`!!document.querySelector('[data-modo="file"]') && document.readyState==='complete'`),'product cards after reload');
|
||||||
await site.click('[data-modo="file"]');
|
await site.click('[data-modo="file"]');
|
||||||
await site.click('[data-cam="tabela"]');
|
// Each product has its own page; the home's parts are not on it.
|
||||||
|
assert.deepEqual(await site.eval(`({path:location.pathname,page:document.documentElement.dataset.rota,
|
||||||
|
cards:getComputedStyle($('cards')).display,cart:getComputedStyle($('carr')).display})`),
|
||||||
|
{path:'/arquivo-por-metro',page:'produto',cards:'none',cart:'none'});
|
||||||
const root=await site.call('DOM.getDocument');
|
const root=await site.call('DOM.getDocument');
|
||||||
const input=await site.call('DOM.querySelector',{nodeId:root.root.nodeId,selector:'#inp'});
|
const input=await site.call('DOM.querySelector',{nodeId:root.root.nodeId,selector:'#inp'});
|
||||||
await site.call('DOM.setFileInputFiles',{nodeId:input.nodeId,files:[resolve('tests/fixtures/local-test.cdr')]});
|
await site.call('DOM.setFileInputFiles',{nodeId:input.nodeId,files:[resolve('tests/fixtures/local-test.cdr')]});
|
||||||
@@ -74,15 +79,48 @@ try{
|
|||||||
await site.fill('#fCnpj','11222333000181');
|
await site.fill('#fCnpj','11222333000181');
|
||||||
await site.fill('#fZap','11999999999');
|
await site.fill('#fZap','11999999999');
|
||||||
await site.fill('#fMail','local-browser@example.test');
|
await site.fill('#fMail','local-browser@example.test');
|
||||||
await pause(800);
|
// Reload only once the cart is in the browser's storage: the save waits for
|
||||||
|
// typing to stop and writes the files, so a fixed pause raced it.
|
||||||
|
await waitFor(()=>site.eval(`new Promise(done=>{const open=indexedDB.open('dtf-local-cart',1);
|
||||||
|
open.onerror=()=>done(false);
|
||||||
|
open.onsuccess=()=>{const all=open.result.transaction('cart').objectStore('cart').getAll();
|
||||||
|
all.onsuccess=()=>done(all.result.some(v=>v.items?.length===1));all.onerror=()=>done(false);};})`),'cart saved in the browser');
|
||||||
await site.call('Page.reload');
|
await site.call('Page.reload');
|
||||||
await waitFor(()=>site.eval('typeof pedido!=="undefined" && pedido.length===1'),'persistent cart recovery');
|
await waitFor(()=>site.eval('typeof pedido!=="undefined" && pedido.length===1'),'persistent cart recovery');
|
||||||
|
// Reloading a product page reopens that product; the cart is its own page.
|
||||||
|
assert.equal(await site.eval('modo'),'file');
|
||||||
|
await site.click('#cartLink');
|
||||||
|
await waitFor(()=>site.eval(`location.pathname==='/carrinho' && getComputedStyle($('carr')).display!=='none' && getComputedStyle($('foco')).display==='none'`),'cart page');
|
||||||
|
// Removing an item is one click and can be undone.
|
||||||
|
await site.click('[data-rmi="0"]');
|
||||||
|
// The empty state is painted on the next animation frame (site-steps.js).
|
||||||
|
await waitFor(()=>site.eval(`!$('carrVazio').hidden||document.documentElement.hasAttribute('data-sem-itens')`),'empty cart state');
|
||||||
|
assert.deepEqual(await site.eval(`({items:pedido.length,empty:!$('carrVazio').hidden||document.documentElement.hasAttribute('data-sem-itens'),undo:!$('desfazer').hidden})`),{items:0,empty:true,undo:true});
|
||||||
|
await site.click('#desfazerBtn');
|
||||||
|
assert.equal(await site.eval('pedido.length===1 && pedido[0].total===21.89 && $("desfazer").hidden'),true);
|
||||||
|
// The items come back after a reload; the customer's details do not.
|
||||||
|
assert.deepEqual(await site.eval(`['fCnpj','fZap','fMail'].map(id=>$(id).value)`),['','','']);
|
||||||
|
assert.equal(await site.eval('document.getElementById("bPagar").disabled'),true);
|
||||||
|
await site.fill('#fCnpj','11222333000181');
|
||||||
|
await site.fill('#fZap','11999999999');
|
||||||
|
await site.fill('#fMail','local-browser@example.test');
|
||||||
assert.equal(await site.eval('pedido[0].localFiles[0].name'),'local-test.cdr');
|
assert.equal(await site.eval('pedido[0].localFiles[0].name'),'local-test.cdr');
|
||||||
|
// An unchecked item stays in the cart but is not paid for now.
|
||||||
|
await site.click('[data-sel="0"]');
|
||||||
|
assert.deepEqual(await site.eval(`({marcado:pedido[0].marcado,pagar:itensAPagar().length,zero:$('carrTot').textContent.includes('0,00'),row:document.querySelector('.item').classList.contains('fora')})`),
|
||||||
|
{marcado:false,pagar:0,zero:true,row:true});
|
||||||
|
await site.click('[data-sel="0"]');
|
||||||
|
assert.equal(await site.eval('pedido[0].marcado===true && itensAPagar().length===1'),true);
|
||||||
assert.equal(await site.eval('pedido[0].total'),21.89);
|
assert.equal(await site.eval('pedido[0].total'),21.89);
|
||||||
assert.equal(await site.eval('document.getElementById("bPagar").disabled'),false);
|
assert.equal(await site.eval('document.getElementById("bPagar").disabled'),false);
|
||||||
await site.click('#bPagar');
|
await site.click('#bPagar');
|
||||||
try{
|
try{
|
||||||
await waitFor(async()=> (await site.text()).includes('Arquivos enviados.'),'browser upload and quote',45000);
|
// A cart the Site can price is approved at once: the customer pays now.
|
||||||
|
await waitFor(async()=> (await site.text()).includes('Total validado no servidor:'),'browser upload and automatic approval',45000);
|
||||||
|
// Payment is its own page: the cart form is gone and the order summary is shown.
|
||||||
|
assert.equal(await site.eval('location.pathname'),'/pagamento');
|
||||||
|
assert.equal(await site.eval('getComputedStyle(document.getElementById("carr")).display'),'none');
|
||||||
|
assert.equal(await site.eval('!document.getElementById("pagResumo").hidden && document.getElementById("pagResumo").textContent.includes("Total")'),true);
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error('Checkout status:',await site.eval('document.getElementById("checkoutStatus")?.textContent'));
|
console.error('Checkout status:',await site.eval('document.getElementById("checkoutStatus")?.textContent'));
|
||||||
throw error;
|
throw error;
|
||||||
@@ -92,11 +130,16 @@ try{
|
|||||||
await kanban.fill('#email',process.env.OPERATOR_EMAIL||'operator@example.test');
|
await kanban.fill('#email',process.env.OPERATOR_EMAIL||'operator@example.test');
|
||||||
await kanban.fill('#password',process.env.OPERATOR_PASSWORD||'local-operator-only');
|
await kanban.fill('#password',process.env.OPERATOR_PASSWORD||'local-operator-only');
|
||||||
await kanban.eval('document.getElementById("login").requestSubmit()');
|
await kanban.eval('document.getElementById("login").requestSubmit()');
|
||||||
await waitFor(async()=> (await kanban.text()).includes(qid.slice(0,8)),'quote on Kanban');
|
// Quotes live in their own tab; an automatic approval is listed as such.
|
||||||
|
await waitFor(()=>kanban.eval('!document.getElementById("app").hidden'),'Kanban sign-in');
|
||||||
assert.equal(await kanban.eval('sessionStorage.getItem("dtf-operator")'),null);
|
assert.equal(await kanban.eval('sessionStorage.getItem("dtf-operator")'),null);
|
||||||
assert.equal(await kanban.eval('document.getElementById("password").value'),'');
|
assert.equal(await kanban.eval('document.getElementById("password").value'),'');
|
||||||
await kanban.eval(`(()=>{const card=[...document.querySelectorAll('.review')].find(x=>x.textContent.includes(${JSON.stringify(qid.slice(0,8))}));card.querySelector('[type=checkbox]').click();card.querySelector('form').requestSubmit();})()`);
|
await kanban.click('[data-tab="quotes"]');
|
||||||
await waitFor(async()=> (await kanban.text()).includes('Aprovada:'),'quote approval');
|
await waitFor(()=>kanban.eval('document.querySelectorAll("#quote-filters button").length===2'),'quote filters');
|
||||||
|
await kanban.eval('document.querySelectorAll("#quote-filters button")[1].click()');
|
||||||
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-quote-pick="${qid}"]')`),'approved quote listed on Kanban');
|
||||||
|
// Approved by the Site itself; the row shows the grade instead of saying so.
|
||||||
|
assert.equal(await kanban.eval(`quoteRows.find(q=>q.id==='${qid}').auto_approved && !!document.querySelector('[data-quote-pick="${qid}"] .grade')`),true);
|
||||||
assert.equal(await site.eval('pedido[0].production.sources[0].copies'),1);
|
assert.equal(await site.eval('pedido[0].production.sources[0].copies'),1);
|
||||||
await site.eval('pedido[0].production.sources[0].copies=2;pintaPedido()');
|
await site.eval('pedido[0].production.sources[0].copies=2;pintaPedido()');
|
||||||
await waitFor(async()=> (await site.text()).includes('O carrinho mudou'),'same-price production edit invalidates quote');
|
await waitFor(async()=> (await site.text()).includes('O carrinho mudou'),'same-price production edit invalidates quote');
|
||||||
@@ -108,27 +151,48 @@ try{
|
|||||||
await site.eval('window.dtfCheckout()');
|
await site.eval('window.dtfCheckout()');
|
||||||
await waitFor(async()=> (await site.text()).includes('Total validado no servidor:'),'approved quote displayed');
|
await waitFor(async()=> (await site.text()).includes('Total validado no servidor:'),'approved quote displayed');
|
||||||
await site.eval('[...document.querySelectorAll("button")].find(x=>x.textContent==="Criar pedido de teste").click()');
|
await site.eval('[...document.querySelectorAll("button")].find(x=>x.textContent==="Criar pedido de teste").click()');
|
||||||
await waitFor(async()=> (await site.text()).includes('Pedido #'),'test payment');
|
// The customer is shown a confirmation, never the order's sequential number.
|
||||||
|
await waitFor(async()=> (await site.text()).includes('Pagamento confirmado'),'test payment');
|
||||||
|
assert.equal((await site.text()).includes('Pedido #'),false);
|
||||||
await kanban.click('#refresh');
|
await kanban.click('#refresh');
|
||||||
await waitFor(()=>kanban.eval(`board.orders.some(o=>o.quote_id===${JSON.stringify(qid)})`),'paid card');
|
await waitFor(()=>kanban.eval(`board.orders.some(o=>o.quote_id===${JSON.stringify(qid)})`),'paid card');
|
||||||
const oid=await kanban.eval(`board.orders.find(o=>o.quote_id===${JSON.stringify(qid)}).id`);
|
const oid=await kanban.eval(`board.orders.find(o=>o.quote_id===${JSON.stringify(qid)}).id`);
|
||||||
assert.deepEqual(await kanban.eval(`(()=>{const spec=board.orders.find(o=>o.id===${JSON.stringify(oid)}).snapshot.items[0].production;const source=spec.sources[0];return {kind:source.kind,copies:source.copies,length:Number(source.length_cm),height:Number(spec.height_cm),placed:spec.placements.length}})()`),
|
assert.deepEqual(await kanban.eval(`(()=>{const spec=board.orders.find(o=>o.id===${JSON.stringify(oid)}).snapshot.items[0].production;const source=spec.sources[0];return {kind:source.kind,copies:source.copies,length:Number(source.length_cm),height:Number(spec.height_cm),placed:spec.placements.length}})()`),
|
||||||
{kind:'sheet',copies:1,length:101,height:101,placed:1});
|
{kind:'sheet',copies:1,length:101,height:101,placed:1});
|
||||||
// Click real transition buttons, including rerender after each move.
|
// Open the order's panel from its card, then click the real transition
|
||||||
|
// buttons there, including the rerender after each move.
|
||||||
|
await kanban.click('[data-tab="board"]');
|
||||||
|
await kanban.click(`[data-card="${oid}"]`);
|
||||||
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-move]')`),'order panel');
|
||||||
for(const [title,state] of [['Arte tratada','tra'],['Fila de impressão','fil'],['Imprimindo','imp'],['Finalizado','fin']]){
|
for(const [title,state] of [['Arte tratada','tra'],['Fila de impressão','fil'],['Imprimindo','imp'],['Finalizado','fin']]){
|
||||||
if(state==='fil'){
|
if(state==='fil'){
|
||||||
await kanban.eval(`(()=>{const card=document.querySelector('[data-order="${oid}"]');[...card.querySelectorAll('button')].find(x=>x.textContent==='Arquivos de produção').click();})()`);
|
// Without an approved final, the next step at the top is approving it,
|
||||||
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-final-item]')`),'final upload controls');
|
// not a move the server would refuse. The CDR has no ready file, so
|
||||||
|
// the button opens the upload form there.
|
||||||
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-approve]')`),'approve final button');
|
||||||
|
assert.equal(await kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-move="fil"]')`),false);
|
||||||
|
await kanban.click(`[data-order="${oid}"] [data-approve]`);
|
||||||
|
await waitFor(()=>kanban.eval(`(()=>{const input=document.querySelector('[data-order="${oid}"] [data-final-item]');return !!input&&!input.closest('form').hidden;})()`),'final upload controls');
|
||||||
const doc=await kanban.call('DOM.getDocument');
|
const doc=await kanban.call('DOM.getDocument');
|
||||||
const input=await kanban.call('DOM.querySelector',{nodeId:doc.root.nodeId,selector:`[data-order="${oid}"] [data-final-item]`});
|
const input=await kanban.call('DOM.querySelector',{nodeId:doc.root.nodeId,selector:`[data-order="${oid}"] [data-final-item]`});
|
||||||
await kanban.call('DOM.setFileInputFiles',{nodeId:input.nodeId,files:[resolve('tests/fixtures/local-test.cdr')]});
|
await kanban.call('DOM.setFileInputFiles',{nodeId:input.nodeId,files:[resolve('tests/fixtures/local-test.cdr')]});
|
||||||
await kanban.fill(`[data-order="${oid}"] input[placeholder="Nota da revisão"]`,'Browser test final file');
|
await kanban.fill(`[data-order="${oid}"] input[placeholder="Nota da revisão"]`,'Browser test final file');
|
||||||
await kanban.eval(`(()=>{const form=document.querySelector('[data-order="${oid}"] form');form.querySelector('[type=checkbox]').click();form.requestSubmit();})()`);
|
await kanban.eval(`(()=>{const check=document.querySelector('[data-order="${oid}"] [data-confirm]');check.click();check.closest('form').requestSubmit();})()`);
|
||||||
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').version===2`),'final file approval');
|
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').version===2`),'final file approval');
|
||||||
}
|
}
|
||||||
await kanban.eval(`(()=>{const card=document.querySelector('[data-order="${oid}"]');[...card.querySelectorAll('button')].find(x=>x.textContent===${JSON.stringify('→ '+title)}).click();})()`);
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-move="${state}"]')`),'move to '+title);
|
||||||
|
await kanban.click(`[data-order="${oid}"] [data-move="${state}"]`);
|
||||||
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').state==='${state}'`),'transition '+state);
|
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').state==='${state}'`),'transition '+state);
|
||||||
}
|
}
|
||||||
|
// Undo a mistaken move from the panel: one stage back with an internal reason.
|
||||||
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-back="imp"]')`),'back button');
|
||||||
|
await kanban.click(`[data-order="${oid}"] [data-back="imp"]`);
|
||||||
|
await kanban.fill(`[data-order="${oid}"] form.reason input`,'Movido por engano (teste)');
|
||||||
|
await kanban.eval(`document.querySelector('[data-order="${oid}"] form.reason input').closest('form').requestSubmit()`);
|
||||||
|
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').state==='imp'`),'undo to Imprimindo');
|
||||||
|
await waitFor(()=>kanban.eval(`!!document.querySelector('[data-order="${oid}"] [data-move="fin"]')`),'move to Finalizado again');
|
||||||
|
await kanban.click(`[data-order="${oid}"] [data-move="fin"]`);
|
||||||
|
await waitFor(()=>kanban.eval(`board.orders.find(o=>o.id==='${oid}').state==='fin'`),'finished again');
|
||||||
// Reload proves the board is persisted on the backend.
|
// Reload proves the board is persisted on the backend.
|
||||||
await kanban.call('Page.reload');
|
await kanban.call('Page.reload');
|
||||||
await waitFor(()=>kanban.eval(`typeof board!=='undefined' && !!board && board.orders.some(o=>o.id==='${oid}'&&o.state==='fin')`),'persisted board');
|
await waitFor(()=>kanban.eval(`typeof board!=='undefined' && !!board && board.orders.some(o=>o.id==='${oid}'&&o.state==='fin')`),'persisted board');
|
||||||
@@ -137,14 +201,20 @@ try{
|
|||||||
await waitFor(()=>site.eval('window.scrollY===0'),'screenshot scroll position');
|
await waitFor(()=>site.eval('window.scrollY===0'),'screenshot scroll position');
|
||||||
await site.screenshot('output/local/site.png');
|
await site.screenshot('output/local/site.png');
|
||||||
await kanban.screenshot('output/local/kanban.png');
|
await kanban.screenshot('output/local/kanban.png');
|
||||||
const portal=await page(siteOrigin+'/portal.html?order='+oid);
|
// A guest sees the order paid in this browser; creating an account keeps it.
|
||||||
|
const portal=await page(siteOrigin+'/conta/pedidos?order='+oid);
|
||||||
await waitFor(async()=> (await portal.text()).includes('Finalizado'),'customer order tracking');
|
await waitFor(async()=> (await portal.text()).includes('Finalizado'),'customer order tracking');
|
||||||
|
await waitFor(()=>portal.eval('document.querySelector("#order-'+oid+' .detalhe")?.hidden===false'),'order from the link opened');
|
||||||
|
await portal.click('#navEntrar');
|
||||||
|
await waitFor(()=>portal.eval('location.pathname==="/conta/entrar" && !document.getElementById("auth").hidden'),'sign-in page');
|
||||||
await portal.fill('#cnpj','11222333000181');await portal.fill('#phone','11999999999');
|
await portal.fill('#cnpj','11222333000181');await portal.fill('#phone','11999999999');
|
||||||
await portal.fill('#register-email','browser-'+Date.now()+'@example.test');
|
await portal.fill('#register-email','browser-'+Date.now()+'@example.test');
|
||||||
await portal.fill('#register-password','local-browser-password-123');
|
await portal.fill('#register-password','local-browser-password-123');
|
||||||
await portal.eval('document.getElementById("register").requestSubmit()');
|
await portal.eval('document.getElementById("register").requestSubmit()');
|
||||||
await waitFor(()=>portal.eval('document.getElementById("auth").hidden'),'customer registration');
|
await waitFor(()=>portal.eval('document.getElementById("auth").hidden && location.pathname==="/conta"'),'customer registration');
|
||||||
assert.ok((await portal.text()).includes('Finalizado'));
|
await waitFor(async()=> (await portal.text()).includes('Finalizado'),'overview shows the last order');
|
||||||
|
await portal.click('[data-view="dados"]');
|
||||||
|
await waitFor(()=>portal.eval('location.pathname==="/conta/dados" && document.getElementById("dCnpj").value==="11.222.333/0001-81"'),'account details');
|
||||||
await portal.screenshot('output/local/portal.png');
|
await portal.screenshot('output/local/portal.png');
|
||||||
// A logout must clear draft file blobs and metadata, including other open Site tabs.
|
// A logout must clear draft file blobs and metadata, including other open Site tabs.
|
||||||
await portal.eval(`(async()=>{const db=await new Promise((resolve,reject)=>{const r=indexedDB.open('dtf-local-cart',1);r.onsuccess=()=>resolve(r.result);r.onerror=reject;});await new Promise((resolve,reject)=>{const tx=db.transaction('cart','readwrite');tx.objectStore('cart').put({items:[new File(['private'],'private.cdr')],expires:Date.now()+86400000},'security-fixture');tx.oncomplete=resolve;tx.onerror=reject;});db.close();})()`);
|
await portal.eval(`(async()=>{const db=await new Promise((resolve,reject)=>{const r=indexedDB.open('dtf-local-cart',1);r.onsuccess=()=>resolve(r.result);r.onerror=reject;});await new Promise((resolve,reject)=>{const tx=db.transaction('cart','readwrite');tx.objectStore('cart').put({items:[new File(['private'],'private.cdr')],expires:Date.now()+86400000},'security-fixture');tx.oncomplete=resolve;tx.onerror=reject;});db.close();})()`);
|
||||||
@@ -155,7 +225,7 @@ try{
|
|||||||
assert.equal(stored,0);
|
assert.equal(stored,0);
|
||||||
assert.deepEqual(portal.errors,[]);
|
assert.deepEqual(portal.errors,[]);
|
||||||
assert.deepEqual(site.errors,[]);assert.deepEqual(kanban.errors,[]);
|
assert.deepEqual(site.errors,[]);assert.deepEqual(kanban.errors,[]);
|
||||||
console.log('PASS: browser Site upload → operator quote → local paid order → all main Kanban states → reload persistence. Order '+oid);
|
console.log('PASS: browser Site upload → automatic approval → local paid order → all main Kanban states → reload persistence. Order '+oid);
|
||||||
console.log('Screenshots: output/local/site.png and output/local/kanban.png');
|
console.log('Screenshots: output/local/site.png and output/local/kanban.png');
|
||||||
console.log('PASS: filename XSS escaping with CSP bypassed, no stored operator password, logout clears browser file blobs.');
|
console.log('PASS: filename XSS escaping with CSP bypassed, no stored operator password, logout clears browser file blobs.');
|
||||||
}catch(error){console.error(error);if(stderr)console.error(stderr.slice(-1500));process.exitCode=1;}
|
}catch(error){console.error(error);if(stderr)console.error(stderr.slice(-1500));process.exitCode=1;}
|
||||||
|
|||||||
159
tests/kanban_test.py
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
"""Kanban artwork pictures, quote filters and the search over orders and quotes.
|
||||||
|
|
||||||
|
Run against the local stack: python3 -m tests.kanban_test
|
||||||
|
"""
|
||||||
|
import base64
|
||||||
|
import secrets
|
||||||
|
from urllib.error import HTTPError
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
from urllib.request import Request
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from app.core import db
|
||||||
|
from tests.smoke_test import BASE, Client, with_host
|
||||||
|
|
||||||
|
# A 1 x 1 PNG: the endpoint reads the type from the bytes.
|
||||||
|
PNG = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==')
|
||||||
|
|
||||||
|
|
||||||
|
def raw(client, method, path, data=None, content_type=None):
|
||||||
|
"""A request whose body is not JSON; returns (status, headers, body)."""
|
||||||
|
headers = with_host({'Content-Type': content_type} if content_type else {})
|
||||||
|
request = Request(BASE + '/api' + path, data=data, headers=headers, method=method)
|
||||||
|
try:
|
||||||
|
with client.opener.open(request, timeout=30) as response:
|
||||||
|
return response.status, response.headers, response.read()
|
||||||
|
except HTTPError as exc:
|
||||||
|
return exc.code, exc.headers, exc.read()
|
||||||
|
|
||||||
|
|
||||||
|
def session_upload(client, name):
|
||||||
|
"""A completed, clean upload owned by this client's session. The bytes are
|
||||||
|
not the subject here, so none are stored."""
|
||||||
|
sid = next(c.value for c in client.jar if c.name == 'dtf_session')
|
||||||
|
uid = uuid4()
|
||||||
|
with db.connect() as c:
|
||||||
|
owner = c.execute('SELECT owner FROM dtf_local.sessions WHERE id=%s', (sid,)).fetchone()['owner']
|
||||||
|
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,expires_at,scan_state)
|
||||||
|
VALUES(%s,%s,%s,1,%s,'none',true,now()+interval '1 day','clean')''', (uid, owner, name, f'originals/{uid}'))
|
||||||
|
return str(uid)
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
tag = secrets.token_hex(4)
|
||||||
|
mail = f'kanban-{tag}@example.test'
|
||||||
|
cnpj = '11.222.333/0001-81'
|
||||||
|
customer = {'mail': mail, 'cnpj': cnpj, 'zap': '(16) 98765-' + str(4000 + int(tag[:3], 16) % 1000)}
|
||||||
|
client = Client()
|
||||||
|
client.call('/session')
|
||||||
|
uid = session_upload(client, f'kanban-{tag}.cdr')
|
||||||
|
|
||||||
|
# The customer's browser sends the picture of its own upload; nothing else.
|
||||||
|
assert raw(client, 'PUT', f'/uploads/{uid}/thumbnail', PNG, 'image/png')[0] == 200
|
||||||
|
assert raw(client, 'PUT', f'/uploads/{uid}/thumbnail', b'<svg onload=alert(1)>', 'image/png')[0] == 415
|
||||||
|
assert raw(client, 'PUT', f'/uploads/{uid}/thumbnail', PNG + b'\0' * 300_001, 'image/png')[0] == 413
|
||||||
|
stranger = Client()
|
||||||
|
stranger.call('/session')
|
||||||
|
assert raw(stranger, 'PUT', f'/uploads/{uid}/thumbnail', PNG, 'image/png')[0] == 404
|
||||||
|
|
||||||
|
# Only an operator reads it, as an image the browser may keep for a while.
|
||||||
|
assert raw(client, 'GET', f'/operator/uploads/{uid}/thumbnail')[0] == 401
|
||||||
|
client.call('/operator/board', operator=True)
|
||||||
|
status, headers, body = raw(client.operator_client, 'GET', f'/operator/uploads/{uid}/thumbnail')
|
||||||
|
assert status == 200 and body == PNG, status
|
||||||
|
assert headers['Content-Type'] == 'image/png' and 'private' in headers['Cache-Control']
|
||||||
|
assert headers['X-Content-Type-Options'] == 'nosniff'
|
||||||
|
|
||||||
|
owner = uuid4()
|
||||||
|
pending, approved, paid, order_id = uuid4(), uuid4(), uuid4(), uuid4()
|
||||||
|
item = lambda mode, warning=False: {'mode': mode, 'uploads': [uid], 'metres': '1.00', 'billed_metres': '1.00',
|
||||||
|
'grade': 100, 'quality_status': 'warning' if warning else 'ok'}
|
||||||
|
draft = lambda *items: {'customer': customer, 'items': list(items), 'freight': {'service': 'pickup'}}
|
||||||
|
try:
|
||||||
|
with db.connect() as c:
|
||||||
|
for qid, content, accepted in [(pending, draft(item('avulsa', warning=True)), None),
|
||||||
|
(approved, draft(item('uvfile')), {'items': [], 'total_cents': 6990}),
|
||||||
|
(paid, draft(item('file')), {'items': [], 'total_cents': 1490})]:
|
||||||
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved)
|
||||||
|
VALUES(%s,%s,%s,%s,%s,%s)''', (qid, owner, uuid4(), 'kanban-fixture', Jsonb(content),
|
||||||
|
Jsonb(accepted) if accepted else None))
|
||||||
|
number = c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment)
|
||||||
|
VALUES(%s,%s,%s,%s,%s) RETURNING number''',
|
||||||
|
(order_id, paid, owner, Jsonb({**draft(item('file')), 'total_cents': 1490}),
|
||||||
|
Jsonb({'provider': 'teste', 'id': f'kanban-{tag}'}))).fetchone()['number']
|
||||||
|
|
||||||
|
def quotes(**params):
|
||||||
|
page = client.call('/operator/quotes?' + urlencode({'q': mail, **params}), operator=True)
|
||||||
|
return page, {q['id'] for q in page['quotes']}
|
||||||
|
|
||||||
|
page, ids = quotes(kind='pending')
|
||||||
|
assert ids == {str(pending)} and page['total'] == 1, ids
|
||||||
|
assert str(uid) in page['thumbnails']
|
||||||
|
assert quotes(kind='pending', layout='avulsa', flag='ressalva')[1] == {str(pending)}
|
||||||
|
assert quotes(kind='pending', product='uv')[1] == set()
|
||||||
|
assert quotes(kind='approved', product='uv', layout='folha')[1] == {str(approved)}
|
||||||
|
assert quotes(kind='approved', layout='avulsa')[1] == set()
|
||||||
|
# CNPJ and WhatsApp match by digits, whatever the punctuation typed.
|
||||||
|
assert client.call('/operator/quotes?' + urlencode({'kind': 'approved', 'q': '11222333'}),
|
||||||
|
operator=True)['total'] >= 1
|
||||||
|
# LIKE's own wildcards are literal: "%%" is not "everything".
|
||||||
|
assert str(pending) not in {q['id'] for q in client.call(
|
||||||
|
'/operator/quotes?' + urlencode({'kind': 'pending', 'q': '%%'}), operator=True)['quotes']}
|
||||||
|
|
||||||
|
# One search: the paid order (by customer and by number) and the two unpaid quotes.
|
||||||
|
found = client.call('/operator/search?' + urlencode({'q': mail}), operator=True)
|
||||||
|
assert {o['id'] for o in found['orders']} == {str(order_id)}
|
||||||
|
assert {q['id'] for q in found['quotes']} == {str(pending), str(approved)}
|
||||||
|
assert str(uid) in found['thumbnails']
|
||||||
|
by_number = client.call('/operator/search?' + urlencode({'q': f'#{number}'}), operator=True)
|
||||||
|
assert str(order_id) in {o['id'] for o in by_number['orders']}
|
||||||
|
assert client.call('/operator/search?q=a', operator=True, expected=422)
|
||||||
|
|
||||||
|
board = client.call('/operator/board', operator=True)
|
||||||
|
assert str(uid) in board['thumbnails']
|
||||||
|
|
||||||
|
# Numbers count real paid orders only: this test order counts once it is not one.
|
||||||
|
before = client.call('/operator/stats?days=7', operator=True)
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.orders SET payment=%s WHERE id=%s",
|
||||||
|
(Jsonb({'provider': 'mercadopago', 'id': f'kanban-{tag}'}), order_id))
|
||||||
|
after = client.call('/operator/stats?days=7', operator=True)
|
||||||
|
assert len(after['daily']) == 7
|
||||||
|
assert (after['orders'], after['cents']) == (before['orders'] + 1, before['cents'] + 1490), (before, after)
|
||||||
|
assert after['daily'][-1]['orders'] == before['daily'][-1]['orders'] + 1
|
||||||
|
assert after['waiting']['rec']['orders'] == before.get('waiting', {}).get('rec', {}).get('orders', 0) + 1
|
||||||
|
client.call('/operator/stats?days=3', operator=True, expected=422)
|
||||||
|
|
||||||
|
# The company's name from the registry: on the board, and found by the search.
|
||||||
|
from app import cnpj as registry
|
||||||
|
# Even after a failed try (the worker may have had no internet), a lookup stores the names.
|
||||||
|
registry.company('11222333000181', db.connect, transport=httpx.MockTransport(lambda request: httpx.Response(
|
||||||
|
200, json={'razao_social': f'KANBAN {tag} COMERCIO LTDA', 'nome_fantasia': f'Loja {tag}'})))
|
||||||
|
board = client.call('/operator/board', operator=True)
|
||||||
|
assert board['names']['11222333000181'] == {'legal': f'KANBAN {tag} COMERCIO LTDA', 'trade': f'Loja {tag}'}
|
||||||
|
found = client.call('/operator/search?' + urlencode({'q': f'loja {tag}'}), operator=True)
|
||||||
|
assert str(order_id) in {o['id'] for o in found['orders']}, found
|
||||||
|
# A failed lookup is kept and not repeated within the day.
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("DELETE FROM dtf_local.cnpj_names WHERE cnpj='11222333000181'")
|
||||||
|
registry.resolve(db.connect, limit=200, transport=httpx.MockTransport(lambda request: httpx.Response(503)))
|
||||||
|
with db.connect() as c:
|
||||||
|
row = c.execute("SELECT fetched_at,failed_at FROM dtf_local.cnpj_names WHERE cnpj='11222333000181'").fetchone()
|
||||||
|
assert row['fetched_at'] is None and row['failed_at'] is not None
|
||||||
|
assert '11222333000181' not in registry.pending(c, 500)
|
||||||
|
Client().call('/operator/stats', expected=401)
|
||||||
|
print('PASS: artwork pictures (owner only, images only), quote filters, the order/quote search and the numbers')
|
||||||
|
finally:
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order_id,))
|
||||||
|
c.execute('DELETE FROM dtf_local.quotes WHERE id=ANY(%s)', ([pending, approved, paid],))
|
||||||
|
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (uid,))
|
||||||
|
c.execute('DELETE FROM dtf_local.uploads WHERE id=%s', (uid,))
|
||||||
|
c.execute("DELETE FROM dtf_local.cnpj_names WHERE cnpj='11222333000181'")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
53
tests/launch_reset_test.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
"""The launch cleanup removes test orders and refuses when real money was paid.
|
||||||
|
|
||||||
|
It empties the order tables, so it runs last. Run inside the API container:
|
||||||
|
python -m tests.launch_reset_test
|
||||||
|
"""
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from app.core import db
|
||||||
|
from ops import launch_reset
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
quote, order, owner = uuid4(), uuid4(), uuid4()
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
|
||||||
|
VALUES(%s,%s,%s,'reset-fixture',%s,%s,now())''',
|
||||||
|
(quote, owner, uuid4(), Jsonb({'customer': {}, 'items': []}), Jsonb({'items': [], 'total_cents': 1490})))
|
||||||
|
c.execute('INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s)',
|
||||||
|
(order, quote, owner, Jsonb({'items': [], 'total_cents': 1490}), Jsonb({'provider': 'teste'})))
|
||||||
|
live = c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,
|
||||||
|
status,amount_cents,response) VALUES(%s,%s,'mercadopago',%s,'card','approved',1490,%s) RETURNING id''',
|
||||||
|
(uuid4(), quote, str(uuid4()), Jsonb({'live_mode': True}))).fetchone()['id']
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
# Real money paid: nothing happens, even when confirmed.
|
||||||
|
assert launch_reset.main(['--confirmar', 'APAGAR'], out=lines.append) == 1
|
||||||
|
assert 'dinheiro real' in lines[-1]
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.payment_intents SET response='{\"live_mode\": false}' WHERE id=%s", (live,))
|
||||||
|
|
||||||
|
# Without the word, it only counts.
|
||||||
|
lines.clear()
|
||||||
|
assert launch_reset.main([], out=lines.append) == 0
|
||||||
|
assert 'Nada foi apagado' in lines[-1]
|
||||||
|
with db.connect() as c:
|
||||||
|
assert c.execute('SELECT 1 FROM dtf_local.orders WHERE id=%s', (order,)).fetchone()
|
||||||
|
|
||||||
|
lines.clear()
|
||||||
|
assert launch_reset.main(['--confirmar', 'APAGAR'], out=lines.append) == 0, lines
|
||||||
|
with db.connect() as c:
|
||||||
|
for table in ('orders', 'quotes', 'payment_intents', 'movements', 'outbox'):
|
||||||
|
assert c.execute(f'SELECT count(*) AS n FROM dtf_local.{table}').fetchone()['n'] == 0, table
|
||||||
|
assert not c.execute('''SELECT 1 FROM dtf_local.uploads WHERE purged_at IS NULL
|
||||||
|
AND expires_at>now()''').fetchone()
|
||||||
|
assert c.execute('SELECT count(*) AS n FROM dtf_local.operators').fetchone()['n'] >= 1
|
||||||
|
assert 'Apagado.' in lines and any('#1' in line for line in lines), lines
|
||||||
|
print('PASS: launch cleanup refuses real payments, counts first, then removes test orders and quotes')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
100
tests/order_actions_test.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
"""Discarding an unpaid quote, deleting a test order, cancelling a paid one.
|
||||||
|
|
||||||
|
Run inside the API container: python -m tests.order_actions_test
|
||||||
|
"""
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from app import payments
|
||||||
|
from app.core import db
|
||||||
|
from tests.smoke_test import Client
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
client = Client()
|
||||||
|
owner = uuid4()
|
||||||
|
mail = f'actions-{uuid4().hex[:8]}@example.test'
|
||||||
|
approved = {'customer': {'mail': mail}, 'items': [], 'total_cents': 1490}
|
||||||
|
discard, test_quote, paid_quote, paid_order = uuid4(), uuid4(), uuid4(), uuid4()
|
||||||
|
newer_quote, newer_order = uuid4(), uuid4()
|
||||||
|
test_order = None
|
||||||
|
try:
|
||||||
|
with db.connect() as c:
|
||||||
|
for qid in (discard, test_quote, paid_quote, newer_quote):
|
||||||
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
|
||||||
|
VALUES(%s,%s,%s,'actions-fixture',%s,%s,now())''',
|
||||||
|
(qid, owner, uuid4(), Jsonb({'customer': {'mail': mail}, 'items': []}), Jsonb(approved)))
|
||||||
|
c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s)''',
|
||||||
|
(paid_order, paid_quote, owner, Jsonb(approved), Jsonb({'provider': 'mercadopago', 'id': '1'})))
|
||||||
|
listed = lambda: {q['id'] for q in client.call('/operator/quotes?' + urlencode({'kind': 'approved', 'q': mail}),
|
||||||
|
operator=True)['quotes']}
|
||||||
|
assert {str(discard), str(test_quote)} <= listed()
|
||||||
|
|
||||||
|
# Discarded: gone from the list, and no payment can make it an order.
|
||||||
|
client.call(f'/operator/quotes/{discard}/discard', {}, operator=True)
|
||||||
|
assert str(discard) not in listed()
|
||||||
|
with db.connect() as c:
|
||||||
|
try:
|
||||||
|
payments.approved_quote(c, discard)
|
||||||
|
raise AssertionError('a discarded quote was payable')
|
||||||
|
except payments.PaymentRefused as refusal:
|
||||||
|
assert str(refusal) == 'quote was discarded'
|
||||||
|
client.call(f'/operator/quotes/{paid_quote}/discard', {}, operator=True, expected=409)
|
||||||
|
|
||||||
|
# A test order is deleted, and its quote is unpaid again.
|
||||||
|
test_order = client.call(f'/operator/quotes/{test_quote}/test-order', {}, operator=True)['id']
|
||||||
|
assert str(test_quote) not in listed()
|
||||||
|
client.call(f'/operator/orders/{test_order}/delete', {}, operator=True)
|
||||||
|
with db.connect() as c:
|
||||||
|
assert not c.execute('SELECT 1 FROM dtf_local.orders WHERE id=%s', (test_order,)).fetchone()
|
||||||
|
test_order = None
|
||||||
|
assert str(test_quote) in listed()
|
||||||
|
|
||||||
|
# A paid order is cancelled with a reason, kept, and can no longer move.
|
||||||
|
# A paid order in progress is not deleted: it is cancelled first.
|
||||||
|
client.call(f'/operator/orders/{paid_order}/delete', {}, operator=True, expected=409)
|
||||||
|
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'x'}, operator=True, expected=422)
|
||||||
|
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'Cliente desistiu'}, operator=True)
|
||||||
|
board = client.call('/operator/board', operator=True)
|
||||||
|
order = next(o for o in board['orders'] if o['id'] == str(paid_order))
|
||||||
|
assert order['cancelled_at'] and order['cancel_reason'] == 'Cliente desistiu'
|
||||||
|
client.call(f'/operator/orders/{paid_order}/move', {'state': 'tra', 'version': order['version'], 'reason': ''},
|
||||||
|
operator=True, expected=409)
|
||||||
|
client.call(f'/operator/orders/{paid_order}/cancel', {'note': 'de novo'}, operator=True, expected=404)
|
||||||
|
|
||||||
|
# Dragged within its column: first paid first, unless moved; moving on resets it.
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.orders SET created_at=now()-interval '1 hour' WHERE id=%s", (paid_order,))
|
||||||
|
c.execute('''INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment,created_at)
|
||||||
|
VALUES(%s,%s,%s,%s,%s,now())''', (newer_order, newer_quote, owner, Jsonb(approved),
|
||||||
|
Jsonb({'provider': 'teste', 'id': 't'})))
|
||||||
|
client.call(f'/operator/orders/{newer_order}/queue', {'before': str(paid_order)}, operator=True)
|
||||||
|
with db.connect() as c:
|
||||||
|
keys = {str(r['id']): r['k'] for r in c.execute(
|
||||||
|
'SELECT id, COALESCE(queue_key, extract(epoch FROM created_at)) AS k FROM dtf_local.orders WHERE id=ANY(%s)',
|
||||||
|
([paid_order, newer_order],)).fetchall()}
|
||||||
|
assert keys[str(newer_order)] < keys[str(paid_order)], keys
|
||||||
|
client.call(f'/operator/orders/{newer_order}/queue', {}, operator=True, expected=422)
|
||||||
|
|
||||||
|
# Cancelled, it can be deleted; its quote is discarded, not unpaid again.
|
||||||
|
client.call(f'/operator/orders/{paid_order}/delete', {}, operator=True)
|
||||||
|
with db.connect() as c:
|
||||||
|
assert not c.execute('SELECT 1 FROM dtf_local.orders WHERE id=%s', (paid_order,)).fetchone()
|
||||||
|
assert c.execute('SELECT discarded_at FROM dtf_local.quotes WHERE id=%s', (paid_quote,)).fetchone()['discarded_at']
|
||||||
|
assert str(paid_quote) not in listed()
|
||||||
|
print('PASS: discard hides and blocks a quote; test orders delete; paid orders cancel, then delete')
|
||||||
|
finally:
|
||||||
|
with db.connect() as c:
|
||||||
|
for oid in filter(None, (test_order, paid_order, newer_order)):
|
||||||
|
for table in ('movements', 'order_files', 'print_files'):
|
||||||
|
c.execute(f'DELETE FROM dtf_local.{table} WHERE order_id=%s', (oid,))
|
||||||
|
c.execute('DELETE FROM dtf_local.outbox WHERE event_key LIKE %s', (f'{oid}:%',))
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (oid,))
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE quote_id=ANY(%s)', ([discard, test_quote, paid_quote, newer_quote],))
|
||||||
|
c.execute('DELETE FROM dtf_local.quotes WHERE id=ANY(%s)', ([discard, test_quote, paid_quote, newer_quote],))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
@@ -12,7 +12,8 @@ from urllib.error import HTTPError
|
|||||||
from urllib.request import Request, urlopen
|
from urllib.request import Request, urlopen
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from tests.smoke_test import BASE, Client, upload_bytes, item_spec, with_host
|
from app.core import db
|
||||||
|
from tests.smoke_test import BASE, Client, approved_quote, upload_bytes, item_spec, with_host
|
||||||
|
|
||||||
SECRET = os.environ.get('PAYMENT_WEBHOOK_SECRET', 'local-webhook-secret').encode()
|
SECRET = os.environ.get('PAYMENT_WEBHOOK_SECRET', 'local-webhook-secret').encode()
|
||||||
|
|
||||||
@@ -42,8 +43,7 @@ def reviewed_quote():
|
|||||||
'mail': 'payment-' + uuid4().hex[:8] + '@example.test'}
|
'mail': 'payment-' + uuid4().hex[:8] + '@example.test'}
|
||||||
quote = customer.call('/quotes', {'request_key': str(uuid4()), 'customer': profile,
|
quote = customer.call('/quotes', {'request_key': str(uuid4()), 'customer': profile,
|
||||||
'items': [item], 'freight': {'service': 'pickup'}})
|
'items': [item], 'freight': {'service': 'pickup'}})
|
||||||
approved = customer.call('/operator/quotes/' + quote['id'] + '/approve',
|
approved = approved_quote(customer, quote, [item])
|
||||||
{'items': [item]}, operator=True)
|
|
||||||
return customer, quote['id'], approved['total_cents']
|
return customer, quote['id'], approved['total_cents']
|
||||||
|
|
||||||
|
|
||||||
@@ -58,10 +58,29 @@ def run():
|
|||||||
assert not customer.call('/quotes/' + quote_id)['order'], 'unsigned delivery created an order'
|
assert not customer.call('/quotes/' + quote_id)['order'], 'unsigned delivery created an order'
|
||||||
print('PASS: unsigned and tampered deliveries are refused and create nothing')
|
print('PASS: unsigned and tampered deliveries are refused and create nothing')
|
||||||
|
|
||||||
|
# Debit cards are not taken, even sent around the payment page.
|
||||||
|
customer.call('/payments/intent', {'quote_id': quote_id, 'method': {
|
||||||
|
'type': 'card', 'token': 'tok', 'payment_method_id': 'debvisa'}}, expected=422)
|
||||||
|
|
||||||
# Starting a PIX twice returns the same one. A card in review blocks every
|
# Starting a PIX twice returns the same one. A card in review blocks every
|
||||||
# further attempt, so one quote can never be charged twice.
|
# further attempt, so one quote can never be charged twice.
|
||||||
pix = customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})
|
pix = customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})
|
||||||
|
assert pix['expires_at']
|
||||||
assert customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})['id'] == pix['id']
|
assert customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})['id'] == pix['id']
|
||||||
|
# Once the code has expired, asking again opens a new one, and only one.
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('''UPDATE dtf_local.payment_intents
|
||||||
|
SET response=jsonb_set(response,'{expires_at}',to_jsonb((now()-interval '1 minute')::text))
|
||||||
|
WHERE provider_payment_id=%s''', (pix['id'],))
|
||||||
|
renewed = customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})
|
||||||
|
assert renewed['id'] != pix['id'], 'an expired PIX was offered again'
|
||||||
|
assert customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'pix'}})['id'] == renewed['id']
|
||||||
|
with db.connect() as c:
|
||||||
|
statuses = {r['provider_payment_id']: r['status'] for r in c.execute(
|
||||||
|
"SELECT provider_payment_id,status FROM dtf_local.payment_intents WHERE quote_id=%s AND method='pix'",
|
||||||
|
(quote_id,)).fetchall()}
|
||||||
|
assert statuses == {pix['id']: 'expired', renewed['id']: 'pending'}, statuses
|
||||||
|
print('PASS: a PIX code expires after 30 minutes and is replaced by exactly one new code')
|
||||||
customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'card'}}, expected=422)
|
customer.call('/payments/intent', {'quote_id': quote_id, 'method': {'type': 'card'}}, expected=422)
|
||||||
card = {'type': 'card', 'token': 'tok-1', 'payment_method_id': 'visa', 'installments': 1}
|
card = {'type': 'card', 'token': 'tok-1', 'payment_method_id': 'visa', 'installments': 1}
|
||||||
customer.call('/payments/intent', {'quote_id': quote_id, 'method': card})
|
customer.call('/payments/intent', {'quote_id': quote_id, 'method': card})
|
||||||
@@ -109,7 +128,7 @@ def run():
|
|||||||
|
|
||||||
# The customer is told once, not once per delivery.
|
# The customer is told once, not once per delivery.
|
||||||
board = Client()
|
board = Client()
|
||||||
events = board.call('/operator/board', operator=True)['events']
|
events = board.call('/operator/events?order=' + str(order['number']), operator=True)['events']
|
||||||
paid = [e for e in events if e['payload'].get('order_id') == order['id']
|
paid = [e for e in events if e['payload'].get('order_id') == order['id']
|
||||||
and e['payload'].get('event') == 'payment_approved']
|
and e['payload'].get('event') == 'payment_approved']
|
||||||
assert len(paid) == 2, f'expected one tiny and one whatsapp event, got {len(paid)}'
|
assert len(paid) == 2, f'expected one tiny and one whatsapp event, got {len(paid)}'
|
||||||
@@ -125,6 +144,45 @@ def run():
|
|||||||
'status': 'pending', 'amount_cents': total})
|
'status': 'pending', 'amount_cents': total})
|
||||||
print('PASS: unknown references and non-approved statuses are recorded without acting')
|
print('PASS: unknown references and non-approved statuses are recorded without acting')
|
||||||
|
|
||||||
|
# An operator's test order runs the production flow and notifies no one.
|
||||||
|
tester, test_quote, _ = reviewed_quote()
|
||||||
|
order = tester.call('/operator/quotes/' + test_quote + '/test-order', {}, operator=True)
|
||||||
|
assert order['payment']['provider'] == 'teste' and order['state'] == 'rec', order
|
||||||
|
assert tester.call('/operator/quotes/' + test_quote + '/test-order', {}, operator=True)['id'] == order['id']
|
||||||
|
version = order['version']
|
||||||
|
for state in ('tra',):
|
||||||
|
moved = tester.call('/operator/orders/' + order['id'] + '/move', {'state': state, 'version': version}, operator=True)
|
||||||
|
version = moved['version']
|
||||||
|
with db.connect() as c:
|
||||||
|
queued = c.execute("SELECT count(*) AS n FROM dtf_local.outbox WHERE event_key LIKE %s", (order['id'] + ':%',)).fetchone()['n']
|
||||||
|
jobs = c.execute('SELECT count(*) AS n FROM dtf_local.print_files WHERE order_id=%s', (order['id'],)).fetchone()['n']
|
||||||
|
assert queued == 0 and jobs == 1, (queued, jobs)
|
||||||
|
print('PASS: an operator test order reaches the board and the print queue, never Tiny or WhatsApp')
|
||||||
|
|
||||||
|
# Files are uploaded before payment. An unpaid cart keeps them briefly; a
|
||||||
|
# paid order keeps them for its 30 days; a payment never starts for files
|
||||||
|
# that are gone.
|
||||||
|
def files_of(qid):
|
||||||
|
with db.connect() as c:
|
||||||
|
q = c.execute('SELECT approved,draft FROM dtf_local.quotes WHERE id=%s', (qid,)).fetchone()
|
||||||
|
return [u for item in (q['approved'] or q['draft'])['items'] for u in item['uploads']]
|
||||||
|
|
||||||
|
def days(ids, since='now()'):
|
||||||
|
with db.connect() as c:
|
||||||
|
return [float(r['d']) for r in c.execute(
|
||||||
|
f'SELECT extract(epoch FROM expires_at-{since})/86400 AS d FROM dtf_local.uploads WHERE id=ANY(%s)',
|
||||||
|
(ids,)).fetchall()]
|
||||||
|
fresh = upload_bytes(customer, b'UNPAID HOLD TEST')
|
||||||
|
assert all(1.99 < d <= 2.0 for d in days([fresh])), days([fresh])
|
||||||
|
assert all(abs(d - 30) < 0.01 for d in days(files_of(quote_id), 'created_at')), days(files_of(quote_id), 'created_at')
|
||||||
|
late, late_quote, _ = reviewed_quote()
|
||||||
|
assert all(d > 1.99 for d in days(files_of(late_quote))), days(files_of(late_quote))
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.uploads SET expires_at=now()-interval '1 second' WHERE id=ANY(%s)",
|
||||||
|
(files_of(late_quote),))
|
||||||
|
late.call('/payments/intent', {'quote_id': late_quote, 'method': {'type': 'pix'}}, expected=410)
|
||||||
|
print('PASS: unpaid files are held 2 days, paid ones 30 days, and expired files are never charged for')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
run()
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from uuid import uuid4
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from tests.payment_test import deliver
|
from tests.payment_test import deliver
|
||||||
from tests.smoke_test import Client, upload_bytes
|
from tests.smoke_test import Client, approved_quote as approval, upload_bytes
|
||||||
|
|
||||||
PT_PER_CM = 72 / 2.54
|
PT_PER_CM = 72 / 2.54
|
||||||
CUSTOMER = {'cnpj': '11222333000181', 'zap': '11999999999', 'mail': 'print-test@example.test'}
|
CUSTOMER = {'cnpj': '11222333000181', 'zap': '11999999999', 'mail': 'print-test@example.test'}
|
||||||
@@ -53,7 +53,7 @@ def loose_item(uid, copies=2):
|
|||||||
def approved_quote(client, item):
|
def approved_quote(client, item):
|
||||||
quote = client.call('/quotes', {'request_key': str(uuid4()), 'customer': CUSTOMER,
|
quote = client.call('/quotes', {'request_key': str(uuid4()), 'customer': CUSTOMER,
|
||||||
'items': [item], 'freight': {'service': 'pickup'}})
|
'items': [item], 'freight': {'service': 'pickup'}})
|
||||||
approved = client.call('/operator/quotes/' + quote['id'] + '/approve', {'items': [item]}, operator=True)
|
approved = approval(client, quote, [item])
|
||||||
return quote['id'], approved['total_cents']
|
return quote['id'], approved['total_cents']
|
||||||
|
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ def run():
|
|||||||
manual = upload_bytes(client, b'LOCAL PRINT TEST - NOT AN IMAGE', name='LOCAL-PRINT-TEST.cdr')
|
manual = upload_bytes(client, b'LOCAL PRINT TEST - NOT AN IMAGE', name='LOCAL-PRINT-TEST.cdr')
|
||||||
order = paid_order(client, loose_item(manual, copies=1))
|
order = paid_order(client, loose_item(manual, copies=1))
|
||||||
order, row = wait_print(client, order['id'], 'manual')
|
order, row = wait_print(client, order['id'], 'manual')
|
||||||
assert 'not an image' in row['detail']['reason'], row
|
assert 'não é uma imagem' in row['detail']['reason'], row
|
||||||
rows = client.call('/operator/orders/' + order['id'] + '/print-files', {}, operator=True)
|
rows = client.call('/operator/orders/' + order['id'] + '/print-files', {}, operator=True)
|
||||||
assert rows[0]['status'] == 'pending'
|
assert rows[0]['status'] == 'pending'
|
||||||
wait_print(client, order['id'], 'manual')
|
wait_print(client, order['id'], 'manual')
|
||||||
@@ -141,17 +141,43 @@ def run():
|
|||||||
outcome = deliver({'event_id': event_id, 'reference': quote_id,
|
outcome = deliver({'event_id': event_id, 'reference': quote_id,
|
||||||
'status': 'approved', 'amount_cents': total - 1})
|
'status': 'approved', 'amount_cents': total - 1})
|
||||||
assert outcome['outcome'].startswith('refused'), outcome
|
assert outcome['outcome'].startswith('refused'), outcome
|
||||||
issues = client.call('/operator/board', operator=True)['payment_issues']
|
issues = client.call('/operator/payment-events?state=open&limit=100', operator=True)['issues']
|
||||||
issue = next(i for i in issues if i['event_id'] == event_id)
|
issue = next(i for i in issues if i['event_id'] == event_id)
|
||||||
|
assert client.call('/operator/board', operator=True)['payment_issues_total'] >= 1
|
||||||
client.call('/operator/payment-events/' + issue['id'] + '/resolve', {'note': 'no'},
|
client.call('/operator/payment-events/' + issue['id'] + '/resolve', {'note': 'no'},
|
||||||
operator=True, expected=422)
|
operator=True, expected=422)
|
||||||
client.call('/operator/payment-events/' + issue['id'] + '/resolve',
|
client.call('/operator/payment-events/' + issue['id'] + '/resolve',
|
||||||
{'note': 'Local test: refunded the underpayment'}, operator=True)
|
{'note': 'Local test: refunded the underpayment'}, operator=True)
|
||||||
client.call('/operator/payment-events/' + issue['id'] + '/resolve',
|
client.call('/operator/payment-events/' + issue['id'] + '/resolve',
|
||||||
{'note': 'Local test: second resolution'}, operator=True, expected=404)
|
{'note': 'Local test: second resolution'}, operator=True, expected=404)
|
||||||
issues = client.call('/operator/board', operator=True)['payment_issues']
|
issues = client.call('/operator/payment-events?state=open&limit=100', operator=True)['issues']
|
||||||
assert not any(i['event_id'] == event_id for i in issues)
|
assert not any(i['event_id'] == event_id for i in issues)
|
||||||
print('PASS: refused paid notification is listed until an operator resolves it')
|
resolved_page = client.call('/operator/payment-events?state=resolved&limit=100', operator=True)
|
||||||
|
assert resolved_page['total'] >= 1
|
||||||
|
resolved = resolved_page['issues']
|
||||||
|
done = next(i for i in resolved if i['event_id'] == event_id)
|
||||||
|
assert done['resolution'] == 'Local test: refunded the underpayment' and done['resolved_by']
|
||||||
|
print('PASS: refused paid notification is listed until an operator resolves it, then kept as history')
|
||||||
|
|
||||||
|
# The send log pages by id and filters by destination, status, event and order.
|
||||||
|
page = client.call('/operator/events?limit=2', operator=True)
|
||||||
|
assert len(page['events']) == 2 and page['total'] > 2
|
||||||
|
second = client.call('/operator/events?limit=2&offset=2', operator=True)
|
||||||
|
assert all(e['id'] < page['events'][-1]['id'] for e in second['events'])
|
||||||
|
assert second['total'] == page['total']
|
||||||
|
tiny = client.call('/operator/events?provider=tiny&event=payment_approved&status=delivered&limit=100', operator=True)
|
||||||
|
assert tiny['events'] and all(e['provider'] == 'tiny' and e['payload']['event'] == 'payment_approved'
|
||||||
|
and e['delivered_at'] for e in tiny['events'])
|
||||||
|
client.call('/operator/events?provider=email', operator=True, expected=422)
|
||||||
|
client.call('/operator/events?limit=500', operator=True, expected=422)
|
||||||
|
quotes = client.call('/operator/quotes?kind=approved&limit=1&offset=0', operator=True)
|
||||||
|
assert 'total' in quotes and len(quotes['quotes']) <= 1
|
||||||
|
finished = client.call('/operator/orders/finished?limit=1', operator=True)
|
||||||
|
if finished['orders']:
|
||||||
|
last = finished['orders'][-1]
|
||||||
|
client.call('/operator/orders/finished?before_created_at=' + last['created_at'].replace('+', '%2B')
|
||||||
|
+ '&before_id=' + last['id'], operator=True)
|
||||||
|
print('PASS: send log, payment history and finished orders page and filter')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
71
tests/reconcile_test.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
"""An approved payment becomes the order even when its notification never arrives.
|
||||||
|
|
||||||
|
Run inside the API container: python -m tests.reconcile_test
|
||||||
|
"""
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from app import payments
|
||||||
|
from app.core import db
|
||||||
|
|
||||||
|
|
||||||
|
class Provider:
|
||||||
|
"""Answers lookups as Mercado Pago would; counts them."""
|
||||||
|
name = 'mercadopago'
|
||||||
|
|
||||||
|
def __init__(self, payment):
|
||||||
|
self.payment, self.asked = payment, 0
|
||||||
|
|
||||||
|
def lookup(self, payment_id):
|
||||||
|
self.asked += 1
|
||||||
|
assert payment_id == str(self.payment['id'])
|
||||||
|
return self.payment
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
quote, payment_id = uuid4(), str(900_000_000 + uuid4().int % 99_999_999)
|
||||||
|
approved = {'customer': {'mail': 'reconcile@example.test'}, 'items': [], 'total_cents': 1490}
|
||||||
|
order_id = None
|
||||||
|
try:
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
|
||||||
|
VALUES(%s,%s,%s,'reconcile-fixture',%s,%s,now())''',
|
||||||
|
(quote, uuid4(), uuid4(), Jsonb({'customer': approved['customer'], 'items': []}), Jsonb(approved)))
|
||||||
|
c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,status,amount_cents,response)
|
||||||
|
VALUES(%s,%s,'mercadopago',%s,'card','approved',1490,'{}')''', (uuid4(), quote, payment_id))
|
||||||
|
|
||||||
|
# Paid less than owed: no order, and nothing applied twice.
|
||||||
|
short = Provider({'id': payment_id, 'status': 'approved', 'currency_id': 'BRL',
|
||||||
|
'transaction_amount': 14.0, 'external_reference': str(quote)})
|
||||||
|
assert payments.reconcile(db.connect, short, quote_id=quote) == ['refused: paid 1400 but quote total is 1490']
|
||||||
|
assert payments.reconcile(db.connect, short, quote_id=quote) == []
|
||||||
|
|
||||||
|
# The provider is asked; its approval of the right amount is the order.
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute("DELETE FROM dtf_local.payment_events WHERE provider='mercadopago' AND event_id=%s",
|
||||||
|
(f'{payment_id}:approved',))
|
||||||
|
paid = Provider({'id': payment_id, 'status': 'approved', 'currency_id': 'BRL',
|
||||||
|
'transaction_amount': 14.9, 'external_reference': str(quote)})
|
||||||
|
outcomes = payments.reconcile(db.connect, paid, quote_id=quote)
|
||||||
|
assert len(outcomes) == 1 and outcomes[0].startswith('order '), outcomes
|
||||||
|
with db.connect() as c:
|
||||||
|
order_id = c.execute('SELECT id FROM dtf_local.orders WHERE quote_id=%s', (quote,)).fetchone()['id']
|
||||||
|
# With the order made, the quote is no longer asked about.
|
||||||
|
asked = paid.asked
|
||||||
|
assert payments.reconcile(db.connect, paid, quote_id=quote) == [] and paid.asked == asked
|
||||||
|
print('PASS: an approved payment is found by asking the provider; a short one is refused; each applies once')
|
||||||
|
finally:
|
||||||
|
with db.connect() as c:
|
||||||
|
if order_id:
|
||||||
|
c.execute("DELETE FROM dtf_local.outbox WHERE event_key LIKE %s", (f'{order_id}:%',))
|
||||||
|
c.execute('DELETE FROM dtf_local.print_files WHERE order_id=%s', (order_id,))
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order_id,))
|
||||||
|
c.execute("DELETE FROM dtf_local.payment_events WHERE provider='mercadopago' AND event_id LIKE %s",
|
||||||
|
(f'{payment_id}:%',))
|
||||||
|
c.execute('DELETE FROM dtf_local.payment_intents WHERE quote_id=%s', (quote,))
|
||||||
|
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
@@ -5,7 +5,7 @@ from botocore.exceptions import ClientError
|
|||||||
from app.core.db import connect
|
from app.core.db import connect
|
||||||
from app.adapters import LocalS3Storage
|
from app.adapters import LocalS3Storage
|
||||||
from app.core.auth import client_ip, password_hash, password_matches
|
from app.core.auth import client_ip, password_hash, password_matches
|
||||||
from app.scanning import ClamAV, require_clean
|
from app.scanning import ClamAV, format_matches, require_clean
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
class FakeRequest:
|
class FakeRequest:
|
||||||
@@ -78,7 +78,13 @@ def run():
|
|||||||
except HTTPException as error:assert error.status_code==409
|
except HTTPException as error:assert error.status_code==409
|
||||||
require_clean({'complete':True,'scan_state':'clean'})
|
require_clean({'complete':True,'scan_state':'clean'})
|
||||||
assert ClamAV().ping() and ClamAV().version().startswith('ClamAV ')
|
assert ClamAV().ping() and ClamAV().version().startswith('ClamAV ')
|
||||||
assert ClamAV().scan(None,134217729)[0]=='rejected'
|
# Above the antivirus limit only a file whose bytes match its name is released.
|
||||||
|
for name,head,ok in (('folha.png',b'\x89PNG\r\n\x1a\n\x00',True),('folha.png',b'MZ\x90\x00',False),
|
||||||
|
('folha.jpg',b'\xff\xd8\xff\xe0',True),('folha.pdf',b'%PDF-1.7',True),
|
||||||
|
('folha.pdf',b'#!/bin/sh',False),('folha.tif',b'II*\x00',True),('folha.psd',b'8BPS',True),
|
||||||
|
('folha.ai',b'%!PS-Adobe',True),('folha.cdr',b'RIFF\x10\x00\x00\x00CDRv',True),
|
||||||
|
('folha.cdr',b'RIFF\x10\x00\x00\x00WEBP',False),('folha.exe',b'MZ',False)):
|
||||||
|
assert format_matches(name,head)==ok,(name,head)
|
||||||
with patch('app.scanning.socket.create_connection',side_effect=OSError('offline')):
|
with patch('app.scanning.socket.create_connection',side_effect=OSError('offline')):
|
||||||
try:ClamAV().scan(None,1);raise AssertionError('Offline scanner returned success')
|
try:ClamAV().scan(None,1);raise AssertionError('Offline scanner returned success')
|
||||||
except OSError:pass
|
except OSError:pass
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ def run():
|
|||||||
assert script_src == "script-src 'self'", script_src
|
assert script_src == "script-src 'self'", script_src
|
||||||
assert "object-src 'none'" in policy
|
assert "object-src 'none'" in policy
|
||||||
assert 'cdnjs' not in policy, 'pdf.js is vendored; no CDN belongs in the policy'
|
assert 'cdnjs' not in policy, 'pdf.js is vendored; no CDN belongs in the policy'
|
||||||
|
# Product pages and the cart are addresses of the Site's page, under the same policy.
|
||||||
|
for page in ('/arquivo-por-metro','/artes-avulsas','/uv-arquivo-por-metro','/uv-artes-avulsas','/carrinho'):
|
||||||
|
assert raw(page,200)['Content-Security-Policy']==policy,page
|
||||||
|
raw('/carrinho/outra-coisa',404)
|
||||||
raw('/api/health',400,{'Host':'attacker.invalid'})
|
raw('/api/health',400,{'Host':'attacker.invalid'})
|
||||||
raw('/api/account/logout',403,{'Content-Type':'application/json','Origin':'https://attacker.invalid'},b'{}')
|
raw('/api/account/logout',403,{'Content-Type':'application/json','Origin':'https://attacker.invalid'},b'{}')
|
||||||
raw('/api/account/logout',403,{'Content-Type':'application/json','Origin':'http://localhost:9999'},b'{}')
|
raw('/api/account/logout',403,{'Content-Type':'application/json','Origin':'http://localhost:9999'},b'{}')
|
||||||
|
|||||||
@@ -94,11 +94,18 @@ def item_spec(mode, metres, grade, uid):
|
|||||||
'quality_status':'unverified' if grade==0 else 'ok',
|
'quality_status':'unverified' if grade==0 else 'ok',
|
||||||
'quality_acknowledged':False}
|
'quality_acknowledged':False}
|
||||||
|
|
||||||
|
def approved_quote(client, quote, items):
|
||||||
|
"""The approval a customer pays against: automatic, or by the operator."""
|
||||||
|
if quote['status']=='approved':
|
||||||
|
return client.call('/quotes/'+quote['id'])['approved']
|
||||||
|
return client.call('/operator/quotes/'+quote['id']+'/approve',{'items':items},operator=True)
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
client=Client();other=Client()
|
client=Client();other=Client()
|
||||||
config=client.call('/session');other.call('/session')
|
config=client.call('/session');other.call('/session')
|
||||||
assert client.call('/health')['integrations']=='fake'
|
assert client.call('/health')['integrations']=='fake'
|
||||||
assert 0 < config['max_upload_bytes'] <= 128 * 1024 * 1024
|
# Sheets of several GB are the normal order: 5 GB per file.
|
||||||
|
assert config['max_upload_bytes'] == 5 * 1024 ** 3
|
||||||
client.call('/uploads',{'name':'too-large.cdr',
|
client.call('/uploads',{'name':'too-large.cdr',
|
||||||
'size':config['max_upload_bytes']+1},expected=413)
|
'size':config['max_upload_bytes']+1},expected=413)
|
||||||
cancelled=client.call('/uploads',{'name':'CANCELLED-PART.cdr','size':3})['id']
|
cancelled=client.call('/uploads',{'name':'CANCELLED-PART.cdr','size':3})['id']
|
||||||
@@ -127,7 +134,8 @@ def run():
|
|||||||
except HTTPError as exc:assert exc.code==403
|
except HTTPError as exc:assert exc.code==403
|
||||||
print('PASS: multipart resume, incomplete rejection, immutable completion, ownership, private/downloaded bytes')
|
print('PASS: multipart resume, incomplete rejection, immutable completion, ownership, private/downloaded bytes')
|
||||||
|
|
||||||
items=[item_spec(m,'2.75',90,uid) for m in ('file','avulsa','uvfile','uv')]
|
# Above QUOTE_AUTO_MAX_METRES (50 m by default), so a person reviews it.
|
||||||
|
items=[item_spec(m,'13',90,uid) for m in ('file','avulsa','uvfile','uv')]
|
||||||
draft={'request_key':str(uuid4()),'customer':{'cnpj':'11222333000181','zap':'11999999999','mail':'local-smoke@example.test'},
|
draft={'request_key':str(uuid4()),'customer':{'cnpj':'11222333000181','zap':'11999999999','mail':'local-smoke@example.test'},
|
||||||
'items':items,'freight':{'service':'mock-standard','postal_code':'14400000'},
|
'items':items,'freight':{'service':'mock-standard','postal_code':'14400000'},
|
||||||
'destination':{'recipient':'Local Smoke Ltda','street':'Rua de Teste','number':'100',
|
'destination':{'recipient':'Local Smoke Ltda','street':'Rua de Teste','number':'100',
|
||||||
@@ -146,6 +154,8 @@ def run():
|
|||||||
client.call('/quotes',{**draft,'items':[{**items[0],'metres':'1.00'}]},expected=422)
|
client.call('/quotes',{**draft,'items':[{**items[0],'metres':'1.00'}]},expected=422)
|
||||||
client.call('/quotes',{**draft,'customer':{**draft['customer'],'cnpj':'11111111111111'}},expected=422)
|
client.call('/quotes',{**draft,'customer':{**draft['customer'],'cnpj':'11111111111111'}},expected=422)
|
||||||
quote=client.call('/quotes',draft)
|
quote=client.call('/quotes',draft)
|
||||||
|
assert quote['status']=='pending_review'
|
||||||
|
assert client.call('/quotes/'+quote['id'])['review_reason']=='Pedido acima de 50 m'
|
||||||
assert client.call('/quotes',draft)['id']==quote['id']
|
assert client.call('/quotes',draft)['id']==quote['id']
|
||||||
client.call('/quotes',{**draft,'freight':{'service':'pickup'},'destination':None},expected=409)
|
client.call('/quotes',{**draft,'freight':{'service':'pickup'},'destination':None},expected=409)
|
||||||
qid=quote['id']
|
qid=quote['id']
|
||||||
@@ -159,9 +169,36 @@ def run():
|
|||||||
corrected=[{**items[0],'metres':'1.01','grade':0},*items[1:]]
|
corrected=[{**items[0],'metres':'1.01','grade':0},*items[1:]]
|
||||||
approved=client.call('/operator/quotes/'+qid+'/approve',{'items':corrected},operator=True)
|
approved=client.call('/operator/quotes/'+qid+'/approve',{'items':corrected},operator=True)
|
||||||
assert approved['items'][0]['total_cents']==2189
|
assert approved['items'][0]['total_cents']==2189
|
||||||
assert approved['total_cents']==2189+6972+19572+23492+int(os.environ.get('MOCK_FREIGHT_CENTS','1500'))
|
assert approved['total_cents']==2189+32370+90870+109070+int(os.environ.get('MOCK_FREIGHT_CENTS','1500'))
|
||||||
assert approved['destination']=={**draft['destination'],'complement':''}
|
assert approved['destination']=={**draft['destination'],'complement':''}
|
||||||
client.call('/operator/quotes/'+qid+'/approve',{'items':items},operator=True,expected=409)
|
client.call('/operator/quotes/'+qid+'/approve',{'items':items},operator=True,expected=409)
|
||||||
|
assert not client.call('/quotes/'+qid)['auto_approved']
|
||||||
|
# A cart the Site priced is approved at once and can be paid straight away,
|
||||||
|
# at the server's own prices; no operator can then change it.
|
||||||
|
# The server works the grade out from the file: this PNG is 6059 px across
|
||||||
|
# 57 cm, 270 DPI, which is grade 90.
|
||||||
|
png=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR'+(6059).to_bytes(4,'big')+(2000).to_bytes(4,'big')+b'\x08\x06\x00\x00\x00'+b'\x00'*4
|
||||||
|
graded=upload_bytes(client,png,name='arte-270dpi.png')
|
||||||
|
small={**draft,'request_key':str(uuid4()),'items':[item_spec('avulsa','2.75',90,graded)]}
|
||||||
|
auto=client.call('/quotes',small)
|
||||||
|
assert auto['status']=='approved'
|
||||||
|
seen=client.call('/quotes/'+auto['id'])
|
||||||
|
assert seen['auto_approved'] and seen['review_reason'] is None
|
||||||
|
assert seen['approved']['total_cents']==6972+int(os.environ.get('MOCK_FREIGHT_CENTS','1500'))
|
||||||
|
assert client.call('/quotes',small)['status']=='approved'
|
||||||
|
client.call('/operator/quotes/'+auto['id']+'/approve',{'items':small['items']},operator=True,expected=409)
|
||||||
|
# The Site grades only art it analysed; a discount on unanalysed art waits for a person.
|
||||||
|
claimed={**item_spec('avulsa','2.75',0,uid),'grade':90}
|
||||||
|
held=client.call('/quotes',{**small,'request_key':str(uuid4()),'items':[claimed]})
|
||||||
|
assert held['status']=='pending_review'
|
||||||
|
assert client.call('/quotes/'+held['id'])['review_reason']=='Nota informada sem análise da arte'
|
||||||
|
# A better grade than the file supports, or one the server cannot check, waits too.
|
||||||
|
inflated=client.call('/quotes',{**small,'request_key':str(uuid4()),'items':[item_spec('avulsa','2.75',100,graded)]})
|
||||||
|
assert inflated['status']=='pending_review'
|
||||||
|
assert client.call('/quotes/'+inflated['id'])['review_reason']=='Nota 100 maior que a do arquivo (90)'
|
||||||
|
unchecked=client.call('/quotes',{**small,'request_key':str(uuid4()),'items':[item_spec('avulsa','2.75',90,uid)]})
|
||||||
|
assert client.call('/quotes/'+unchecked['id'])['review_reason']=='Nota não conferida no servidor'
|
||||||
|
print('PASS: priced carts are approved at checkout; large or inconsistent ones wait for review')
|
||||||
client.call('/orders/dev-paid',{'quote_id':qid,'total_cents':1},expected=422)
|
client.call('/orders/dev-paid',{'quote_id':qid,'total_cents':1},expected=422)
|
||||||
other.call('/orders/dev-paid',{'quote_id':qid},expected=404)
|
other.call('/orders/dev-paid',{'quote_id':qid},expected=404)
|
||||||
# Concurrent retries must produce precisely one payment/order/outbox pair.
|
# Concurrent retries must produce precisely one payment/order/outbox pair.
|
||||||
@@ -185,14 +222,35 @@ def run():
|
|||||||
version+=1;assert moved['version']==version
|
version+=1;assert moved['version']==version
|
||||||
client.call('/operator/orders/'+oid+'/move',{'state':'rec','version':0},operator=True,expected=409)
|
client.call('/operator/orders/'+oid+'/move',{'state':'rec','version':0},operator=True,expected=409)
|
||||||
assert len(client.call('/operator/orders/'+oid+'/history',operator=True))==6
|
assert len(client.call('/operator/orders/'+oid+'/history',operator=True))==6
|
||||||
|
# A mistaken move can be undone one stage at a time, with an internal
|
||||||
|
# reason. The customer is not told again: going back and forward once more
|
||||||
|
# adds no messages (the outbox count below stays 8).
|
||||||
|
client.call('/operator/orders/'+oid+'/move',{'state':'imp','version':version},operator=True,expected=422)
|
||||||
|
client.call('/operator/orders/'+oid+'/move',{'state':'tra','version':version},operator=True,expected=409)
|
||||||
|
back=client.call('/operator/orders/'+oid+'/move',{'state':'imp','version':version,'reason':'Movido por engano'},operator=True)
|
||||||
|
assert back['state']=='imp';version+=1
|
||||||
|
client.call('/operator/orders/'+oid+'/move',{'state':'fin','version':version},operator=True);version+=1
|
||||||
|
history=client.call('/operator/orders/'+oid+'/history',operator=True)
|
||||||
|
assert len(history)==8 and history[-2]['back'] and history[-2]['reason']=='Movido por engano' and not history[-1]['back']
|
||||||
|
# The customer sees the stages and the correction's reason, never the
|
||||||
|
# internal reason for going back.
|
||||||
|
seen=client.call('/customer/orders/'+oid)['history']
|
||||||
|
assert len(seen)==7 and all(h['reason']=='' or h['to_state']=='cor' for h in seen)
|
||||||
|
assert any(h['to_state']=='cor' and h['reason']=='Local test correction' for h in seen)
|
||||||
|
print('PASS: a mistaken move is undone one stage back with a reason, without messaging the customer again')
|
||||||
print('PASS: all modes, authoritative review/prices/freight, tamper rejection, concurrent payment idempotency, transitions and history')
|
print('PASS: all modes, authoritative review/prices/freight, tamper rejection, concurrent payment idempotency, transitions and history')
|
||||||
deadline=time.monotonic()+30
|
deadline=time.monotonic()+30
|
||||||
while time.monotonic()<deadline:
|
while time.monotonic()<deadline:
|
||||||
events=[e for e in client.call('/operator/board',operator=True)['events'] if e['payload']['order_id']==oid]
|
events=[e for e in client.call('/operator/events?order='+str(order['number']),operator=True)['events'] if e['payload']['order_id']==oid]
|
||||||
if len(events)==8 and all(e['delivered_at'] and e['receipt'] for e in events):break
|
if len(events)==8 and all(e['delivered_at'] and e['receipt'] for e in events):break
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:raise AssertionError('Mock outbox did not drain')
|
else:raise AssertionError('Mock outbox did not drain')
|
||||||
assert len({e['event_key'] for e in events})==8
|
assert len({e['event_key'] for e in events})==8
|
||||||
|
# Tiny sets the pickup situação from the order it is sent; the fake sale
|
||||||
|
# has no Tiny id, so the real adapter would search for it.
|
||||||
|
ready={e['provider']:e['payload'] for e in events if e['payload']['event']=='ready'}
|
||||||
|
assert ready['tiny']['order']['freight']==order['snapshot']['freight'] and 'tiny_id' in ready['tiny']
|
||||||
|
assert 'order' not in ready['whatsapp']
|
||||||
print(f"PASS: 8 durable fake receipts. Local test order #{order['number']} retained in Finalizado.")
|
print(f"PASS: 8 durable fake receipts. Local test order #{order['number']} retained in Finalizado.")
|
||||||
return oid
|
return oid
|
||||||
|
|
||||||
|
|||||||
38
tests/test_cnpj.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
"""The CNPJ registry lookup against a fake BrasilAPI. Runs where httpx is installed."""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from app import cnpj
|
||||||
|
|
||||||
|
|
||||||
|
def transport(status, body=None):
|
||||||
|
return httpx.MockTransport(lambda request: httpx.Response(status, json=body or {}))
|
||||||
|
|
||||||
|
|
||||||
|
class CnpjTests(unittest.TestCase):
|
||||||
|
def test_names_come_from_the_registry_tidied(self):
|
||||||
|
found = cnpj.fetch('11222333000181', transport(200, {
|
||||||
|
'razao_social': 'LOJA TESTE COMERCIO LTDA ', 'nome_fantasia': 'Loja Teste'}))
|
||||||
|
self.assertEqual(found, {'legal_name': 'LOJA TESTE COMERCIO LTDA', 'trade_name': 'Loja Teste'})
|
||||||
|
self.assertEqual(cnpj.fetch('11222333000181', transport(200, {'razao_social': 'X LTDA', 'nome_fantasia': ''})),
|
||||||
|
{'legal_name': 'X LTDA', 'trade_name': None})
|
||||||
|
|
||||||
|
def test_an_unknown_cnpj_has_no_names_and_an_outage_is_an_error(self):
|
||||||
|
self.assertEqual(cnpj.fetch('00000000000000', transport(404)), {'legal_name': None, 'trade_name': None})
|
||||||
|
with self.assertRaises(httpx.HTTPError):
|
||||||
|
cnpj.fetch('11222333000181', transport(503))
|
||||||
|
|
||||||
|
def test_new_tiny_contacts_are_named_after_the_company(self):
|
||||||
|
from app.tiny import contact_payload
|
||||||
|
order = {'customer': {'cnpj': '11222333000181', 'mail': 'loja@example.test', 'zap': '16999999999'},
|
||||||
|
'destination': None}
|
||||||
|
named = contact_payload(order, {'legal': 'LOJA TESTE LTDA', 'trade': 'Loja Teste'})
|
||||||
|
self.assertEqual((named['nome'], named['fantasia']), ('LOJA TESTE LTDA', 'Loja Teste'))
|
||||||
|
plain = contact_payload(order)
|
||||||
|
self.assertEqual(plain['nome'], 'loja@example.test')
|
||||||
|
self.assertNotIn('fantasia', plain)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
86
tests/test_grade_check.py
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
"""The server's grade against real image and PDF files.
|
||||||
|
|
||||||
|
Runs where Pillow and pikepdf are installed (the API image).
|
||||||
|
"""
|
||||||
|
import io
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import pikepdf
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from app import grade_check
|
||||||
|
|
||||||
|
|
||||||
|
class FakeFiles:
|
||||||
|
def __init__(self, blobs):
|
||||||
|
self.blobs = blobs
|
||||||
|
|
||||||
|
def head(self, row):
|
||||||
|
return self.blobs[row['name']][:grade_check.HEAD_BYTES]
|
||||||
|
|
||||||
|
def pdf_dpi(self, row):
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||||
|
handle.write(self.blobs[row['name']])
|
||||||
|
handle.flush()
|
||||||
|
return grade_check.pdf_dpi(handle.name)
|
||||||
|
|
||||||
|
|
||||||
|
def encoded(fmt, size, **options):
|
||||||
|
out = io.BytesIO()
|
||||||
|
Image.new('RGBA' if fmt in ('PNG', 'WEBP') else 'RGB', size, (200, 30, 30)).save(out, fmt, **options)
|
||||||
|
return out.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
def item(kind, grade, sources):
|
||||||
|
return {'grade': grade, 'production': {'sources': [
|
||||||
|
{'upload_id': name, 'kind': kind, 'width_cm': width, 'rotation_degrees': rotation}
|
||||||
|
for name, width, rotation in sources]}}
|
||||||
|
|
||||||
|
|
||||||
|
class GradeCheckTests(unittest.TestCase):
|
||||||
|
def test_image_sizes_from_the_first_bytes(self):
|
||||||
|
for fmt, options in (('PNG', {}), ('JPEG', {'quality': 80}), ('JPEG', {'progressive': True}),
|
||||||
|
('WEBP', {'lossless': True}), ('WEBP', {'quality': 80})):
|
||||||
|
self.assertEqual(grade_check.image_size(encoded(fmt, (1234, 567), **options)), (1234, 567), (fmt, options))
|
||||||
|
# A JPEG with a large metadata block before the frame header.
|
||||||
|
exif = Image.Exif()
|
||||||
|
exif[0x010E] = 'x' * 60000
|
||||||
|
self.assertEqual(grade_check.image_size(encoded('JPEG', (800, 600), exif=exif)), (800, 600))
|
||||||
|
self.assertIsNone(grade_check.image_size(b'not an image at all'))
|
||||||
|
|
||||||
|
def test_pdf_dpi_is_the_images_area_weighted(self):
|
||||||
|
out = io.BytesIO()
|
||||||
|
Image.new('RGB', (1500, 750), 'white').save(out, 'PDF', resolution=150)
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||||
|
handle.write(out.getvalue()); handle.flush()
|
||||||
|
self.assertEqual(grade_check.pdf_dpi(handle.name), 150)
|
||||||
|
vector = pikepdf.new()
|
||||||
|
vector.add_blank_page(page_size=(1615, 850))
|
||||||
|
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||||
|
vector.save(handle.name)
|
||||||
|
self.assertEqual(grade_check.pdf_dpi(handle.name), 300)
|
||||||
|
|
||||||
|
def test_grades_follow_the_site(self):
|
||||||
|
# 57 cm is 22.44 in: 6059 px is 270 DPI, grade 90; 3366 px is 150 DPI, grade 50.
|
||||||
|
files = FakeFiles({'a.png': encoded('PNG', (6059, 100)), 'b.jpg': encoded('JPEG', (3366, 100)),
|
||||||
|
'tall.png': encoded('PNG', (100, 2362)), 'x.cdr': b'CDR'})
|
||||||
|
rows = {n: {'name': n, 'size': 1} for n in files.blobs}
|
||||||
|
# A sheet takes the worst sheet's grade.
|
||||||
|
self.assertEqual(grade_check.item_grade(files, item('sheet', 0, [('a.png', 57, 0)]), rows), 90)
|
||||||
|
self.assertEqual(grade_check.item_grade(files, item('sheet', 0, [('a.png', 57, 0), ('b.jpg', 57, 0)]), rows), 50)
|
||||||
|
# Artworks average; a rotated one is measured along its height.
|
||||||
|
self.assertEqual(grade_check.item_grade(files, item('artwork', 0, [('a.png', 57, 0), ('b.jpg', 57, 0)]), rows), 70)
|
||||||
|
self.assertEqual(grade_check.item_grade(files, item('artwork', 0, [('tall.png', 20, 90)]), rows), 100)
|
||||||
|
self.assertIsNone(grade_check.item_grade(files, item('sheet', 0, [('x.cdr', 57, 0)]), rows))
|
||||||
|
# What the cart claims is checked, with a point or two of rounding.
|
||||||
|
claim = lambda g, src: grade_check.mismatch(files, [item('sheet', g, src)], rows)
|
||||||
|
self.assertIsNone(claim(90, [('a.png', 57, 0)]))
|
||||||
|
self.assertIsNone(claim(92, [('a.png', 57, 0)]))
|
||||||
|
self.assertEqual(claim(100, [('a.png', 57, 0)]), 'Nota 100 maior que a do arquivo (90)')
|
||||||
|
self.assertEqual(claim(40, [('x.cdr', 57, 0)]), 'Nota não conferida no servidor')
|
||||||
|
self.assertIsNone(claim(0, [('x.cdr', 57, 0)]))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
191
tests/test_jadlog.py
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
"""The Jadlog quote client against a fake HTTP transport: payload and errors.
|
||||||
|
|
||||||
|
This proves the manual's contract only; app.jadlog_probe must still confirm
|
||||||
|
it on the client's account. Runs where httpx is installed.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
from decimal import Decimal
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from app.jadlog import QUOTE_URL, JadlogError, JadlogFreight, JadlogQuotes
|
||||||
|
from app.jadlog_probe import run
|
||||||
|
|
||||||
|
CNPJ = '11.222.333/0001-81'
|
||||||
|
|
||||||
|
|
||||||
|
def client(handler, **settings):
|
||||||
|
return JadlogQuotes(token=settings.pop('token', 'tok-1'), cnpj=CNPJ, conta='123456',
|
||||||
|
transport=httpx.MockTransport(handler), **settings)
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogQuoteTest(unittest.TestCase):
|
||||||
|
def test_payload_follows_the_manual_and_price_becomes_centavos(self):
|
||||||
|
seen = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
seen.append(request)
|
||||||
|
item = json.loads(request.content)['frete'][0]
|
||||||
|
return httpx.Response(200, json={'frete': [{**item, 'vltotal': 23.455, 'prazo': 4}]})
|
||||||
|
|
||||||
|
result = client(handler).quote('01310-100', 1.5, 12990)
|
||||||
|
request = seen[0]
|
||||||
|
self.assertEqual(str(request.url), QUOTE_URL)
|
||||||
|
self.assertEqual(request.headers['authorization'], 'tok-1')
|
||||||
|
item = json.loads(request.content)['frete'][0]
|
||||||
|
self.assertEqual(item, {'cepori': '14402310', 'cepdes': '01310100', 'frap': 'N', 'peso': 1.5,
|
||||||
|
'cnpj': '11222333000181', 'conta': '123456', 'contrato': None,
|
||||||
|
'modalidade': 3, 'tpentrega': 'D', 'tpseguro': 'N',
|
||||||
|
'vldeclarado': 129.9, 'vlcoleta': 0})
|
||||||
|
self.assertEqual((result['total_cents'], result['days']), (2346, 4))
|
||||||
|
|
||||||
|
def test_contract_is_sent_only_when_configured(self):
|
||||||
|
def handler(request):
|
||||||
|
item = json.loads(request.content)['frete'][0]
|
||||||
|
self.assertEqual(item['contrato'], '042')
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 10, 'prazo': 2}]})
|
||||||
|
|
||||||
|
client(handler, contrato='042').quote('01310100', 1, 100)
|
||||||
|
|
||||||
|
def test_account_is_sent_as_configured(self):
|
||||||
|
def handler(request):
|
||||||
|
self.assertEqual(json.loads(request.content)['frete'][0]['conta'], '123456-7')
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 10, 'prazo': 2}]})
|
||||||
|
|
||||||
|
JadlogQuotes(token='tok', cnpj=CNPJ, conta=' 123456-7 ', transport=httpx.MockTransport(handler)).quote('01310100', 1, 100)
|
||||||
|
|
||||||
|
def test_account_and_item_errors_are_raised_with_jadlog_text(self):
|
||||||
|
replies = [
|
||||||
|
httpx.Response(200, json={'frete': [{}], 'error': {'id': -1, 'descricao': 'frete[0].contrato Numero de contrato invalido'}}),
|
||||||
|
httpx.Response(200, json={'frete': [{'erro': {'id': 2, 'descricao': 'CEP destino invalido'}}]}),
|
||||||
|
httpx.Response(200, json={'frete': [{'prazo': 3}]}),
|
||||||
|
httpx.Response(502, text='<html>bad gateway</html>'),
|
||||||
|
]
|
||||||
|
messages = ['contrato invalido', 'CEP destino invalido', 'no freight value', 'not JSON']
|
||||||
|
for reply, message in zip(replies, messages):
|
||||||
|
with self.subTest(message=message), self.assertRaisesRegex(JadlogError, message):
|
||||||
|
client(lambda request, reply=reply: reply).quote('01310100', 1, 100)
|
||||||
|
|
||||||
|
def test_missing_credentials_refuse_to_start(self):
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
JadlogQuotes(token='', cnpj=CNPJ)
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
JadlogQuotes(token='tok', cnpj='123')
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogFreightTest(unittest.TestCase):
|
||||||
|
WEIGHTS = {'JADLOG_PESO_BASE_KG': '0.2', 'JADLOG_PESO_POR_METRO_KG': '0.15', 'FREIGHT_PRODUCTION_DAYS': '2'}
|
||||||
|
|
||||||
|
def freight(self, handler, **env):
|
||||||
|
with mock.patch.dict(os.environ, {**self.WEIGHTS, **env}):
|
||||||
|
return JadlogFreight(client(handler))
|
||||||
|
|
||||||
|
def test_package_weight_value_and_days(self):
|
||||||
|
seen = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
seen.append(json.loads(request.content)['frete'][0])
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 18.4, 'prazo': 3}]})
|
||||||
|
|
||||||
|
quote = self.freight(handler).quote('jadlog', '01310100', '2.8', 6972)
|
||||||
|
# 0.2 kg of packaging plus 0.15 kg for each of the 2.8 billed metres.
|
||||||
|
self.assertEqual((seen[0]['peso'], seen[0]['vldeclarado'], seen[0]['cepdes']), (0.62, 69.72, '01310100'))
|
||||||
|
self.assertEqual((quote['total_cents'], quote['days'], quote['service']), (1840, 5, 'jadlog'))
|
||||||
|
|
||||||
|
def test_pickup_is_free_and_bad_requests_are_refused(self):
|
||||||
|
freight = self.freight(lambda request: httpx.Response(500))
|
||||||
|
self.assertEqual(freight.quote('pickup', '')['total_cents'], 0)
|
||||||
|
for args in (('mock-standard', '01310100', '1'), ('jadlog', '0131', '1'), ('jadlog', '01310100', None)):
|
||||||
|
with self.subTest(args=args), self.assertRaises(ValueError):
|
||||||
|
freight.quote(*args)
|
||||||
|
|
||||||
|
def test_a_jadlog_failure_is_a_clear_refusal(self):
|
||||||
|
freight = self.freight(lambda request: httpx.Response(200, json={'frete': [{}], 'error': {'id': -1, 'descricao': 'CEP destino invalido'}}))
|
||||||
|
with self.assertRaisesRegex(ValueError, 'CEP destino invalido'):
|
||||||
|
freight.quote('jadlog', '01310100', '1', 100)
|
||||||
|
|
||||||
|
def test_the_box_cubed_weight_wins_when_larger(self):
|
||||||
|
seen = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
seen.append(json.loads(request.content)['frete'][0]['peso'])
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 30, 'prazo': 3}]})
|
||||||
|
|
||||||
|
# 56 x 14 x 14 cm is 0.010976 m³; at 300 kg/m³ that is 3.293 kg.
|
||||||
|
freight = self.freight(handler, JADLOG_PESO_BASE_KG='0.3', JADLOG_PESO_POR_METRO_KG='0.075',
|
||||||
|
JADLOG_CAIXA_CM='56 x 14 x 14', JADLOG_CUBAGEM_KG_M3='300')
|
||||||
|
self.assertEqual(freight.cubed_kg(), Decimal('3.293'))
|
||||||
|
quote = freight.quote('jadlog', '01310100', '1', 1490)
|
||||||
|
self.assertEqual((seen[0], quote['weight_kg'], quote['real_kg']), (3.293, '3.293', '0.375'))
|
||||||
|
# Past the box's cubed weight, the film's real weight is what counts.
|
||||||
|
freight.quote('jadlog', '01310100', '60', 1490)
|
||||||
|
self.assertEqual(seen[1], 4.8)
|
||||||
|
|
||||||
|
def test_a_long_order_ships_in_more_boxes(self):
|
||||||
|
seen = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
seen.append(json.loads(request.content)['frete'][0]['peso'])
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 30, 'prazo': 3}]})
|
||||||
|
|
||||||
|
freight = self.freight(handler, JADLOG_PESO_BASE_KG='0.3', JADLOG_PESO_POR_METRO_KG='0.075',
|
||||||
|
JADLOG_CAIXA_CM='63x15,5x15,5', JADLOG_CUBAGEM_KG_M3='300',
|
||||||
|
JADLOG_METROS_POR_CAIXA='60')
|
||||||
|
# 63 x 15.5 x 15.5 cm is 0.01513575 m³: 4.541 kg cubed for one box.
|
||||||
|
self.assertEqual((freight.boxes('60'), freight.boxes('60.01'), freight.boxes('121')), (1, 2, 3))
|
||||||
|
quote = freight.quote('jadlog', '01310100', '61', 1490)
|
||||||
|
# Two boxes: 0.6 kg of packaging plus 4.575 kg of film, against 9.081 kg cubed.
|
||||||
|
self.assertEqual((quote['boxes'], quote['real_kg'], seen[0]), (2, '5.175', 9.081))
|
||||||
|
|
||||||
|
def test_a_box_needs_its_factor(self):
|
||||||
|
for env in ({'JADLOG_CAIXA_CM': '56x14x14'}, {'JADLOG_CUBAGEM_KG_M3': '300'},
|
||||||
|
{'JADLOG_CAIXA_CM': '56x14', 'JADLOG_CUBAGEM_KG_M3': '300'},
|
||||||
|
{'JADLOG_CAIXA_CM': '56x14x14', 'JADLOG_CUBAGEM_KG_M3': '0'},
|
||||||
|
{'JADLOG_METROS_POR_CAIXA': '0'}):
|
||||||
|
with self.subTest(env=env), self.assertRaises(RuntimeError):
|
||||||
|
self.freight(lambda request: httpx.Response(200), **env)
|
||||||
|
# Neither: the real weight only, as before the box was known.
|
||||||
|
self.assertIsNone(self.freight(lambda request: httpx.Response(200)).cubed_kg())
|
||||||
|
|
||||||
|
def test_the_weight_has_no_default(self):
|
||||||
|
with mock.patch.dict(os.environ, {}, clear=True), self.assertRaises(KeyError):
|
||||||
|
JadlogFreight(client(lambda request: httpx.Response(200)))
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
self.freight(lambda request: httpx.Response(200), JADLOG_PESO_POR_METRO_KG='0')
|
||||||
|
|
||||||
|
|
||||||
|
class JadlogProbeTest(unittest.TestCase):
|
||||||
|
def test_bearer_is_tried_once_after_a_401_and_kept(self):
|
||||||
|
headers = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
headers.append(request.headers['authorization'])
|
||||||
|
if not request.headers['authorization'].startswith('Bearer '):
|
||||||
|
return httpx.Response(401, json={'descricao': 'TOKEN INVALIDO', 'id': 1})
|
||||||
|
return httpx.Response(200, json={'frete': [{'vltotal': 12.5, 'prazo': 3}]})
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
status = run(client(handler), ['01310100', '20040002'], ['1'], 10000, lines.append)
|
||||||
|
self.assertEqual(status, 0)
|
||||||
|
self.assertEqual(headers, ['tok-1', 'Bearer tok-1', 'Bearer tok-1'])
|
||||||
|
self.assertIn('Bearer', lines[0])
|
||||||
|
self.assertIn('R$ 12,50', lines[1])
|
||||||
|
|
||||||
|
def test_first_failure_stops_the_run(self):
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def handler(request):
|
||||||
|
calls.append(request)
|
||||||
|
return httpx.Response(200, json={'frete': [{}], 'error': {'id': -1, 'descricao': 'CNPJ invalido'}})
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
self.assertEqual(run(client(handler), ['01310100', '20040002'], ['1', '2'], 10000, lines.append), 1)
|
||||||
|
self.assertEqual(len(calls), 1)
|
||||||
|
self.assertIn('CNPJ invalido', lines[0])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
42
tests/test_large_files.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
"""Large sheets: when the original is its own print file, and the format check."""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.printjobs import whole_sheet
|
||||||
|
from app.scanning import format_matches
|
||||||
|
|
||||||
|
ROW = {'id': 'u1', 'name': 'folha.png', 'size': 3 * 1024 ** 3, 'scan_state': 'clean',
|
||||||
|
'purged_at': None, 'expired': False}
|
||||||
|
|
||||||
|
|
||||||
|
def sheet(**changes):
|
||||||
|
source = {'kind': 'sheet', 'width_cm': 57, 'length_cm': 500, 'copies': 1}
|
||||||
|
place = {'x_cm': 0, 'y_cm': 0, 'rotation_degrees': 0, 'mirrored': False}
|
||||||
|
for key, value in changes.items():
|
||||||
|
(source if key in source else place)[key] = value
|
||||||
|
return {'uploads': ['u1'], 'production': {'film_width_cm': 57, 'sources': [source], 'placements': [place]}}
|
||||||
|
|
||||||
|
|
||||||
|
class WholeSheetTest(unittest.TestCase):
|
||||||
|
def test_a_finished_sheet_placed_whole_is_its_own_print_file(self):
|
||||||
|
self.assertIs(whole_sheet(sheet(), {'u1': ROW}), ROW)
|
||||||
|
|
||||||
|
def test_anything_else_is_prepared_by_hand(self):
|
||||||
|
for changes in ({'copies': 2}, {'kind': 'artwork'}, {'rotation_degrees': 90}, {'mirrored': True},
|
||||||
|
{'x_cm': 1}, {'width_cm': 50}):
|
||||||
|
with self.subTest(changes=changes):
|
||||||
|
self.assertIsNone(whole_sheet(sheet(**changes), {'u1': ROW}))
|
||||||
|
self.assertIsNone(whole_sheet(sheet(), {'u1': {**ROW, 'name': 'folha.cdr'}}))
|
||||||
|
self.assertIsNone(whole_sheet(sheet(), {'u1': {**ROW, 'scan_state': 'pending'}}))
|
||||||
|
self.assertIsNone(whole_sheet(sheet(), {'u1': {**ROW, 'expired': True}}))
|
||||||
|
|
||||||
|
|
||||||
|
class FormatTest(unittest.TestCase):
|
||||||
|
def test_bytes_must_match_the_name(self):
|
||||||
|
self.assertTrue(format_matches('A.PNG', b'\x89PNG\r\n\x1a\n'))
|
||||||
|
self.assertTrue(format_matches('a.tiff', b'MM\x00*'))
|
||||||
|
self.assertFalse(format_matches('a.png', b'%PDF-1.4'))
|
||||||
|
self.assertFalse(format_matches('semextensao', b'\x89PNG\r\n\x1a\n'))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -8,6 +8,7 @@ import hashlib
|
|||||||
import hmac
|
import hmac
|
||||||
import json
|
import json
|
||||||
import unittest
|
import unittest
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
@@ -36,6 +37,10 @@ class MercadoPagoTests(unittest.TestCase):
|
|||||||
self.requests.append(request)
|
self.requests.append(request)
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
payment_id = request.url.path.rsplit('/', 1)[-1]
|
payment_id = request.url.path.rsplit('/', 1)[-1]
|
||||||
|
if payment_id == '500':
|
||||||
|
return httpx.Response(500, json={'message': 'internal_error'})
|
||||||
|
if payment_id not in self.payments:
|
||||||
|
return httpx.Response(404, json={'message': 'Payment not found'})
|
||||||
return httpx.Response(200, json=self.payments[payment_id])
|
return httpx.Response(200, json=self.payments[payment_id])
|
||||||
body = json.loads(request.content)
|
body = json.loads(request.content)
|
||||||
payment = {'id': 555, 'status': 'pending', 'status_detail': 'pending_waiting_transfer',
|
payment = {'id': 555, 'status': 'pending', 'status_detail': 'pending_waiting_transfer',
|
||||||
@@ -57,6 +62,24 @@ class MercadoPagoTests(unittest.TestCase):
|
|||||||
'x-request-id': 'req-1'}, b'{}', {'data.id': '123456'}))
|
'x-request-id': 'req-1'}, b'{}', {'data.id': '123456'}))
|
||||||
self.assertFalse(self.mp.verify({}, b'{}', {'data.id': '123456'}))
|
self.assertFalse(self.mp.verify({}, b'{}', {'data.id': '123456'}))
|
||||||
|
|
||||||
|
def test_a_secret_pasted_with_spaces_still_verifies(self):
|
||||||
|
mp = MercadoPagoPayment('TEST-token', SECRET + ' \n', 'https://dtf.example/api/payments/webhook',
|
||||||
|
transport=httpx.MockTransport(lambda r: httpx.Response(200)), clock=lambda: NOW)
|
||||||
|
headers = {'x-signature': signature('123456', 'req-1', NOW), 'x-request-id': 'req-1'}
|
||||||
|
self.assertTrue(mp.verify(headers, b'{}', {'data.id': '123456'}))
|
||||||
|
self.assertTrue(mp.secret_was_trimmed)
|
||||||
|
self.assertEqual(mp.last_signature, 'documentado')
|
||||||
|
|
||||||
|
def test_close_variants_of_the_signed_text_are_named(self):
|
||||||
|
# Signed without the request id, though the header came.
|
||||||
|
headers = {'x-signature': signature('123456', None, NOW), 'x-request-id': 'req-1'}
|
||||||
|
self.assertTrue(self.mp.verify(headers, b'{}', {'data.id': '123456'}))
|
||||||
|
self.assertEqual(self.mp.last_signature, 'sem request-id')
|
||||||
|
# Still the secret's: another secret matches no variant.
|
||||||
|
forged = {'x-signature': signature('123456', None, NOW, 'other'), 'x-request-id': 'req-1'}
|
||||||
|
self.assertFalse(self.mp.verify(forged, b'{}', {'data.id': '123456'}))
|
||||||
|
self.assertIsNone(self.mp.last_signature)
|
||||||
|
|
||||||
def test_absent_values_are_left_out_and_alphanumeric_ids_lowercased(self):
|
def test_absent_values_are_left_out_and_alphanumeric_ids_lowercased(self):
|
||||||
self.assertTrue(self.mp.verify({'x-signature': signature('abc123', None, NOW)}, b'{}',
|
self.assertTrue(self.mp.verify({'x-signature': signature('abc123', None, NOW)}, b'{}',
|
||||||
{'data.id': 'ABC123'}))
|
{'data.id': 'ABC123'}))
|
||||||
@@ -78,6 +101,15 @@ class MercadoPagoTests(unittest.TestCase):
|
|||||||
self.assertEqual(self.requests[-1].headers['authorization'], 'Bearer TEST-token')
|
self.assertEqual(self.requests[-1].headers['authorization'], 'Bearer TEST-token')
|
||||||
self.assertIsNone(self.mp.parse(json.dumps({'type': 'merchant_order', 'data': {'id': '1'}}).encode()))
|
self.assertIsNone(self.mp.parse(json.dumps({'type': 'merchant_order', 'data': {'id': '1'}}).encode()))
|
||||||
|
|
||||||
|
def test_notification_for_an_unknown_payment_is_acknowledged(self):
|
||||||
|
# The panel's "Simular notificação" sends a payment id that does not
|
||||||
|
# exist. Raising would answer 500 and Mercado Pago would retry for ever.
|
||||||
|
body = json.dumps({'id': 43, 'type': 'payment', 'data': {'id': '123456'}}).encode()
|
||||||
|
self.assertIsNone(self.mp.parse(body, {'data.id': '123456', 'type': 'payment'}))
|
||||||
|
# Any other failure still raises, so a real notification is retried.
|
||||||
|
with self.assertRaises(httpx.HTTPStatusError):
|
||||||
|
self.mp.parse(json.dumps({'type': 'payment', 'data': {'id': '500'}}).encode(), {'data.id': '500'})
|
||||||
|
|
||||||
def test_amounts_outside_brl_centavos_are_not_trusted(self):
|
def test_amounts_outside_brl_centavos_are_not_trusted(self):
|
||||||
for payment in ({'currency_id': 'USD', 'transaction_amount': 10},
|
for payment in ({'currency_id': 'USD', 'transaction_amount': 10},
|
||||||
{'currency_id': 'BRL', 'transaction_amount': 10.001},
|
{'currency_id': 'BRL', 'transaction_amount': 10.001},
|
||||||
@@ -97,11 +129,20 @@ class MercadoPagoTests(unittest.TestCase):
|
|||||||
request = self.requests[-1]
|
request = self.requests[-1]
|
||||||
body = json.loads(request.content)
|
body = json.loads(request.content)
|
||||||
self.assertEqual(request.headers['x-idempotency-key'],
|
self.assertEqual(request.headers['x-idempotency-key'],
|
||||||
'dtf-quote-11111111-2222-3333-4444-555555555555-pix')
|
'dtf-quote-11111111-2222-3333-4444-555555555555-pix-1')
|
||||||
self.assertEqual((body['payment_method_id'], body['transaction_amount']), ('pix', 123.45))
|
self.assertEqual((body['payment_method_id'], body['transaction_amount']), ('pix', 123.45))
|
||||||
self.assertEqual(body['external_reference'], '11111111-2222-3333-4444-555555555555')
|
self.assertEqual(body['external_reference'], '11111111-2222-3333-4444-555555555555')
|
||||||
self.assertEqual(body['notification_url'], 'https://dtf.example/api/payments/webhook')
|
self.assertEqual(body['notification_url'], 'https://dtf.example/api/payments/webhook')
|
||||||
self.assertEqual((created['pix_qr_code'], created['status']), ('000201PIX', 'pending'))
|
self.assertEqual((created['pix_qr_code'], created['status']), ('000201PIX', 'pending'))
|
||||||
|
# The code expires in 30 minutes, in the format Mercado Pago documents.
|
||||||
|
expires = datetime.fromisoformat(body['date_of_expiration'])
|
||||||
|
self.assertRegex(body['date_of_expiration'], r'^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}-03:00$')
|
||||||
|
self.assertAlmostEqual((expires - datetime.now(timezone.utc)).total_seconds(), 1800, delta=60)
|
||||||
|
self.assertEqual(created['expires_at'], body['date_of_expiration'])
|
||||||
|
# A new code after the last expired is the next attempt, not the same payment.
|
||||||
|
self.mp.create('11111111-2222-3333-4444-555555555555', 12345,
|
||||||
|
{'mail': 'a@example.test', 'cnpj': '11222333000181'}, {'type': 'pix', 'attempt': 2})
|
||||||
|
self.assertTrue(self.requests[-1].headers['x-idempotency-key'].endswith('-pix-2'))
|
||||||
|
|
||||||
def test_card_payment_uses_the_browser_token_only(self):
|
def test_card_payment_uses_the_browser_token_only(self):
|
||||||
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
||||||
@@ -116,6 +157,31 @@ class MercadoPagoTests(unittest.TestCase):
|
|||||||
{'type': 'card', 'token': 'tok_def', 'payment_method_id': 'visa'})
|
{'type': 'card', 'token': 'tok_def', 'payment_method_id': 'visa'})
|
||||||
self.assertNotEqual(self.requests[-1].headers['x-idempotency-key'], first_key)
|
self.assertNotEqual(self.requests[-1].headers['x-idempotency-key'], first_key)
|
||||||
self.assertTrue(first_key.startswith('dtf-quote-q-card-'))
|
self.assertTrue(first_key.startswith('dtf-quote-q-card-'))
|
||||||
|
# 3-D Secure is asked of debit only; the cardholder is the payer.
|
||||||
|
self.assertNotIn('three_d_secure_mode', body)
|
||||||
|
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
||||||
|
{'type': 'card', 'token': 'tok_deb', 'payment_method_id': 'debmaster',
|
||||||
|
'payer_document_type': 'CPF', 'payer_document': '12345678909'})
|
||||||
|
debit = json.loads(self.requests[-1].content)
|
||||||
|
self.assertEqual(debit['three_d_secure_mode'], 'optional')
|
||||||
|
self.assertEqual(debit['payer']['identification'], {'type': 'CPF', 'number': '12345678909'})
|
||||||
|
self.assertEqual(body['payer']['identification'], {'type': 'CNPJ', 'number': '11222333000181'})
|
||||||
|
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
||||||
|
{'type': 'card', 'token': 'tok_iss', 'payment_method_id': 'master', 'issuer_id': '24'})
|
||||||
|
self.assertNotIn('issuer_id', json.loads(self.requests[-1].content))
|
||||||
|
self.assertIsNone(self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
||||||
|
{'type': 'card', 'token': 'tok_ghi', 'payment_method_id': 'visa'})['challenge'])
|
||||||
|
|
||||||
|
def test_a_card_the_bank_must_confirm_returns_its_challenge(self):
|
||||||
|
def handler(request):
|
||||||
|
return httpx.Response(201, json={'id': 777, 'status': 'pending', 'status_detail': 'pending_challenge',
|
||||||
|
'three_ds_info': {'external_resource_url': 'https://acs.bank.example/challenge',
|
||||||
|
'creq': 'eyJjcmVxIjoiMSJ9'}})
|
||||||
|
mp = MercadoPagoPayment('TEST-token', SECRET, transport=httpx.MockTransport(handler), clock=lambda: NOW)
|
||||||
|
created = mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
|
||||||
|
{'type': 'card', 'token': 'tok_debit', 'payment_method_id': 'debvisa'})
|
||||||
|
self.assertEqual((created['status'], created['status_detail']), ('pending', 'pending_challenge'))
|
||||||
|
self.assertEqual(created['challenge'], {'url': 'https://acs.bank.example/challenge', 'creq': 'eyJjcmVxIjoiMSJ9'})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
30
tests/test_payment_messages.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
"""Refused payments become one plain sentence for the customer."""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from app import payment_messages as messages
|
||||||
|
|
||||||
|
|
||||||
|
class PaymentMessageTests(unittest.TestCase):
|
||||||
|
def test_a_refused_card_says_what_happened_and_what_to_do(self):
|
||||||
|
self.assertIn('limite suficiente', messages.rejection('cc_rejected_insufficient_amount'))
|
||||||
|
self.assertIn('código de segurança', messages.rejection('cc_rejected_bad_filled_security_code'))
|
||||||
|
self.assertIn('Ligue para o banco', messages.rejection('cc_rejected_call_for_authorize'))
|
||||||
|
# An unknown or missing code still reads as a sentence, never as the code.
|
||||||
|
for detail in ('cc_rejected_something_new', None, ''):
|
||||||
|
self.assertEqual(messages.rejection(detail), messages.REJECTED)
|
||||||
|
self.assertNotIn('cc_', messages.rejection(detail))
|
||||||
|
|
||||||
|
def test_a_refused_request_names_the_field_to_fix(self):
|
||||||
|
body = {'message': 'invalid parameters', 'cause': [{'code': 324, 'description': 'Invalid identification'}]}
|
||||||
|
self.assertIn('CPF ou CNPJ', messages.refusal(body))
|
||||||
|
self.assertEqual(messages.refusal({'cause': [{'code': 10113}]}), messages.NOT_PROCESSED)
|
||||||
|
self.assertEqual(messages.refusal({}), messages.NOT_PROCESSED)
|
||||||
|
|
||||||
|
def test_quote_refusals_are_in_portuguese(self):
|
||||||
|
self.assertIn('24 horas', messages.quote_refusal('quote expired before payment'))
|
||||||
|
self.assertIn('cancelada', messages.quote_refusal('quote was discarded'))
|
||||||
|
self.assertTrue(messages.quote_refusal('anything else').startswith('Não foi possível'))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -115,19 +115,19 @@ class PrintFileTests(unittest.TestCase):
|
|||||||
|
|
||||||
def test_refuses_what_it_cannot_reproduce(self):
|
def test_refuses_what_it_cannot_reproduce(self):
|
||||||
png = self.save(quadrants(), 'a.png')
|
png = self.save(quadrants(), 'a.png')
|
||||||
with self.assertRaisesRegex(Unsupported, 'proportions'):
|
with self.assertRaisesRegex(Unsupported, 'proporções'):
|
||||||
self.render(item([placement(0, 0, 20, 20)], [source(20, 20)], 20), [png])
|
self.render(item([placement(0, 0, 20, 20)], [source(20, 20)], 20), [png])
|
||||||
with self.assertRaisesRegex(Unsupported, 'billed'):
|
with self.assertRaisesRegex(Unsupported, 'cobrados'):
|
||||||
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 150, billed='1.0'), [png])
|
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 150, billed='1.0'), [png])
|
||||||
fake_pdf = os.path.join(self.dir.name, 'art.pdf')
|
fake_pdf = os.path.join(self.dir.name, 'art.pdf')
|
||||||
with open(fake_pdf, 'wb') as handle:
|
with open(fake_pdf, 'wb') as handle:
|
||||||
handle.write(b'%PDF-1.4\n%%EOF\n')
|
handle.write(b'%PDF-1.4\n%%EOF\n')
|
||||||
with self.assertRaisesRegex(Unsupported, 'not a PDF'):
|
with self.assertRaisesRegex(Unsupported, 'não é um PDF'):
|
||||||
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [fake_pdf])
|
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [fake_pdf])
|
||||||
cdr = os.path.join(self.dir.name, 'art.cdr')
|
cdr = os.path.join(self.dir.name, 'art.cdr')
|
||||||
with open(cdr, 'wb') as handle:
|
with open(cdr, 'wb') as handle:
|
||||||
handle.write(b'not artwork')
|
handle.write(b'not artwork')
|
||||||
with self.assertRaisesRegex(Unsupported, 'not an image'):
|
with self.assertRaisesRegex(Unsupported, 'não é uma imagem'):
|
||||||
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [cdr])
|
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [cdr])
|
||||||
|
|
||||||
def test_long_layouts_scale_user_space_instead_of_splitting(self):
|
def test_long_layouts_scale_user_space_instead_of_splitting(self):
|
||||||
@@ -247,12 +247,12 @@ class PdfSourceTests(unittest.TestCase):
|
|||||||
self.assertEqual(detail['min_dpi'], round(80 / (20 / 2.54)))
|
self.assertEqual(detail['min_dpi'], round(80 / (20 / 2.54)))
|
||||||
|
|
||||||
def test_refuses_pdfs_it_cannot_place(self):
|
def test_refuses_pdfs_it_cannot_place(self):
|
||||||
with self.assertRaisesRegex(Unsupported, '2 pages'):
|
with self.assertRaisesRegex(Unsupported, '2 páginas'):
|
||||||
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [self.pdf('two.pdf', pages=2)])
|
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10), [self.pdf('two.pdf', pages=2)])
|
||||||
with self.assertRaisesRegex(Unsupported, 'password'):
|
with self.assertRaisesRegex(Unsupported, 'senha'):
|
||||||
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10),
|
self.render(item([placement(0, 0, 20, 10)], [source(20, 10)], 10),
|
||||||
[self.pdf('locked.pdf', password='secret')])
|
[self.pdf('locked.pdf', password='secret')])
|
||||||
with self.assertRaisesRegex(Unsupported, 'proportions'):
|
with self.assertRaisesRegex(Unsupported, 'proporções'):
|
||||||
self.render(item([placement(0, 0, 20, 20)], [source(20, 20)], 20), [self.pdf('square.pdf')])
|
self.render(item([placement(0, 0, 20, 20)], [source(20, 20)], 20), [self.pdf('square.pdf')])
|
||||||
|
|
||||||
@unittest.skipIf(fitz is None, 'PyMuPDF is not installed')
|
@unittest.skipIf(fitz is None, 'PyMuPDF is not installed')
|
||||||
|
|||||||
38
tests/test_quote_review.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
"""Which quotes the Site approves at checkout and which wait for a person."""
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from app.quote_review import review_reason
|
||||||
|
|
||||||
|
|
||||||
|
def item(mode='avulsa', metres='2', grade=90, quality='ok', version=2):
|
||||||
|
return {'mode': mode, 'metres': metres, 'grade': grade, 'quality_status': quality,
|
||||||
|
'quality_acknowledged': quality == 'warning', 'production': {'version': version}}
|
||||||
|
|
||||||
|
|
||||||
|
class ReviewReasonTest(unittest.TestCase):
|
||||||
|
def reason(self, *items, **env):
|
||||||
|
with mock.patch.dict(os.environ, env):
|
||||||
|
return review_reason({'items': list(items)})
|
||||||
|
|
||||||
|
def test_a_priced_cart_is_approved(self):
|
||||||
|
self.assertIsNone(self.reason(item()))
|
||||||
|
# Accepted resolution warnings and unanalysed art at the full rate too.
|
||||||
|
self.assertIsNone(self.reason(item(quality='warning'), item(grade=0, quality='unverified')))
|
||||||
|
|
||||||
|
def test_large_orders_wait_for_review(self):
|
||||||
|
self.assertIsNone(self.reason(item(metres='30'), item(metres='20')))
|
||||||
|
self.assertEqual(self.reason(item(metres='30'), item(metres='20.1')), 'Pedido acima de 50 m')
|
||||||
|
self.assertEqual(self.reason(item(metres='6'), QUOTE_AUTO_MAX_METRES='5'), 'Pedido acima de 5 m')
|
||||||
|
|
||||||
|
def test_a_discount_the_site_could_not_have_given_waits(self):
|
||||||
|
self.assertEqual(self.reason(item(grade=90, quality='unverified')), 'Nota informada sem análise da arte')
|
||||||
|
|
||||||
|
def test_old_layouts_and_switching_it_off(self):
|
||||||
|
self.assertEqual(self.reason(item(version=1)), 'Montagem antiga')
|
||||||
|
self.assertEqual(self.reason(item(), QUOTE_AUTO_APPROVE='false'), 'Aprovação automática desligada')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -23,7 +23,9 @@ PAID = {'order_id': 'b6f1c0de-0000-4000-8000-000000000001', 'number': 42, 'event
|
|||||||
'state': 'SP', 'postal_code': '14400000'},
|
'state': 'SP', 'postal_code': '14400000'},
|
||||||
'total_cents': 8472}}
|
'total_cents': 8472}}
|
||||||
PRODUCTS = {'TINY_PRODUCT_TEXTIL_FOLHA': '101', 'TINY_PRODUCT_TEXTIL_AVULSA': '102',
|
PRODUCTS = {'TINY_PRODUCT_TEXTIL_FOLHA': '101', 'TINY_PRODUCT_TEXTIL_AVULSA': '102',
|
||||||
'TINY_PRODUCT_UV_FOLHA': '103', 'TINY_PRODUCT_UV_AVULSA': '104'}
|
'TINY_PRODUCT_UV_FOLHA': '103', 'TINY_PRODUCT_UV_AVULSA': '104', 'TINY_FORMA_ENVIO_RETIRADA': '77'}
|
||||||
|
PICKUP = {**PAID, 'order': {**PAID['order'], 'destination': None, 'freight': {'service': 'pickup', 'total_cents': 0}}}
|
||||||
|
READY = {'order_id': PAID['order_id'], 'number': 42, 'event': 'ready', 'reason': '', 'order': PICKUP['order']}
|
||||||
|
|
||||||
|
|
||||||
class FakeAuth:
|
class FakeAuth:
|
||||||
@@ -31,39 +33,70 @@ class FakeAuth:
|
|||||||
return 'access-1'
|
return 'access-1'
|
||||||
|
|
||||||
|
|
||||||
@mock.patch.dict(os.environ, PRODUCTS)
|
class FakeTinyCase(unittest.TestCase):
|
||||||
class TinyTests(unittest.TestCase):
|
"""A Tiny account in memory: contacts, orders, products and formas de envio."""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.contacts = []
|
self.contacts = []
|
||||||
self.orders = []
|
self.orders = []
|
||||||
self.calls = []
|
self.calls = []
|
||||||
|
self.refuse_status = 0
|
||||||
|
self.methods = {'77': {'id': 77, 'nome': 'Retirar pessoalmente', 'tipo': '6'},
|
||||||
|
'78': {'id': 78, 'nome': 'Correios', 'tipo': '1'}}
|
||||||
|
self.products = {value: {'id': int(value), 'sku': f'DTF-{value}', 'descricao': f'Produto {value}',
|
||||||
|
'situacao': 'A', 'precos': {'preco': 14.9}} for value in PRODUCTS.values()}
|
||||||
|
|
||||||
def handler(request):
|
def handler(request):
|
||||||
path = request.url.path.removeprefix('/public-api/v3')
|
path = request.url.path.removeprefix('/public-api/v3')
|
||||||
self.calls.append((request.method, path))
|
self.calls.append((request.method, path))
|
||||||
assert request.headers['authorization'] == 'Bearer access-1'
|
assert request.headers['authorization'] == 'Bearer access-1'
|
||||||
if request.method == 'GET' and path == '/contatos':
|
if request.method == 'GET' and path == '/contatos':
|
||||||
cnpj = request.url.params['cpfCnpj']
|
cnpj = request.url.params.get('cpfCnpj')
|
||||||
return httpx.Response(200, json={'itens': [c for c in self.contacts if c['cpfCnpj'] == cnpj]})
|
return httpx.Response(200, json={'itens': [c for c in self.contacts if cnpj in (None, c['cpfCnpj'])]})
|
||||||
if request.method == 'POST' and path == '/contatos':
|
if request.method == 'POST' and path == '/contatos':
|
||||||
contact = {**json.loads(request.content), 'id': 500 + len(self.contacts)}
|
contact = {**json.loads(request.content), 'id': 500 + len(self.contacts)}
|
||||||
self.contacts.append(contact)
|
self.contacts.append(contact)
|
||||||
return httpx.Response(200, json={'id': contact['id']})
|
return httpx.Response(200, json={'id': contact['id']})
|
||||||
|
if request.method == 'GET' and path.startswith('/produtos/'):
|
||||||
|
wanted = path.rsplit('/', 1)[-1]
|
||||||
|
if wanted not in self.products:
|
||||||
|
return httpx.Response(404, json={'mensagem': 'não encontrado'})
|
||||||
|
return httpx.Response(200, json=self.products[wanted])
|
||||||
|
if request.method == 'GET' and path == '/produtos':
|
||||||
|
return httpx.Response(200, json={'itens': list(self.products.values())})
|
||||||
|
if request.method == 'GET' and path.startswith('/formas-envio/'):
|
||||||
|
wanted = path.rsplit('/', 1)[-1]
|
||||||
|
if wanted not in self.methods:
|
||||||
|
return httpx.Response(404, json={'mensagem': 'não encontrado'})
|
||||||
|
return httpx.Response(200, json=self.methods[wanted])
|
||||||
|
if request.method == 'GET' and path == '/formas-envio':
|
||||||
|
return httpx.Response(200, json={'itens': list(self.methods.values())})
|
||||||
|
if request.method == 'PUT' and path.startswith('/pedidos/') and path.endswith('/situacao'):
|
||||||
|
if self.refuse_status:
|
||||||
|
self.refuse_status -= 1
|
||||||
|
return httpx.Response(503, text='indisponível')
|
||||||
|
wanted = int(path.split('/')[2])
|
||||||
|
next(o for o in self.orders if o['id'] == wanted)['situacao'] = json.loads(request.content)['situacao']
|
||||||
|
return httpx.Response(204)
|
||||||
if request.method == 'GET' and path == '/pedidos':
|
if request.method == 'GET' and path == '/pedidos':
|
||||||
return httpx.Response(200, json={'itens': [{'id': o['id']} for o in self.orders]})
|
return httpx.Response(200, json={'itens': [{'id': o['id']} for o in self.orders]})
|
||||||
if request.method == 'GET' and path.startswith('/pedidos/'):
|
if request.method == 'GET' and path.startswith('/pedidos/'):
|
||||||
wanted = int(path.rsplit('/', 1)[-1])
|
wanted = int(path.rsplit('/', 1)[-1])
|
||||||
return httpx.Response(200, json=next(o for o in self.orders if o['id'] == wanted))
|
return httpx.Response(200, json=next(o for o in self.orders if o['id'] == wanted))
|
||||||
if request.method == 'POST' and path == '/pedidos':
|
if request.method == 'POST' and path == '/pedidos':
|
||||||
order = {**json.loads(request.content), 'id': 9000 + len(self.orders),
|
order = {'situacao': 0, **json.loads(request.content), 'id': 9000 + len(self.orders),
|
||||||
'numeroPedido': str(100 + len(self.orders))}
|
'numeroPedido': str(100 + len(self.orders))}
|
||||||
self.orders.append(order)
|
self.orders.append(order)
|
||||||
return httpx.Response(200, json={'id': order['id'], 'numeroPedido': order['numeroPedido']})
|
return httpx.Response(200, json={'id': order['id'], 'numeroPedido': order['numeroPedido']})
|
||||||
return httpx.Response(404)
|
return httpx.Response(404)
|
||||||
|
|
||||||
|
self.handler = handler
|
||||||
self.tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(handler),
|
self.tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(handler),
|
||||||
today=date(2026, 9, 24))
|
today=date(2026, 9, 24))
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, PRODUCTS)
|
||||||
|
class TinyTests(FakeTinyCase):
|
||||||
def test_payload_carries_contact_products_address_and_freight(self):
|
def test_payload_carries_contact_products_address_and_freight(self):
|
||||||
pedido = order_payload(PAID, 777, date(2026, 9, 24))
|
pedido = order_payload(PAID, 777, date(2026, 9, 24))
|
||||||
self.assertEqual((pedido['idContato'], pedido['numeroOrdemCompra'], pedido['data']),
|
self.assertEqual((pedido['idContato'], pedido['numeroOrdemCompra'], pedido['data']),
|
||||||
@@ -106,11 +139,254 @@ class TinyTests(unittest.TestCase):
|
|||||||
self.tiny.deliver('k3', PAID)
|
self.tiny.deliver('k3', PAID)
|
||||||
self.assertNotIn(('POST', '/pedidos'), self.calls)
|
self.assertNotIn(('POST', '/pedidos'), self.calls)
|
||||||
|
|
||||||
|
def test_connection_check_only_reads(self):
|
||||||
|
from app.tiny import check
|
||||||
|
results = check(auth=FakeAuth(), transport=httpx.MockTransport(self.handler))
|
||||||
|
self.assertEqual(set(results.values()), {'ok'})
|
||||||
|
self.assertEqual(len(results), 7)
|
||||||
|
self.assertTrue(all(method == 'GET' for method, _ in self.calls))
|
||||||
|
self.products['103']['situacao'] = 'I'
|
||||||
|
with mock.patch.dict(os.environ, {'TINY_PRODUCT_UV_AVULSA': ''}):
|
||||||
|
results = check(auth=FakeAuth(), transport=httpx.MockTransport(self.handler))
|
||||||
|
self.assertIn('não está ativo', results['DTF UV 28,5 cm · folha montada'])
|
||||||
|
self.assertEqual(results['DTF UV 28,5 cm · artes avulsas'], 'TINY_PRODUCT_UV_AVULSA sem id')
|
||||||
|
self.assertEqual(results['pedidos'], 'ok')
|
||||||
|
denied = check(auth=FakeAuth(), transport=httpx.MockTransport(lambda r: httpx.Response(403, text='forbidden')))
|
||||||
|
self.assertTrue(denied['pedidos'].startswith('GET /pedidos: 403'))
|
||||||
|
|
||||||
def test_rate_limit_is_a_retryable_failure(self):
|
def test_rate_limit_is_a_retryable_failure(self):
|
||||||
tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(lambda r: httpx.Response(429)))
|
tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(lambda r: httpx.Response(429)))
|
||||||
with self.assertRaisesRegex(TinyError, 'rate limit'):
|
with self.assertRaisesRegex(TinyError, 'rate limit'):
|
||||||
tiny.deliver('k4', PAID)
|
tiny.deliver('k4', PAID)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, PRODUCTS)
|
||||||
|
class TinyStatusTests(FakeTinyCase):
|
||||||
|
"""Situações that the client's Tiny -> n8n notices turn into WhatsApp messages."""
|
||||||
|
|
||||||
|
def puts(self):
|
||||||
|
return [call for call in self.calls if call[0] == 'PUT']
|
||||||
|
|
||||||
|
def test_nothing_changes_while_status_updates_are_off(self):
|
||||||
|
self.assertEqual(self.tiny.deliver('k1', PICKUP)['status'], 'created')
|
||||||
|
self.assertEqual(self.orders[0]['situacao'], 0)
|
||||||
|
self.assertEqual(self.tiny.deliver('k2', READY)['status'], 'not-applicable')
|
||||||
|
self.assertEqual(self.puts(), [])
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_paid_order_is_approved_once(self):
|
||||||
|
first = self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual((first['status'], first['situacao']), ('created', 'Aprovada'))
|
||||||
|
self.assertEqual(self.orders[0]['situacao'], 3)
|
||||||
|
second = self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual(second['status'], 'already-created')
|
||||||
|
self.assertNotIn('situacao', second)
|
||||||
|
self.assertEqual(len(self.puts()), 1)
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_retry_after_a_failed_approval_finishes_it(self):
|
||||||
|
self.refuse_status = 1
|
||||||
|
with self.assertRaises(TinyError):
|
||||||
|
self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual((len(self.orders), self.orders[0]['situacao']), (1, 0))
|
||||||
|
retry = self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual((retry['status'], retry['situacao']), ('already-created', 'Aprovada'))
|
||||||
|
self.assertEqual(self.calls.count(('POST', '/pedidos')), 1)
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_an_order_already_moved_on_is_not_approved_again(self):
|
||||||
|
self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.orders[0]['situacao'] = 7
|
||||||
|
self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual(self.orders[0]['situacao'], 7)
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_ready_pickup_order_is_set_ready_once_by_its_known_id(self):
|
||||||
|
sale = self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.calls.clear()
|
||||||
|
ready = self.tiny.deliver('k2', {**READY, 'tiny_id': sale['tiny_id']})
|
||||||
|
self.assertEqual((ready['status'], ready['situacao'], ready['tiny_number']),
|
||||||
|
('status-updated', 'Pronto para envio', sale['tiny_number']))
|
||||||
|
self.assertEqual(self.orders[0]['situacao'], 7)
|
||||||
|
self.assertNotIn(('GET', '/pedidos'), self.calls, 'a known id needs no search')
|
||||||
|
again = self.tiny.deliver('k2', {**READY, 'tiny_id': sale['tiny_id']})
|
||||||
|
self.assertEqual(again['status'], 'status-unchanged')
|
||||||
|
self.assertEqual(len(self.puts()), 1)
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_ready_without_an_id_finds_the_order(self):
|
||||||
|
self.tiny.deliver('k1', PICKUP)
|
||||||
|
self.assertEqual(self.tiny.deliver('k2', READY)['status'], 'status-updated')
|
||||||
|
self.assertEqual(self.orders[0]['situacao'], 7)
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_ready_before_the_sale_reached_tiny_is_retried(self):
|
||||||
|
with self.assertRaisesRegex(TinyError, 'not in Tiny yet'):
|
||||||
|
self.tiny.deliver('k2', READY)
|
||||||
|
self.assertEqual(self.puts(), [])
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {'TINY_STATUS_UPDATES': 'true'})
|
||||||
|
def test_ready_delivery_order_waits_for_freight(self):
|
||||||
|
self.tiny.deliver('k1', PAID)
|
||||||
|
self.calls.clear()
|
||||||
|
ready = self.tiny.deliver('k2', {**READY, 'order': PAID['order']})
|
||||||
|
self.assertEqual(ready['status'], 'not-applicable')
|
||||||
|
self.assertEqual(self.calls, [])
|
||||||
|
|
||||||
|
def test_pickup_orders_carry_the_pickup_forma_de_envio(self):
|
||||||
|
self.assertEqual(order_payload(PICKUP, 1)['transportador'], {'formaEnvio': {'id': 77}})
|
||||||
|
self.assertNotIn('transportador', order_payload(PAID, 1))
|
||||||
|
with mock.patch.dict(os.environ, {'TINY_FORMA_ENVIO_RETIRADA': ''}):
|
||||||
|
self.assertNotIn('transportador', order_payload(PICKUP, 1))
|
||||||
|
|
||||||
|
|
||||||
|
TEXTIL = {**PAID, 'order': {**PAID['order'], 'items': [
|
||||||
|
{'mode': 'file', 'grade': 100, 'billed_metres': '2.8', 'unit_cents': 1490, 'total_cents': 4172}]}}
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {**PRODUCTS, 'TINY_SKU_TEXTIL_FOLHA': 'DTFIMP.TX'})
|
||||||
|
class TinySkuTests(FakeTinyCase):
|
||||||
|
"""Têxtil folha sold as the client's SKUs: 1 metro plus 10 to 90 cm."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
skus = {'DTFIMP.TX.100CM': 200, **{f'DTFIMP.TX.{cm:04d}CM': 200 + cm // 10 for cm in range(10, 100, 10)},
|
||||||
|
# Same family, different product: must never be taken for 10 cm.
|
||||||
|
'DTFIMP.TX.010CM': 299}
|
||||||
|
for code, pid in skus.items():
|
||||||
|
self.products[str(pid)] = {'id': pid, 'sku': code, 'descricao': code, 'situacao': 'A'}
|
||||||
|
|
||||||
|
def test_metres_become_metre_skus_plus_one_centimetre_sku_at_the_price_paid(self):
|
||||||
|
self.tiny.deliver('k1', TEXTIL)
|
||||||
|
lines = [(i['produto']['id'], i['quantidade'], i['valorUnitario']) for i in self.orders[0]['itens']]
|
||||||
|
# 2,80 m at R$ 14,90: 2 × "1 metro" + 1 × "80 cm", adding up to R$ 41,72.
|
||||||
|
self.assertEqual(lines, [(200, 2, 14.9), (208, 1, 11.92)])
|
||||||
|
self.assertAlmostEqual(sum(q * v for _, q, v in lines), 41.72)
|
||||||
|
self.assertIn('2.8 m', self.orders[0]['itens'][0]['infoAdicional'])
|
||||||
|
# Each code is looked up once, then remembered.
|
||||||
|
self.tiny.deliver('k2', {**TEXTIL, 'number': 43})
|
||||||
|
self.assertEqual(self.calls.count(('GET', '/produtos')), 2)
|
||||||
|
|
||||||
|
def test_whole_metres_and_small_rests(self):
|
||||||
|
from app.tiny import sku_lines
|
||||||
|
item = lambda metres: {'billed_metres': metres, 'unit_cents': 1990}
|
||||||
|
self.assertEqual(sku_lines(item('1'), 'P'), [('P.100CM', 1, 1990)])
|
||||||
|
self.assertEqual(sku_lines(item('1.3'), 'P'), [('P.100CM', 1, 1990), ('P.0030CM', 1, 597)])
|
||||||
|
self.assertEqual(sku_lines(item('12.4'), 'P'), [('P.100CM', 12, 1990), ('P.0040CM', 1, 796)])
|
||||||
|
with self.assertRaises(TinyError):
|
||||||
|
sku_lines(item('1.25'), 'P')
|
||||||
|
|
||||||
|
def test_a_missing_sku_fails_before_the_order_is_created(self):
|
||||||
|
del self.products['208']
|
||||||
|
with self.assertRaisesRegex(TinyError, 'DTFIMP.TX.0080CM'):
|
||||||
|
self.tiny.deliver('k3', TEXTIL)
|
||||||
|
self.assertNotIn(('POST', '/pedidos'), self.calls)
|
||||||
|
|
||||||
|
def test_other_modes_keep_their_product_id(self):
|
||||||
|
self.tiny.deliver('k4', PAID)
|
||||||
|
self.assertEqual(self.orders[0]['itens'][0]['produto'], {'id': 102})
|
||||||
|
|
||||||
|
def test_settings_check_and_probe_report_the_skus(self):
|
||||||
|
from app.tiny import check, required_settings
|
||||||
|
self.assertNotIn('TINY_PRODUCT_TEXTIL_FOLHA', required_settings())
|
||||||
|
self.assertIn('TINY_PRODUCT_TEXTIL_AVULSA', required_settings())
|
||||||
|
self.assertEqual(check(orders=self.tiny)['DTF Têxtil 57 cm · folha montada'], 'ok')
|
||||||
|
self.products['205']['situacao'] = 'I'
|
||||||
|
self.tiny.skus.clear()
|
||||||
|
self.assertIn('DTFIMP.TX.0050CM', check(orders=self.tiny)['DTF Têxtil 57 cm · folha montada'])
|
||||||
|
from app import tiny_probe
|
||||||
|
lines = []
|
||||||
|
tiny_probe.main(['pedido', '--cnpj', '11222333000181', '--email', 'a@example.test',
|
||||||
|
'--celular', '16999999999', '--metros', '1,3'], orders=self.tiny, out=lines.append)
|
||||||
|
text = '\n'.join(lines)
|
||||||
|
self.assertIn('"quantidade": 1', text)
|
||||||
|
self.assertIn('"valorUnitario": 4.47', text)
|
||||||
|
self.assertEqual(self.orders, [])
|
||||||
|
|
||||||
|
|
||||||
|
UV = {**PAID, 'order': {**PAID['order'], 'items': [
|
||||||
|
{'mode': 'uvfile', 'grade': 100, 'billed_metres': '2.3', 'unit_cents': 6990, 'total_cents': 16077}]}}
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, {**PRODUCTS, 'TINY_SKU10_UV_FOLHA': 'DTFIMP.UV.010CM'})
|
||||||
|
class TinyStepSkuTests(FakeTinyCase):
|
||||||
|
"""UV sold as one SKU per 10 cm, the only UV SKU the client has."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.products['300'] = {'id': 300, 'sku': 'DTFIMP.UV.010CM', 'descricao': 'UV 10 cm', 'situacao': 'A'}
|
||||||
|
|
||||||
|
def test_the_length_in_ten_centimetre_steps_at_the_price_paid(self):
|
||||||
|
self.tiny.deliver('k1', UV)
|
||||||
|
lines = [(i['produto']['id'], i['quantidade'], i['valorUnitario']) for i in self.orders[0]['itens']]
|
||||||
|
self.assertEqual(lines, [(300, 23, 6.99)]) # 2,30 m at R$ 69,90 = R$ 160,77
|
||||||
|
|
||||||
|
def test_a_rest_that_does_not_divide_gets_its_own_line(self):
|
||||||
|
from app.tiny import step_lines
|
||||||
|
lines = step_lines({'billed_metres': '1.3', 'unit_cents': 1495}, 'C')
|
||||||
|
self.assertEqual(lines, [('C', 12, 149), ('C', 1, 155)])
|
||||||
|
self.assertEqual(sum(q * c for _, q, c in lines), 1943)
|
||||||
|
|
||||||
|
def test_settings_and_check_know_the_step_sku(self):
|
||||||
|
from app.tiny import check, required_settings
|
||||||
|
self.assertNotIn('TINY_PRODUCT_UV_FOLHA', required_settings())
|
||||||
|
self.assertEqual(check(orders=self.tiny)['DTF UV 28,5 cm · folha montada'], 'ok')
|
||||||
|
del self.products['300']
|
||||||
|
self.tiny.skus.clear()
|
||||||
|
self.assertIn('DTFIMP.UV.010CM', check(orders=self.tiny)['DTF UV 28,5 cm · folha montada'])
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ, PRODUCTS)
|
||||||
|
class TinyProbeTests(FakeTinyCase):
|
||||||
|
"""The supervised console tool run against the fake Tiny."""
|
||||||
|
|
||||||
|
def probe(self, *argv):
|
||||||
|
from app import tiny_probe
|
||||||
|
lines = []
|
||||||
|
code = tiny_probe.main(list(argv), orders=self.tiny, out=lines.append)
|
||||||
|
return code, '\n'.join(lines)
|
||||||
|
|
||||||
|
def test_products_and_settings_are_listed_without_writing(self):
|
||||||
|
code, text = self.probe('produtos', 'DTF')
|
||||||
|
self.assertEqual(code, 0)
|
||||||
|
self.assertIn('101\tDTF-101\tProduto 101', text)
|
||||||
|
code, text = self.probe('conferir')
|
||||||
|
self.assertEqual(code, 0)
|
||||||
|
self.assertIn('TINY_PRODUCT_UV_AVULSA (DTF UV 28,5 cm · artes avulsas): ok DTF-104', text)
|
||||||
|
self.assertIn('TINY_FORMA_ENVIO_RETIRADA (retirada): ok Retirar pessoalmente', text)
|
||||||
|
code, text = self.probe('formas-envio')
|
||||||
|
self.assertIn('77\t6\tRetirar pessoalmente', text)
|
||||||
|
self.assertTrue(all(method == 'GET' for method, _ in self.calls))
|
||||||
|
|
||||||
|
def test_order_is_shown_and_not_created_without_confirmation(self):
|
||||||
|
code, text = self.probe('pedido', '--cnpj', '11.222.333/0001-81', '--email', 'a@example.test',
|
||||||
|
'--celular', '16999999999')
|
||||||
|
self.assertEqual(code, 0)
|
||||||
|
self.assertIn('"numeroOrdemCompra": "DTF-TESTE-', text)
|
||||||
|
self.assertIn('Nada foi criado', text)
|
||||||
|
self.assertEqual(self.orders, [])
|
||||||
|
self.assertTrue(all(method == 'GET' for method, _ in self.calls))
|
||||||
|
|
||||||
|
def test_confirmed_order_is_created_once_and_found_on_resend(self):
|
||||||
|
code, text = self.probe('pedido', '--cnpj', '11222333000181', '--email', 'a@example.test',
|
||||||
|
'--celular', '16999999999', '--modo', 'uv', '--confirmar')
|
||||||
|
self.assertEqual(code, 0, text)
|
||||||
|
self.assertEqual(len(self.orders), 1)
|
||||||
|
self.assertEqual(self.orders[0]['itens'][0]['produto'], {'id': 104})
|
||||||
|
self.assertIn('2º envio: already-created', text)
|
||||||
|
|
||||||
|
def test_resend_is_skipped_when_the_search_cannot_find_the_order(self):
|
||||||
|
from app import tiny_probe
|
||||||
|
blind = lambda request: (httpx.Response(200, json={'itens': []})
|
||||||
|
if request.method == 'GET' and request.url.path.endswith('/pedidos')
|
||||||
|
else self.handler(request))
|
||||||
|
self.tiny = TinyOrders(auth=FakeAuth(), transport=httpx.MockTransport(blind), today=date(2026, 9, 24))
|
||||||
|
payload = tiny_probe.test_order('11222333000181', 'a@example.test', '16999999999', 'file')
|
||||||
|
lines = []
|
||||||
|
self.assertFalse(tiny_probe.pedido(self.tiny, payload, True, lines.append, wait=lambda s: None))
|
||||||
|
self.assertEqual(len(self.orders), 1)
|
||||||
|
self.assertIn('o 2º envio não foi feito', lines[-1])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
71
tests/thumbnail_backfill_test.py
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
"""Old orders get their artwork picture from the stored image, once.
|
||||||
|
|
||||||
|
Run inside the API container: python -m tests.thumbnail_backfill_test
|
||||||
|
"""
|
||||||
|
import io
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
from psycopg.types.json import Jsonb
|
||||||
|
|
||||||
|
from app.core import db
|
||||||
|
from ops import backfill_thumbnails
|
||||||
|
|
||||||
|
|
||||||
|
def png(width, height):
|
||||||
|
out = io.BytesIO()
|
||||||
|
Image.new('RGBA', (width, height), (255, 0, 0, 128)).save(out, 'PNG')
|
||||||
|
return out.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
class Storage:
|
||||||
|
"""Objects by key, written where the worker asks."""
|
||||||
|
def __init__(self, objects):
|
||||||
|
self.objects, self.fetched = objects, []
|
||||||
|
|
||||||
|
def fetch(self, key, path):
|
||||||
|
"""Other uploads in a shared database are not this test's: they fail like unreadable files."""
|
||||||
|
self.fetched.append(key)
|
||||||
|
with open(path, 'wb') as handle:
|
||||||
|
handle.write(self.objects[key])
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
owner, quote, order = uuid4(), uuid4(), uuid4()
|
||||||
|
image, drawing = uuid4(), uuid4()
|
||||||
|
files = {image: ('folha.png', png(1200, 3000)), drawing: ('arte.cdr', b'CDR')}
|
||||||
|
storage = Storage({f'backfill/{uid}': content for uid, (_, content) in files.items()})
|
||||||
|
try:
|
||||||
|
with db.connect() as c:
|
||||||
|
for uid, (name, content) in files.items():
|
||||||
|
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,
|
||||||
|
expires_at,scan_state) VALUES(%s,%s,%s,%s,%s,'m',true,now()+interval '1 day','clean')''',
|
||||||
|
(uid, owner, name, len(content), f'backfill/{uid}'))
|
||||||
|
items = [{'mode': 'file', 'uploads': [str(image), str(drawing)]}]
|
||||||
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft)
|
||||||
|
VALUES(%s,%s,%s,'backfill-fixture',%s)''', (quote, owner, uuid4(), Jsonb({'items': items})))
|
||||||
|
c.execute('INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s)',
|
||||||
|
(order, quote, owner, Jsonb({'items': items, 'total_cents': 1490}), Jsonb({'provider': 'teste'})))
|
||||||
|
lines = []
|
||||||
|
assert backfill_thumbnails.main([], out=lines.append, storage=storage) == 0
|
||||||
|
with db.connect() as c:
|
||||||
|
made = c.execute('SELECT mime,data FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (image,)).fetchone()
|
||||||
|
assert made and made['mime'] == 'image/webp'
|
||||||
|
with Image.open(io.BytesIO(bytes(made['data']))) as picture:
|
||||||
|
assert picture.format == 'WEBP' and picture.size[0] <= 480 and picture.size[1] <= 720, picture.size
|
||||||
|
assert not c.execute('SELECT 1 FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (drawing,)).fetchone()
|
||||||
|
# The CDR is never fetched; a second run does not fetch the image again.
|
||||||
|
assert storage.fetched.count(f'backfill/{image}') == 1 and f'backfill/{drawing}' not in storage.fetched
|
||||||
|
backfill_thumbnails.main([], out=lines.append, storage=storage)
|
||||||
|
assert storage.fetched.count(f'backfill/{image}') == 1
|
||||||
|
print('PASS: old orders get a WebP picture from a stored PNG; drawings are skipped; a rerun repeats nothing')
|
||||||
|
finally:
|
||||||
|
with db.connect() as c:
|
||||||
|
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=ANY(%s)', (list(files),))
|
||||||
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,))
|
||||||
|
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
|
||||||
|
c.execute('DELETE FROM dtf_local.uploads WHERE id=ANY(%s)', (list(files),))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
@@ -33,8 +33,12 @@ def run():
|
|||||||
|
|
||||||
def exercise():
|
def exercise():
|
||||||
issued = []
|
issued = []
|
||||||
|
server = {'mode': 'session', 'calls': 0}
|
||||||
|
|
||||||
def token_server(request):
|
def token_server(request):
|
||||||
|
server['calls'] += 1
|
||||||
|
if server['mode'] == 'down':
|
||||||
|
return httpx.Response(503, text='unavailable')
|
||||||
form = {k: v[0] for k, v in parse_qs(request.content.decode()).items()}
|
form = {k: v[0] for k, v in parse_qs(request.content.decode()).items()}
|
||||||
assert (form['client_id'], form['client_secret']) == ('test-client', 'test-secret')
|
assert (form['client_id'], form['client_secret']) == ('test-client', 'test-secret')
|
||||||
if form['grant_type'] == 'authorization_code':
|
if form['grant_type'] == 'authorization_code':
|
||||||
@@ -44,8 +48,13 @@ def exercise():
|
|||||||
return httpx.Response(400, json={'error': 'invalid_grant'})
|
return httpx.Response(400, json={'error': 'invalid_grant'})
|
||||||
n = len(issued) + 1
|
n = len(issued) + 1
|
||||||
issued.append(f'refresh-{n}')
|
issued.append(f'refresh-{n}')
|
||||||
|
if server['mode'] == 'offline':
|
||||||
return httpx.Response(200, json={'access_token': f'access-{n}', 'expires_in': 14400,
|
return httpx.Response(200, json={'access_token': f'access-{n}', 'expires_in': 14400,
|
||||||
'refresh_token': f'refresh-{n}', 'refresh_expires_in': 86400})
|
'refresh_token': f'refresh-{n}', 'refresh_expires_in': 0,
|
||||||
|
'scope': 'openid offline_access profile'})
|
||||||
|
return httpx.Response(200, json={'access_token': f'access-{n}', 'expires_in': 14400,
|
||||||
|
'refresh_token': f'refresh-{n}', 'refresh_expires_in': 86400,
|
||||||
|
'scope': 'openid profile'})
|
||||||
|
|
||||||
auth = TinyAuth(transport=httpx.MockTransport(token_server))
|
auth = TinyAuth(transport=httpx.MockTransport(token_server))
|
||||||
assert auth.status() == {'connected': False}
|
assert auth.status() == {'connected': False}
|
||||||
@@ -59,6 +68,7 @@ def exercise():
|
|||||||
query = {k: v[0] for k, v in parse_qs(url.query).items()}
|
query = {k: v[0] for k, v in parse_qs(url.query).items()}
|
||||||
assert url.netloc == 'accounts.tiny.com.br' and query['client_id'] == 'test-client'
|
assert url.netloc == 'accounts.tiny.com.br' and query['client_id'] == 'test-client'
|
||||||
assert query['redirect_uri'].endswith('/api/operator/tiny/callback') and len(query['state']) > 30
|
assert query['redirect_uri'].endswith('/api/operator/tiny/callback') and len(query['state']) > 30
|
||||||
|
assert query['scope'] == 'openid offline_access'
|
||||||
try:
|
try:
|
||||||
auth.complete('good-code', 'forged-state')
|
auth.complete('good-code', 'forged-state')
|
||||||
raise AssertionError('a state no operator created must be refused')
|
raise AssertionError('a state no operator created must be refused')
|
||||||
@@ -72,6 +82,7 @@ def exercise():
|
|||||||
pass
|
pass
|
||||||
status = auth.status()
|
status = auth.status()
|
||||||
assert status['connected'] and status['connected_by'] == 'operator@example.test'
|
assert status['connected'] and status['connected_by'] == 'operator@example.test'
|
||||||
|
assert status['problem'] is None and not status['offline'] and status['expires_at']
|
||||||
assert auth.access_token() == 'access-1'
|
assert auth.access_token() == 'access-1'
|
||||||
print('PASS: operator-started state is required, single-use, and stores the connection')
|
print('PASS: operator-started state is required, single-use, and stores the connection')
|
||||||
|
|
||||||
@@ -85,6 +96,23 @@ def exercise():
|
|||||||
assert auth.access_token() == 'access-2'
|
assert auth.access_token() == 'access-2'
|
||||||
print('PASS: expiring access token refreshed once, refresh token rotated and stored')
|
print('PASS: expiring access token refreshed once, refresh token rotated and stored')
|
||||||
|
|
||||||
|
# Tiny unreachable: the failure is shown, the connection kept, and the next
|
||||||
|
# renewal clears it.
|
||||||
|
with connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.provider_tokens SET access_expires_at=now() WHERE provider='tiny'")
|
||||||
|
server['mode'] = 'down'
|
||||||
|
try:
|
||||||
|
auth.access_token()
|
||||||
|
raise AssertionError('an unreachable token server must fail the renewal')
|
||||||
|
except httpx.HTTPError:
|
||||||
|
pass
|
||||||
|
status = auth.status()
|
||||||
|
assert status['connected'] and status['problem'] == 'renewal-failing' and status['failed_at']
|
||||||
|
server['mode'] = 'session'
|
||||||
|
assert auth.access_token() == 'access-3'
|
||||||
|
assert auth.status()['problem'] is None
|
||||||
|
print('PASS: a failed renewal is shown and cleared by the next successful one')
|
||||||
|
|
||||||
# A refused refresh (revoked in Tiny) asks for a new connection, not a retry loop.
|
# A refused refresh (revoked in Tiny) asks for a new connection, not a retry loop.
|
||||||
with connect() as c:
|
with connect() as c:
|
||||||
c.execute("""UPDATE dtf_local.provider_tokens SET access_expires_at=now(), refresh_token='revoked'
|
c.execute("""UPDATE dtf_local.provider_tokens SET access_expires_at=now(), refresh_token='revoked'
|
||||||
@@ -94,12 +122,63 @@ def exercise():
|
|||||||
raise AssertionError('a refused refresh must report the connection as lost')
|
raise AssertionError('a refused refresh must report the connection as lost')
|
||||||
except TinyNotConnected:
|
except TinyNotConnected:
|
||||||
pass
|
pass
|
||||||
|
status = auth.status()
|
||||||
|
assert not status['connected'] and status['problem'] == 'refused'
|
||||||
|
calls = server['calls']
|
||||||
|
try:
|
||||||
|
auth.access_token()
|
||||||
|
raise AssertionError('a refused connection must stay refused')
|
||||||
|
except TinyNotConnected:
|
||||||
|
pass
|
||||||
|
assert server['calls'] == calls, 'a refused refresh token must not be sent again'
|
||||||
with connect() as c:
|
with connect() as c:
|
||||||
c.execute("UPDATE dtf_local.provider_tokens SET refresh_expires_at=%s WHERE provider='tiny'",
|
c.execute("UPDATE dtf_local.provider_tokens SET refresh_expires_at=%s WHERE provider='tiny'",
|
||||||
(datetime.now(timezone.utc) - timedelta(seconds=1),))
|
(datetime.now(timezone.utc) - timedelta(seconds=1),))
|
||||||
assert not auth.status()['connected']
|
assert not auth.status()['connected']
|
||||||
print('PASS: revoked or expired connections report that Tiny must be connected again')
|
print('PASS: revoked or expired connections report that Tiny must be connected again')
|
||||||
|
|
||||||
|
# Reconnecting with an offline grant: no daily end, and the refusal is cleared.
|
||||||
|
server['mode'] = 'offline'
|
||||||
|
state = parse_qs(urlparse(auth.authorize_url('operator@example.test')).query)['state'][0]
|
||||||
|
auth.complete('good-code', state)
|
||||||
|
status = auth.status()
|
||||||
|
assert status['connected'] and status['offline'] and status['expires_at'] is None
|
||||||
|
assert status['problem'] is None
|
||||||
|
with connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.provider_tokens SET access_expires_at=now() WHERE provider='tiny'")
|
||||||
|
auth.access_token()
|
||||||
|
assert auth.status()['offline'] and auth.status()['expires_at'] is None
|
||||||
|
print('PASS: an offline grant is stored without a daily expiry and survives renewal')
|
||||||
|
|
||||||
|
# Tiny refusing offline_access restarts the authorisation without it, once.
|
||||||
|
state = parse_qs(urlparse(auth.authorize_url('operator@example.test')).query)['state'][0]
|
||||||
|
retry = parse_qs(urlparse(auth.without_offline(state)).query)
|
||||||
|
assert retry['scope'] == ['openid'] and retry['state'][0] != state
|
||||||
|
try:
|
||||||
|
auth.without_offline(state)
|
||||||
|
raise AssertionError('the refused state must be spent')
|
||||||
|
except TinyError:
|
||||||
|
pass
|
||||||
|
# A session grant close to its end is flagged while renewals are not happening.
|
||||||
|
server['mode'] = 'session'
|
||||||
|
auth.complete('good-code', retry['state'][0])
|
||||||
|
with connect() as c:
|
||||||
|
c.execute("UPDATE dtf_local.provider_tokens SET refresh_expires_at=now()+interval '2 hours' WHERE provider='tiny'")
|
||||||
|
status = auth.status()
|
||||||
|
assert status['connected'] and status['problem'] == 'expiring' and not status['offline']
|
||||||
|
print('PASS: a refused offline scope falls back once; a connection near its end is flagged')
|
||||||
|
|
||||||
|
# Tiny's redirect back with an error instead of a code.
|
||||||
|
from app.api.operator import tiny_callback
|
||||||
|
declined = parse_qs(urlparse(auth.authorize_url('operator@example.test')).query)['state'][0]
|
||||||
|
assert tiny_callback('', declined, 'access_denied').headers['location'] == '/?tiny=failed'
|
||||||
|
assert tiny_callback('', '', '').headers['location'] == '/?tiny=failed'
|
||||||
|
scoped = parse_qs(urlparse(auth.authorize_url('operator@example.test')).query)['state'][0]
|
||||||
|
location = urlparse(tiny_callback('', scoped, 'invalid_scope').headers['location'])
|
||||||
|
assert location.netloc == 'accounts.tiny.com.br' and parse_qs(location.query)['scope'] == ['openid']
|
||||||
|
assert tiny_callback('', scoped, 'invalid_scope').headers['location'] == '/?tiny=failed'
|
||||||
|
print('PASS: a declined or malformed callback returns to the Kanban; a refused scope retries without it')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
run()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""Customer identity, correction and final-file trust boundaries against local stack."""
|
"""Customer identity, correction and final-file trust boundaries against local stack."""
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
from tests.smoke_test import Client, upload_bytes, item_spec
|
from tests.smoke_test import Client, approved_quote, upload_bytes, item_spec
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
customer=Client();other=Client();customer.call('/session');other.call('/session')
|
customer=Client();other=Client();customer.call('/session');other.call('/session')
|
||||||
@@ -9,13 +9,29 @@ def run():
|
|||||||
item=item_spec('file','1.01',0,uid)
|
item=item_spec('file','1.01',0,uid)
|
||||||
profile={'cnpj':'11222333000181','zap':'11999999999','mail':'workflow-'+uuid4().hex[:8]+'@example.test'}
|
profile={'cnpj':'11222333000181','zap':'11999999999','mail':'workflow-'+uuid4().hex[:8]+'@example.test'}
|
||||||
q=customer.call('/quotes',{'request_key':str(uuid4()),'customer':profile,'items':[item],'freight':{'service':'pickup'}})
|
q=customer.call('/quotes',{'request_key':str(uuid4()),'customer':profile,'items':[item],'freight':{'service':'pickup'}})
|
||||||
customer.call('/operator/quotes/'+q['id']+'/approve',{'items':[item]},operator=True)
|
approved_quote(customer,q,[item])
|
||||||
order=customer.call('/orders/dev-paid',{'quote_id':q['id']});oid=order['id']
|
order=customer.call('/orders/dev-paid',{'quote_id':q['id']});oid=order['id']
|
||||||
before=list(customer.jar)[0].value
|
before=list(customer.jar)[0].value
|
||||||
password='local-test-password-'+uuid4().hex
|
password='local-test-password-'+uuid4().hex
|
||||||
customer.call('/account/register',{'customer':profile,'password':password})
|
customer.call('/account/register',{'customer':profile,'password':password})
|
||||||
assert customer.call('/account/me')['customer']['mail']==profile['mail']
|
assert customer.call('/account/me')['customer']['mail']==profile['mail']
|
||||||
assert customer.call('/customer/orders')['orders'][0]['id']==oid
|
assert [e['id'] for e in customer.call('/customer/orders')['items'] if e['kind']=='order'][0]==oid
|
||||||
|
# The list filters, pages and counts by group.
|
||||||
|
listing=customer.call('/customer/orders?status=prod&page=1&size=1')
|
||||||
|
assert listing['counts']['prod']>=1 and listing['total']==listing['counts']['prod'] and len(listing['items'])==1, listing
|
||||||
|
assert customer.call('/customer/orders?status=fin')['items']==[]
|
||||||
|
assert customer.call('/customer/orders?number='+str(order['number']))['items'][0]['id']==oid
|
||||||
|
# The account's details change; the CNPJ does not, and e-mail and password need the password.
|
||||||
|
address={'recipient':'Workflow Ltda','street':'Rua de Teste','number':'10','complement':'',
|
||||||
|
'district':'Centro','city':'Franca','state':'SP','postal_code':'14400000'}
|
||||||
|
saved=customer.call('/account/profile',{'zap':'(16) 98888-7777','address':address})['customer']
|
||||||
|
assert saved['zap']=='16988887777' and saved['cnpj']==profile['cnpj'] and saved['address']['city']=='Franca'
|
||||||
|
assert customer.call('/account/me')['customer']['address']['postal_code']=='14400000'
|
||||||
|
customer.call('/account/profile',{'zap':'16988887777','cnpj':'00000000000000'},expected=422)
|
||||||
|
customer.call('/account/email',{'email':'x-'+profile['mail'],'password':'wrong-password'},expected=401)
|
||||||
|
customer.call('/account/password',{'current':'wrong-password','new':'another-password-123'},expected=401)
|
||||||
|
other.call('/account/profile',{'zap':'16988887777'},expected=401)
|
||||||
|
print('PASS: customer order list filters and pages; account details change, CNPJ does not')
|
||||||
# Email/CNPJ do not grant ownership; only current guest session is migrated.
|
# Email/CNPJ do not grant ownership; only current guest session is migrated.
|
||||||
other.call('/customer/orders/'+oid,expected=404)
|
other.call('/customer/orders/'+oid,expected=404)
|
||||||
other.call('/account/login',{'email':profile['mail'],'password':'wrong-password'},expected=401)
|
other.call('/account/login',{'email':profile['mail'],'password':'wrong-password'},expected=401)
|
||||||
@@ -45,7 +61,13 @@ def run():
|
|||||||
link=customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download')
|
link=customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download')
|
||||||
assert urlopen(link['url']).read()==b'LOCAL FINAL VERSION ONE'
|
assert urlopen(link['url']).read()==b'LOCAL FINAL VERSION ONE'
|
||||||
guest=Client();guest.call('/session');guest.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404)
|
guest=Client();guest.call('/session');guest.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404)
|
||||||
version=move('fil',version);version=move('imp',version);version=move('cor',version)
|
version=move('fil',version)
|
||||||
|
# The print queue's files come named after their order, straight from storage.
|
||||||
|
queued=customer.call('/operator/queue/finals',operator=True)['files']
|
||||||
|
mine=[f for f in queued if urlopen(f['url']).read()==b'LOCAL FINAL VERSION ONE']
|
||||||
|
assert len(mine)==1 and mine[0]['name'].startswith('pedido-%d-item-1-' % mine[0]['number']), queued
|
||||||
|
customer.call('/operator/queue/finals',expected=401)
|
||||||
|
version=move('imp',version);version=move('cor',version)
|
||||||
customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404)
|
customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404)
|
||||||
premature_final=upload_bytes(customer,b'FINAL BEFORE CUSTOMER CORRECTION',order_id=oid)
|
premature_final=upload_bytes(customer,b'FINAL BEFORE CUSTOMER CORRECTION',order_id=oid)
|
||||||
version=customer.call('/operator/orders/'+oid+'/final-files',
|
version=customer.call('/operator/orders/'+oid+'/final-files',
|
||||||
@@ -61,6 +83,11 @@ def run():
|
|||||||
assert not any(f['kind']=='final' and f['active'] for f in files), 'new correction kept a stale final active'
|
assert not any(f['kind']=='final' and f['active'] for f in files), 'new correction kept a stale final active'
|
||||||
version=move('tra',version)
|
version=move('tra',version)
|
||||||
customer.call('/operator/orders/'+oid+'/move',{'state':'fil','version':version},operator=True,expected=409)
|
customer.call('/operator/orders/'+oid+'/move',{'state':'fil','version':version},operator=True,expected=409)
|
||||||
|
# The correction itself can be approved as the final of the item it corrects.
|
||||||
|
version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':correction_id}],'note':'Correction is print-ready'},operator=True)['version']
|
||||||
|
files=customer.call('/operator/orders/'+oid+'/files',operator=True)
|
||||||
|
assert any(f['kind']=='final' and f['active'] and f['upload_id']==correction_id for f in files)
|
||||||
|
customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':correction_id}],'note':'Twice'},operator=True,expected=409)
|
||||||
new_final=upload_bytes(customer,b'LOCAL FINAL VERSION TWO',order_id=oid)
|
new_final=upload_bytes(customer,b'LOCAL FINAL VERSION TWO',order_id=oid)
|
||||||
version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':new_final}],'note':'Checked corrected final'},operator=True)['version']
|
version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':new_final}],'note':'Checked corrected final'},operator=True)['version']
|
||||||
for state in ('fil','imp','fin'):version=move(state,version)
|
for state in ('fil','imp','fin'):version=move(state,version)
|
||||||
@@ -72,7 +99,7 @@ def run():
|
|||||||
old_cookie=list(other.jar)[0].value
|
old_cookie=list(other.jar)[0].value
|
||||||
other.call('/account/logout',{})
|
other.call('/account/logout',{})
|
||||||
cookie.value=old_cookie;revoked.jar.set_cookie(cookie);revoked.call('/customer/orders',expected=401)
|
cookie.value=old_cookie;revoked.jar.set_cookie(cookie);revoked.call('/customer/orders',expected=401)
|
||||||
other.call('/session');assert other.call('/customer/orders')['orders']==[]
|
other.call('/session');assert other.call('/customer/orders')['items']==[]
|
||||||
print('PASS: logout revokes server session and signed-out visitors cannot see account orders')
|
print('PASS: logout revokes server session and signed-out visitors cannot see account orders')
|
||||||
|
|
||||||
if __name__=='__main__':run()
|
if __name__=='__main__':run()
|
||||||
|
|||||||
41
web/cart.js
@@ -1,9 +1,15 @@
|
|||||||
/* Browser-local cart recovery. Files are stored only for an unfinished cart, for 24h. */
|
/* Browser-local cart recovery. Files are stored only for an unfinished cart, for 24h.
|
||||||
|
Only the items are kept: the customer's details and address are typed again
|
||||||
|
after a reload or a closed tab, so nothing personal stays in the browser. */
|
||||||
(() => {
|
(() => {
|
||||||
|
// A browser may put back what was typed before the reload without telling
|
||||||
|
// the page, which then shows values it does not know and cannot pay with.
|
||||||
|
const CAMPOS=['fCnpj','fZap','fMail','cepIn','eNome','eRua','eNum','eComp','eBairro','eCidade','eUf'];
|
||||||
|
for(const id of CAMPOS)if($(id))$(id).value='';
|
||||||
let database,scope,timer,restoring=true;
|
let database,scope,timer,restoring=true;
|
||||||
let signedOut=false;
|
let signedOut=false;
|
||||||
window.addEventListener('dtf-private-data-cleared',()=>{signedOut=true;clearTimeout(timer);pedido=[];itemAtual=null;folhas=[];artes=[];});
|
window.addEventListener('dtf-private-data-cleared',()=>{signedOut=true;clearTimeout(timer);pedido=[];itemAtual=null;folhas=[];artes=[];});
|
||||||
const notice=document.createElement('p');notice.style.cssText='font:13px system-ui;color:#56616d;padding:8px 20px';
|
const notice=document.createElement('p');notice.className='carrAviso';
|
||||||
document.getElementById('carr').prepend(notice);
|
document.getElementById('carr').prepend(notice);
|
||||||
function transaction(mode,fn){return new Promise((resolve,reject)=>{const tx=database.transaction('cart',mode);const request=fn(tx.objectStore('cart'));let result;request.onsuccess=()=>result=request.result;tx.oncomplete=()=>resolve(result);tx.onerror=()=>reject(tx.error);tx.onabort=()=>reject(tx.error);});}
|
function transaction(mode,fn){return new Promise((resolve,reject)=>{const tx=database.transaction('cart',mode);const request=fn(tx.objectStore('cart'));let result;request.onsuccess=()=>result=request.result;tx.oncomplete=()=>resolve(result);tx.onerror=()=>reject(tx.error);tx.onabort=()=>reject(tx.error);});}
|
||||||
async function save(){
|
async function save(){
|
||||||
@@ -11,8 +17,8 @@
|
|||||||
const items=[...pedido,...(itemAtual?[itemAtual]:[])];
|
const items=[...pedido,...(itemAtual?[itemAtual]:[])];
|
||||||
try{
|
try{
|
||||||
if(!items.length){await transaction('readwrite',store=>store.delete(scope));return;}
|
if(!items.length){await transaction('readwrite',store=>store.delete(scope));return;}
|
||||||
await transaction('readwrite',store=>store.put({items,customer:{...cliente},delivery:{...entrega},expires:Date.now()+86400000},scope));
|
await transaction('readwrite',store=>store.put({items,expires:Date.now()+86400000},scope));
|
||||||
notice.textContent='Carrinho salvo neste navegador por 24 horas. Você pode remover itens e adicionar outros após recarregar.';
|
notice.textContent='';
|
||||||
}catch(error){notice.textContent='Não foi possível salvar o carrinho neste navegador. Mantenha esta página aberta até enviar o pedido.';}
|
}catch(error){notice.textContent='Não foi possível salvar o carrinho neste navegador. Mantenha esta página aberta até enviar o pedido.';}
|
||||||
}
|
}
|
||||||
window.dtfClearCart=async()=>{clearTimeout(timer);if(database&&scope)await transaction('readwrite',store=>store.delete(scope));notice.textContent='';};
|
window.dtfClearCart=async()=>{clearTimeout(timer);if(database&&scope)await transaction('readwrite',store=>store.delete(scope));notice.textContent='';};
|
||||||
@@ -30,19 +36,26 @@
|
|||||||
for(const key of records){const value=await transaction('readonly',store=>store.get(key));if(value.expires<Date.now())await transaction('readwrite',store=>store.delete(key));}
|
for(const key of records){const value=await transaction('readonly',store=>store.get(key));if(value.expires<Date.now())await transaction('readwrite',store=>store.delete(key));}
|
||||||
const saved=await transaction('readonly',store=>store.get(scope));
|
const saved=await transaction('readonly',store=>store.get(scope));
|
||||||
if(saved && !pedido.length && !itemAtual){
|
if(saved && !pedido.length && !itemAtual){
|
||||||
pedido=saved.items;cliente=saved.customer;entrega=saved.delivery;
|
pedido=saved.items;
|
||||||
// Freight must be quoted again; restored browser values are never final.
|
|
||||||
if(entrega.tipo==='frete'){entrega.cotado=false;entrega.valor=0;}
|
|
||||||
for(const [id,key] of [['fCnpj','cnpj'],['fZap','zap'],['fMail','mail']])$(id).value=cliente[key];
|
|
||||||
$('cepIn').value=entrega.cep;
|
|
||||||
entrega.end=entrega.end||{};
|
|
||||||
for(const [id,key] of [['eNome','nome'],['eRua','rua'],['eNum','num'],['eComp','comp'],
|
|
||||||
['eBairro','bairro'],['eCidade','cidade'],['eUf','uf']])$(id).value=entrega.end[key]||'';
|
|
||||||
$('atual').style.display='none';pintaEntrega();
|
$('atual').style.display='none';pintaEntrega();
|
||||||
notice.textContent='Carrinho recuperado. Remova e adicione novamente um item se precisar alterar sua montagem.';
|
notice.textContent='Carrinho recuperado. Remova e adicione novamente um item se precisar alterar sua montagem.';
|
||||||
}else{
|
}
|
||||||
|
// A signed-in customer's details come from the account, with the saved
|
||||||
|
// delivery address, whether or not a cart was recovered.
|
||||||
const account=await window.dtfApi('/account/me');
|
const account=await window.dtfApi('/account/me');
|
||||||
if(account.customer && !cliente.mail){cliente={...account.customer};for(const [id,key] of [['fCnpj','cnpj'],['fZap','zap'],['fMail','mail']])$(id).value=cliente[key];pintaEntrega();}
|
const c=account.customer;
|
||||||
|
if(c && !cliente.mail){
|
||||||
|
cliente={cnpj:c.cnpj,zap:c.zap,mail:c.mail};
|
||||||
|
// The fields' own input handlers format them, as if typed.
|
||||||
|
for(const [id,key] of [['fCnpj','cnpj'],['fZap','zap'],['fMail','mail']]){$(id).value=cliente[key];$(id).dispatchEvent(new Event('input'));}
|
||||||
|
const a=c.address;
|
||||||
|
if(a && !entrega.cep){
|
||||||
|
entrega.cep=a.postal_code;
|
||||||
|
entrega.end={nome:a.recipient,rua:a.street,num:a.number,comp:a.complement||'',bairro:a.district,cidade:a.city,uf:a.state};
|
||||||
|
$('cepIn').value=a.postal_code.replace(/^(\d{5})(\d{3})$/,'$1-$2');
|
||||||
|
for(const [id,key] of [['eNome','nome'],['eRua','rua'],['eNum','num'],['eComp','comp'],['eBairro','bairro'],['eCidade','cidade'],['eUf','uf']])$(id).value=entrega.end[key];
|
||||||
|
}
|
||||||
|
pintaEntrega();
|
||||||
}
|
}
|
||||||
}catch(error){notice.textContent='Recuperação de carrinho indisponível; o pedido ainda pode ser feito nesta sessão.';}
|
}catch(error){notice.textContent='Recuperação de carrinho indisponível; o pedido ainda pode ser feito nesta sessão.';}
|
||||||
finally{restoring=false;}
|
finally{restoring=false;}
|
||||||
|
|||||||
581
web/checkout.js
@@ -1,7 +1,13 @@
|
|||||||
/* Checkout bridge only: approved commercial functions in web/index.html stay intact. */
|
/* Checkout bridge only: approved commercial functions in web/index.html stay intact.
|
||||||
|
The cart sends the order; the quote is paid on its own page (/pagamento). */
|
||||||
(() => {
|
(() => {
|
||||||
const status = document.getElementById('checkoutStatus');
|
const status = document.getElementById('checkoutStatus');
|
||||||
const actions = document.getElementById('checkoutActions');
|
const actions = document.getElementById('checkoutActions');
|
||||||
|
const resumo = document.getElementById('pagResumo');
|
||||||
|
const naPagina = () => ['pagamento','pix'].includes(document.documentElement.dataset.rota);
|
||||||
|
const naPix = () => document.documentElement.dataset.rota === 'pix';
|
||||||
|
let shownCart = null;
|
||||||
|
let pixClock = null;
|
||||||
let busy = false;
|
let busy = false;
|
||||||
let draftId = localStorage.getItem('dtf-quote');
|
let draftId = localStorage.getItem('dtf-quote');
|
||||||
let requestKey = localStorage.getItem('dtf-request-key');
|
let requestKey = localStorage.getItem('dtf-request-key');
|
||||||
@@ -9,7 +15,7 @@
|
|||||||
let quotedCart = localStorage.getItem('dtf-quote-cart');
|
let quotedCart = localStorage.getItem('dtf-quote-cart');
|
||||||
let refreshVersion = 0;
|
let refreshVersion = 0;
|
||||||
function cartSnapshot() {
|
function cartSnapshot() {
|
||||||
const items=[...pedido,...(itemAtual?[itemAtual]:[])];
|
const items=itensAPagar();
|
||||||
return JSON.stringify({customer:cliente,delivery:entrega,items:items.map(item=>({
|
return JSON.stringify({customer:cliente,delivery:entrega,items:items.map(item=>({
|
||||||
mode:item.modo,metres:item.metros,grade:item.nota,production:item.production,
|
mode:item.modo,metres:item.metros,grade:item.nota,production:item.production,
|
||||||
quality:item.qualityStatus,
|
quality:item.qualityStatus,
|
||||||
@@ -17,12 +23,14 @@
|
|||||||
files:(item.localFiles||[]).map(file=>({name:file.name,size:file.size,lastModified:file.lastModified}))
|
files:(item.localFiles||[]).map(file=>({name:file.name,size:file.size,lastModified:file.lastModified}))
|
||||||
}))});
|
}))});
|
||||||
}
|
}
|
||||||
function clearDraft() {
|
// The quote this browser is paying, forgotten. clearDraft also empties the
|
||||||
|
// payment area; forgetDraft leaves on screen what is there (a confirmation).
|
||||||
|
function forgetDraft() {
|
||||||
draftId=null;quotedCart=null;requestKey=null;requestBody=null;
|
draftId=null;quotedCart=null;requestKey=null;requestBody=null;
|
||||||
for(const key of ['dtf-quote','dtf-quote-cart','dtf-request-key','dtf-request-body'])
|
for(const key of ['dtf-quote','dtf-quote-cart','dtf-request-key','dtf-request-body'])
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
actions.replaceChildren();
|
|
||||||
}
|
}
|
||||||
|
function clearDraft() { forgetDraft(); actions.replaceChildren(); }
|
||||||
const api = async (path, body) => {
|
const api = async (path, body) => {
|
||||||
const response = await fetch('/api'+path, {
|
const response = await fetch('/api'+path, {
|
||||||
credentials: 'same-origin', headers: {'Content-Type':'application/json'},
|
credentials: 'same-origin', headers: {'Content-Type':'application/json'},
|
||||||
@@ -32,51 +40,250 @@
|
|||||||
let data;
|
let data;
|
||||||
try { data = text ? JSON.parse(text) : {}; }
|
try { data = text ? JSON.parse(text) : {}; }
|
||||||
catch (_) { throw new Error(response.ok ? 'Resposta inválida do serviço.' : 'O serviço está indisponível. Tente novamente em instantes.'); }
|
catch (_) { throw new Error(response.ok ? 'Resposta inválida do serviço.' : 'O serviço está indisponível. Tente novamente em instantes.'); }
|
||||||
if (!response.ok) { const error=new Error(typeof data.detail === 'string' ? data.detail : 'Confira os dados do pedido ('+response.status+').');error.status=response.status;throw error; }
|
if (!response.ok) { const error=new Error(problem(data.detail));error.status=response.status;throw error; }
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
// A refusal as one sentence: the server's own, or the first field a form
|
||||||
|
// check named, never a status code.
|
||||||
|
function problem(detail){
|
||||||
|
if (typeof detail === 'string') return detail;
|
||||||
|
// Only the checks written for customers speak Portuguese ("Value error, …").
|
||||||
|
const ours=Array.isArray(detail)&&detail.find(d=>typeof d?.msg==='string'&&d.msg.startsWith('Value error, '));
|
||||||
|
if (ours) return ours.msg.slice('Value error, '.length);
|
||||||
|
return 'Não foi possível concluir. Confira os dados e tente de novo.';
|
||||||
|
}
|
||||||
const ready = api('/session');
|
const ready = api('/session');
|
||||||
ready.then(session=>{
|
ready.then(session=>{
|
||||||
window.dtfUploadMaxBytes=session.max_upload_bytes;
|
window.dtfUploadMaxBytes=session.max_upload_bytes;
|
||||||
const limit=document.getElementById('zLimite');
|
const limit=document.getElementById('zLimite');
|
||||||
if(limit)limit.textContent='Até '+(session.max_upload_bytes/1048576).toFixed(0)+
|
if(limit)limit.textContent='Até '+(session.max_upload_bytes/1073741824).toFixed(0)+' GB por arquivo.';
|
||||||
' MB por arquivo enquanto a verificação de segurança para arquivos grandes é preparada.';
|
|
||||||
}).catch(()=>{});
|
}).catch(()=>{});
|
||||||
window.dtfSessionReady=ready;
|
window.dtfSessionReady=ready;
|
||||||
window.dtfApi=api;
|
window.dtfApi=api;
|
||||||
ready.catch(error => { status.textContent = error.message; });
|
ready.catch(error => { status.textContent = error.message; });
|
||||||
function message(text) { status.textContent = text; }
|
// One message line under the steps. Its tone says what it is: a refusal or
|
||||||
function button(label, handler) {
|
// a missing field is an error (red), a cart that changed a warning, a
|
||||||
|
// created order a confirmation, the rest information.
|
||||||
|
function message(text, tone='info') { status.textContent = text; status.dataset.tone = text ? tone : ''; }
|
||||||
|
function button(label, handler, into = actions, className = 'pagBtn') {
|
||||||
const el = document.createElement('button');
|
const el = document.createElement('button');
|
||||||
|
el.type = 'button';
|
||||||
|
el.className = className;
|
||||||
el.textContent = label;
|
el.textContent = label;
|
||||||
el.style.cssText = 'margin:8px 8px 0 0;padding:8px 14px;cursor:pointer';
|
|
||||||
el.onclick = handler;
|
el.onclick = handler;
|
||||||
actions.append(el);
|
into.append(el);
|
||||||
return el;
|
return el;
|
||||||
}
|
}
|
||||||
async function upload(file) {
|
function node(tag, text, className) {
|
||||||
const session=await ready;
|
const el=document.createElement(tag);
|
||||||
return window.dtfUpload(file,{api,progress:message,scope:session.cart_scope});
|
if (text!=null) el.textContent=text;
|
||||||
|
if (className) el.className=className;
|
||||||
|
return el;
|
||||||
}
|
}
|
||||||
|
// What the customer is paying for, from the server's approval.
|
||||||
|
function pintaResumo(quote) {
|
||||||
|
const approved = quote ? quote.approved : null;
|
||||||
|
resumo.hidden = !approved;
|
||||||
|
if (!approved) { resumo.replaceChildren(); return; }
|
||||||
|
const linha = (label, value, className='l') => {
|
||||||
|
const row=document.createElement('div'); row.className=className;
|
||||||
|
const name=document.createElement('span'); name.textContent=label;
|
||||||
|
const amount=document.createElement('b'); amount.textContent=value;
|
||||||
|
row.append(name,amount); return row;
|
||||||
|
};
|
||||||
|
const rows = approved.items.map(item => linha(
|
||||||
|
(MODOS[item.mode]?.tit || item.mode)+' · '+fmtM(Number(item.billed_metres))+' m', rs(item.total_cents/100)));
|
||||||
|
const pickup = approved.freight.service === 'pickup';
|
||||||
|
const prazo = approved.freight.days ? ' · até '+approved.freight.days+' dias úteis' : '';
|
||||||
|
rows.push(linha(pickup ? 'Retirada em Franca' : 'Frete'+prazo,
|
||||||
|
approved.freight.total_cents ? rs(approved.freight.total_cents/100) : 'Grátis'));
|
||||||
|
rows.push(linha('Total', rs(approved.total_cents/100), 'tot'));
|
||||||
|
resumo.replaceChildren(node('h4','Resumo do pedido'), ...rows);
|
||||||
|
}
|
||||||
|
function esqueletoPagamento() {
|
||||||
|
const linhas=['skel skelLinha','skel skelLinha','skel skelLinha curta'].map(c=>node('div',null,c));
|
||||||
|
resumo.hidden=false; resumo.replaceChildren(node('h4','Resumo do pedido'),...linhas);
|
||||||
|
actions.replaceChildren(node('div',null,'skel skelLinha curta'),node('div',null,'skel skelBloco'));
|
||||||
|
}
|
||||||
|
function semPedido() {
|
||||||
|
pintaResumo(null);
|
||||||
|
actions.replaceChildren();
|
||||||
|
message('Nenhum pedido aguardando pagamento.');
|
||||||
|
button('Ir para o carrinho', () => vaiPara(CARRINHO));
|
||||||
|
}
|
||||||
|
// Files start uploading as soon as they are in the cart, so a sheet of
|
||||||
|
// several GB is on its way while the customer fills in the order. The
|
||||||
|
// checkout waits for whatever is still going.
|
||||||
|
const envios=new Map();
|
||||||
|
const chaveArquivo=f=>[f.name,f.size,f.lastModified].join('|');
|
||||||
|
function enviar(file) {
|
||||||
|
const k=chaveArquivo(file);
|
||||||
|
let e=envios.get(k);
|
||||||
|
if (!e) {
|
||||||
|
e={file, sent:0, done:false, failed:null};
|
||||||
|
e.promise=(async()=>{
|
||||||
|
const session=await ready;
|
||||||
|
return window.dtfUpload(file,{api,scope:session.cart_scope,progress:()=>{},
|
||||||
|
onBytes:n=>{e.sent=n;pintaEnvio();}});
|
||||||
|
})();
|
||||||
|
e.promise.then(()=>{e.done=true;falhas.delete(k);pintaEnvio();},
|
||||||
|
error=>{envios.delete(k);falhas.set(k,{file,error});pintaEnvio();});
|
||||||
|
envios.set(k,e);
|
||||||
|
}
|
||||||
|
return e.promise;
|
||||||
|
}
|
||||||
|
// The Kanban shows each order's artwork from the small picture this page
|
||||||
|
// already made of it (site-upload.js). Never blocks the order: a picture that
|
||||||
|
// fails to go leaves the Kanban with the layout drawing it shows today.
|
||||||
|
const fotosEnviadas=new Set();
|
||||||
|
function enviaFoto(uid, foto) {
|
||||||
|
if (!uid || !foto || fotosEnviadas.has(uid)) return;
|
||||||
|
fotosEnviadas.add(uid);
|
||||||
|
try {
|
||||||
|
const [cabecalho, dados] = foto.split(',');
|
||||||
|
const bin = atob(dados), bytes = new Uint8Array(bin.length);
|
||||||
|
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
||||||
|
fetch('/api/uploads/'+uid+'/thumbnail', {method:'PUT', body:bytes,
|
||||||
|
headers:{'Content-Type':cabecalho.slice(5).split(';')[0]}})
|
||||||
|
.then(r=>{ if (!r.ok) fotosEnviadas.delete(uid); }, ()=>fotosEnviadas.delete(uid));
|
||||||
|
} catch (e) { fotosEnviadas.delete(uid); }
|
||||||
|
}
|
||||||
|
// A failed upload is shown and waits for the customer, never retried behind
|
||||||
|
// their back: a file the check refused would fail again on every change.
|
||||||
|
const falhas=new Map();
|
||||||
|
function tentaDeNovo() {
|
||||||
|
const arquivos=[...falhas.values()].map(f=>f.file);
|
||||||
|
falhas.clear();
|
||||||
|
arquivos.forEach(f=>enviar(f).catch(()=>{}));
|
||||||
|
pintaEnvio();
|
||||||
|
}
|
||||||
|
const arquivosDoCarrinho=()=>[...pedido.filter(it=>it.marcado!==false),...(busy&&itemAtual?[itemAtual]:[])].flatMap(it=>it.localFiles||[]);
|
||||||
|
const gb=n=>(n/1073741824).toLocaleString('pt-BR',{maximumFractionDigits:1})+' GB';
|
||||||
|
const mb=n=>n>=1073741824 ? gb(n) : Math.round(n/1048576)+' MB';
|
||||||
|
// Where the uploads stand, and roughly how long is left from the recent speed.
|
||||||
|
const amostras=[];
|
||||||
|
function estadoEnvio() {
|
||||||
|
const files=arquivosDoCarrinho(); if(!files.length) return null;
|
||||||
|
let total=0, sent=0, pendentes=0;
|
||||||
|
for (const f of files) {
|
||||||
|
const e=envios.get(chaveArquivo(f));
|
||||||
|
total+=f.size; sent+=e ? Math.min(e.sent,f.size) : 0;
|
||||||
|
if (!e || !e.done) pendentes++;
|
||||||
|
}
|
||||||
|
const agora=Date.now();
|
||||||
|
amostras.push([agora,sent]); while(amostras.length>2 && agora-amostras[0][0]>15000) amostras.shift();
|
||||||
|
const [t0,s0]=amostras[0], taxa=agora>t0 ? (sent-s0)/((agora-t0)/1000) : 0;
|
||||||
|
const fase=!pendentes ? 'pronto' : sent>=total ? 'verificando' : 'enviando';
|
||||||
|
return {total, sent, fase, pct:Math.floor(sent/total*100),
|
||||||
|
restante: fase==='enviando' && taxa>0 ? (total-sent)/taxa : null};
|
||||||
|
}
|
||||||
|
const falta=s=>s<60 ? 'falta menos de 1 min' : 'faltam cerca de '+duracao(s);
|
||||||
|
// The time left, measured from the upload's own speed; until there is a
|
||||||
|
// measure, a range from the size.
|
||||||
|
const restante=e=>e.restante!=null ? falta(e.restante) : 'estimativa '+faixaEnvio(e.total-e.sent);
|
||||||
|
function textoEnvio(e) {
|
||||||
|
if (!e) return '';
|
||||||
|
if (e.fase==='pronto') return 'Arquivos enviados';
|
||||||
|
if (e.fase==='verificando') return 'Arquivos enviados · verificando a segurança…';
|
||||||
|
return 'Enviando seus arquivos · '+restante(e);
|
||||||
|
}
|
||||||
|
function pintaEnvio() {
|
||||||
|
const e=estadoEnvio(), texto=textoEnvio(e);
|
||||||
|
const el=document.getElementById('envioArq');
|
||||||
|
const falhou=arquivosDoCarrinho().map(f=>falhas.get(chaveArquivo(f))).filter(Boolean);
|
||||||
|
const topo=document.getElementById('cartEnvio');
|
||||||
|
if (falhou.length) {
|
||||||
|
if (el) {
|
||||||
|
el.replaceChildren(document.createTextNode('Não foi possível enviar '+falhou[0].file.name+': '+
|
||||||
|
(falhou[0].error?.message||'erro no envio').replace(/\.$/,'')+(falhou.length>1?' (e mais '+(falhou.length-1)+')':'')+'. '));
|
||||||
|
const b=document.createElement('button'); b.type='button'; b.className='envioDeNovo'; b.textContent='Tentar de novo';
|
||||||
|
b.onclick=tentaDeNovo; el.append(b);
|
||||||
|
el.classList.remove('ok'); el.classList.add('erro');
|
||||||
|
}
|
||||||
|
if (topo) topo.textContent='· falha no envio';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (el) el.classList.remove('erro');
|
||||||
|
if (el) {
|
||||||
|
el.replaceChildren();
|
||||||
|
if (e && e.fase!=='pronto') {
|
||||||
|
const barra=document.createElement('div'); barra.className='envioBarra';
|
||||||
|
const i=document.createElement('i'); i.style.width=(e.fase==='verificando'?100:e.pct)+'%'; barra.append(i);
|
||||||
|
el.append(barra);
|
||||||
|
}
|
||||||
|
if (e?.fase==='pronto') el.insertAdjacentHTML('beforeend', icone('okCirculo'));
|
||||||
|
if (texto) el.append(document.createTextNode(texto));
|
||||||
|
if (e?.fase==='enviando') {
|
||||||
|
const nota=document.createElement('small'); nota.className='envioNota';
|
||||||
|
nota.textContent='O tempo depende da velocidade da sua internet. Mantenha a página aberta.';
|
||||||
|
el.append(nota);
|
||||||
|
}
|
||||||
|
el.classList.toggle('ok', e?.fase==='pronto');
|
||||||
|
}
|
||||||
|
// Visible on every page while it runs.
|
||||||
|
if (topo) topo.textContent = !e || e.fase==='pronto' ? '' : e.fase==='verificando' ? '· verificando' : e.restante!=null ? '· '+duracao(e.restante) : '· enviando';
|
||||||
|
if (busy && texto) {
|
||||||
|
message(texto);
|
||||||
|
$('bPagarTx').textContent = e && e.fase!=='pronto' ? (e.fase==='verificando' ? 'Verificando os arquivos…' : 'Enviando arquivos… '+restante(e)) : $('bPagarTx').textContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Leaving the page pauses an upload; it resumes, but the customer is warned.
|
||||||
|
window.addEventListener('beforeunload',event=>{
|
||||||
|
const e=estadoEnvio();
|
||||||
|
if (e && e.fase==='enviando') { event.preventDefault(); event.returnValue=''; }
|
||||||
|
});
|
||||||
|
// Only what is in the cart: the item on the product page may still change.
|
||||||
|
window.addEventListener('dtf-cart-changed',()=>{
|
||||||
|
arquivosDoCarrinho().forEach(f=>{ if(!falhas.has(chaveArquivo(f))) enviar(f).catch(()=>{}); });
|
||||||
|
pintaEnvio();
|
||||||
|
});
|
||||||
|
async function upload(file) {
|
||||||
|
return enviar(file);
|
||||||
|
}
|
||||||
|
// The cart's package: billed metres and value. The charged freight is
|
||||||
|
// quoted again by the server from the approved items.
|
||||||
|
const pacote = () => {
|
||||||
|
const items=itensAPagar();
|
||||||
|
return {metres:items.reduce((t,it)=>t+(it.cob||0),0), cents:Math.round(items.reduce((t,it)=>t+(it.total||0),0)*100)};
|
||||||
|
};
|
||||||
|
let freightQuoted = null;
|
||||||
window.dtfFreight = async () => {
|
window.dtfFreight = async () => {
|
||||||
const cep = entrega.cep;
|
const cep = entrega.cep;
|
||||||
|
const service = (await ready).freight_service;
|
||||||
|
if (!service) {
|
||||||
|
$('cepMsg').textContent = 'A entrega ainda não está disponível. Escolha a retirada em Franca.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const {metres, cents} = pacote();
|
||||||
|
if (!metres) { $('cepMsg').textContent = 'Adicione um item ao pedido para cotar o frete.'; return; }
|
||||||
|
$('cepMsg').textContent = 'Cotando o frete…';
|
||||||
try {
|
try {
|
||||||
const result = await api('/freight',{service:'mock-standard',postal_code:cep});
|
const result = await api('/freight',{service,postal_code:cep,metres:metres.toFixed(2),declared_cents:cents});
|
||||||
if (entrega.cep !== cep || entrega.tipo !== 'frete') return;
|
if (entrega.cep !== cep || entrega.tipo !== 'frete') return;
|
||||||
entrega.valor = result.total_cents/100;
|
entrega.valor = result.total_cents/100;
|
||||||
|
entrega.dias = result.days || null;
|
||||||
entrega.cotado = true;
|
entrega.cotado = true;
|
||||||
$('cepMsg').textContent = 'Frete estimado: '+rs(entrega.valor)+'.';
|
freightQuoted = JSON.stringify(pacote());
|
||||||
|
$('cepMsg').textContent = '';
|
||||||
pintaEntrega();
|
pintaEntrega();
|
||||||
} catch(error) { $('cepMsg').textContent = error.message; }
|
} catch(error) { $('cepMsg').textContent = error.message; }
|
||||||
};
|
};
|
||||||
|
// A different package is a different freight: quote it again.
|
||||||
|
window.addEventListener('dtf-cart-changed',()=>{
|
||||||
|
if (entrega.tipo==='frete' && entrega.cotado && freightQuoted && freightQuoted!==JSON.stringify(pacote())) {
|
||||||
|
entrega.cotado=false; freightQuoted=null; window.dtfFreight();
|
||||||
|
}
|
||||||
|
});
|
||||||
window.dtfCheckout = async () => {
|
window.dtfCheckout = async () => {
|
||||||
if (busy) return;
|
if (busy) return;
|
||||||
if (draftId) { await refresh(); status.scrollIntoView({behavior:'smooth',block:'nearest'}); return; }
|
// The cart that was sent goes straight to its payment; a changed cart is sent again.
|
||||||
|
if (draftId && quotedCart === cartSnapshot()) { await refresh(true); return; }
|
||||||
|
if (draftId) clearDraft();
|
||||||
if (!clienteOk() || !entrega.cotado) return;
|
if (!clienteOk() || !entrega.cotado) return;
|
||||||
if (!enderecoOk()) return message('Preencha o endereço de entrega.');
|
if (!enderecoOk()) return message('Preencha o endereço de entrega.','error');
|
||||||
if (!cartPodeEnviar()) return message('Revise a qualidade e confirme a ressalva de cada item antes de enviar o pedido.');
|
if (!cartPodeEnviar()) return message('Revise a qualidade e confirme a ressalva de cada item antes de enviar o pedido.','error');
|
||||||
const cart = [...pedido,...(itemAtual?[itemAtual]:[])];
|
const cart = itensAPagar();
|
||||||
if (!cart.length) return message('Adicione um item ao pedido.');
|
if (!cart.length) return message('Adicione um item ao pedido.','error');
|
||||||
const initialCart=cartSnapshot();
|
const initialCart=cartSnapshot();
|
||||||
busy = true;
|
busy = true;
|
||||||
$('bPagar').disabled = true;
|
$('bPagar').disabled = true;
|
||||||
@@ -84,10 +291,14 @@
|
|||||||
await ready;
|
await ready;
|
||||||
if((await api('/session')).cart_scope !== (await ready).cart_scope) throw new Error('Sua conta ou sessão mudou. Recarregue a página antes de enviar o carrinho.');
|
if((await api('/session')).cart_scope !== (await ready).cart_scope) throw new Error('Sua conta ou sessão mudou. Recarregue a página antes de enviar o carrinho.');
|
||||||
const items=[];
|
const items=[];
|
||||||
|
pintaEnvio();
|
||||||
for (const item of cart) {
|
for (const item of cart) {
|
||||||
if (!item.localFiles?.length) throw new Error('Selecione novamente os arquivos deste item.');
|
if (!item.localFiles?.length) throw new Error('Selecione novamente os arquivos deste item.');
|
||||||
const uploads=[];
|
const uploads=[];
|
||||||
for (const file of item.localFiles) uploads.push(await upload(file));
|
for (const [index,file] of item.localFiles.entries()) {
|
||||||
|
uploads.push(await upload(file));
|
||||||
|
enviaFoto(uploads[index], item.fotos?.[index]);
|
||||||
|
}
|
||||||
if (item.production?.version!==2 || item.production.sources?.length!==uploads.length)
|
if (item.production?.version!==2 || item.production.sources?.length!==uploads.length)
|
||||||
throw new Error('A montagem deste item precisa ser refeita antes da cotação.');
|
throw new Error('A montagem deste item precisa ser refeita antes da cotação.');
|
||||||
items.push({mode:item.modo, metres:String(item.metros), grade:item.nota, uploads,
|
items.push({mode:item.modo, metres:String(item.metros), grade:item.nota, uploads,
|
||||||
@@ -95,7 +306,9 @@
|
|||||||
upload_id:uploads[index],...source}))},
|
upload_id:uploads[index],...source}))},
|
||||||
quality_status:item.qualityStatus,quality_acknowledged:item.qualityAcknowledged});
|
quality_status:item.qualityStatus,quality_acknowledged:item.qualityAcknowledged});
|
||||||
}
|
}
|
||||||
const content = {customer:{...cliente},items,freight:{service:entrega.tipo==='retira'?'pickup':'mock-standard',postal_code:entrega.tipo==='retira'?'':entrega.cep}};
|
const service = entrega.tipo==='retira' ? 'pickup' : (await ready).freight_service;
|
||||||
|
if (!service) throw new Error('A entrega ainda não está disponível. Escolha a retirada em Franca.');
|
||||||
|
const content = {customer:{...cliente},items,freight:{service,postal_code:entrega.tipo==='retira'?'':entrega.cep}};
|
||||||
const destination = destinoApi();
|
const destination = destinoApi();
|
||||||
if (destination) content.destination = destination;
|
if (destination) content.destination = destination;
|
||||||
if (cartSnapshot()!==initialCart) throw new Error('O carrinho mudou durante o envio. Confira os itens e envie de novo.');
|
if (cartSnapshot()!==initialCart) throw new Error('O carrinho mudou durante o envio. Confira os itens e envie de novo.');
|
||||||
@@ -108,108 +321,222 @@
|
|||||||
const quote = await api('/quotes',{request_key:requestKey,...content});
|
const quote = await api('/quotes',{request_key:requestKey,...content});
|
||||||
quotedCart=initialCart;localStorage.setItem('dtf-quote-cart',quotedCart);
|
quotedCart=initialCart;localStorage.setItem('dtf-quote-cart',quotedCart);
|
||||||
draftId=quote.id; localStorage.setItem('dtf-quote',draftId);
|
draftId=quote.id; localStorage.setItem('dtf-quote',draftId);
|
||||||
await refresh();
|
await refresh(true);
|
||||||
status.scrollIntoView({behavior:'smooth',block:'nearest'});
|
} catch(error) { message(error.message,'error'); }
|
||||||
} catch(error) { message(error.message); }
|
|
||||||
finally { busy=false; pintaEntrega(); }
|
finally { busy=false; pintaEntrega(); }
|
||||||
};
|
};
|
||||||
async function refresh() {
|
// `go`: the customer asked to pay, so the cart page moves to the payment page.
|
||||||
if (!draftId) return;
|
async function refresh(go) {
|
||||||
|
if (!naPagina()) { if (!go || !draftId) return; }
|
||||||
|
else if (!draftId) { semPedido(); return; }
|
||||||
const version=++refreshVersion, shownId=draftId;
|
const version=++refreshVersion, shownId=draftId;
|
||||||
|
if (naPagina() && !actions.children.length) esqueletoPagamento();
|
||||||
try {
|
try {
|
||||||
await ready;
|
await ready;
|
||||||
const quote=await api('/quotes/'+shownId);
|
const quote=await api('/quotes/'+shownId);
|
||||||
if(version!==refreshVersion || draftId!==shownId) return;
|
if(version!==refreshVersion || draftId!==shownId) return;
|
||||||
|
// Asked to pay a cart identical to one already paid (the same test
|
||||||
|
// repeated): that is a new order, never the paid one shown again. The
|
||||||
|
// confirmation itself keeps its quote, so it stays on screen.
|
||||||
|
if (go && quote.status==='paid') { forgetDraft(); return window.dtfCheckout(); }
|
||||||
|
if (!naPagina()) { message(''); vaiPara(PAGAMENTO); return; }
|
||||||
|
shownCart=cartSnapshot();
|
||||||
|
clearInterval(pixClock);
|
||||||
|
unmountCard();
|
||||||
actions.replaceChildren();
|
actions.replaceChildren();
|
||||||
if (quote.status!=='paid' && (!quotedCart || quotedCart!==cartSnapshot())) {
|
message('');
|
||||||
message('O carrinho mudou ou não está disponível neste navegador. A cotação anterior continua separada; envie o carrinho atual para uma nova revisão.');
|
pintaResumo(quote);
|
||||||
|
document.querySelector('.pagVolta').hidden = quote.status==='paid';
|
||||||
|
if (quote.status==='paid') { confirmado(quote); return; }
|
||||||
|
if (naPix() && quote.status!=='approved') { vaiPara(PAGAMENTO); return; }
|
||||||
|
if (!quotedCart || quotedCart!==cartSnapshot()) {
|
||||||
|
message('O carrinho mudou ou não está disponível neste navegador. A cotação anterior continua separada; envie o carrinho atual para uma nova revisão.','warn');
|
||||||
button('Enviar carrinho atual',()=>{clearDraft();window.dtfCheckout();});
|
button('Enviar carrinho atual',()=>{clearDraft();window.dtfCheckout();});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (quote.status==='pending_review') {
|
if (quote.status==='pending_review') {
|
||||||
message('Arquivos enviados. No Kanban, confira metragem e nota e aprove a cotação '+draftId.slice(0,8)+'.');
|
message((await ready).environment==='local'
|
||||||
|
? 'Arquivos enviados. No Kanban, confira metragem e nota e aprove a cotação '+draftId.slice(0,8)+'.'
|
||||||
|
: 'Arquivos enviados. Nossa equipe está conferindo a cotação '+draftId.slice(0,8)+'; o valor final aparece aqui em seguida.');
|
||||||
} else if (quote.status==='approved') {
|
} else if (quote.status==='approved') {
|
||||||
message('Total validado no servidor: '+rs(quote.approved.total_cents/100)+' · inclui frete. Cotação válida por 24 horas.');
|
|
||||||
if ((await ready).payment_provider === 'mercadopago') {
|
if ((await ready).payment_provider === 'mercadopago') {
|
||||||
button('Pagar com PIX',async event=>{
|
if (naPix()) await paginaPix(quote, version);
|
||||||
if (!quotedCart || quotedCart!==cartSnapshot()) { await refresh(); return; }
|
else await escolhaPagamento(quote, version);
|
||||||
event.target.disabled=true;
|
|
||||||
try { showPix(await api('/payments/intent',{quote_id:draftId,method:{type:'pix'}})); }
|
|
||||||
catch(error) { message(error.message); event.target.disabled=false; }
|
|
||||||
});
|
|
||||||
if ((await ready).payment_public_key) {
|
|
||||||
button('Pagar com cartão',async event=>{
|
|
||||||
if (!quotedCart || quotedCart!==cartSnapshot()) { await refresh(); return; }
|
|
||||||
event.target.disabled=true;
|
|
||||||
try { await showCard(quote.approved.total_cents); }
|
|
||||||
catch(error) { message(error.message); event.target.disabled=false; }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((await ready).environment !== 'local') {
|
if ((await ready).environment !== 'local') {
|
||||||
message('Cotação revisada. O pagamento online ainda não está disponível.');
|
message('Cotação revisada. O pagamento online ainda não está disponível.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Local stack only: the simulated payment.
|
||||||
|
message('Total validado no servidor: '+rs(quote.approved.total_cents/100)+' · inclui frete. Cotação válida por 24 horas.');
|
||||||
button('Criar pedido de teste',async event=>{
|
button('Criar pedido de teste',async event=>{
|
||||||
if (!quotedCart || quotedCart!==cartSnapshot()) { await refresh(); return; }
|
if (!quotedCart || quotedCart!==cartSnapshot()) { await refresh(); return; }
|
||||||
event.target.disabled=true;
|
event.target.disabled=true;
|
||||||
try {
|
try {
|
||||||
const order=await api('/orders/dev-paid',{quote_id:draftId});
|
const order=await api('/orders/dev-paid',{quote_id:draftId});
|
||||||
pedido=[]; itemAtual=null; limpaPaineis();
|
await pagos();
|
||||||
await window.dtfClearCart?.();
|
message('Pedido #'+order.number+' criado e disponível no Kanban.','ok');
|
||||||
message('Pedido #'+order.number+' criado e disponível no Kanban.');
|
|
||||||
await refresh();
|
await refresh();
|
||||||
} catch(error) { message(error.message); event.target.disabled=false; }
|
} catch(error) { message(error.message,'error'); event.target.disabled=false; }
|
||||||
});
|
});
|
||||||
} else if (quote.status==='paid') {
|
|
||||||
message('Pedido #'+quote.order.number+' · etapa: '+quote.order.state+'.');
|
|
||||||
button('Novo pedido',()=>{clearDraft();location.reload();});
|
|
||||||
} else {
|
} else {
|
||||||
message('Cotação expirada. Envie o carrinho para uma nova revisão.');
|
message(quote.status==='discarded'
|
||||||
button('Nova cotação',clearDraft);
|
? 'Esta cotação foi cancelada pela nossa equipe. Envie o carrinho de novo ou fale com a gente.'
|
||||||
|
: 'Cotação expirada. Envie o carrinho de novo para pagar.','warn');
|
||||||
|
button('Voltar ao carrinho',()=>{clearDraft();vaiPara(CARRINHO);});
|
||||||
}
|
}
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
if(version!==refreshVersion || draftId!==shownId) return;
|
if(version!==refreshVersion || draftId!==shownId) return;
|
||||||
message(error.message);
|
message(error.message,'error');
|
||||||
actions.replaceChildren();
|
actions.replaceChildren();
|
||||||
button('Limpar referência e tentar de novo',clearDraft);
|
button('Limpar referência e tentar de novo',clearDraft);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// PIX: the provider's QR code and copy-and-paste code. The order is created
|
// The order is created by Mercado Pago's notification, never by this page,
|
||||||
// by the provider's notification, not by this page, so the page only waits.
|
// so after paying the page only waits for the order to exist.
|
||||||
let pixTimer=null;
|
let waitTimer=null;
|
||||||
function showPix(intent) {
|
function waitForOrder() {
|
||||||
actions.replaceChildren();
|
clearInterval(waitTimer);
|
||||||
if (!intent.pix_qr_code) { message('Não foi possível gerar o PIX. Tente de novo em instantes.'); return; }
|
waitTimer=setInterval(async()=>{
|
||||||
message('Pague o PIX de '+rs(intent.total_cents/100)+'. O pedido entra na produção assim que o pagamento for confirmado.');
|
|
||||||
if (intent.pix_qr_code_base64) {
|
|
||||||
const img=document.createElement('img');
|
|
||||||
img.src='data:image/png;base64,'+intent.pix_qr_code_base64;
|
|
||||||
img.alt='QR code do PIX';img.width=220;img.height=220;img.style.display='block';
|
|
||||||
actions.append(img);
|
|
||||||
}
|
|
||||||
const code=document.createElement('input');
|
|
||||||
code.readOnly=true;code.value=intent.pix_qr_code;code.style.cssText='width:100%;margin-top:8px;padding:8px';
|
|
||||||
actions.append(code);
|
|
||||||
button('Copiar código PIX',async()=>{ try{ await navigator.clipboard.writeText(intent.pix_qr_code); message('Código copiado.'); }catch(_){ code.select(); } });
|
|
||||||
clearInterval(pixTimer);
|
|
||||||
pixTimer=setInterval(async()=>{
|
|
||||||
try {
|
try {
|
||||||
const quote=await api('/quotes/'+draftId);
|
const quote=await api('/quotes/'+draftId);
|
||||||
if (quote.status==='paid') {
|
if (quote.status==='paid') {
|
||||||
clearInterval(pixTimer);
|
clearInterval(waitTimer);
|
||||||
pedido=[]; itemAtual=null; limpaPaineis();
|
await pagos();
|
||||||
await window.dtfClearCart?.();
|
|
||||||
await refresh();
|
await refresh();
|
||||||
|
} else if (quote.payment?.status==='rejected') {
|
||||||
|
// Refused after the bank's confirmation or the provider's review.
|
||||||
|
clearInterval(waitTimer);
|
||||||
|
await refresh();
|
||||||
|
message(quote.payment.message||'O pagamento foi recusado. Tente de novo ou escolha outra forma de pagamento.','error');
|
||||||
}
|
}
|
||||||
} catch(_) {}
|
} catch(_) {}
|
||||||
},5000);
|
},3000);
|
||||||
}
|
}
|
||||||
|
// 3-D Secure: the bank's own page, opened by posting its request in a frame.
|
||||||
|
function showChallenge(challenge, into) {
|
||||||
|
unmountCard();
|
||||||
|
const frame=document.createElement('iframe');
|
||||||
|
frame.name='confirmacaoBanco'; frame.title='Confirmação do banco'; frame.className='desafio';
|
||||||
|
const form=document.createElement('form');
|
||||||
|
form.method='POST'; form.action=challenge.url; form.target=frame.name;
|
||||||
|
const input=document.createElement('input');
|
||||||
|
input.type='hidden'; input.name='creq'; input.value=challenge.creq;
|
||||||
|
form.append(input);
|
||||||
|
into.replaceChildren(node('p','Confirme o pagamento na página do seu banco, abaixo.','pagNota'),frame,form);
|
||||||
|
form.submit();
|
||||||
|
form.remove();
|
||||||
|
}
|
||||||
|
function confirmado(quote) {
|
||||||
|
clearInterval(waitTimer);
|
||||||
|
const box=node('div',null,'confirmado');
|
||||||
|
const titulo=node('h4','Pagamento confirmado');
|
||||||
|
titulo.insertAdjacentHTML('afterbegin', icone('okCirculo'));
|
||||||
|
box.append(titulo,
|
||||||
|
node('p','Recebemos seu pedido e ele já está na produção. Você acompanha cada etapa em Meus pedidos.'));
|
||||||
|
button('Ver meus pedidos',()=>location.assign('/conta/pedidos?order='+quote.order.id),box);
|
||||||
|
actions.append(box);
|
||||||
|
}
|
||||||
|
const cartOk=()=>!!quotedCart && quotedCart===cartSnapshot();
|
||||||
|
// What was paid leaves the cart; unchecked items stay for another order.
|
||||||
|
async function pagos() {
|
||||||
|
pedido=pedido.filter(it=>it.marcado===false); itemAtual=null; limpaPaineis();
|
||||||
|
if (pedido.length) pintaPedido(); else await window.dtfClearCart?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Paying: card (the default, paid on this page) or PIX (paid on its own page).
|
||||||
|
async function escolhaPagamento(quote, version) {
|
||||||
|
const session=await ready;
|
||||||
|
// Credit card and PIX: the store does not take debit cards.
|
||||||
|
const opcoes=[...(session.payment_public_key?[['credito','Cartão de crédito']]:[]),['pix','PIX']];
|
||||||
|
let escolhido=opcoes[0][0];
|
||||||
|
const grupo=node('div',null,'metodos');
|
||||||
|
grupo.setAttribute('role','radiogroup'); grupo.setAttribute('aria-label','Forma de pagamento');
|
||||||
|
const area=node('div',null,'area');
|
||||||
|
const radios=opcoes.map(([key,label])=>{
|
||||||
|
const el=button(label,()=>{ if(escolhido!==key){ escolhido=key; pinta(); } },grupo,'metodo');
|
||||||
|
el.setAttribute('role','radio'); el.dataset.metodo=key;
|
||||||
|
el.insertAdjacentHTML('afterbegin', icone(key==='pix'?'qrcode':'cartao'));
|
||||||
|
return el;
|
||||||
|
});
|
||||||
|
actions.append(node('h4','Forma de pagamento'),grupo,area);
|
||||||
|
async function pinta() {
|
||||||
|
radios.forEach(el=>el.setAttribute('aria-checked',String(el.dataset.metodo===escolhido)));
|
||||||
|
unmountCard(); area.replaceChildren(); message('');
|
||||||
|
if (escolhido!=='pix') { await showCard(quote.approved, area, version); return; }
|
||||||
|
area.append(node('p','Clique em Pagar para gerar o QR code do PIX.','pagNota'));
|
||||||
|
button('Pagar',async event=>{
|
||||||
|
if (!cartOk()) { await refresh(); return; }
|
||||||
|
event.target.disabled=true;
|
||||||
|
try {
|
||||||
|
await api('/payments/intent',{quote_id:draftId,method:{type:'pix'}});
|
||||||
|
vaiPara(PAGAMENTO_PIX);
|
||||||
|
} catch(error) { message(error.message,'error'); event.target.disabled=false; }
|
||||||
|
},area);
|
||||||
|
}
|
||||||
|
await pinta();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The PIX page. The intent is idempotent on the quote, so reopening this
|
||||||
|
// page shows the same QR code instead of creating another charge.
|
||||||
|
async function paginaPix(quote, version) {
|
||||||
|
let intent;
|
||||||
|
try { intent=await api('/payments/intent',{quote_id:draftId,method:{type:'pix'}}); }
|
||||||
|
catch(error) {
|
||||||
|
if (version!==refreshVersion) return;
|
||||||
|
message(error.message,'error');
|
||||||
|
button('Escolher outra forma de pagamento',()=>vaiPara(PAGAMENTO),actions,'pagBtn sec');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (version!==refreshVersion) return;
|
||||||
|
if (!intent.pix_qr_code) { message('Não foi possível gerar o PIX. Tente de novo em instantes.','error'); return; }
|
||||||
|
const box=node('div',null,'pixBox');
|
||||||
|
box.append(node('h4','Pague com PIX'),
|
||||||
|
node('p','Abra o app do seu banco, escolha pagar com PIX e leia o QR code ou cole o código abaixo.','pagNota'));
|
||||||
|
if (intent.pix_qr_code_base64) {
|
||||||
|
const img=document.createElement('img');
|
||||||
|
img.src='data:image/png;base64,'+intent.pix_qr_code_base64;
|
||||||
|
img.alt='QR code do PIX';img.width=240;img.height=240;
|
||||||
|
box.append(img);
|
||||||
|
}
|
||||||
|
const linha=node('div',null,'codigo');
|
||||||
|
const code=document.createElement('input');
|
||||||
|
code.readOnly=true;code.value=intent.pix_qr_code;code.setAttribute('aria-label','Código PIX copia e cola');
|
||||||
|
linha.append(code);
|
||||||
|
const copiar=button('Copiar código',async()=>{
|
||||||
|
try{ await navigator.clipboard.writeText(intent.pix_qr_code);
|
||||||
|
copiar.replaceChildren(); copiar.insertAdjacentHTML('beforeend', icone('ok')+'<span>Código copiado</span>'); }
|
||||||
|
catch(_){ code.select(); }
|
||||||
|
},linha);
|
||||||
|
copiar.insertAdjacentHTML('afterbegin', icone('copiar'));
|
||||||
|
const prazo=node('p',null,'prazo');
|
||||||
|
prazo.insertAdjacentHTML('afterbegin', icone('relogio'));
|
||||||
|
const prazoTx=node('span'); prazo.append(prazoTx);
|
||||||
|
box.append(linha,prazo,node('p','Aguardando a confirmação do pagamento…','aguarda'));
|
||||||
|
actions.append(box);
|
||||||
|
waitForOrder();
|
||||||
|
// The code stops working when it expires; a new one is a click away.
|
||||||
|
const fim=Date.parse(intent.expires_at);
|
||||||
|
if (!Number.isFinite(fim)) { prazo.remove(); return; }
|
||||||
|
const tick=()=>{
|
||||||
|
const s=Math.max(0,Math.ceil((fim-Date.now())/1000));
|
||||||
|
prazoTx.textContent='Pague em '+String(Math.floor(s/60)).padStart(2,'0')+':'+String(s%60).padStart(2,'0');
|
||||||
|
if (s>0) return;
|
||||||
|
clearInterval(pixClock);
|
||||||
|
const fimBox=node('div',null,'pixBox');
|
||||||
|
fimBox.append(node('h4','O código PIX expirou'),
|
||||||
|
node('p','Gere um novo código para pagar. O anterior não pode mais ser pago.','pagNota'));
|
||||||
|
button('Gerar novo PIX',()=>refresh(),fimBox);
|
||||||
|
actions.replaceChildren(fimBox);
|
||||||
|
};
|
||||||
|
tick();
|
||||||
|
pixClock=setInterval(tick,1000);
|
||||||
|
}
|
||||||
|
|
||||||
// Card: Mercado Pago's own form (Card Payment Brick). The card is typed into
|
// Card: Mercado Pago's own form (Card Payment Brick). The card is typed into
|
||||||
// Mercado Pago's secure fields and becomes a one-time token; the number never
|
// Mercado Pago's secure fields and becomes a one-time token; the number never
|
||||||
// reaches this page's code or our server. As with PIX, the order is created
|
// reaches this page's code or our server.
|
||||||
// by the provider's notification, so the page only waits for it.
|
|
||||||
let sdkLoading=null;
|
let sdkLoading=null;
|
||||||
function loadMercadoPago() {
|
function loadMercadoPago() {
|
||||||
if (window.MercadoPago) return Promise.resolve();
|
if (window.MercadoPago) return Promise.resolve();
|
||||||
@@ -222,62 +549,84 @@
|
|||||||
});
|
});
|
||||||
return sdkLoading;
|
return sdkLoading;
|
||||||
}
|
}
|
||||||
|
// The cardholder's document as the card form collected it.
|
||||||
|
function cardholder(id) {
|
||||||
|
const number=String(id?.number||'').replace(/\D/g,'');
|
||||||
|
const type=String(id?.type||'').toUpperCase();
|
||||||
|
return ['CPF','CNPJ'].includes(type) && /^[0-9]{11,14}$/.test(number)
|
||||||
|
? {payer_document_type:type, payer_document:number} : {};
|
||||||
|
}
|
||||||
let cardBrick=null;
|
let cardBrick=null;
|
||||||
async function showCard(totalCents) {
|
function unmountCard() {
|
||||||
await loadMercadoPago();
|
if (cardBrick) { try { cardBrick.unmount(); } catch(_) {} cardBrick=null; }
|
||||||
const session=await ready;
|
}
|
||||||
actions.replaceChildren();
|
async function showCard(approved, into, version) {
|
||||||
const holder=document.createElement('div');
|
const holder=node('div');
|
||||||
holder.id='cardPaymentBrick';
|
holder.id='cardPaymentBrick';
|
||||||
holder.style.cssText='max-width:520px;margin-top:8px';
|
const espera=node('div',null,'skel skelBloco');
|
||||||
actions.append(holder);
|
into.append(espera,holder);
|
||||||
message('Pagamento com cartão: '+rs(totalCents/100)+'.');
|
try { await loadMercadoPago(); }
|
||||||
if (cardBrick) { try { await cardBrick.unmount(); } catch(_) {} }
|
catch(error) { espera.remove(); message(error.message,'error'); return; }
|
||||||
|
const session=await ready;
|
||||||
|
// The customer may have switched to PIX, or the page re-rendered, meanwhile.
|
||||||
|
if (!holder.isConnected || version!==refreshVersion) return;
|
||||||
|
unmountCard();
|
||||||
const mp=new window.MercadoPago(session.payment_public_key,{locale:'pt-BR'});
|
const mp=new window.MercadoPago(session.payment_public_key,{locale:'pt-BR'});
|
||||||
cardBrick=await mp.bricks().create('cardPayment','cardPaymentBrick',{
|
cardBrick=await mp.bricks().create('cardPayment','cardPaymentBrick',{
|
||||||
initialization:{amount:totalCents/100, payer:{email:cliente.mail}},
|
initialization:{amount:approved.total_cents/100, payer:{email:approved.customer.mail}},
|
||||||
customization:{paymentMethods:{maxInstallments:12}},
|
// Credit cards only: the store does not take debit.
|
||||||
|
customization:{paymentMethods:{maxInstallments:12,types:{excluded:['debit_card']}},
|
||||||
|
// The option above already names the card; the form's own title
|
||||||
|
// ("crédito ou débito") would contradict it.
|
||||||
|
visual:{hideFormTitle:true,
|
||||||
|
style:{customVariables:{baseColor:'#FFA81A',buttonTextColor:'#03060B'}}}},
|
||||||
callbacks:{
|
callbacks:{
|
||||||
onReady:()=>{},
|
onReady:()=>{ espera.remove(); },
|
||||||
onError:error=>{ console.error(error); message('Erro no formulário do cartão. Confira os dados e tente de novo.'); },
|
onError:error=>{ console.error(error); message('Erro no formulário do cartão. Confira os dados e tente de novo.','error'); },
|
||||||
onSubmit:async data=>{
|
onSubmit:async data=>{
|
||||||
const result=await api('/payments/intent',{quote_id:draftId,method:{
|
if (!cartOk()) { await refresh(); throw new Error('cart changed'); }
|
||||||
|
message('');
|
||||||
|
let result;
|
||||||
|
try {
|
||||||
|
result=await api('/payments/intent',{quote_id:draftId,method:{
|
||||||
type:'card', token:data.token, payment_method_id:data.payment_method_id,
|
type:'card', token:data.token, payment_method_id:data.payment_method_id,
|
||||||
installments:Number(data.installments)||1,
|
installments:Number(data.installments)||1,
|
||||||
issuer_id:data.issuer_id==null?null:String(data.issuer_id)}});
|
issuer_id:data.issuer_id==null?null:String(data.issuer_id),
|
||||||
|
...cardholder(data.payer?.identification)}});
|
||||||
|
} catch(error) {
|
||||||
|
// Rejecting stops the form's spinner; the reason is shown above it.
|
||||||
|
message(error.message || 'Não foi possível concluir o pagamento. Tente de novo.','error');
|
||||||
|
status.scrollIntoView({behavior:'smooth',block:'center'});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
if (result.challenge) { showChallenge(result.challenge, into); waitForOrder(); return; }
|
||||||
if (result.status==='approved' || result.status==='pending') {
|
if (result.status==='approved' || result.status==='pending') {
|
||||||
message(result.status==='approved'
|
unmountCard();
|
||||||
? 'Pagamento aprovado. Seu pedido entra na produção em instantes.'
|
into.replaceChildren(node('p',result.status==='approved'
|
||||||
: 'Pagamento em análise pelo Mercado Pago. Avisamos assim que for confirmado.');
|
? 'Pagamento aprovado. Confirmando seu pedido…'
|
||||||
|
: 'Pagamento em análise pelo Mercado Pago. Esta página atualiza assim que ele for confirmado.','pagNota'));
|
||||||
waitForOrder();
|
waitForOrder();
|
||||||
} else {
|
} else {
|
||||||
message('Pagamento recusado pelo Mercado Pago ('+(result.status_detail||result.status)+'). '+
|
message(result.message||'O pagamento foi recusado. Confira os dados ou use outro cartão ou pague com PIX.','error');
|
||||||
'Confira os dados ou use outro cartão.');
|
|
||||||
throw new Error('rejected');
|
throw new Error('rejected');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
if (!holder.isConnected || version!==refreshVersion) unmountCard();
|
||||||
function waitForOrder() {
|
|
||||||
clearInterval(pixTimer);
|
|
||||||
pixTimer=setInterval(async()=>{
|
|
||||||
try {
|
|
||||||
const quote=await api('/quotes/'+draftId);
|
|
||||||
if (quote.status==='paid') {
|
|
||||||
clearInterval(pixTimer);
|
|
||||||
pedido=[]; itemAtual=null; limpaPaineis();
|
|
||||||
await window.dtfClearCart?.();
|
|
||||||
await refresh();
|
|
||||||
}
|
|
||||||
} catch(_) {}
|
|
||||||
},5000);
|
|
||||||
}
|
}
|
||||||
const quoteFromPortal=new URLSearchParams(location.search).get('quote');
|
const quoteFromPortal=new URLSearchParams(location.search).get('quote');
|
||||||
if(quoteFromPortal && /^[0-9a-f-]{36}$/.test(quoteFromPortal)){
|
if(quoteFromPortal && /^[0-9a-f-]{36}$/.test(quoteFromPortal)){
|
||||||
if(draftId!==quoteFromPortal){quotedCart=null;localStorage.removeItem('dtf-quote-cart');}
|
if(draftId!==quoteFromPortal){quotedCart=null;localStorage.removeItem('dtf-quote-cart');}
|
||||||
draftId=quoteFromPortal;localStorage.setItem('dtf-quote',draftId);
|
draftId=quoteFromPortal;localStorage.setItem('dtf-quote',draftId);
|
||||||
}
|
}
|
||||||
window.addEventListener('dtf-cart-changed',()=>{if(draftId) refresh();});
|
// Re-render the payment page only when the cart really changed: re-rendering
|
||||||
|
// would remount the card form under the customer's typing.
|
||||||
|
window.addEventListener('dtf-cart-changed',()=>{if(draftId && naPagina() && shownCart!==cartSnapshot()) refresh();});
|
||||||
|
// The cart page keeps only the sending progress and errors, never a payment.
|
||||||
|
window.addEventListener('dtf-page-changed',()=>{
|
||||||
|
if (naPagina()) refresh();
|
||||||
|
else if (!busy) { refreshVersion++; clearInterval(pixClock); message(''); actions.replaceChildren(); pintaResumo(null); }
|
||||||
|
});
|
||||||
refresh();
|
refresh();
|
||||||
})();
|
})();
|
||||||
|
|||||||
971
web/index.html
424
web/kanban.css
Normal file
@@ -0,0 +1,424 @@
|
|||||||
|
/* Kanban DTF. The previous look is kept in git tag ui-v1. */
|
||||||
|
:root{
|
||||||
|
--bg:#0F1113;--bar:#13161A;--col:#13161A;--card:#1A1D21;--card2:#23282D;--panel:#15181B;
|
||||||
|
--line:#262A2F;--line2:#2A2F35;--line3:#3A4047;
|
||||||
|
--tx:#ECE9E4;--tx2:#D5D2CC;--muted:#A3A9B0;--faint:#8B929A;
|
||||||
|
--accent:#FF8B2B;--accent-tx:#FFB575;--ok:#6FD19C;--warn:#F2B33D;--bad:#F4968B;--bad-line:#5A3530;
|
||||||
|
--rec:#2BB6D9;--tra:#E0508F;--fil:#E9C23A;--imp:#D9D6D0;--cor:#F06A5B;--fin:#3FB97A;
|
||||||
|
--mono:Manrope,system-ui,sans-serif;
|
||||||
|
}
|
||||||
|
*{box-sizing:border-box}
|
||||||
|
html,body{margin:0;background:var(--bg);color:var(--tx)}
|
||||||
|
body{font:14px/1.45 Manrope,system-ui,sans-serif;font-variant-numeric:tabular-nums}
|
||||||
|
button,input,textarea{font:inherit;color:inherit}
|
||||||
|
button{cursor:pointer}
|
||||||
|
button:disabled{opacity:.5;cursor:default}
|
||||||
|
a{color:var(--accent-tx)}
|
||||||
|
[hidden]{display:none!important}
|
||||||
|
.mono{font-family:var(--mono)}
|
||||||
|
.muted{color:var(--muted)}
|
||||||
|
.faint{color:var(--faint)}
|
||||||
|
.btn{height:36px;padding:0 12px;border-radius:8px;border:1px solid var(--line2);background:var(--card2);color:var(--tx);font-size:14px;font-weight:600;white-space:nowrap}
|
||||||
|
.btn:hover{border-color:var(--line3)}
|
||||||
|
.btn.ghost{background:transparent;color:var(--muted)}
|
||||||
|
.btn.primary{height:44px;border:0;background:var(--accent);color:#0F1113;font-weight:800;font-size:14.5px;padding:0 18px}
|
||||||
|
.btn.primary:hover{filter:brightness(1.08)}
|
||||||
|
.btn.danger{height:44px;background:transparent;border-color:var(--bad-line);color:var(--bad);font-weight:700;font-size:14.5px}
|
||||||
|
.btn.warn{border:0;background:var(--warn);color:#0F1113;font-weight:800}
|
||||||
|
.chip{height:34px;padding:0 12px;border-radius:8px;border:1px solid var(--line2);background:transparent;color:var(--muted);font-size:14px;font-weight:600}
|
||||||
|
.chip[aria-pressed=true]{border-color:var(--accent);background:rgba(255,139,43,.12);color:var(--accent-tx)}
|
||||||
|
.badge{font-size:13.5px;font-weight:700;border-radius:999px;padding:1px 8px;background:var(--accent);color:#0F1113}
|
||||||
|
.badge.warn{background:var(--warn);color:#0F1113}
|
||||||
|
.badge:empty{display:none}
|
||||||
|
.eyebrow{font-size:13.5px;color:var(--faint);font-weight:700;text-transform:uppercase;letter-spacing:.06em;margin:0 0 8px}
|
||||||
|
input[type=text],input[type=email],input[type=password],input[type=number],input:not([type]),textarea{height:40px;padding:0 12px;border-radius:8px;border:1px solid var(--line3);background:var(--bg);color:var(--tx)}
|
||||||
|
textarea{height:auto;padding:10px 12px;min-height:72px;width:100%;resize:vertical}
|
||||||
|
input[type=checkbox]{width:18px;height:18px;accent-color:var(--accent)}
|
||||||
|
input[type=file]{font-size:14px;color:var(--muted);max-width:100%}
|
||||||
|
.check{display:flex;align-items:center;gap:10px;font-size:14px;color:var(--tx2)}
|
||||||
|
.ok{color:var(--ok)}.warn{color:var(--warn)}.bad{color:var(--bad)}
|
||||||
|
|
||||||
|
/* Sign-in */
|
||||||
|
.login{max-width:380px;margin:12vh auto 0;padding:32px;border:1px solid var(--line2);border-radius:14px;background:var(--panel);display:flex;flex-direction:column;gap:14px}
|
||||||
|
.login label{display:flex;flex-direction:column;gap:6px;font-size:14px;font-weight:600;color:var(--muted)}
|
||||||
|
.login .hint{font-size:13.5px;color:var(--faint);margin:0}
|
||||||
|
|
||||||
|
/* Top bar */
|
||||||
|
.top{height:64px;padding:0 24px;display:flex;align-items:center;gap:22px;border-bottom:1px solid var(--line);background:var(--bar);position:sticky;top:0;z-index:5}
|
||||||
|
.brand{display:flex;align-items:center;gap:10px;font-weight:800;font-size:16px;letter-spacing:-.01em;white-space:nowrap}
|
||||||
|
.brand i{width:28px;height:28px;border-radius:7px;background:var(--accent);color:#0F1113;font-style:normal;font-weight:800;font-size:15px;display:flex;align-items:center;justify-content:center}
|
||||||
|
.tabs{display:flex;gap:4px;align-self:stretch}
|
||||||
|
.tabs button{display:flex;align-items:center;gap:8px;padding:0 14px;border:0;border-bottom:2px solid transparent;background:none;color:var(--muted);font-weight:600;font-size:14.5px}
|
||||||
|
.tabs button[aria-selected=true]{color:var(--tx);font-weight:700;border-bottom-color:var(--accent)}
|
||||||
|
.tabs .count{font-family:var(--mono);font-size:13.5px;color:var(--muted)}
|
||||||
|
.searchbox{position:relative;margin-left:auto;flex:0 1 380px;min-width:200px}
|
||||||
|
.search{display:flex;align-items:center;gap:8px;width:100%;box-sizing:border-box;height:38px;padding:0 12px;border:1px solid var(--line2);border-radius:9px;background:var(--card);color:var(--faint)}
|
||||||
|
.search:focus-within{border-color:var(--accent);box-shadow:0 0 0 3px rgba(255,139,43,.18)}
|
||||||
|
.search input{flex:1;min-width:0;border:0;background:transparent;height:auto;padding:0;outline:none}
|
||||||
|
.search kbd{font:inherit;font-size:12px;padding:0 6px;border:1px solid var(--line3);border-radius:5px;color:var(--faint)}
|
||||||
|
.search:focus-within kbd{display:none}
|
||||||
|
/* The search results: orders and unpaid quotes, grouped. */
|
||||||
|
.results{position:absolute;top:calc(100% + 8px);right:0;width:580px;max-height:min(70vh,580px);overflow:auto;z-index:8;padding:6px 0;border:1px solid var(--line3);border-radius:12px;background:var(--card);box-shadow:0 18px 48px rgba(0,0,0,.5)}
|
||||||
|
.rgroup{padding:8px 14px 4px;font-size:12.5px;font-weight:700;letter-spacing:.04em;text-transform:uppercase;color:var(--faint)}
|
||||||
|
.rhit{display:flex;align-items:center;gap:12px;width:100%;padding:8px 14px;border:0;background:transparent;text-align:left}
|
||||||
|
.rhit:hover,.rhit[aria-selected=true]{background:rgba(255,139,43,.08)}
|
||||||
|
.rtext{flex:1;min-width:0;display:flex;flex-direction:column;gap:2px}
|
||||||
|
.rtext div{display:flex;gap:8px;align-items:baseline;min-width:0}
|
||||||
|
.rtext b{font-size:14px;flex-shrink:0}
|
||||||
|
.rtext div span{font-size:13.5px;color:var(--faint);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||||
|
.rmeta{font-size:13px;color:var(--muted)}
|
||||||
|
.rbadge{flex-shrink:0;font-size:12.5px;font-weight:700;padding:2px 9px;border-radius:999px;background:color-mix(in srgb,var(--c,#8B929A) 18%,transparent);color:var(--c,var(--tx2))}
|
||||||
|
.rempty{margin:0;padding:14px;font-size:14px;color:var(--muted)}
|
||||||
|
.rhelp{margin-top:6px;padding:8px 14px 4px;border-top:1px solid var(--line);font-size:12.5px;color:var(--faint)}
|
||||||
|
.tinydot{display:flex;align-items:center;gap:8px;font-size:14px;color:var(--muted);white-space:nowrap}
|
||||||
|
.tinydot::before{content:"";width:8px;height:8px;border-radius:999px;background:var(--dot,#5B6168)}
|
||||||
|
.tinydot:empty{display:none}
|
||||||
|
.who{font-size:14px;color:var(--muted);white-space:nowrap;max-width:220px;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
/* With five tabs, the operator's e-mail is the first thing to give way; it stays on Sair's tooltip. */
|
||||||
|
@media (max-width:1560px){.who{display:none}}
|
||||||
|
#status{position:fixed;left:50%;bottom:24px;transform:translateX(-50%);z-index:20;margin:0;max-width:min(640px,calc(100vw - 32px));padding:10px 16px;border-radius:10px;border:1px solid var(--line3);background:#1E2226;color:var(--tx);font-size:14px;box-shadow:0 10px 30px rgba(0,0,0,.45)}
|
||||||
|
#status:empty,#login-status:empty{display:none}
|
||||||
|
#status.error{border-color:var(--bad-line);color:var(--bad)}
|
||||||
|
#login-status.error{color:var(--bad)}
|
||||||
|
|
||||||
|
/* Board */
|
||||||
|
main{padding:0 24px 24px}
|
||||||
|
.bar{display:flex;align-items:center;gap:16px;flex-wrap:wrap;padding:18px 0 12px}
|
||||||
|
.bar h1,.split h1,.page h1{margin:0;font-size:22px;font-weight:800;letter-spacing:-.02em}
|
||||||
|
.chips{display:flex;gap:8px;flex-wrap:wrap}
|
||||||
|
.bar .chips{margin-left:auto}
|
||||||
|
.kan{display:grid;grid-template-columns:repeat(6,minmax(220px,1fr));gap:12px;overflow-x:auto;padding-bottom:8px;align-items:start}
|
||||||
|
.col{display:flex;flex-direction:column;gap:10px;background:var(--col);border:1px solid #22262B;border-top:3px solid var(--c,#22262B);border-radius:12px;padding:12px;min-height:320px}
|
||||||
|
.col.allowed{border-color:rgba(255,139,43,.45);background:#17140F}
|
||||||
|
.col.target{border-color:var(--accent)}
|
||||||
|
.col-head{display:flex;flex-direction:column;gap:6px;padding:2px 2px 8px;border-bottom:1px solid #22262B}
|
||||||
|
.col-head div{display:flex;align-items:center;gap:8px}
|
||||||
|
.col-head h2{margin:0;font-size:14.5px;font-weight:700}
|
||||||
|
.col-head .swatch{width:10px;height:10px;border-radius:3px;background:var(--c)}
|
||||||
|
.col-head .n{margin-left:auto;font-family:var(--mono);font-size:13.5px;color:var(--muted)}
|
||||||
|
.col-head small{font-size:13.5px;color:var(--faint)}
|
||||||
|
.col-head .col-sub{justify-content:space-between;min-height:30px}
|
||||||
|
.col-head .col-sub small{white-space:nowrap}
|
||||||
|
.col .empty{font-size:13.5px;color:var(--faint);padding:8px 2px}
|
||||||
|
.card{text-align:left;display:flex;flex-direction:column;gap:8px;padding:12px;border-radius:10px;border:1px solid var(--line2);background:var(--card);width:100%}
|
||||||
|
.card:hover,.card.open{border-color:var(--line3);background:#1E2226}
|
||||||
|
.card .row{display:flex;align-items:baseline;gap:8px;width:100%}
|
||||||
|
.card .num{font-size:14.5px;font-weight:800}
|
||||||
|
.card .age{margin-left:auto;font-size:13.5px;color:var(--faint)}
|
||||||
|
.card .cust{font-size:14px;font-weight:600;color:var(--tx);width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
.tag-teste{font-size:12.5px;font-weight:800;letter-spacing:.04em;padding:2px 7px;border-radius:6px;background:var(--warn);color:#1a1300}
|
||||||
|
.late{font-size:13.5px;color:#F06A5B;font-weight:600}
|
||||||
|
|
||||||
|
/* Order panel */
|
||||||
|
.drawer{position:fixed;inset:0;z-index:10;display:flex;justify-content:flex-end}
|
||||||
|
.scrim{position:absolute;inset:0;background:rgba(8,9,11,.6)}
|
||||||
|
.panel{position:relative;width:min(600px,100vw);height:100%;background:var(--panel);border-left:1px solid var(--line2);display:flex;flex-direction:column}
|
||||||
|
.panel-head{padding:20px 24px;border-bottom:1px solid var(--line);display:flex;flex-direction:column;gap:14px;flex-shrink:0}
|
||||||
|
/* The actions never shrink to make room: the body below scrolls instead. */
|
||||||
|
.panel-head>*,.panel-actions>*{flex-shrink:0}
|
||||||
|
.panel-actions{display:flex;flex-direction:column;gap:10px}
|
||||||
|
.panel-head .row{display:flex;align-items:center;gap:12px;flex-wrap:wrap}
|
||||||
|
.panel-head .num{font-size:22px;font-weight:800}
|
||||||
|
.stage{font-size:13.5px;font-weight:700;padding:4px 10px;border-radius:999px;background:color-mix(in srgb,var(--c) 18%,transparent);color:var(--c)}
|
||||||
|
.close{margin-left:auto;width:40px;height:40px;border-radius:8px;border:1px solid var(--line2);background:transparent;color:var(--muted);display:flex;align-items:center;justify-content:center}
|
||||||
|
.steps{margin:0;padding:0;list-style:none;display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:6px}
|
||||||
|
.steps li{display:flex;flex-direction:column;gap:6px;font-size:12.5px;color:var(--faint)}
|
||||||
|
.steps li::before{content:"";height:4px;border-radius:4px;background:var(--line2)}
|
||||||
|
.steps li.done{color:var(--muted)}.steps li.done::before{background:var(--c)}
|
||||||
|
.steps li.now{color:var(--tx);font-weight:700}.steps li.now::before{background:var(--c)}
|
||||||
|
.moves{display:flex;gap:10px;flex-wrap:wrap}
|
||||||
|
.moves .primary{flex:1}
|
||||||
|
.moves{align-items:center}
|
||||||
|
/* Going back is rare: a link, not a button the size of going forward. */
|
||||||
|
.linkbtn{background:none;border:0;padding:6px 2px;color:var(--muted);font:inherit;font-size:13.5px;cursor:pointer;text-decoration:underline;text-underline-offset:3px}
|
||||||
|
.linkbtn:hover{color:var(--tx)}
|
||||||
|
.morewrap{position:relative;margin-left:auto}
|
||||||
|
.more{min-width:44px;font-weight:800;letter-spacing:1px}
|
||||||
|
.menu{position:absolute;right:0;top:calc(100% + 6px);z-index:5;min-width:200px;display:flex;flex-direction:column;padding:6px;
|
||||||
|
background:var(--card2);border:1px solid var(--line3);border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.4)}
|
||||||
|
.menu[hidden]{display:none}
|
||||||
|
.menuitem{background:none;border:0;text-align:left;padding:9px 10px;border-radius:7px;font:inherit;font-size:14px;color:inherit;cursor:pointer}
|
||||||
|
.menuitem:hover{background:rgba(255,255,255,.06)}
|
||||||
|
.menuitem.bad{color:var(--bad)}
|
||||||
|
.reason{display:flex;flex-direction:column;gap:8px}
|
||||||
|
.panel-body{flex:1;overflow:auto;padding:20px 24px 32px;display:flex;flex-direction:column;gap:24px}
|
||||||
|
.facts{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px}
|
||||||
|
.facts b{display:block;font-size:14.5px}
|
||||||
|
.facts span{font-size:14px;color:var(--muted);overflow-wrap:anywhere}
|
||||||
|
.item{display:flex;gap:14px;padding:14px;border:1px solid var(--line2);border-radius:12px;background:var(--card)}
|
||||||
|
.item.attention{border-color:#4A3A1E;background:#1C1A15}
|
||||||
|
.item .preview{width:96px;flex-shrink:0;border-radius:8px;background:var(--card2);display:flex;align-items:flex-start;justify-content:center;padding:6px}
|
||||||
|
.item .preview svg{width:100%;height:auto;display:block}
|
||||||
|
.item .body{flex:1;min-width:0;display:flex;flex-direction:column;gap:8px}
|
||||||
|
.item .title{display:flex;align-items:baseline;gap:8px}
|
||||||
|
.item .title b{font-size:14.5px}
|
||||||
|
.item .title .mono{margin-left:auto;font-size:14px}
|
||||||
|
.item .line{font-size:13.5px;color:var(--muted)}
|
||||||
|
.item .file{display:flex;align-items:center;gap:8px;font-size:14px}
|
||||||
|
.buttons{display:flex;gap:8px;flex-wrap:wrap}
|
||||||
|
.finals{display:flex;flex-direction:column;gap:12px}
|
||||||
|
.finals .rev{font-size:14px;display:flex;flex-direction:column;gap:4px;padding:10px 12px;border:1px solid var(--line2);border-radius:10px}
|
||||||
|
.finalform{display:flex;flex-direction:column;gap:12px;padding:14px;border:1px solid var(--line2);border-radius:12px}
|
||||||
|
.finalform[hidden]{display:none}
|
||||||
|
.nextstep .finalform{margin-top:12px;background:var(--panel)}
|
||||||
|
.finalform .linkbtn{align-self:center}
|
||||||
|
.finalform input[type=file]{color:var(--muted);font:inherit;font-size:13.5px;font-weight:500}
|
||||||
|
.finalform input[type=file]::file-selector-button{height:34px;padding:0 12px;margin-right:10px;border-radius:8px;border:1px solid var(--line2);background:var(--card2);color:var(--tx);font:inherit;font-size:13.5px;font-weight:600;cursor:pointer}
|
||||||
|
.finalform .slot{display:flex;flex-direction:column;gap:6px;font-size:14px;font-weight:600}
|
||||||
|
.timeline{margin:0;padding:0;list-style:none;display:flex;flex-direction:column;gap:10px}
|
||||||
|
.timeline li::before{content:"";width:8px;height:8px;margin-top:6px;border-radius:999px;background:var(--c,var(--muted));flex-shrink:0}
|
||||||
|
|
||||||
|
/* Quotes */
|
||||||
|
#tab-quotes{padding-top:18px}
|
||||||
|
/* Cotações fills the window and no more: the top bar (64px), the tab's 18px
|
||||||
|
and main's 24px. Its parts scroll inside, never the page. */
|
||||||
|
.split{display:flex;gap:0;border:1px solid var(--line);border-radius:12px;overflow:hidden;height:calc(100vh - 106px);min-height:480px}
|
||||||
|
#quote-list{flex:1;min-height:0;overflow:hidden;display:flex;flex-direction:column;gap:8px}
|
||||||
|
#quote-pager:empty{display:none}
|
||||||
|
#quote-pager .pager{margin-top:0}
|
||||||
|
/* About a third of the screen: the rows carry a picture, the customer and the price. */
|
||||||
|
.split .list{width:clamp(460px,34vw,640px);flex-shrink:0;border-right:1px solid var(--line);padding:16px;display:flex;flex-direction:column;gap:12px;background:#111316}
|
||||||
|
.qitem{text-align:left;display:flex;align-items:center;gap:12px;padding:12px;border-radius:10px;border:1px solid var(--line2);background:var(--card);width:100%}
|
||||||
|
.qitem .qmain{flex:1;min-width:0}
|
||||||
|
.qsearch{display:flex;align-items:center;gap:8px;height:38px;padding:0 12px;border:1px solid var(--line2);border-radius:9px;background:var(--card);color:var(--faint)}
|
||||||
|
.qsearch:focus-within{border-color:var(--accent)}
|
||||||
|
.qsearch input{flex:1;min-width:0;border:0;background:transparent;height:auto;padding:0;outline:none}
|
||||||
|
.chips.small{gap:6px}
|
||||||
|
.chips.small .chip{height:30px;padding:0 10px;font-size:13px}
|
||||||
|
.qitem[aria-current=true]{border-color:var(--accent);background:#1F1A15}
|
||||||
|
.pane{flex:1;min-width:0;min-height:0;display:flex;flex-direction:column}
|
||||||
|
.pane .head{padding:18px 24px;border-bottom:1px solid var(--line);display:flex;align-items:center;gap:14px;flex-wrap:wrap}
|
||||||
|
.pane .head b{font-size:18px;overflow-wrap:anywhere}
|
||||||
|
.pane .content{flex:1;min-height:0;overflow:auto;padding:20px 24px;display:flex;flex-direction:column;gap:16px}
|
||||||
|
.pane .fields{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:14px}
|
||||||
|
.pane .fields label{display:flex;flex-direction:column;gap:6px;font-size:13.5px;font-weight:600;color:var(--muted)}
|
||||||
|
.pane .fields input[readonly]{color:var(--faint);background:var(--panel);border-color:var(--line2)}
|
||||||
|
.pane .foot{padding:16px 24px;border-top:1px solid var(--line);background:var(--bar);display:flex;align-items:center;gap:16px;flex-wrap:wrap}
|
||||||
|
.pane .foot .total{margin-left:auto;font-family:var(--mono);color:var(--muted)}
|
||||||
|
.pane .foot .total b{color:var(--tx)}
|
||||||
|
.pane .note{padding:24px;color:var(--muted)}
|
||||||
|
|
||||||
|
/* Payments and integrations */
|
||||||
|
.page{display:flex;flex-direction:column;gap:22px;padding-top:18px}
|
||||||
|
.page h2{margin:0 0 10px;font-size:15px;font-weight:800}
|
||||||
|
.issue{display:flex;align-items:center;gap:16px;flex-wrap:wrap;padding:16px 18px;border-radius:12px;border:1px solid #4A3A1E;background:#1C1A15;margin-bottom:8px}
|
||||||
|
.issue .text{flex:1;min-width:260px;display:flex;flex-direction:column;gap:4px}
|
||||||
|
.issue .text b{font-size:14.5px}
|
||||||
|
.issue .text span{font-size:14px;color:var(--muted);overflow-wrap:anywhere}
|
||||||
|
.issue form{display:flex;gap:8px;align-items:center}
|
||||||
|
.issue.done{border-color:var(--line2);background:var(--card)}
|
||||||
|
.pager{display:flex;align-items:center;gap:16px;flex-wrap:wrap;padding:14px 4px;border-top:1px solid var(--line);margin-top:8px;font-size:14px;color:var(--muted)}
|
||||||
|
.pager-size{display:flex;align-items:center;gap:8px}
|
||||||
|
.pager-size select{height:34px;padding:0 8px;border-radius:8px;border:1px solid var(--line3);background:var(--bg);color:var(--tx)}
|
||||||
|
.pager-range{margin-left:auto}
|
||||||
|
.pager-nav{display:flex;align-items:center;gap:6px}
|
||||||
|
.pager .pg{width:34px;height:34px;border-radius:8px;border:1px solid var(--line2);background:transparent;color:var(--tx);font-size:16px;line-height:1}
|
||||||
|
.pager .pg:disabled{color:var(--faint);opacity:.45}
|
||||||
|
.pager-page{display:flex;align-items:center;gap:6px;margin:0 4px}
|
||||||
|
.pager-page input{width:56px;height:34px;text-align:center;padding:0 4px}
|
||||||
|
.pager.compact{gap:10px;padding:12px 0 0}
|
||||||
|
.pager.compact .pager-range{margin-left:0}
|
||||||
|
.pager.compact .pager-nav{margin-left:auto}
|
||||||
|
.filters{display:flex;gap:12px;flex-wrap:wrap;align-items:flex-end;margin-bottom:12px}
|
||||||
|
.field{display:flex;flex-direction:column;gap:6px;font-size:13.5px;font-weight:600;color:var(--muted)}
|
||||||
|
.field select,.field input{height:38px;min-width:160px;padding:0 10px;border-radius:8px;border:1px solid var(--line3);background:var(--bg);color:var(--tx)}
|
||||||
|
.field input{min-width:110px;width:110px}
|
||||||
|
.issue input{width:320px}
|
||||||
|
.pagehead{display:flex;align-items:center;gap:16px;flex-wrap:wrap}
|
||||||
|
.statchips{display:flex;gap:8px;margin-left:auto}
|
||||||
|
.stats-note{margin:-12px 0 0;font-size:13.5px}
|
||||||
|
.kpis{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px}
|
||||||
|
.kpi{display:flex;flex-direction:column;gap:4px;padding:14px 16px;border:1px solid var(--line2);border-radius:12px;background:var(--card)}
|
||||||
|
.kpi span{font-size:13.5px;color:var(--muted)}
|
||||||
|
.kpi b{font-size:24px;font-weight:700;letter-spacing:-.01em;font-variant-numeric:proportional-nums}
|
||||||
|
.kpi small{font-size:13px;color:var(--faint)}
|
||||||
|
.statbox{padding:16px 18px;border:1px solid var(--line2);border-radius:12px;background:var(--card)}
|
||||||
|
.chart{position:relative}
|
||||||
|
.chart svg{display:block;width:100%;height:auto}
|
||||||
|
.chart .grid{stroke:var(--line);stroke-width:1}
|
||||||
|
.chart .tick{fill:var(--faint);font-size:12px;font-family:var(--mono)}
|
||||||
|
.chart .bar{fill:var(--accent)}
|
||||||
|
.chart .hit{fill:transparent;cursor:default}
|
||||||
|
.chart .hit:hover{fill:rgba(255,255,255,.03)}
|
||||||
|
.chart .tip{position:absolute;transform:translate(-50%,calc(-100% - 8px));pointer-events:none;padding:6px 10px;border-radius:8px;
|
||||||
|
background:var(--card2);border:1px solid var(--line3);font-size:13px;white-space:nowrap;color:var(--tx)}
|
||||||
|
.charttable{margin-top:10px;font-size:13.5px;color:var(--muted)}
|
||||||
|
.charttable summary{cursor:pointer}
|
||||||
|
.charttable table{margin-top:8px}
|
||||||
|
#stats-stages .swatch{display:inline-block;width:10px;height:10px;border-radius:3px;background:var(--c);margin-right:8px}
|
||||||
|
.grid4{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:12px}
|
||||||
|
.integration{display:flex;flex-direction:column;gap:10px;padding:16px;border-radius:12px;border:1px solid var(--line2);background:var(--card)}
|
||||||
|
.integration .head{display:flex;align-items:center;gap:8px}
|
||||||
|
.integration .head b{font-size:15px}
|
||||||
|
.integration .head .dot{width:9px;height:9px;border-radius:999px;background:var(--dot,#5B6168)}
|
||||||
|
.integration .head em{margin-left:auto;font-style:normal;font-size:13.5px;font-weight:700;color:var(--dotx,var(--muted))}
|
||||||
|
.integration p{margin:0;font-size:14px;color:var(--muted)}
|
||||||
|
.integration .buttons{margin-top:auto}
|
||||||
|
table.log{width:100%;border-collapse:collapse;font-size:14px}
|
||||||
|
table.log th{text-align:left;color:var(--faint);font-weight:600;padding:8px 12px;border-bottom:1px solid var(--line)}
|
||||||
|
table.log td{padding:10px 12px;border-bottom:1px solid #1E2226;vertical-align:top}
|
||||||
|
.good{color:var(--ok);font-weight:600}.retry{color:var(--warn);font-weight:600}
|
||||||
|
|
||||||
|
@media (max-width:900px){
|
||||||
|
.top{flex-wrap:wrap;height:auto;padding:10px 16px;gap:10px}
|
||||||
|
.searchbox{order:5;width:100%;flex-basis:100%;margin-left:0}
|
||||||
|
.search{width:100%}
|
||||||
|
.results{width:100%}
|
||||||
|
main{padding:0 16px 16px}
|
||||||
|
.split{flex-direction:column}
|
||||||
|
.split{height:auto}
|
||||||
|
#quote-list{overflow:visible}
|
||||||
|
.split .list{width:auto;border-right:0;border-bottom:1px solid var(--line)}
|
||||||
|
.facts,.pane .fields{grid-template-columns:1fr}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Icons beside a label: the download buttons, where a card ships, the WhatsApp link. */
|
||||||
|
.btn.with-icon{display:inline-flex;align-items:center;gap:6px}
|
||||||
|
a.zap{display:inline-flex;align-items:center;gap:4px;vertical-align:bottom}
|
||||||
|
|
||||||
|
/* Artwork pictures, made by the customer's browser. The checkerboard shows
|
||||||
|
what is transparent; without a picture, the layout drawing stands in. */
|
||||||
|
.art{position:relative;overflow:hidden;display:flex;align-items:center;justify-content:center;flex-shrink:0;border-radius:8px;border:1px solid var(--line2);
|
||||||
|
background-color:#2A2F35;background-image:conic-gradient(#323840 25%,transparent 0 50%,#323840 0 75%,transparent 0);background-size:12px 12px}
|
||||||
|
/* The whole box from the start: a lazy image with no size yet may never be loaded. */
|
||||||
|
.art img{display:block;width:100%;height:100%;object-fit:contain}
|
||||||
|
.art.layout{background:var(--card2);padding:4px}
|
||||||
|
.art.layout svg{display:block;height:100%;width:auto;max-width:100%}
|
||||||
|
.art.none{background:var(--card2)}
|
||||||
|
.art .noart{font-size:12px;font-weight:700;color:var(--faint)}
|
||||||
|
.card .card-art{width:100%;height:96px}
|
||||||
|
.qart{width:56px;height:56px}
|
||||||
|
.rart{width:44px;height:44px;background-size:8px 8px}
|
||||||
|
.item .preview.with-art{width:220px;padding:8px;flex-direction:column;align-items:stretch;gap:8px}
|
||||||
|
.pane .item .preview.with-art{width:320px}
|
||||||
|
.ptabs{display:flex;gap:4px}
|
||||||
|
.ptabs button{height:28px;padding:0 10px;border-radius:7px;border:0;background:transparent;color:var(--muted);font-size:12.5px;font-weight:600}
|
||||||
|
.ptabs button[aria-pressed=true]{background:var(--card);color:var(--tx);font-weight:700}
|
||||||
|
.preview .view{height:220px;border-radius:8px;display:flex;gap:6px;align-items:center;justify-content:center;overflow:hidden}
|
||||||
|
.pane .preview .view{height:300px}
|
||||||
|
.preview .view.art{padding:6px;border:1px solid var(--line2);
|
||||||
|
background-color:#2A2F35;background-image:conic-gradient(#323840 25%,transparent 0 50%,#323840 0 75%,transparent 0);background-size:14px 14px}
|
||||||
|
.preview .view.art img{flex:1;min-width:0;height:100%;object-fit:contain}
|
||||||
|
.preview .view.layout{padding:6px;background:var(--bg)}
|
||||||
|
.item .preview .view.layout svg{height:100%;width:auto;max-width:100%}
|
||||||
|
|
||||||
|
/* v2: one badge system. Grade by the price ladder's bands, product, status,
|
||||||
|
warnings; text always says what the colour means. */
|
||||||
|
.grade{display:inline-flex;align-items:center;justify-content:center;min-width:30px;height:22px;padding:0 7px;border-radius:6px;font-size:12.5px;font-weight:800;flex-shrink:0}
|
||||||
|
.grade.g90{color:#6FD19C;background:rgba(111,209,156,.16)}
|
||||||
|
.grade.g75{color:#B5DB6E;background:rgba(181,219,110,.15)}
|
||||||
|
.grade.g60{color:#F2B33D;background:rgba(242,179,61,.16)}
|
||||||
|
.grade.g40{color:#FF9B4A;background:rgba(255,155,74,.16)}
|
||||||
|
.grade.g0{color:#F4968B;background:rgba(244,150,139,.16)}
|
||||||
|
.grade.gnone{color:var(--muted);border:1px solid var(--line3)}
|
||||||
|
.pbadge{display:inline-flex;align-items:center;height:22px;padding:0 8px;border-radius:999px;font-size:12px;font-weight:700;color:var(--tx2);background:var(--card2);white-space:nowrap}
|
||||||
|
.pbadge.uv{color:#C9B8FF;background:rgba(167,139,250,.14)}
|
||||||
|
.pill{display:inline-flex;align-items:center;gap:4px;height:22px;padding:0 9px;border-radius:999px;font-size:12px;font-weight:700;color:var(--tx2);background:var(--card2);white-space:nowrap;max-width:100%;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
.pill.ok{color:var(--ok);background:rgba(111,209,156,.12)}
|
||||||
|
.pill.warn{color:var(--warn);background:rgba(242,179,61,.12)}
|
||||||
|
.pill.bad{color:var(--bad);background:rgba(244,150,139,.14)}
|
||||||
|
.pill.accent{color:var(--accent-tx);background:rgba(255,139,43,.14)}
|
||||||
|
.pill.info{color:var(--rec);background:rgba(43,182,217,.14)}
|
||||||
|
.pill.muted{color:var(--muted)}
|
||||||
|
.place{display:inline-flex;align-items:center;gap:5px;font-size:13px;color:var(--muted);white-space:nowrap}
|
||||||
|
.meta{display:flex;align-items:center;gap:6px;flex-wrap:wrap;min-width:0}
|
||||||
|
.meta .mtr{font-size:13px;color:var(--tx2)}
|
||||||
|
.meta .age{margin-left:auto;font-size:12.5px;color:var(--faint)}
|
||||||
|
.flags{display:flex;flex-wrap:wrap;gap:5px}
|
||||||
|
.chip .chip-n{margin-left:6px;font-size:12px;opacity:.75}
|
||||||
|
|
||||||
|
/* Production cards */
|
||||||
|
.card .meta{width:100%}
|
||||||
|
.card .flags{width:100%;padding-top:8px;border-top:1px solid var(--line)}
|
||||||
|
|
||||||
|
/* Quote rows */
|
||||||
|
.qitem .qmain{flex:1;min-width:0;display:flex;flex-direction:column;gap:6px}
|
||||||
|
.qhead{display:flex;align-items:center;gap:8px;min-width:0}
|
||||||
|
.qmail{min-width:0}
|
||||||
|
.qmail b{display:block;font-size:14.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
.qtotal{margin-left:auto;font-size:15px;flex-shrink:0}
|
||||||
|
.qtotal.muted{font-size:13px;font-weight:600}
|
||||||
|
.newdot{width:7px;height:7px;flex-shrink:0;border-radius:999px;background:var(--accent)}
|
||||||
|
|
||||||
|
/* Quote detail */
|
||||||
|
.pane .head{align-items:flex-start;gap:24px}
|
||||||
|
.qtitle{flex:1;min-width:0;display:flex;flex-direction:column;gap:8px}
|
||||||
|
.qname{display:flex;align-items:center;gap:10px;flex-wrap:wrap}
|
||||||
|
.qname b{font-size:19px}
|
||||||
|
.qsub{display:flex;align-items:center;flex-wrap:wrap;gap:8px 16px;font-size:13.5px;color:var(--muted)}
|
||||||
|
.qsub>span{display:inline-flex;align-items:center;gap:6px}
|
||||||
|
.qbig{text-align:right;flex-shrink:0}
|
||||||
|
.qbig span{display:block;font-size:12.5px;color:var(--faint)}
|
||||||
|
.qbig b{font-size:26px;font-weight:800}
|
||||||
|
.qsums{display:flex;justify-content:flex-end;align-items:baseline;gap:6px;font-size:14px;color:var(--muted)}
|
||||||
|
.qsums b{color:var(--tx)}
|
||||||
|
.copy{width:26px;height:26px;padding:0;border-radius:6px;border:1px solid var(--line2);background:transparent;color:var(--faint);display:inline-flex;align-items:center;justify-content:center}
|
||||||
|
.copy:hover{color:var(--tx);border-color:var(--line3)}
|
||||||
|
.zapbtn{display:inline-flex;align-items:center;gap:6px;height:28px;padding:0 10px;border-radius:7px;background:rgba(111,209,156,.1);color:var(--ok);text-decoration:none;font-size:13px;font-weight:600}
|
||||||
|
.zapbtn:hover{background:rgba(111,209,156,.18)}
|
||||||
|
.zapbtn.big{height:36px;padding:0 14px;font-size:14px;border-radius:8px}
|
||||||
|
|
||||||
|
/* Facts: small labelled boxes */
|
||||||
|
.factgrid{margin:0;display:grid;grid-template-columns:repeat(auto-fill,minmax(130px,1fr));gap:6px}
|
||||||
|
.factgrid div{padding:8px 10px;border-radius:8px;background:var(--bg)}
|
||||||
|
.factgrid dt{font-size:12px;color:var(--faint)}
|
||||||
|
.factgrid dd{margin:2px 0 0;font-size:14px;font-weight:700}
|
||||||
|
.callout{display:flex;flex-direction:column;gap:3px;padding:10px 12px;border-radius:9px;background:rgba(242,179,61,.1)}
|
||||||
|
.callout b{font-size:13.5px;color:var(--warn)}
|
||||||
|
.callout span{font-size:13px;color:var(--tx2)}
|
||||||
|
|
||||||
|
/* Order panel */
|
||||||
|
.paidline{display:flex;align-items:baseline;flex-wrap:wrap;gap:8px;font-size:14px;color:var(--muted)}
|
||||||
|
.paidline b{font-size:18px;color:var(--tx)}
|
||||||
|
.fcard{display:flex;flex-direction:column;gap:6px;padding:12px 14px;border:1px solid var(--line);border-radius:10px;background:var(--card)}
|
||||||
|
.fcard .eyebrow{margin:0}
|
||||||
|
.fcard>b{display:flex;align-items:center;gap:6px}
|
||||||
|
.fcard .line{display:inline-flex;align-items:center;gap:6px;font-size:13.5px;color:var(--muted)}
|
||||||
|
.fcard .zapbtn{align-self:flex-start}
|
||||||
|
.nextstep{display:flex;align-items:center;gap:14px;padding:14px 16px;border:1px dashed var(--line3);border-radius:12px}
|
||||||
|
.nextstep>div{flex:1;display:flex;flex-direction:column;gap:2px}
|
||||||
|
.nextstep b{font-size:14.5px}
|
||||||
|
.nextstep span{font-size:13.5px;color:var(--muted)}
|
||||||
|
.nextstep .nextmark,.nextmark{width:32px;height:32px;flex-shrink:0;border-radius:9px;background:rgba(255,139,43,.14);color:var(--accent-tx);display:flex;align-items:center;justify-content:center;font-weight:800}
|
||||||
|
.nextstep.lead{align-items:flex-start;border-style:solid;border-color:rgba(255,139,43,.45);background:rgba(255,139,43,.06)}
|
||||||
|
.readyfiles{display:flex;flex-direction:column;gap:6px;margin-top:8px}
|
||||||
|
.readyfile{display:flex;align-items:center;gap:10px;padding:6px 6px 6px 12px;background:var(--card2);border-radius:8px}
|
||||||
|
.readyfile .faint{flex-shrink:0;font-size:13px;color:var(--faint)}
|
||||||
|
.readyfile b{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13.5px}
|
||||||
|
.stepactions{display:flex;gap:10px;margin-top:12px;flex-wrap:wrap}
|
||||||
|
.btn.small{height:30px;padding:0 10px;font-size:13px}
|
||||||
|
.subactions{display:flex;margin-top:-4px}
|
||||||
|
.timeline li{display:flex;align-items:baseline;gap:10px;font-size:14px}
|
||||||
|
.timeline li div{flex:1;min-width:0}
|
||||||
|
.timeline small{flex-shrink:0;color:var(--faint);font-size:13px}
|
||||||
|
/* Without a picture, a long sheet's layout drawing stays a preview, not a column. */
|
||||||
|
.item .preview:not(.with-art) svg{width:auto;max-width:100%;max-height:260px;margin:0 auto}
|
||||||
|
|
||||||
|
/* The buyer: a name over the e-mail, or the e-mail alone. */
|
||||||
|
.buyer{display:flex;flex-direction:column;min-width:0}
|
||||||
|
.buyer b{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
.buyer-mail{font-size:12.5px;color:var(--faint);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||||||
|
.card .buyer b{font-size:14px;font-weight:600}
|
||||||
|
.qname .buyer b{font-size:19px}
|
||||||
|
|
||||||
|
/* Cotações' heading says what the list is. */
|
||||||
|
.lhead{display:flex;align-items:baseline;gap:10px}
|
||||||
|
.lhead span{font-size:13.5px;color:var(--faint)}
|
||||||
|
/* Discard and cancel: quiet until meant. */
|
||||||
|
.btn.discard{margin-right:auto}
|
||||||
|
.btn.discard:hover{color:var(--bad);border-color:var(--bad-line)}
|
||||||
|
.callout.bad{background:rgba(244,150,139,.12)}
|
||||||
|
.callout.bad b{color:var(--bad)}
|
||||||
|
|
||||||
|
/* The order code the customer sees, beside the operators' number. */
|
||||||
|
.ocode{display:inline-flex;align-items:center;gap:6px;font-size:13px;color:var(--muted)}
|
||||||
|
|
||||||
|
/* Where a card dragged within its column will land. */
|
||||||
|
.card.drop-before{box-shadow:0 -3px 0 0 var(--accent)}
|
||||||
|
.card.drop-after{box-shadow:0 3px 0 0 var(--accent)}
|
||||||
|
|
||||||
|
/* "Atualizar" and how often it happens by itself, as one control. */
|
||||||
|
.refresh{flex-shrink:0;display:inline-flex;align-items:stretch;border:1px solid var(--line2);border-radius:8px;overflow:hidden}
|
||||||
|
.refresh .btn{border:0;border-radius:0;display:inline-flex;align-items:center;gap:6px}
|
||||||
|
.refresh select{width:88px;height:36px;padding:0 24px 0 10px;border:0;border-left:1px solid var(--line2);background:transparent;color:var(--muted);font:inherit;font-size:13.5px;font-weight:600;cursor:pointer}
|
||||||
|
.refresh select option{background:var(--card);color:var(--tx)}
|
||||||
111
web/kanban.html
@@ -1,21 +1,96 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="pt-BR"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
<html lang="pt-BR"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||||
<title>DTF · Kanban local</title>
|
<title>Kanban DTF</title>
|
||||||
<style>
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
:root{--bg:#0B0D10;--card:#15181D;--card2:#1C2026;--linha:#282C34;--tx:#EDEBE6;--fraco:#a1a6af;--ciano:#00B8DA}
|
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap">
|
||||||
*{box-sizing:border-box}body{background:var(--bg);color:var(--tx);font:14px/1.5 system-ui,sans-serif;padding:20px;margin:0}
|
<link rel="stylesheet" href="/kanban.css">
|
||||||
h1{font-size:24px;margin:0}h2{font-size:18px}header{display:flex;align-items:center;gap:18px;flex-wrap:wrap;margin-bottom:20px}
|
</head><body>
|
||||||
.aviso{background:#152026;border-left:3px solid var(--ciano);padding:12px;color:#b9cbd2;margin:16px 0}
|
<form id="login" class="login" hidden>
|
||||||
button,input,select{font:inherit;border:1px solid #56616d;border-radius:5px;padding:8px;background:var(--card2);color:var(--tx)}
|
<div class="brand"><i aria-hidden="true">D</i>Kanban DTF</div>
|
||||||
button{cursor:pointer}button:hover{border-color:var(--ciano)}button:disabled{opacity:.5}input[type=number]{width:100px}
|
<label>E-mail <input id="email" type="email" autocomplete="username" required></label>
|
||||||
label{display:inline-flex;gap:8px;align-items:center;margin:5px}#kan{display:grid;grid-template-columns:repeat(6,minmax(220px,1fr));gap:10px;overflow-x:auto;padding-bottom:16px}
|
<label>Senha <input id="password" type="password" autocomplete="current-password" required></label>
|
||||||
.col{background:var(--card);border:1px solid var(--linha);border-radius:9px;min-height:250px;padding:10px}.col h2{font-size:14px;border-bottom:2px solid var(--cc);padding-bottom:10px}.col.alvo{border-color:var(--ciano)}
|
<button class="btn primary">Entrar</button>
|
||||||
.cd,.review{background:var(--card2);border:1px solid var(--linha);border-left:3px solid var(--cc,var(--ciano));border-radius:6px;padding:12px;margin-bottom:10px}.cd{cursor:grab}.cd p{overflow-wrap:anywhere}.meta{color:var(--fraco);font-size:12px}.actions{display:flex;gap:6px;flex-wrap:wrap;margin-top:8px}.error{color:#ffad83}.print{margin-top:8px;padding-top:8px;border-top:1px solid var(--linha)}.print button{margin-left:6px}#tiny{font-size:12px;color:var(--fraco);margin-right:8px}#tiny button{margin-left:6px}#status{min-height:24px}details{margin:16px 0}pre{white-space:pre-wrap;overflow-wrap:anywhere}a{color:var(--ciano)}
|
<p id="login-status" class="hint" role="alert"></p>
|
||||||
</style></head><body>
|
</form>
|
||||||
<header><h1>Kanban DTF</h1><span id="tiny"></span><button id="refresh">Atualizar</button><button id="logout">Sair</button></header>
|
|
||||||
<div class="aviso">Confira a cotação manualmente. Em Arquivos de produção, envie e aprove os arquivos finais de todos os itens antes de colocar o pedido na fila. Não há validação automática de arte.</div>
|
<div id="app" hidden>
|
||||||
<form id="login"><label>E-mail <input id="email" type="email" autocomplete="username" required></label><label>Senha <input id="password" type="password" autocomplete="current-password" required></label><button>Entrar</button></form>
|
<header class="top">
|
||||||
|
<div class="brand"><i aria-hidden="true">D</i>Kanban DTF</div>
|
||||||
|
<nav class="tabs" aria-label="Seções">
|
||||||
|
<button type="button" data-tab="board" aria-selected="true">Produção <span class="count" id="count-board"></span></button>
|
||||||
|
<button type="button" data-tab="quotes" aria-selected="false">Cotações <span class="badge" id="badge-quotes"></span></button>
|
||||||
|
<button type="button" data-tab="payments" aria-selected="false">Pagamentos <span class="badge warn" id="badge-payments"></span></button>
|
||||||
|
<button type="button" data-tab="stats" aria-selected="false">Números</button>
|
||||||
|
<button type="button" data-tab="integrations" aria-selected="false">Integrações</button>
|
||||||
|
</nav>
|
||||||
|
<div class="searchbox">
|
||||||
|
<label class="search">
|
||||||
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><circle cx="11" cy="11" r="7"></circle><path d="M20 20l-3.5-3.5"></path></svg>
|
||||||
|
<input id="search" type="search" placeholder="Buscar em pedidos e cotações: cliente, CNPJ, nº" aria-label="Buscar em pedidos e cotações" autocomplete="off" role="combobox" aria-expanded="false" aria-controls="search-results">
|
||||||
|
<kbd>/</kbd>
|
||||||
|
</label>
|
||||||
|
<div id="search-results" class="results" role="listbox" aria-label="Resultados da busca" hidden></div>
|
||||||
|
</div>
|
||||||
|
<span id="tiny" class="tinydot"></span>
|
||||||
|
<span id="who" class="who"></span>
|
||||||
|
<div class="refresh">
|
||||||
|
<button class="btn ghost" id="refresh" type="button"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12a9 9 0 1 1-2.64-6.36"></path><path d="M21 3v6h-6"></path></svg>Atualizar</button>
|
||||||
|
<select id="refresh-every" aria-label="Atualizar sozinho a cada">
|
||||||
|
<option value="0">Desligado</option><option value="10">10 s</option><option value="30">30 s</option>
|
||||||
|
<option value="60">1 min</option><option value="300">5 min</option><option value="900">15 min</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button class="btn ghost" id="logout" type="button">Sair</button>
|
||||||
|
</header>
|
||||||
<p id="status" role="status"></p>
|
<p id="status" role="status"></p>
|
||||||
<section id="payments"></section><section id="reviews"></section><div id="kan"></div>
|
<main>
|
||||||
<details><summary>Eventos locais de integração</summary><pre id="events"></pre></details>
|
<section id="tab-board">
|
||||||
<script src="/upload.js"></script><script src="/kanban.js?v=pdf-print-1"></script></body></html>
|
<div class="bar"><h1>Produção</h1><span class="muted" id="board-summary"></span><div class="chips" id="filters"></div></div>
|
||||||
|
<div class="kan" id="kan"></div>
|
||||||
|
</section>
|
||||||
|
<section id="tab-quotes" hidden>
|
||||||
|
<div class="split">
|
||||||
|
<div class="list">
|
||||||
|
<div class="lhead"><h1>Cotações</h1><span>pedidos ainda não pagos</span></div>
|
||||||
|
<div class="chips" id="quote-filters"></div>
|
||||||
|
<label class="qsearch">
|
||||||
|
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><circle cx="11" cy="11" r="7"></circle><path d="M20 20l-3.5-3.5"></path></svg>
|
||||||
|
<input id="quote-q" type="search" placeholder="Filtrar por e-mail, CNPJ ou WhatsApp" aria-label="Filtrar cotações por cliente" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
<div class="chips small" id="quote-chips"></div>
|
||||||
|
<div id="quote-list"></div>
|
||||||
|
<div id="quote-pager"></div>
|
||||||
|
</div>
|
||||||
|
<div class="pane" id="reviews"></div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section id="tab-payments" class="page" hidden>
|
||||||
|
<h1>Pagamentos</h1>
|
||||||
|
<section id="payments"></section>
|
||||||
|
</section>
|
||||||
|
<section id="tab-stats" class="page" hidden>
|
||||||
|
<div class="pagehead"><h1>Números</h1><div class="statchips" id="stats-period"></div></div>
|
||||||
|
<p class="faint stats-note">Pedidos pagos no período. Pedidos de teste e cancelados ficam de fora.</p>
|
||||||
|
<div class="kpis" id="stats-kpis"></div>
|
||||||
|
<section class="statbox"><h2>Metros por dia</h2><div class="chart" id="stats-chart"></div></section>
|
||||||
|
<section class="statbox"><h2>Tempo em cada etapa</h2>
|
||||||
|
<table class="log"><thead><tr><th scope="col">Etapa</th><th scope="col">Tempo mediano</th><th scope="col">Pedidos que passaram</th><th scope="col">Agora</th></tr></thead><tbody id="stats-stages"></tbody></table>
|
||||||
|
</section>
|
||||||
|
</section>
|
||||||
|
<section id="tab-integrations" class="page" hidden>
|
||||||
|
<h1>Integrações</h1>
|
||||||
|
<section><div class="grid4" id="integrations"></div></section>
|
||||||
|
<section><h2>Registro de envios</h2>
|
||||||
|
<div class="filters" id="event-filters"></div>
|
||||||
|
<table class="log"><thead><tr><th scope="col">Quando</th><th scope="col">Pedido</th><th scope="col">Destino</th><th scope="col">Evento</th><th scope="col">Resultado</th></tr></thead><tbody id="events"></tbody></table>
|
||||||
|
<div id="events-more"></div>
|
||||||
|
</section>
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="drawer" id="drawer" hidden>
|
||||||
|
<div class="scrim" id="scrim"></div>
|
||||||
|
<aside class="panel" id="panel" aria-label="Pedido"></aside>
|
||||||
|
</div>
|
||||||
|
<script src="/upload.js"></script><script src="/kanban.js"></script></body></html>
|
||||||
|
|||||||
1551
web/kanban.js
152
web/portal.css
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
/* Minha conta: the Site's look (tokens and shell from site-shell.css and
|
||||||
|
site-v2.css) for sign-in, the overview, the orders and the details. */
|
||||||
|
[hidden]{display:none!important}
|
||||||
|
body{background:var(--cinza)}
|
||||||
|
.topo .acts a[aria-current="page"]{color:var(--laranja-escuro)}
|
||||||
|
#quem{max-width:240px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||||
|
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
||||||
|
.ped{padding-top:28px;padding-bottom:64px;min-height:70vh}
|
||||||
|
.ped h1{font-size:30px;font-weight:800;margin-bottom:14px}
|
||||||
|
.pedAviso{margin-bottom:14px;padding:12px 16px;border-radius:var(--raio2);background:var(--aviso-fundo);border:1px solid var(--aviso-linha);font-size:15px}
|
||||||
|
.pedMsg{margin-bottom:14px;padding:10px 14px;border-radius:var(--raio2);background:var(--verde-fundo);color:var(--verde);font-size:15px}
|
||||||
|
.pedMsg.erro{background:#FDE8E6;color:#B42318}
|
||||||
|
.pedMsg:empty{display:none}
|
||||||
|
|
||||||
|
/* Buttons, as on the Site. */
|
||||||
|
.bPri{display:inline-flex;align-items:center;justify-content:center;gap:8px;height:48px;padding:0 22px;border:0;border-radius:var(--raio2);background:var(--laranja);color:var(--preto);font-family:"Inter",sans-serif;font-weight:800;font-size:16px;text-decoration:none;cursor:pointer}
|
||||||
|
.bPri:hover:not(:disabled){background:#FFB53D}
|
||||||
|
.bPri.bMini{height:38px;padding:0 16px;font-size:15px}
|
||||||
|
.bSec{display:inline-flex;align-items:center;justify-content:center;gap:7px;height:40px;padding:0 16px;border:1px solid #D6D6D6;border-radius:var(--raio2);background:#fff;color:var(--preto);font-family:"Inter",sans-serif;font-weight:700;font-size:15px;cursor:pointer}
|
||||||
|
.bSec:hover:not(:disabled){border-color:var(--preto)}
|
||||||
|
.bSec .ic{width:18px;height:18px}
|
||||||
|
.bLink{align-self:flex-start;margin-top:12px;padding:0;border:0;background:none;color:var(--laranja-escuro);font-family:inherit;font-weight:600;font-size:15px;cursor:pointer;text-decoration:underline;text-underline-offset:3px}
|
||||||
|
button:disabled{opacity:.6;cursor:default}
|
||||||
|
|
||||||
|
/* Forms. */
|
||||||
|
.acessoGrid{display:grid;grid-template-columns:repeat(auto-fit,minmax(300px,1fr));gap:16px;align-items:start}
|
||||||
|
.bloco{display:flex;flex-direction:column;margin-bottom:16px;padding:22px;border:1px solid var(--linha);border-radius:var(--raio);background:#fff}
|
||||||
|
.bloco h2{font-size:20px;font-weight:800}
|
||||||
|
.nota2{margin:4px 0 8px;font-size:14.5px;color:var(--fraco)}
|
||||||
|
.nota2 a{color:var(--laranja-escuro)}
|
||||||
|
.bloco label{margin:10px 0 5px;font-size:14px;font-weight:600;color:var(--texto2)}
|
||||||
|
.bloco label .obs{font-weight:400;color:var(--fraco)}
|
||||||
|
.bloco input,.busca2 input,.busca2 select,.correcaoForm textarea{width:100%;height:46px;padding:0 13px;border:1px solid #D6D6D6;border-radius:var(--raio2);font:inherit;font-size:16px;background:#fff;color:var(--preto)}
|
||||||
|
.bloco input[readonly]{background:var(--cinza);color:var(--texto2)}
|
||||||
|
.bloco input:focus,.busca2 input:focus,.busca2 select:focus,.correcaoForm textarea:focus{outline:none;border-color:var(--laranja);box-shadow:0 0 0 3px rgba(255,168,26,.2)}
|
||||||
|
.bloco>.bPri,.bloco>.bSec{margin-top:16px;align-self:flex-start}
|
||||||
|
.duas{display:grid;grid-template-columns:1fr 1fr;gap:0 12px}
|
||||||
|
.tres{display:grid;grid-template-columns:1fr 1fr 90px;gap:0 12px}
|
||||||
|
.duas>div,.tres>div{display:flex;flex-direction:column}
|
||||||
|
|
||||||
|
/* The account area: menu on the side, one view beside it. */
|
||||||
|
.area{display:grid;grid-template-columns:210px minmax(0,1fr);gap:28px;align-items:start}
|
||||||
|
.lateral{position:sticky;top:90px;display:flex;flex-direction:column;gap:2px;padding:10px;border:1px solid var(--linha);border-radius:var(--raio);background:#fff}
|
||||||
|
.lateral a,.lateral button{display:block;padding:10px 12px;border:0;border-radius:var(--raio2);background:none;color:var(--texto2);font:inherit;font-size:15.5px;text-align:left;text-decoration:none;cursor:pointer}
|
||||||
|
.lateral a:hover,.lateral button:hover{background:var(--cinza);color:var(--preto)}
|
||||||
|
.lateral a.on{background:var(--aviso-fundo);color:var(--laranja-escuro);font-weight:700}
|
||||||
|
.lateral button{margin-top:6px;border-top:1px solid var(--linha);border-radius:0 0 var(--raio2) var(--raio2)}
|
||||||
|
.conta{margin:-8px 0 16px;font-size:15px;color:var(--fraco)}
|
||||||
|
.acessoIntro{margin:-6px 0 14px;font-size:15px;color:var(--texto2)}
|
||||||
|
.acessoIntro:empty{display:none}
|
||||||
|
.tituloGrupo{margin:22px 0 10px;font-size:18px;font-weight:800}
|
||||||
|
|
||||||
|
/* Overview. */
|
||||||
|
.metricas{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:12px}
|
||||||
|
.metrica{display:flex;flex-direction:column;gap:4px;padding:16px 18px;border:1px solid var(--linha);border-radius:var(--raio);background:#fff;color:var(--texto2);font-size:14.5px;text-decoration:none}
|
||||||
|
.metrica:hover{border-color:var(--laranja)}
|
||||||
|
.metrica b{font-family:"Inter",sans-serif;font-size:28px;font-weight:800;color:var(--preto)}
|
||||||
|
.metrica.alerta{border-color:#F3B7A9;background:#FFF6F4}
|
||||||
|
.verTodos{display:inline-block;margin-top:12px;color:var(--laranja-escuro);font-weight:700;text-decoration:none}
|
||||||
|
|
||||||
|
/* Orders: filters, rows, pages. */
|
||||||
|
.filtros{display:flex;gap:8px;flex-wrap:wrap;margin-bottom:10px}
|
||||||
|
.chipF{height:36px;padding:0 14px;border:1px solid #D6D6D6;border-radius:999px;background:#fff;color:var(--texto2);font:inherit;font-size:14.5px;font-weight:600;cursor:pointer}
|
||||||
|
.chipF:hover{border-color:var(--preto)}
|
||||||
|
.chipF.on{border-color:var(--laranja);background:var(--aviso-fundo);color:var(--laranja-escuro)}
|
||||||
|
.busca2{display:grid;grid-template-columns:minmax(0,1fr) 200px;gap:10px;margin-bottom:12px}
|
||||||
|
.lista{border:1px solid var(--linha);border-radius:var(--raio);background:#fff;overflow:hidden}
|
||||||
|
.lista:empty{display:none}
|
||||||
|
.item+.item{border-top:1px solid var(--linha)}
|
||||||
|
.item.pendente{background:#FFFCF6}
|
||||||
|
.linha{display:grid;grid-template-columns:90px minmax(0,1fr) auto 110px 90px;gap:14px;align-items:center;padding:14px 18px;cursor:pointer}
|
||||||
|
.item.pendente .linha{cursor:default}
|
||||||
|
.linha:hover{background:var(--cinza)}
|
||||||
|
.linha .num{font-family:"Inter",sans-serif;font-size:16px}
|
||||||
|
.oque{display:flex;flex-direction:column;min-width:0}
|
||||||
|
.oque span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:15px}
|
||||||
|
.oque .data{font-size:14px;color:var(--fraco)}
|
||||||
|
.estadoW{display:flex;align-items:center;gap:6px}
|
||||||
|
.estado{padding:4px 10px;border-radius:999px;font-family:"Inter",sans-serif;font-size:13.5px;font-weight:700;background:var(--cinza);color:var(--texto2);white-space:nowrap}
|
||||||
|
.via{font-size:13px;font-weight:700;color:var(--fraco)}
|
||||||
|
.est-fin{background:var(--verde-fundo);color:var(--verde)}
|
||||||
|
.est-cor{background:#FDE8E6;color:#B42318}
|
||||||
|
.est-pay{background:#FFF1D6;color:#8A4B00}
|
||||||
|
.est-rev{background:#EEF1F5;color:#45505E}
|
||||||
|
.est-imp,.est-fil,.est-tra,.est-rec{background:#E7F0FB;color:#1D4E89}
|
||||||
|
.linha .valor{font-family:"Inter",sans-serif;font-size:16px;text-align:right}
|
||||||
|
.acao{display:flex;justify-content:flex-end}
|
||||||
|
.acao .bSec{height:36px;padding:0 14px}
|
||||||
|
.paginas{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-top:12px}
|
||||||
|
.pags{display:flex;gap:6px}
|
||||||
|
.pag{min-width:38px;height:38px;border:1px solid #D6D6D6;border-radius:var(--raio2);background:#fff;font:inherit;font-weight:700;cursor:pointer}
|
||||||
|
.pag.on{border-color:var(--laranja);background:var(--aviso-fundo);color:var(--laranja-escuro);cursor:default}
|
||||||
|
.vazio{display:flex;flex-direction:column;align-items:flex-start;gap:12px;padding:26px 22px;font-size:16px;color:var(--texto2)}
|
||||||
|
|
||||||
|
/* An order opened in the list. */
|
||||||
|
.detalhe{padding:4px 18px 18px;border-top:1px dashed var(--linha);background:#fff}
|
||||||
|
.trilha{display:flex;list-style:none;margin:18px 0 6px;padding:0}
|
||||||
|
.trilha li{flex:1;position:relative;display:flex;flex-direction:column;align-items:center;gap:6px;text-align:center;font-size:14px;color:var(--fraco)}
|
||||||
|
.trilha li::before{content:"";position:absolute;top:14px;left:-50%;width:100%;height:2px;background:#E0E0E0;z-index:0}
|
||||||
|
.trilha li:first-child::before{display:none}
|
||||||
|
.trilha li.feito::before,.trilha li.atual::before{background:var(--verde)}
|
||||||
|
.trilha .marca{position:relative;z-index:1;width:30px;height:30px;border-radius:50%;display:flex;align-items:center;justify-content:center;border:2px solid #D0D0D0;background:#fff;font-family:"Inter",sans-serif;font-weight:700;font-size:14px}
|
||||||
|
.trilha .marca .ic{width:16px;height:16px;stroke-width:3}
|
||||||
|
.trilha li.feito{color:var(--verde)}
|
||||||
|
.trilha li.feito .marca{background:var(--verde-fundo);border-color:var(--verde-fundo);color:var(--verde)}
|
||||||
|
.trilha li.atual{color:var(--preto)}
|
||||||
|
.trilha li.atual .marca{background:var(--laranja);border-color:var(--laranja)}
|
||||||
|
.trilha b{font-weight:600}
|
||||||
|
.trilha li.atual b{font-weight:800}
|
||||||
|
.pedAlerta{display:flex;gap:10px;align-items:flex-start;margin:16px 0 4px;padding:12px 14px;border-radius:var(--raio2);background:#FDE8E6;color:#7A1C10;font-size:15px}
|
||||||
|
.pedAlerta .ic{flex:none;width:20px;height:20px;margin-top:1px;color:#B42318}
|
||||||
|
.itensPed{list-style:none;margin:14px 0 0;padding:0;border-top:1px solid var(--linha)}
|
||||||
|
.itensPed li{display:flex;gap:12px;align-items:baseline;padding:10px 0;border-bottom:1px solid var(--linha);font-size:15.5px}
|
||||||
|
.itensPed .nome{flex:1;font-weight:600}
|
||||||
|
.itensPed .qt{color:var(--fraco)}
|
||||||
|
.itensPed b{min-width:96px;text-align:right;font-family:"Inter",sans-serif}
|
||||||
|
.itensPed .frete .nome{font-weight:400;color:var(--texto2)}
|
||||||
|
.entregaPed{display:flex;align-items:center;gap:8px;margin-top:12px;font-size:15px;color:var(--texto2)}
|
||||||
|
.entregaPed .ic{flex:none;width:19px;height:19px;color:var(--laranja-escuro)}
|
||||||
|
.secao{margin-top:16px}
|
||||||
|
.secao h4{margin-bottom:8px;font-size:16px;font-weight:800}
|
||||||
|
.historico{list-style:none;margin:0;padding:0 0 0 14px;border-left:2px solid var(--linha)}
|
||||||
|
.historico li{position:relative;padding:0 0 12px 12px;font-size:15px}
|
||||||
|
.historico li::before{content:"";position:absolute;left:-20px;top:6px;width:10px;height:10px;border-radius:50%;background:var(--laranja)}
|
||||||
|
.historico li b{margin-right:8px}
|
||||||
|
.historico li span{color:var(--fraco)}
|
||||||
|
.historico .motivo{margin-top:4px;padding:8px 10px;border-radius:8px;background:#FDE8E6;font-size:14.5px}
|
||||||
|
.arquivo{display:flex;align-items:center;gap:12px;padding:10px 0;border-bottom:1px solid var(--linha)}
|
||||||
|
.arquivo>div{flex:1;min-width:0;display:flex;flex-direction:column}
|
||||||
|
.arquivo b{overflow-wrap:anywhere;font-size:15px}
|
||||||
|
.arquivo span{font-size:14px;color:var(--fraco)}
|
||||||
|
.correcaoForm{display:flex;flex-direction:column;padding:16px;border-radius:var(--raio2);background:var(--cinza)}
|
||||||
|
.correcaoForm label{margin:10px 0 5px;font-size:14px;font-weight:600;color:var(--texto2)}
|
||||||
|
.correcaoForm textarea{height:auto;padding:10px 13px;resize:vertical}
|
||||||
|
.correcaoForm .bPri{margin-top:14px;align-self:flex-start}
|
||||||
|
|
||||||
|
@media (max-width:820px){
|
||||||
|
.area{grid-template-columns:1fr;gap:16px}
|
||||||
|
.lateral{position:static;flex-direction:row;flex-wrap:wrap}
|
||||||
|
.lateral button{margin:0;border:0;border-radius:var(--raio2)}
|
||||||
|
.linha{grid-template-columns:minmax(0,1fr) auto;gap:6px 12px}
|
||||||
|
.linha .num{grid-column:1}
|
||||||
|
.linha .oque{grid-column:1/-1;grid-row:2}
|
||||||
|
.linha .estadoW{grid-column:2;grid-row:1;justify-self:end}
|
||||||
|
.linha .valor{grid-column:1;grid-row:3;text-align:left}
|
||||||
|
.linha .acao{grid-column:2;grid-row:3}
|
||||||
|
}
|
||||||
|
@media (max-width:600px){
|
||||||
|
.ped h1{font-size:26px}
|
||||||
|
.trilha b{font-size:12.5px}
|
||||||
|
.duas,.tres,.busca2{grid-template-columns:1fr}
|
||||||
|
}
|
||||||
177
web/portal.html
@@ -1,11 +1,166 @@
|
|||||||
<!doctype html><html lang="pt-BR"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Dropstar · Meus pedidos DTF</title>
|
<!DOCTYPE html>
|
||||||
<style>
|
<html lang="pt-BR">
|
||||||
*{box-sizing:border-box}body{margin:0;background:#f6f7f9;color:#20252d;font:15px/1.5 system-ui,sans-serif}header{background:white;border-bottom:1px solid #ddd;padding:20px max(20px,calc((100% - 1100px)/2));display:flex;gap:24px;align-items:center;flex-wrap:wrap}.brand{color:#ef8500;font-size:24px;font-style:italic;font-weight:900}a{color:#8a4e00}main{max-width:1100px;margin:24px auto;padding:0 20px}h1{font-size:28px}h2{font-size:21px}h3{font-size:17px}.notice{background:#fff1d4;border-left:4px solid #ffa900;padding:12px 16px}.grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(280px,1fr));gap:20px}.card{background:white;border:1px solid #ddd;border-radius:10px;padding:20px;margin:16px 0}.card p{overflow-wrap:anywhere}.muted{color:#596471}label{display:block;margin:10px 0}input,textarea,button{font:inherit;border:1px solid #9aa0a8;border-radius:6px;padding:10px}input:not([type=file]),textarea{width:100%}button{background:#ffa900;border-color:#ffa900;cursor:pointer}button:disabled{opacity:.6}form{margin:10px 0}.actions{display:flex;gap:10px;flex-wrap:wrap;align-items:center}#message{min-height:24px;color:#9b3800}li{margin:8px 0}.tag{background:#eef2f6;padding:5px 10px;border-radius:6px}details{margin:12px 0}[hidden]{display:none!important}
|
<head>
|
||||||
</style></head><body>
|
<meta charset="utf-8">
|
||||||
<header><a class="brand" href="/">DROPSTAR</a><a href="/">Enviar arte</a><span>Minha conta · DTF</span><button id="logout" hidden>Sair</button></header>
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<main><h1>Meus pedidos DTF</h1><p class="notice">Ambiente de desenvolvimento local. Pagamentos e notificações são simulados. Use apenas dados de teste.</p><p id="message" role="status"></p>
|
<title>Minha conta · DTF Dropstar</title>
|
||||||
<div id="account"></div><section id="auth" class="grid">
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
<form id="login" class="card"><h2>Entrar</h2><label>E-mail <input id="email" type="email" autocomplete="username" required></label><label>Senha <input id="password" type="password" autocomplete="current-password" required></label><button>Entrar</button></form>
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||||
<form id="register" class="card"><h2>Criar conta local</h2><label>CNPJ <input id="cnpj" required></label><label>WhatsApp <input id="phone" required></label><label>E-mail <input id="register-email" type="email" autocomplete="email" required></label><label>Senha (12 caracteres ou mais) <input id="register-password" type="password" minlength="12" maxlength="128" autocomplete="new-password" required></label><button>Criar conta</button><p class="muted">Pedidos desta sessão serão associados à sua conta. Não há envio de e-mail nem recuperação de senha nesta versão local.</p></form>
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
|
||||||
</section><div class="actions"><h2>Acompanhamento</h2><button id="refresh">Atualizar pedidos</button></div><p id="guest" class="muted"></p><section id="quotes"></section><section id="orders"></section></main>
|
<link rel="stylesheet" href="/site-shell.css">
|
||||||
<script src="/privacy.js"></script><script src="/upload.js"></script><script src="/portal.js"></script></body></html>
|
<link rel="stylesheet" href="/site-v2.css">
|
||||||
|
<link rel="stylesheet" href="/portal.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="tarja">
|
||||||
|
ENVIE SEU ARQUIVO DTF PELO SITE · DTF TÊXTIL COM O MENOR PREÇO DO METRO EM FRANCA-SP
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<header class="topo"><div class="w">
|
||||||
|
<a class="logo" href="https://www.dropstaratacado.com.br" aria-label="Dropstar Atacado"><img src="https://cdn.vnda.com.br/dropstar/2025/12/17/logo-header-15125700-16129080.png?v=1766009286" alt="Dropstar"></a>
|
||||||
|
<div class="acts">
|
||||||
|
<a href="/conta" aria-current="page"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg><span class="actTx" id="quem">Minha conta</span></a>
|
||||||
|
<a href="/carrinho"><svg class="ic" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/></svg><span class="actTx">Carrinho</span></a>
|
||||||
|
</div>
|
||||||
|
</div></header>
|
||||||
|
|
||||||
|
<main class="ped w">
|
||||||
|
<p class="pedAviso" id="dev-notice" hidden>Ambiente de desenvolvimento local. Pagamentos e notificações são simulados. Use apenas dados de teste.</p>
|
||||||
|
<p class="pedMsg" id="message" role="status"></p>
|
||||||
|
|
||||||
|
<!-- /conta/entrar -->
|
||||||
|
<section class="acesso" id="auth" hidden aria-labelledby="tEntrar">
|
||||||
|
<h1 id="tEntrar">Minha conta</h1>
|
||||||
|
<div class="acessoGrid">
|
||||||
|
<form class="bloco" id="login">
|
||||||
|
<h2>Entrar</h2>
|
||||||
|
<p class="nota2">Para ver seus pedidos e seus dados.</p>
|
||||||
|
<label for="email">E-mail</label>
|
||||||
|
<input id="email" type="email" autocomplete="username" required>
|
||||||
|
<label for="password">Senha</label>
|
||||||
|
<input id="password" type="password" autocomplete="current-password" required>
|
||||||
|
<button class="bPri">Entrar</button>
|
||||||
|
<button type="button" class="bLink" id="esqueci">Esqueci minha senha</button>
|
||||||
|
<p class="nota2" id="esqueciTx" hidden>A recuperação por e-mail chega em breve. Enquanto isso, fale com a Dropstar pelo WhatsApp <a href="https://wa.me/message/NJ76CLIOUGXLB1" target="_blank" rel="noopener">16 99428-0056</a>.</p>
|
||||||
|
</form>
|
||||||
|
<form class="bloco" id="register">
|
||||||
|
<h2>Criar conta</h2>
|
||||||
|
<p class="nota2">Os pedidos feitos neste navegador passam para a sua conta.</p>
|
||||||
|
<div class="duas">
|
||||||
|
<div><label for="cnpj">CNPJ</label><input id="cnpj" inputmode="numeric" autocomplete="off" required></div>
|
||||||
|
<div><label for="phone">WhatsApp</label><input id="phone" inputmode="numeric" autocomplete="tel" required></div>
|
||||||
|
</div>
|
||||||
|
<label for="register-email">E-mail</label>
|
||||||
|
<input id="register-email" type="email" autocomplete="email" required>
|
||||||
|
<label for="register-password">Senha <span class="obs">12 caracteres ou mais</span></label>
|
||||||
|
<input id="register-password" type="password" minlength="12" maxlength="128" autocomplete="new-password" required>
|
||||||
|
<button class="bPri">Criar conta</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- /conta, /conta/pedidos, /conta/dados -->
|
||||||
|
<div class="area" id="area" hidden>
|
||||||
|
<nav class="lateral" aria-label="Minha conta">
|
||||||
|
<a href="/conta" data-view="geral" data-conta>Visão geral</a>
|
||||||
|
<a href="/conta/pedidos" data-view="pedidos">Pedidos</a>
|
||||||
|
<a href="/conta/dados" data-view="dados" data-conta>Dados da conta</a>
|
||||||
|
<a href="/conta/entrar" data-view="entrar" id="navEntrar">Entrar</a>
|
||||||
|
<button type="button" id="logout" hidden>Sair</button>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
<section class="vista" id="vGeral" hidden aria-labelledby="tGeral">
|
||||||
|
<h1 id="tGeral">Olá</h1>
|
||||||
|
<p class="conta" id="account"></p>
|
||||||
|
<div class="metricas" id="metricas"></div>
|
||||||
|
<h2 class="tituloGrupo">Último pedido</h2>
|
||||||
|
<div class="lista" id="ultimo"></div>
|
||||||
|
<a class="verTodos" href="/conta/pedidos" data-view="pedidos">Ver todos os pedidos ›</a>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="vista" id="vPedidos" hidden aria-labelledby="tPedidos">
|
||||||
|
<h1 id="tPedidos">Pedidos</h1>
|
||||||
|
<p class="acessoIntro" id="guest"></p>
|
||||||
|
<div class="filtros" id="filtros" role="group" aria-label="Filtrar pedidos"></div>
|
||||||
|
<div class="busca2">
|
||||||
|
<label class="sr" for="fNumero">Código do pedido</label>
|
||||||
|
<input id="fNumero" autocomplete="off" placeholder="Código do pedido">
|
||||||
|
<label class="sr" for="fPeriodo">Período</label>
|
||||||
|
<select id="fPeriodo">
|
||||||
|
<option value="30">Últimos 30 dias</option>
|
||||||
|
<option value="90">Últimos 90 dias</option>
|
||||||
|
<option value="365">Último ano</option>
|
||||||
|
<option value="0" selected>Todo o período</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="lista" id="orders"></div>
|
||||||
|
<div class="paginas" id="paginas"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="vista" id="vDados" hidden aria-labelledby="tDados">
|
||||||
|
<h1 id="tDados">Dados da conta</h1>
|
||||||
|
<form class="bloco" id="fPerfil">
|
||||||
|
<h2>Empresa e entrega</h2>
|
||||||
|
<div class="duas">
|
||||||
|
<div><label for="dCnpj">CNPJ</label><input id="dCnpj" readonly aria-readonly="true"></div>
|
||||||
|
<div><label for="dZap">WhatsApp</label><input id="dZap" inputmode="numeric" autocomplete="tel" required></div>
|
||||||
|
</div>
|
||||||
|
<div class="duas">
|
||||||
|
<div><label for="dCep">CEP</label><input id="dCep" inputmode="numeric" maxlength="9" placeholder="00000-000"></div>
|
||||||
|
<div><label for="dNome">Quem recebe</label><input id="dNome" maxlength="120"></div>
|
||||||
|
</div>
|
||||||
|
<label for="dRua">Rua / avenida</label><input id="dRua" maxlength="160">
|
||||||
|
<div class="duas">
|
||||||
|
<div><label for="dNum">Número</label><input id="dNum" maxlength="20"></div>
|
||||||
|
<div><label for="dComp">Complemento (opcional)</label><input id="dComp" maxlength="80"></div>
|
||||||
|
</div>
|
||||||
|
<div class="tres">
|
||||||
|
<div><label for="dBairro">Bairro</label><input id="dBairro" maxlength="80"></div>
|
||||||
|
<div><label for="dCidade">Cidade</label><input id="dCidade" maxlength="80"></div>
|
||||||
|
<div><label for="dUf">UF</label><input id="dUf" maxlength="2"></div>
|
||||||
|
</div>
|
||||||
|
<button class="bPri">Salvar</button>
|
||||||
|
</form>
|
||||||
|
<form class="bloco" id="fEmail">
|
||||||
|
<h2>E-mail</h2>
|
||||||
|
<label for="dMail">E-mail</label><input id="dMail" type="email" autocomplete="email" required>
|
||||||
|
<label for="dMailSenha">Senha atual, para confirmar</label><input id="dMailSenha" type="password" autocomplete="current-password" required>
|
||||||
|
<button class="bSec">Alterar e-mail</button>
|
||||||
|
</form>
|
||||||
|
<form class="bloco" id="fSenha">
|
||||||
|
<h2>Senha</h2>
|
||||||
|
<label for="dAtual">Senha atual</label><input id="dAtual" type="password" autocomplete="current-password" required>
|
||||||
|
<label for="dNova">Nova senha <span class="obs">12 caracteres ou mais</span></label><input id="dNova" type="password" minlength="12" maxlength="128" autocomplete="new-password" required>
|
||||||
|
<button class="bSec">Alterar senha</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer><div class="w">
|
||||||
|
<div class="frow">
|
||||||
|
<span class="fmarca"><img src="https://cdn.vnda.com.br/dropstar/2025/12/17/logo-header-15125700-16129080.png?v=1766009286" alt="Dropstar">DTF</span>
|
||||||
|
<div class="flinks">
|
||||||
|
<a href="https://www.dropstaratacado.com.br" target="_blank" rel="noopener">Loja de atacado</a>
|
||||||
|
<a href="https://www.dropstaratacado.com.br/p/termos" target="_blank" rel="noopener">Trocas e privacidade</a>
|
||||||
|
<a href="https://www.dropstaratacado.com.br/p/atendimento" target="_blank" rel="noopener">Fale conosco</a>
|
||||||
|
<a href="https://www.instagram.com/dropstar.ofc/" target="_blank" rel="noopener">Instagram</a>
|
||||||
|
</div>
|
||||||
|
<div class="fcont">
|
||||||
|
<a href="https://wa.me/message/NJ76CLIOUGXLB1" target="_blank" rel="noopener">16 99428-0056</a>
|
||||||
|
· arquivos 16 99230-2546<br>
|
||||||
|
<a href="mailto:atendimentodropstar@gmail.com">atendimentodropstar@gmail.com</a><br>
|
||||||
|
Segunda a sexta, 07h às 17h
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="fbase">
|
||||||
|
<span>© Dropstar Atacado · CNPJ 55.786.635/0001-58 · Franca-SP</span>
|
||||||
|
<span>Visa · Master · Elo · Amex · Pix · Boleto</span>
|
||||||
|
<span>têxtil 57 cm · UV 28,5 cm</span>
|
||||||
|
</div>
|
||||||
|
</div></footer>
|
||||||
|
|
||||||
|
<script src="/privacy.js"></script><script src="/upload.js"></script><script src="/portal.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|||||||
379
web/portal.js
@@ -1,61 +1,327 @@
|
|||||||
|
/* Minha conta: sign-in, the overview, the orders (filtered, a page at a time,
|
||||||
|
with the cart waiting for payment among them) and the account's details.
|
||||||
|
One page; the address says which part shows (/conta, /conta/entrar,
|
||||||
|
/conta/pedidos, /conta/dados). */
|
||||||
const $=id=>document.getElementById(id);
|
const $=id=>document.getElementById(id);
|
||||||
const node=(tag,text)=>{const el=document.createElement(tag);if(text!==undefined)el.textContent=text;return el;};
|
const node=(tag,text,className)=>{const el=document.createElement(tag);if(text!==undefined&&text!==null)el.textContent=text;if(className)el.className=className;return el;};
|
||||||
const money=c=>(c/100).toLocaleString('pt-BR',{style:'currency',currency:'BRL'});
|
const money=c=>(c/100).toLocaleString('pt-BR',{style:'currency',currency:'BRL'});
|
||||||
let scope,states={};
|
const quando=d=>new Date(d).toLocaleString('pt-BR',{day:'2-digit',month:'2-digit',year:'numeric',hour:'2-digit',minute:'2-digit'});
|
||||||
|
const dia=d=>new Date(d).toLocaleDateString('pt-BR',{day:'2-digit',month:'2-digit',year:'2-digit'});
|
||||||
|
const metros=m=>Number(m).toLocaleString('pt-BR',{minimumFractionDigits:2,maximumFractionDigits:2})+' m';
|
||||||
|
const soDigitos=v=>String(v||'').replace(/\D/g,'');
|
||||||
|
const fmtCnpj=v=>soDigitos(v).replace(/^(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/,'$1.$2.$3/$4-$5');
|
||||||
|
const fmtZap=v=>{const d=soDigitos(v);return d.length===11?'('+d.slice(0,2)+') '+d.slice(2,7)+'-'+d.slice(7):d.length===10?'('+d.slice(0,2)+') '+d.slice(2,6)+'-'+d.slice(6):v;};
|
||||||
|
const fmtCep=v=>{const d=soDigitos(v).slice(0,8);return d.length>5?d.slice(0,5)+'-'+d.slice(5):d;};
|
||||||
|
// Lucide shapes (ISC licence), the same as on the Site.
|
||||||
|
const ICONES={
|
||||||
|
baixar:'<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/><path d="M12 15V3"/>',
|
||||||
|
local:'<path d="M20 10c0 4.99-5.54 10.19-7.4 11.8a1 1 0 0 1-1.2 0C9.54 20.19 4 14.99 4 10a8 8 0 0 1 16 0"/><circle cx="12" cy="10" r="3"/>',
|
||||||
|
caminhao:'<path d="M14 18V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v11a1 1 0 0 0 1 1h2"/><path d="M15 18H9"/><path d="M19 18h2a1 1 0 0 0 1-1v-3.65a1 1 0 0 0-.22-.62l-3.48-4.35A1 1 0 0 0 17.52 8H14"/><circle cx="17" cy="18" r="2"/><circle cx="7" cy="18" r="2"/>',
|
||||||
|
ok:'<path d="M20 6 9 17l-5-5"/>',
|
||||||
|
alerta:'<path d="M12 7v6"/><path d="M12 17h.01"/>',
|
||||||
|
};
|
||||||
|
function icone(nome){
|
||||||
|
const span=document.createElement('span');
|
||||||
|
span.innerHTML='<svg class="ic" viewBox="0 0 24 24" aria-hidden="true" focusable="false">'+ICONES[nome]+'</svg>';
|
||||||
|
return span.firstChild;
|
||||||
|
}
|
||||||
|
const PRODUTO={file:'DTF Têxtil · folha montada',avulsa:'DTF Têxtil · artes avulsas',
|
||||||
|
uvfile:'DTF UV · folha montada',uv:'DTF UV · artes avulsas'};
|
||||||
|
const TRILHA=['rec','tra','fil','imp','fin'];
|
||||||
|
const TAMANHO=10;
|
||||||
|
let scope,states={},environment='',conta=null;
|
||||||
|
let filtro={status:'all',code:'',days:0,page:1};
|
||||||
|
|
||||||
async function api(path,body){const r=await fetch('/api'+path,{headers:{'Content-Type':'application/json'},...(body===undefined?{}:{method:'POST',body:JSON.stringify(body)})});const d=await r.json();if(!r.ok){const error=new Error(typeof d.detail==='string'?d.detail:'Confira os campos informados.');error.status=r.status;throw error;}return d;}
|
async function api(path,body){const r=await fetch('/api'+path,{headers:{'Content-Type':'application/json'},...(body===undefined?{}:{method:'POST',body:JSON.stringify(body)})});const d=await r.json();if(!r.ok){const error=new Error(typeof d.detail==='string'?d.detail:'Confira os campos informados.');error.status=r.status;throw error;}return d;}
|
||||||
function button(text,fn){const b=node('button',text);b.onclick=async()=>{b.disabled=true;try{await fn();}catch(e){$('message').textContent=e.message;}finally{b.disabled=false;}};return b;}
|
const aviso=(text,erro)=>{$('message').textContent=text||'';$('message').classList.toggle('erro',!!erro);};
|
||||||
|
function button(text,fn,className='bSec'){const b=node('button',text,className);b.type='button';b.onclick=async event=>{event?.stopPropagation?.();b.disabled=true;try{await fn();}catch(e){aviso(e.message,true);}finally{b.disabled=false;}};return b;}
|
||||||
function forgetCheckout(){for(const k of ['dtf-quote','dtf-request-key','dtf-request-body'])localStorage.removeItem(k);}
|
function forgetCheckout(){for(const k of ['dtf-quote','dtf-request-key','dtf-request-body'])localStorage.removeItem(k);}
|
||||||
async function load(){
|
|
||||||
try{
|
// ---- Addresses -----------------------------------------------------------
|
||||||
scope=(await api('/session')).cart_scope;
|
|
||||||
const account=await api('/account/me');
|
function vista(){
|
||||||
$('auth').hidden=!!account.customer;$('logout').hidden=!account.customer;
|
const p=location.pathname.replace(/\/+$/,'');
|
||||||
$('account').textContent=account.customer?'Conta: '+account.customer.mail:'';
|
if(p==='/conta/entrar') return 'entrar';
|
||||||
$('guest').textContent=account.customer?'':'Você está vendo apenas os pedidos desta sessão. Crie uma conta para acessar seus pedidos em outro navegador.';
|
if(p==='/conta/dados') return 'dados';
|
||||||
const data=await api('/customer/orders');states=data.states;
|
if(p==='/conta') return 'geral';
|
||||||
$('quotes').replaceChildren();
|
return 'pedidos'; // /conta/pedidos, and the old /portal.html
|
||||||
for(const quote of data.quotes){
|
|
||||||
const card=node('article');card.className='card';card.append(node('h3','Cotação '+quote.id.slice(0,8)));
|
|
||||||
card.append(node('p',quote.approved?'Total aprovado: '+money(quote.approved.total_cents):'Aguardando conferência de metragem e nota.'));
|
|
||||||
const link=node('a','Abrir cotação no Site');link.href='/?quote='+quote.id;card.append(link);$('quotes').append(card);
|
|
||||||
}
|
}
|
||||||
$('orders').replaceChildren();
|
function ir(caminho){history.pushState({},'',caminho);mostra().catch(e=>aviso(e.message,true));}
|
||||||
if(!data.orders.length)$('orders').append(node('p','Nenhum pedido pago nesta conta ou sessão.'));
|
window.addEventListener('popstate',()=>mostra().catch(e=>aviso(e.message,true)));
|
||||||
for(const order of data.orders){
|
document.addEventListener('click',event=>{
|
||||||
const card=node('article');card.className='card';card.id='order-'+order.id;
|
const a=event.target.closest('a[data-view]');
|
||||||
card.append(node('h3','Pedido #'+order.number),node('p',states[order.state]+' · '+money(order.snapshot.total_cents)+' · pagamento local'));
|
if(!a||event.metaKey||event.ctrlKey||event.shiftKey)return;
|
||||||
card.append(node('p',new Date(order.created_at).toLocaleString('pt-BR')));
|
event.preventDefault();ir(a.getAttribute('href'));
|
||||||
const content=node('div');
|
});
|
||||||
card.append(button('Ver detalhes e arquivos',()=>details(order.id,content)),content);$('orders').append(card);
|
|
||||||
if(new URLSearchParams(location.search).get('order')===order.id)await details(order.id,content);
|
async function mostra(){
|
||||||
|
aviso('');
|
||||||
|
const v=vista(), proximo=new URLSearchParams(location.search).get('next');
|
||||||
|
// Overview and details are the account's; orders also work for a guest.
|
||||||
|
if((v==='geral'||v==='dados')&&!conta){history.replaceState({},'','/conta/entrar?next='+encodeURIComponent(location.pathname));return mostra();}
|
||||||
|
if(v==='entrar'&&conta){history.replaceState({},'',proximo&&/^\/conta(\/[a-z]+)?$/.test(proximo)?proximo:'/conta');return mostra();}
|
||||||
|
$('auth').hidden=v!=='entrar';
|
||||||
|
$('area').hidden=v==='entrar';
|
||||||
|
$('vGeral').hidden=v!=='geral';$('vPedidos').hidden=v!=='pedidos';$('vDados').hidden=v!=='dados';
|
||||||
|
for(const a of document.querySelectorAll('.lateral a'))a.classList.toggle('on',a.dataset.view===v);
|
||||||
|
for(const a of document.querySelectorAll('.lateral [data-conta]'))a.hidden=!conta;
|
||||||
|
$('navEntrar').hidden=!!conta;$('logout').hidden=!conta;
|
||||||
|
document.title=({entrar:'Entrar',geral:'Minha conta',pedidos:'Pedidos',dados:'Dados da conta'})[v]+' · DTF Dropstar';
|
||||||
|
if(v==='geral')await geral();
|
||||||
|
else if(v==='pedidos')await pedidos();
|
||||||
|
else if(v==='dados')dados();
|
||||||
}
|
}
|
||||||
}catch(error){$('message').textContent=error.message;}
|
|
||||||
|
// ---- Orders --------------------------------------------------------------
|
||||||
|
|
||||||
|
function trilha(state){
|
||||||
|
const ol=node('ol',null,'trilha');
|
||||||
|
const atual=TRILHA.indexOf(state);
|
||||||
|
TRILHA.forEach((s,i)=>{
|
||||||
|
const feito=i<atual||state==='fin';
|
||||||
|
const li=node('li',null,feito?'feito':i===atual?'atual':'');
|
||||||
|
const marca=node('span',null,'marca');
|
||||||
|
if(feito)marca.append(icone('ok'));else marca.textContent=String(i+1);
|
||||||
|
li.append(marca,node('b',states[s]||s));ol.append(li);
|
||||||
|
});
|
||||||
|
return ol;
|
||||||
}
|
}
|
||||||
async function details(id,container){
|
function estado(e){
|
||||||
|
if(e.kind==='quote')return e.state==='review'?['Em conferência','est-rev']:['Aguardando pagamento','est-pay'];
|
||||||
|
if(e.state==='cancelled')return ['Cancelado','est-cor'];
|
||||||
|
return [states[e.state]||e.state,'est-'+e.state];
|
||||||
|
}
|
||||||
|
function resumoItens(items){
|
||||||
|
if(!items.length)return '';
|
||||||
|
const total=items.reduce((t,i)=>t+Number(i.billed_metres||0),0);
|
||||||
|
return (items.length===1?PRODUTO[items[0].mode]||items[0].mode:items.length+' itens')+(total?' · '+metros(total):'');
|
||||||
|
}
|
||||||
|
function linha(e){
|
||||||
|
const row=node('div',null,'linha');
|
||||||
|
const [rotulo,cls]=estado(e);
|
||||||
|
const num=node('b',e.kind==='quote'?'Carrinho':'Pedido '+e.code,'num');
|
||||||
|
const oque=node('div',null,'oque');oque.append(node('span',resumoItens(e.items)),node('span',dia(e.created_at),'data'));
|
||||||
|
const st=node('span',null,'estadoW');st.append(node('span',rotulo,'estado '+cls));
|
||||||
|
if(e.kind==='quote'&&e.pay_with==='pix')st.append(node('span','PIX','via'));
|
||||||
|
const valor=node('b',e.total_cents!=null?money(e.total_cents):'—','valor');
|
||||||
|
const acao=node('div',null,'acao');
|
||||||
|
row.append(num,oque,st,valor,acao);
|
||||||
|
const wrap=node('div',null,'item'+(e.kind==='quote'?' pendente':'')+(e.state==='cor'?' correcao':''));
|
||||||
|
wrap.append(row);
|
||||||
|
if(e.kind==='quote'){
|
||||||
|
if(e.state==='pay'){
|
||||||
|
const pagar=node('a','Pagar','bPri bMini');
|
||||||
|
pagar.href=(e.pay_with==='pix'?'/pagamento/pix':'/pagamento')+'?quote='+e.id;
|
||||||
|
acao.append(pagar);
|
||||||
|
}
|
||||||
|
return wrap;
|
||||||
|
}
|
||||||
|
wrap.id='order-'+e.id;
|
||||||
|
const detalhe=node('div',null,'detalhe');detalhe.hidden=true;
|
||||||
|
const alterna=async()=>{
|
||||||
|
if(!detalhe.hidden){detalhe.hidden=true;ver.textContent='Ver';return;}
|
||||||
|
await details(e.id,detalhe,e.created_at);detalhe.hidden=false;ver.textContent='Fechar';
|
||||||
|
};
|
||||||
|
const ver=button('Ver',alterna);
|
||||||
|
acao.append(ver);
|
||||||
|
row.addEventListener('click',event=>{if(!event.target.closest('button,a'))ver.click();});
|
||||||
|
wrap.append(detalhe);
|
||||||
|
wrap.abrir=async()=>{if(detalhe.hidden)await alterna();};
|
||||||
|
return wrap;
|
||||||
|
}
|
||||||
|
function filtros(counts,groups){
|
||||||
|
const box=$('filtros');box.replaceChildren();
|
||||||
|
const todos=Object.values(counts).reduce((a,b)=>a+b,0);
|
||||||
|
for(const [key,label] of [['all','Todos'],...Object.entries(groups)]){
|
||||||
|
const n=key==='all'?todos:counts[key];
|
||||||
|
const chip=node('button',label+(n?' · '+n:''),'chipF'+(filtro.status===key?' on':''));chip.type='button';
|
||||||
|
chip.setAttribute('aria-pressed',String(filtro.status===key));
|
||||||
|
chip.onclick=()=>{filtro={...filtro,status:key,page:1};pedidos().catch(x=>aviso(x.message,true));};
|
||||||
|
box.append(chip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function paginas(total){
|
||||||
|
const box=$('paginas');box.replaceChildren();
|
||||||
|
const n=Math.ceil(total/TAMANHO);
|
||||||
|
if(n<=1){if(total)box.append(node('span',total+(total===1?' pedido':' pedidos'),'nota2'));return;}
|
||||||
|
const de=(filtro.page-1)*TAMANHO+1, ate=Math.min(total,filtro.page*TAMANHO);
|
||||||
|
box.append(node('span',de+'–'+ate+' de '+total+' pedidos','nota2'));
|
||||||
|
const nav=node('div',null,'pags');
|
||||||
|
const vai=p=>{filtro.page=p;pedidos().then(()=>$('vPedidos').scrollIntoView({block:'start'})).catch(x=>aviso(x.message,true));};
|
||||||
|
const b=(t,p,label)=>{const x=node('button',t,'pag'+(p===filtro.page?' on':''));x.type='button';if(label)x.setAttribute('aria-label',label);
|
||||||
|
if(p===filtro.page)x.setAttribute('aria-current','page');else if(p<1||p>n)x.disabled=true;else x.onclick=()=>vai(p);nav.append(x);};
|
||||||
|
b('‹',filtro.page-1,'Página anterior');
|
||||||
|
for(let p=Math.max(1,filtro.page-2);p<=Math.min(n,filtro.page+2);p++)b(String(p),p);
|
||||||
|
b('›',filtro.page+1,'Próxima página');
|
||||||
|
box.append(nav);
|
||||||
|
}
|
||||||
|
async function pedidos(){
|
||||||
|
$('guest').textContent=conta?'':'Você está vendo os pedidos feitos neste navegador. Entre ou crie uma conta para vê-los em qualquer lugar.';
|
||||||
|
const q=new URLSearchParams({status:filtro.status,days:String(filtro.days),page:String(filtro.page),size:String(TAMANHO)});
|
||||||
|
if(filtro.code)q.set('code',filtro.code);
|
||||||
|
const data=await api('/customer/orders?'+q);states=data.states;
|
||||||
|
filtros(data.counts,data.groups);
|
||||||
|
const lista=$('orders');lista.replaceChildren();
|
||||||
|
if(!data.items.length){
|
||||||
|
const livre=filtro.status==='all'&&!filtro.code&&!filtro.days;
|
||||||
|
const vazio=node('div',null,'vazio');
|
||||||
|
vazio.append(node('p',livre?'Seus pedidos aparecem aqui.':'Nenhum pedido com esse filtro.'));
|
||||||
|
if(livre){const a=node('a','Enviar uma arte','bPri');a.href='/#envio';vazio.append(a);}
|
||||||
|
lista.append(vazio);
|
||||||
|
}
|
||||||
|
for(const e of data.items)lista.append(linha(e));
|
||||||
|
paginas(data.total);
|
||||||
|
const aberto=new URLSearchParams(location.search).get('order');
|
||||||
|
if(aberto&&/^[0-9a-f-]{36}$/.test(aberto)){
|
||||||
|
let alvo=$('order-'+aberto);
|
||||||
|
if(!alvo){
|
||||||
|
// Not on this page of the list: shown on its own, above it.
|
||||||
|
try{const o=await api('/customer/orders/'+aberto);
|
||||||
|
alvo=linha({kind:'order',id:o.id,number:o.number,code:o.code,state:o.state,total_cents:o.snapshot.total_cents,
|
||||||
|
created_at:o.created_at,items:o.snapshot.items});lista.prepend(alvo);}catch(e){alvo=null;}
|
||||||
|
}
|
||||||
|
if(alvo){await alvo.abrir();alvo.scrollIntoView({block:'start'});}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let buscaTimer;
|
||||||
|
// The order code the customer was given: letters A–F and digits only.
|
||||||
|
$('fNumero').addEventListener('input',e=>{e.target.value=e.target.value.toUpperCase().replace(/[^0-9A-F]/g,'').slice(0,8);clearTimeout(buscaTimer);
|
||||||
|
buscaTimer=setTimeout(()=>{filtro={...filtro,code:e.target.value,page:1};pedidos().catch(x=>aviso(x.message,true));},350);});
|
||||||
|
$('fPeriodo').addEventListener('change',e=>{filtro={...filtro,days:Number(e.target.value),page:1};pedidos().catch(x=>aviso(x.message,true));});
|
||||||
|
|
||||||
|
function itensPedido(snapshot){
|
||||||
|
const ul=node('ul',null,'itensPed');
|
||||||
|
for(const item of snapshot.items){
|
||||||
|
const li=node('li');
|
||||||
|
li.append(node('span',PRODUTO[item.mode]||item.mode,'nome'),node('span',metros(item.billed_metres),'qt'),node('b',money(item.total_cents)));
|
||||||
|
ul.append(li);
|
||||||
|
}
|
||||||
|
const f=snapshot.freight;
|
||||||
|
if(f&&f.total_cents){const li=node('li',null,'frete');li.append(node('span','Frete','nome'),node('span',f.days?'até '+f.days+' dias úteis':'','qt'),node('b',money(f.total_cents)));ul.append(li);}
|
||||||
|
return ul;
|
||||||
|
}
|
||||||
|
function entrega(snapshot){
|
||||||
|
const d=snapshot.destination, p=node('p',null,'entregaPed');
|
||||||
|
p.append(icone(d?'caminhao':'local'),node('span',d?'Entrega pela Jadlog · '+d.street+', '+d.number+' · '+d.city+'/'+d.state:'Retirada em Franca · Av. Major Elias Motta, 1281'));
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
async function details(id,container,criado){
|
||||||
const order=await api('/customer/orders/'+id);container.replaceChildren();
|
const order=await api('/customer/orders/'+id);container.replaceChildren();
|
||||||
for(const [i,item] of order.snapshot.items.entries())container.append(node('p',(i+1)+'. '+item.mode+' · '+item.billed_metres+' m · nota '+item.grade+' · '+money(item.total_cents)));
|
if(order.state==='cor'){
|
||||||
const history=node('ol');
|
const p=node('p',null,'pedAlerta');p.append(icone('alerta'),node('span','A equipe precisa de uma correção na sua arte. Veja o motivo abaixo e envie o arquivo corrigido.'));
|
||||||
for(const h of order.history)history.append(node('li',new Date(h.created_at).toLocaleString('pt-BR')+' · '+states[h.to_state]+(h.reason?' — '+h.reason:'')));
|
container.append(p);
|
||||||
container.append(history);
|
}else container.append(trilha(order.state));
|
||||||
|
container.append(itensPedido(order.snapshot),entrega(order.snapshot));
|
||||||
|
const hist=node('div',null,'secao');hist.append(node('h4','Andamento'));
|
||||||
|
const ol=node('ol',null,'historico');
|
||||||
|
// Payment is where the order starts: the first step is not a movement.
|
||||||
|
if(criado){const li=node('li');li.append(node('b',states.rec||'Arte recebida'),node('span',quando(criado)));ol.append(li);}
|
||||||
|
for(const h of order.history){
|
||||||
|
const li=node('li');li.append(node('b',states[h.to_state]),node('span',quando(h.created_at)));
|
||||||
|
if(h.reason)li.append(node('p',h.reason,'motivo'));
|
||||||
|
ol.append(li);
|
||||||
|
}
|
||||||
|
hist.append(ol);container.append(hist);
|
||||||
|
if(order.files.length){
|
||||||
|
const arq=node('div',null,'secao');arq.append(node('h4','Arquivos'));
|
||||||
for(const file of order.files){
|
for(const file of order.files){
|
||||||
const row=node('p',(file.kind==='final'?'Arquivo final':'Correção')+' · item '+(file.item_index+1)+' · '+file.name+' · '+(file.expired?'expirado':!file.active?'substituído':'disponível até '+new Date(file.expires_at).toLocaleDateString('pt-BR')));
|
const row=node('div',null,'arquivo');
|
||||||
if(file.active&&!file.expired)row.append(button('Baixar',async()=>{const d=await api('/customer/orders/'+id+'/files/'+file.id+'/download');const link=node('a');link.href=d.url;link.download=d.name;link.referrerPolicy='no-referrer';link.click();}));
|
const info=node('div');
|
||||||
container.append(row);
|
info.append(node('b',file.name),node('span',(file.kind==='final'?'Arquivo final':'Correção')+' · item '+(file.item_index+1)+' · '+
|
||||||
|
(file.expired?'expirado':!file.active?'substituído':'disponível até '+dia(file.expires_at))));
|
||||||
|
row.append(info);
|
||||||
|
if(file.active&&!file.expired){
|
||||||
|
const b=button('Baixar',async()=>{const d=await api('/customer/orders/'+id+'/files/'+file.id+'/download');const link=node('a');link.href=d.url;link.download=d.name;link.referrerPolicy='no-referrer';link.click();});
|
||||||
|
b.prepend(icone('baixar'));row.append(b);
|
||||||
|
}
|
||||||
|
arq.append(row);
|
||||||
|
}
|
||||||
|
container.append(arq);
|
||||||
}
|
}
|
||||||
if(order.state==='cor'){
|
if(order.state==='cor'){
|
||||||
const form=node('form');form.append(node('h3','Enviar arte corrigida'));
|
const form=node('form',null,'secao correcaoForm');form.append(node('h4','Enviar arte corrigida'));
|
||||||
const inputs=order.snapshot.items.map((item,index)=>{const label=node('label','Item '+(index+1)+' · '+item.mode);const input=node('input');input.type='file';input.multiple=true;input.dataset.item=index;label.append(input);form.append(label);return input;});
|
const inputs=order.snapshot.items.map((item,index)=>{
|
||||||
const note=node('textarea');note.placeholder='Descreva a correção';note.required=true;note.maxLength=1000;form.append(note);
|
const label=node('label',(index+1)+'. '+(PRODUTO[item.mode]||item.mode));
|
||||||
const submit=node('button','Enviar correção');submit.type='submit';form.append(submit);
|
const input=node('input');input.type='file';input.multiple=true;input.dataset.item=index;
|
||||||
|
form.append(label,input);return input;
|
||||||
|
});
|
||||||
|
const nl=node('label','O que foi corrigido');const note=node('textarea');note.required=true;note.maxLength=1000;note.rows=3;
|
||||||
|
form.append(nl,note);
|
||||||
|
const submit=node('button','Enviar correção','bPri');submit.type='submit';form.append(submit);
|
||||||
form.onsubmit=async event=>{event.preventDefault();submit.disabled=true;try{
|
form.onsubmit=async event=>{event.preventDefault();submit.disabled=true;try{
|
||||||
const files=[];for(const [index,input] of inputs.entries())for(const file of input.files)files.push({item_index:index,upload_id:await dtfUpload(file,{api,scope:scope+':correction:'+id+':'+order.version,progress:text=>$('message').textContent=text})});
|
const files=[];for(const [index,input] of inputs.entries())for(const file of input.files)files.push({item_index:index,upload_id:await dtfUpload(file,{api,scope:scope+':correction:'+id+':'+order.version,progress:text=>aviso(text)})});
|
||||||
if(!files.length)throw new Error('Selecione pelo menos um arquivo corrigido.');
|
if(!files.length)throw new Error('Selecione pelo menos um arquivo corrigido.');
|
||||||
await api('/customer/orders/'+id+'/corrections',{version:order.version,files,note:note.value});
|
await api('/customer/orders/'+id+'/corrections',{version:order.version,files,note:note.value});
|
||||||
$('message').textContent='Correção enviada. Aguarde a conferência da equipe.';await details(id,container);
|
aviso('Correção enviada. A equipe confere e segue com o pedido.');await details(id,container,criado);
|
||||||
}catch(error){$('message').textContent=error.message;}finally{submit.disabled=false;}};
|
}catch(error){aviso(error.message,true);}finally{submit.disabled=false;}};
|
||||||
container.append(form);
|
container.append(form);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- Overview ------------------------------------------------------------
|
||||||
|
|
||||||
|
async function geral(){
|
||||||
|
$('account').textContent='CNPJ '+fmtCnpj(conta.cnpj)+' · '+conta.mail;
|
||||||
|
const data=await api('/customer/orders?page=1&size=1');states=data.states;
|
||||||
|
const box=$('metricas');box.replaceChildren();
|
||||||
|
for(const [key,label] of [['pay','Aguardando pagamento'],['prod','Em produção'],['cor','Correção'],['fin','Finalizados']]){
|
||||||
|
if(key==='cor'&&!data.counts.cor)continue;
|
||||||
|
const a=node('a',null,'metrica'+(key==='cor'?' alerta':''));a.href='/conta/pedidos';a.dataset.view='pedidos';
|
||||||
|
a.addEventListener('click',()=>{filtro={status:key,code:'',days:0,page:1};});
|
||||||
|
a.append(node('span',label),node('b',String(data.counts[key])));
|
||||||
|
box.append(a);
|
||||||
|
}
|
||||||
|
const ultimo=$('ultimo');ultimo.replaceChildren();
|
||||||
|
if(data.items.length)ultimo.append(linha(data.items[0]));
|
||||||
|
else{const v=node('div',null,'vazio');v.append(node('p','Seu primeiro pedido aparece aqui.'));const a=node('a','Enviar uma arte','bPri');a.href='/#envio';v.append(a);ultimo.append(v);}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- Account details -----------------------------------------------------
|
||||||
|
|
||||||
|
const CAMPOS_END=[['dNome','recipient'],['dRua','street'],['dNum','number'],['dComp','complement'],['dBairro','district'],['dCidade','city'],['dUf','state']];
|
||||||
|
function dados(){
|
||||||
|
$('dCnpj').value=fmtCnpj(conta.cnpj);$('dZap').value=fmtZap(conta.zap);$('dMail').value=conta.mail;
|
||||||
|
const end=conta.address||{};
|
||||||
|
$('dCep').value=fmtCep(end.postal_code||'');
|
||||||
|
for(const [id,k] of CAMPOS_END)$(id).value=end[k]||'';
|
||||||
|
$('dMailSenha').value='';$('dAtual').value='';$('dNova').value='';
|
||||||
|
}
|
||||||
|
$('dZap').addEventListener('input',e=>{e.target.value=fmtZap(soDigitos(e.target.value).slice(0,11));});
|
||||||
|
$('dCep').addEventListener('input',async e=>{
|
||||||
|
e.target.value=fmtCep(e.target.value);
|
||||||
|
const cep=soDigitos(e.target.value);
|
||||||
|
if(cep.length!==8)return;
|
||||||
|
try{const a=await api('/cep/'+cep);
|
||||||
|
for(const [id,v] of [['dRua',a.street],['dBairro',a.district],['dCidade',a.city],['dUf',a.state]])if(v)$(id).value=v;
|
||||||
|
(['dNome','dRua','dNum'].map($).find(el=>!el.value.trim())||$('dNum')).focus();
|
||||||
|
}catch(_){/* sem consulta: o cliente preenche */}
|
||||||
|
});
|
||||||
|
$('fPerfil').onsubmit=async event=>{
|
||||||
|
event.preventDefault();
|
||||||
|
const cep=soDigitos($('dCep').value);
|
||||||
|
const campos=Object.fromEntries(CAMPOS_END.map(([id,k])=>[k,$(id).value.trim()]));
|
||||||
|
const algum=cep||Object.values(campos).some(Boolean);
|
||||||
|
const address=algum?{...campos,state:campos.state.toUpperCase(),postal_code:cep}:null;
|
||||||
|
try{
|
||||||
|
const r=await api('/account/profile',{zap:$('dZap').value,address});
|
||||||
|
conta=r.customer;aviso('Dados salvos.');
|
||||||
|
}catch(e){aviso(e.status===422?(algum?'Confira o WhatsApp e o endereço: todos os campos, menos o complemento, são necessários.':'Confira o WhatsApp.'):e.message,true);}
|
||||||
|
};
|
||||||
|
$('fEmail').onsubmit=async event=>{
|
||||||
|
event.preventDefault();
|
||||||
|
try{const r=await api('/account/email',{email:$('dMail').value,password:$('dMailSenha').value});
|
||||||
|
conta=r.customer;$('dMailSenha').value='';$('quem').textContent=conta.mail;aviso('E-mail alterado. Use o novo e-mail para entrar.');}
|
||||||
|
catch(e){aviso(e.status===422?'Confira o e-mail.':e.message,true);}
|
||||||
|
};
|
||||||
|
$('fSenha').onsubmit=async event=>{
|
||||||
|
event.preventDefault();
|
||||||
|
try{await api('/account/password',{current:$('dAtual').value,new:$('dNova').value});
|
||||||
|
$('dAtual').value='';$('dNova').value='';aviso('Senha alterada.');}
|
||||||
|
catch(e){aviso(e.status===422?'A nova senha precisa ter 12 caracteres ou mais.':e.message,true);}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---- Signing in ----------------------------------------------------------
|
||||||
|
|
||||||
async function migrateGuestCart(previous,next){
|
async function migrateGuestCart(previous,next){
|
||||||
if(previous===next)return;
|
if(previous===next)return;
|
||||||
const database=await new Promise((resolve,reject)=>{const r=indexedDB.open('dtf-local-cart',1);r.onupgradeneeded=()=>r.result.createObjectStore('cart');r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});
|
const database=await new Promise((resolve,reject)=>{const r=indexedDB.open('dtf-local-cart',1);r.onupgradeneeded=()=>r.result.createObjectStore('cart');r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});
|
||||||
@@ -63,14 +329,25 @@ async function migrateGuestCart(previous,next){
|
|||||||
database.close();
|
database.close();
|
||||||
}
|
}
|
||||||
async function authenticate(path,body){
|
async function authenticate(path,body){
|
||||||
const previous=scope,guest=!(await api('/account/me')).customer;
|
const previous=scope,guest=!conta;
|
||||||
await api(path,body);forgetCheckout();$('message').textContent='Acesso confirmado.';
|
const r=await api(path,body);forgetCheckout();
|
||||||
const next=(await api('/session')).cart_scope;
|
const next=(await api('/session')).cart_scope;scope=next;
|
||||||
if(guest)try{await migrateGuestCart(previous,next);}catch{$('message').textContent='Acesso confirmado. Não foi possível transferir o carrinho salvo neste navegador.';}
|
if(guest)try{await migrateGuestCart(previous,next);}catch{aviso('Não foi possível transferir o carrinho salvo neste navegador.',true);}
|
||||||
await load();$('password').value='';$('register-password').value='';
|
conta=r.customer;$('quem').textContent=conta.mail;
|
||||||
|
$('password').value='';$('register-password').value='';
|
||||||
|
await mostra();
|
||||||
}
|
}
|
||||||
$('login').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/login',{email:$('email').value,password:$('password').value});}catch(e){$('message').textContent=e.message;}};
|
$('login').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/login',{email:$('email').value,password:$('password').value});}catch(e){aviso(e.message,true);}};
|
||||||
$('register').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/register',{customer:{cnpj:$('cnpj').value,zap:$('phone').value,mail:$('register-email').value},password:$('register-password').value});}catch(e){$('message').textContent=e.message;}};
|
$('register').onsubmit=async event=>{event.preventDefault();try{await authenticate('/account/register',{customer:{cnpj:$('cnpj').value,zap:$('phone').value,mail:$('register-email').value},password:$('register-password').value});}catch(e){aviso(e.status===422?'Confira o CNPJ, o WhatsApp e o e-mail. A senha precisa ter 12 caracteres ou mais.':e.message,true);}};
|
||||||
$('logout').onclick=async()=>{try{await window.dtfForgetPrivateData();await api('/account/logout',{});location.reload();}catch(error){$('message').textContent='Não foi possível concluir a limpeza local. Feche as abas do Site e limpe os dados deste site no navegador.';}};
|
$('esqueci').onclick=()=>{$('esqueciTx').hidden=!$('esqueciTx').hidden;};
|
||||||
$('refresh').onclick=load;
|
$('logout').onclick=async()=>{try{await window.dtfForgetPrivateData();await api('/account/logout',{});location.assign('/conta/entrar');}catch(error){aviso('Não foi possível limpar os dados deste navegador. Feche as abas do Site e limpe os dados do site nas configurações do navegador.',true);}};
|
||||||
load();
|
|
||||||
|
(async()=>{
|
||||||
|
try{
|
||||||
|
const session=await api('/session');scope=session.cart_scope;environment=session.environment;
|
||||||
|
$('dev-notice').hidden=environment!=='local';
|
||||||
|
conta=(await api('/account/me')).customer;
|
||||||
|
if(conta)$('quem').textContent=conta.mail;
|
||||||
|
await mostra();
|
||||||
|
}catch(error){aviso(error.message,true);}
|
||||||
|
})();
|
||||||
|
|||||||