feat: a cancelled order can be deleted
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m55s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m2s
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 2m55s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m2s
"Excluir pedido" now also works on a paid order once it is cancelled; one in progress must be cancelled first, with its reason. Deleting a cancelled order also discards its quote, so it neither returns to Cotações as unpaid nor can be paid again. The payment stays recorded and the audit log keeps who deleted which order and the cancellation reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -357,21 +357,29 @@ def discard_quote(uid: UUID, user=Depends(operator)):
|
||||
return {'ok': True}
|
||||
|
||||
|
||||
@router.post('/api/operator/orders/{uid}/delete-test')
|
||||
def delete_test_order(uid: UUID, user=Depends(operator)):
|
||||
"""A test order (no payment, never sent to Tiny or WhatsApp) is deleted
|
||||
with its history and files' records. Its quote is unpaid again."""
|
||||
@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')
|
||||
if not payments.is_test(row):
|
||||
raise HTTPException(409, 'Só pedidos de teste podem ser excluídos. Um pedido pago se cancela.')
|
||||
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,))
|
||||
audit('test_order_deleted', order=row['number'], operator=user)
|
||||
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}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user