login

<     >

2023-08-07 11:56:04 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

smart shifting

if a new shift simply reverts the last shift then
the last shift is removed instead of adding the new one
except if a comment was added since the last shift

this way shifts become quite cheap in terms of DB size

diff --git a/data.lua b/data.lua
index f427554..b172d45 100644
--- a/data.lua
+++ b/data.lua
@@ -220,10 +220,23 @@ function Model:update_ticket(tick_id, title, desc, priority)
 end
 
 function Model:shift_ticket(user_id, ticket, state_id)
-    self.db:execute([[
-        INSERT INTO Shift(ticket_id, old_stt_id, new_stt_id, user_id, time)
-        VALUES (?, ?, ?, ?, unixepoch());
-    ]], ticket.id, ticket.state_id, state_id, user_id)
+    local query = "SELECT * FROM %s WHERE ticket_id = ? ORDER BY time DESC LIMIT 1;"
+    local last_comment = self.db:execute(query:format("Comment"), ticket.id)[1]
+    local last_shift = self.db:execute(query:format("Shift"), ticket.id)[1]
+    if last_comment ~= nil and last_shift ~= nil and last_shift.time < last_comment.time then
+        -- don't undo last shift if a comment has been added after it
+        last_shift = nil
+    end
+    if last_shift ~= nil and last_shift.old_stt_id == state_id then
+        -- we're undoing the last shift: delete it from DB
+        self.db:execute("DELETE FROM Shift WHERE id = ?;", last_shift.id)
+    else
+        -- we're doing a new shift: add it to DB
+        self.db:execute([[
+            INSERT INTO Shift(ticket_id, old_stt_id, new_stt_id, user_id, time)
+            VALUES (?, ?, ?, ?, unixepoch());
+        ]], ticket.id, ticket.state_id, state_id, user_id)
+    end
     self.db:execute("UPDATE Ticket SET state_id = ? WHERE id = ?;", state_id, ticket.id)
 end
 

diff --git a/skopos.lua b/skopos.lua
index e3ecbd5..553bb80 100644
--- a/skopos.lua
+++ b/skopos.lua
@@ -281,8 +281,7 @@ function App:routes()
         if proj == nil then return "not found", 404 end
         local tick = self.model:get_ticket(proj.id, code)
         if tick == nil then return "not found", 404 end
-        self.model:shift_ticket(user.id, tick, state_id)
-        self:log(LOG_INFO, "user "..user.nick.." shifted ticket "..name.."#"..code)
+        self.model:shift_ticket(user.id, tick, tonumber(state_id))
         local path
         if req.query.from == "proj" then
             path = "/p/"..name