feat: the folders catch up on what passed while the Kanban computer was off
All checks were successful
Build and deploy / Validate source (push) Successful in 4s
Build and deploy / Integration suite on a real stack (push) Successful in 3m27s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 56s

Dropstar's internal rules (2026-10-26): the folder must reproduce what passed,
not only what sits in the columns when the computer comes back, or a room
running 24 hours leaves a gap a week in the Finalizado archive.

- the Kanban remembers when the folders were chosen and asks for every order
  paid since then, whatever its stage; each file goes to its stage's folder,
  so an order finished while the computer was off lands in 3. Finalizado
- orders paid before the folders were chosen still stay out, so setting them
  up never pulls old orders back into the queue
- the server's file list for the folders and Baixar now loads only the orders
  that have a file, not every order in the stages

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-10-27 10:15:16 -03:00
parent 35e64d77d7
commit 6a921415ea
4 changed files with 57 additions and 21 deletions

View File

@@ -970,32 +970,35 @@ def named_files(rows):
return files
def print_ready(c, request, states, only=None):
def print_ready(c, request, states, only=None, paid_after=None):
"""The file to print for each item of the orders in these stages, in queue
order: the approved final, or before approval the generated print file
(unless the customer has sent a correction since). `only` narrows it to
these orders. An operator who works on some machines gets only those
machines' orders."""
these orders, `paid_after` to the orders paid since then. An operator who
works on some machines gets only those machines' orders."""
ids = None if only is None else list(only)
rows = c.execute(f'''SELECT o.id AS order_id,o.state,o.number,f.item_index,f.upload_id,u.object_key,u.name,
COALESCE(o.queue_key, extract(epoch FROM o.created_at)) AS position
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=ANY(%s) AND (%s::uuid[] IS NULL OR o.id=ANY(%s)) AND o.cancelled_at IS NULL AND {FINAL_OK}
WHERE o.state=ANY(%s) AND (%s::uuid[] IS NULL OR o.id=ANY(%s))
AND (%s::timestamptz IS NULL OR o.created_at>=%s) AND o.cancelled_at IS NULL AND {FINAL_OK}
UNION ALL
SELECT o.id,o.state,o.number,p.item_index,p.upload_id,u.object_key,u.name,
COALESCE(o.queue_key, extract(epoch FROM o.created_at))
FROM dtf_local.orders o
JOIN dtf_local.print_files p ON p.order_id=o.id JOIN dtf_local.uploads u ON u.id=p.upload_id
WHERE o.state=ANY(%s) AND (%s::uuid[] IS NULL OR o.id=ANY(%s)) AND o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND p.status='ready'
WHERE o.state=ANY(%s) AND (%s::uuid[] IS NULL OR o.id=ANY(%s))
AND (%s::timestamptz IS NULL OR o.created_at>=%s) AND o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND p.status='ready'
AND u.expires_at>now() AND u.purged_at IS NULL AND u.scan_state='clean'
AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=o.id AND f.kind='final'
AND f.active AND f.item_index=p.item_index)
AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=o.id AND f.kind='correction')
ORDER BY position,order_id,item_index''', (list(states), ids, ids, list(states), ids, ids)).fetchall()
staged = assign_machines(c, c.execute('''SELECT * FROM dtf_local.orders
WHERE state=ANY(%s) AND (%s::uuid[] IS NULL OR id=ANY(%s)) AND cancelled_at IS NULL''',
(list(states), ids, ids)).fetchall())
ORDER BY position,order_id,item_index''',
(list(states), ids, ids, paid_after, paid_after, list(states), ids, ids, paid_after, paid_after)).fetchall()
# Only the orders that have a file, for their machines: not every order in the stages.
staged = assign_machines(c, c.execute('SELECT * FROM dtf_local.orders WHERE id=ANY(%s)',
(list({row['order_id'] for row in rows}),)).fetchall())
mine = {o['id'] for o in visible(request, staged)}
return [row for row in rows if row['order_id'] in mine]
@@ -1016,6 +1019,9 @@ def queue_finals(request: Request, auto: bool = False, user=Depends(can('print')
class FolderSync(StrictModel):
early: bool = False
held: list[UUID] = Field(default_factory=list, max_length=1000)
# When the folders were chosen: the orders paid since are fetched whatever
# their stage, so what passed while the computer was off is not lost.
since: datetime | None = None
@router.post('/api/operator/queue/folder')
@@ -1024,7 +1030,10 @@ def queue_folder(body: FolderSync, request: Request, user=Depends(can('print')))
three folders (2026-10-26): the files of orders in Arte recebida and Arte
conferida, and, for the orders the folders already hold, their files while
they print, are checked in Revisão and sit in Finalizado, so one taken out
of a folder can be fetched again when the order moves on. Without it, only
of a folder can be fetched again when the order moves on. With `since`,
also every order paid after the folders were chosen, whatever its stage:
a computer that was off catches up on what passed meanwhile (Dropstar's
rules, 2026-10-26). Without it, only
the queue's (Firefox's downloads). `states` answers for the orders the
folders hold files of: a stage, 'cancelled', or nothing when the order was
deleted, so their files can be removed."""
@@ -1032,6 +1041,10 @@ def queue_folder(body: FolderSync, request: Request, user=Depends(can('print')))
rows = print_ready(c, request, ('rec', 'tra') if body.early else ('tra',))
if body.early and body.held:
rows += print_ready(c, request, ('imp', 'rev', 'fin'), only=body.held)
if body.early and body.since:
seen = {(r['order_id'], r['item_index'], r['upload_id']) for r in rows}
rows += [r for r in print_ready(c, request, ('imp', 'rev', 'fin'), paid_after=body.since)
if (r['order_id'], r['item_index'], r['upload_id']) not in seen]
files = named_files(rows)
held = c.execute('SELECT id,state,cancelled_at FROM dtf_local.orders WHERE id=ANY(%s)',
(body.held,)).fetchall() if body.held else []