aboutsummaryrefslogtreecommitdiff
path: root/backend/seckelapi/config/functions.toml
blob: deb487be400d51e7db16e801075edf6c88ab4ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# auto generation of things
[auto_generation]

[auto_generation.assets]
field = "asset_numeric_id"
type = "numeric"
length = 8
range_min = 10000000
range_max = 99999999
max_attempts = 10
# on what event seckel api schould try to generate auto gen value incaase client send empty value
on_action = "insert"

[scheduled_queries]

# Single idempotent task that sets the correct state atomically to avoid double-trigger inserts
[[scheduled_queries.tasks]]
name = "sync_overdue_and_stolen"
description = "Atomically set lending_status to Overdue (1-13 days late) or Stolen (>=14 days late) only if it changed"
query = """
  -- Use max lateness per asset to avoid flip-flopping due to multiple open lending rows
  -- Removed issue_tracker check from WHERE clause to avoid MySQL trigger conflict
  UPDATE assets a
  INNER JOIN (
      SELECT lh.asset_id, MAX(DATEDIFF(CURDATE(), lh.due_date)) AS days_late
      FROM lending_history lh
      WHERE lh.return_date IS NULL
        AND lh.due_date IS NOT NULL
      GROUP BY lh.asset_id
  ) late ON a.id = late.asset_id
  SET a.lending_status = CASE
      WHEN a.asset_type IN ('N','B') AND late.days_late >= 14 THEN 'Stolen'
      WHEN a.asset_type IN ('N','B') AND late.days_late BETWEEN 1 AND 13 THEN 'Overdue'
      ELSE a.lending_status
  END
  WHERE a.asset_type IN ('N','B')
    AND (
      (late.days_late >= 14 AND a.lending_status <> 'Stolen')
      OR
      (late.days_late BETWEEN 1 AND 13 AND a.lending_status <> 'Overdue')
    )
"""
interval_minutes = 2
run_on_startup = true
enabled = true