1
0

functions.toml 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # auto generation of things
  2. [auto_generation]
  3. [auto_generation.assets]
  4. field = "asset_numeric_id"
  5. type = "numeric"
  6. length = 8
  7. range_min = 10000000
  8. range_max = 99999999
  9. max_attempts = 10
  10. # on what event seckel api schould try to generate auto gen value incaase client send empty value
  11. on_action = "insert"
  12. [scheduled_queries]
  13. # Single idempotent task that sets the correct state atomically to avoid double-trigger inserts
  14. [[scheduled_queries.tasks]]
  15. name = "sync_overdue_and_stolen"
  16. description = "Atomically set lending_status to Overdue (1-13 days late) or Stolen (>=14 days late) only if it changed"
  17. query = """
  18. -- Use max lateness per asset to avoid flip-flopping due to multiple open lending rows
  19. -- Removed issue_tracker check from WHERE clause to avoid MySQL trigger conflict
  20. UPDATE assets a
  21. INNER JOIN (
  22. SELECT lh.asset_id, MAX(DATEDIFF(CURDATE(), lh.due_date)) AS days_late
  23. FROM lending_history lh
  24. WHERE lh.return_date IS NULL
  25. AND lh.due_date IS NOT NULL
  26. GROUP BY lh.asset_id
  27. ) late ON a.id = late.asset_id
  28. SET a.lending_status = CASE
  29. WHEN a.asset_type IN ('N','B') AND late.days_late >= 14 THEN 'Stolen'
  30. WHEN a.asset_type IN ('N','B') AND late.days_late BETWEEN 1 AND 13 THEN 'Overdue'
  31. ELSE a.lending_status
  32. END
  33. WHERE a.asset_type IN ('N','B')
  34. AND (
  35. (late.days_late >= 14 AND a.lending_status <> 'Stolen')
  36. OR
  37. (late.days_late BETWEEN 1 AND 13 AND a.lending_status <> 'Overdue')
  38. )
  39. """
  40. interval_minutes = 2
  41. run_on_startup = true
  42. enabled = true