feat: the art grade becomes configurable, off by default (front 8.5)

Gestão sets in the Site tab below which grade the designer is warned and
below which the designer treatment is required (only while it is offered).
The decision is stored with the approved quote, so a later change never
rewrites old orders; the card shows 'Nota N: conferir a arte', the Site
forces the treatment box with the reason. With no rule nothing changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-09 16:13:30 -03:00
parent 1d0353fe54
commit 272755d2bc
16 changed files with 338 additions and 13 deletions

View File

@@ -6,7 +6,7 @@ import httpx
from fastapi import APIRouter, HTTPException, Request, Response
from ..core import db
from .. import catalog, mail
from .. import catalog, grade_rules, mail
from ..core.auth import client_ip, owner, new_session, rate_limit
from ..core.limits import upload_limit_bytes
from ..core.pricing import manual_extra_cents
@@ -40,6 +40,7 @@ def session(request: Request, response: Response):
session_id = new_session(c, response)
with db.connect() as c:
products = catalog.current(c)
rules = grade_rules.current_rules(c)
treatment = products['tratamento']
return {'environment': ENVIRONMENT, 'cart_scope': str(session_id), 'part_bytes': PART_BYTES,
'max_upload_bytes': upload_limit_bytes(), 'payment_provider': payment.name,
@@ -48,6 +49,9 @@ def session(request: Request, response: Response):
'manual_extra_cents': manual_extra_cents(),
# Zero keeps the designer's treatment hidden, as an off product does.
'treatment_cents': treatment['unit_cents'] if treatment['enabled'] else 0,
# The grade rules the Site needs for the treatment requirement
# (front 8.5): {} when nothing is configured.
'grade_rules': rules,
# What the Site sells now: a switched-off product is hidden there.
# Bands travel with the price; the Site reads them as numbers.
'products': {mode: {**products[mode],

View File

@@ -8,7 +8,7 @@ from uuid import UUID, uuid4
from fastapi import APIRouter, Depends, HTTPException
from psycopg.types.json import Jsonb
from .. import catalog, coupons, payments
from .. import catalog, coupons, grade_rules, payments
from ..core import db
from ..core.auth import owner
from ..core.models import QuoteRequest
@@ -40,6 +40,12 @@ def create_quote(body: QuoteRequest, session_id=Depends(owner)):
item['stroke_choice_at'] = accepted_at
reason = quote_review.review_reason(draft)
with db.connect() as c:
# The art grade rules (front 8.5): the treatment may be required. With
# no rule this is empty and nothing below changes.
flags = grade_rules.triggered(grade_rules.current_rules(c), draft['items'],
catalog.treatment_offered(c))
if 'tratamento' in flags and not draft.get('treatment'):
raise HTTPException(422, grade_rules.treatment_message(flags['tratamento']))
if reason is not None or code:
# Waits for review: check the delivery now, priced at approval. A
# coupon is checked on the same priced items (front 8.3).

View File

@@ -14,10 +14,10 @@ from fastapi import APIRouter, Depends, HTTPException
from psycopg.errors import UniqueViolation
from psycopg.types.json import Jsonb
from .. import catalog
from .. import catalog, grade_rules
from ..core import db
from ..core.auth import audit, can
from ..core.models import CouponNew, CouponUpdate, SitePriceNew, SiteProductToggle
from ..core.models import CouponNew, CouponUpdate, GradeRules, SitePriceNew, SiteProductToggle
router = APIRouter()
@@ -233,3 +233,30 @@ def delete_coupon(coupon_id: UUID, user=Depends(can('manage'))):
raise HTTPException(404, 'Cupom não encontrado.')
audit('site_coupon_deleted', coupon=str(coupon_id), code=row['code'], operator=user)
return {'ok': True}
@router.get('/api/operator/site/grade-rules')
def site_grade_rules(user=Depends(can('manage'))):
with db.connect() as c:
rules = grade_rules.current_rules(c)
offered = catalog.treatment_offered(c)
return {'aviso': rules.get('aviso'), 'tratamento': rules.get('tratamento'),
'treatment_offered': offered}
@router.post('/api/operator/site/grade-rules')
def save_grade_rules(body: GradeRules, user=Depends(can('manage'))):
if body.aviso is not None and body.tratamento is not None and body.tratamento > body.aviso:
raise HTTPException(422, 'A regra do tratamento precisa ser menor ou igual à do aviso.')
with db.connect() as c:
for action, below in (('aviso', body.aviso), ('tratamento', body.tratamento)):
if below is None:
c.execute('DELETE FROM dtf_local.grade_rules WHERE action=%s', (action,))
else:
c.execute('''INSERT INTO dtf_local.grade_rules(action,below,updated_by,updated_at)
VALUES(%s,%s,%s,now())
ON CONFLICT (action) DO UPDATE SET below=EXCLUDED.below,
updated_by=EXCLUDED.updated_by, updated_at=now()''',
(action, below, user))
audit('site_grade_rules_changed', aviso=body.aviso, tratamento=body.tratamento, operator=user)
return {'ok': True}