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

83
tests/grade_rules_test.py Normal file
View File

@@ -0,0 +1,83 @@
"""The art grade rules: the designer warning and the required treatment (8.5).
Run against the local stack: SITE_BASE_URL=http://site SITE_HOST_HEADER=localhost
python -m tests.grade_rules_test
"""
import secrets
from uuid import uuid4
from app.core import db
from tests.print_file_test import artwork
from tests.smoke_test import Client, item_spec, upload_bytes
def quote_body(item, treatment, mail):
body = {'request_key': str(uuid4()),
'customer': {'cnpj': '11222333000181', 'zap': '11999999999', 'mail': mail},
'items': [item], 'freight': {'service': 'pickup'}}
if treatment:
body['treatment'] = True
return body
def run():
tag = secrets.token_hex(4)
mail = f'grade-{tag}@example.test'
client = Client()
client.call('/session')
manager = Client()
manager.call('/session')
manager.call('/operator/me', operator=True)
manager = manager.operator_client
price_id = quote_id = upload_id = None
rules_set = False
treatment_enabled = True
try:
products = {row['id']: row for row in manager.call('/operator/site/products')['products']}
treatment_enabled = products['tratamento']['enabled']
manager.call('/operator/site/products/tratamento', {'enabled': True})
price_id = manager.call('/operator/site/products/tratamento/prices', {'unit_cents': 1000})['id']
manager.call('/operator/site/grade-rules', {'aviso': 100, 'tratamento': 100})
rules_set = True
rules = manager.call('/operator/site/grade-rules')
assert rules['aviso'] == 100 and rules['tratamento'] == 100, rules
assert rules['treatment_offered'] is True, rules
upload_id = upload_bytes(client, artwork(), name=f'LOCAL-GRADE-{tag}.png')
item = {**item_spec('uv', '1', 50, upload_id),
'quality_status': 'warning', 'quality_acknowledged': True}
refused = client.call('/quotes', quote_body(item, False, mail), expected=422)
assert refused['detail'] == ('Sua arte tem nota 50: para imprimir com qualidade, '
'o tratamento pelo designer é obrigatório neste pedido.'), refused
made = client.call('/quotes', quote_body(item, True, mail))
quote_id = made['id']
assert made['status'] == 'approved', made
approved = client.call('/quotes/' + quote_id)['approved']
assert approved['grade_flags'] == {'aviso': 50, 'tratamento': 50}, approved
assert approved['treatment']['cents'] == 1000, approved
assert approved['total_cents'] == 9990, approved
print('PASS: the grade rule requires the designer treatment and flags the order'
' with the worst grade')
finally:
if rules_set:
try:
manager.call('/operator/site/grade-rules', {'aviso': None, 'tratamento': None})
except Exception:
pass
with db.connect() as c:
if quote_id:
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote_id,))
if price_id:
c.execute('DELETE FROM dtf_local.product_prices WHERE id=%s', (price_id,))
if upload_id:
c.execute('DELETE FROM dtf_local.uploads WHERE id=%s', (upload_id,))
try:
manager.call('/operator/site/products/tratamento', {'enabled': treatment_enabled})
except Exception:
pass
if __name__ == '__main__':
run()

42
tests/test_grade_rules.py Normal file
View File

@@ -0,0 +1,42 @@
"""Run from repository root: python3 -m unittest tests.test_grade_rules -v."""
import unittest
from app.grade_rules import triggered, treatment_message
def item(grade):
return {'grade': grade}
class GradeRulesTest(unittest.TestCase):
def test_nothing_changes_without_rules(self):
self.assertEqual(triggered({}, [item(10)], True), {})
self.assertEqual(triggered({}, [item(10)], False), {})
self.assertEqual(triggered({'aviso': 70}, [], True), {})
def test_aviso_only_and_the_worst_item(self):
self.assertEqual(triggered({'aviso': 70}, [item(69), item(90)], True), {'aviso': 69})
self.assertEqual(triggered({'aviso': 70}, [item(90)], True), {})
def test_both_rules_and_treatment_not_offered(self):
both = {'aviso': 70, 'tratamento': 50}
self.assertEqual(triggered(both, [item(45)], True), {'aviso': 45, 'tratamento': 45})
self.assertEqual(triggered(both, [item(62)], True), {'aviso': 62})
# The rule has no effect while the treatment is not offered.
self.assertEqual(triggered(both, [item(45)], False), {'aviso': 45})
self.assertEqual(triggered({'tratamento': 50}, [item(45)], False), {})
def test_the_boundary_is_exclusive(self):
self.assertEqual(triggered({'aviso': 70}, [item(70)], True), {})
self.assertEqual(triggered({'tratamento': 50}, [item(50)], True), {})
self.assertEqual(triggered({'aviso': 70}, [item(69)], True), {'aviso': 69})
def test_treatment_message(self):
self.assertEqual(
treatment_message(45),
'Sua arte tem nota 45: para imprimir com qualidade, o tratamento pelo designer '
'é obrigatório neste pedido.')
if __name__ == '__main__':
unittest.main()