refactored to simpler pending action system

This commit is contained in:
Luke Miller 2021-06-17 21:27:09 -04:00
parent 12623ee4a2
commit 761a058b6a
2 changed files with 12 additions and 21 deletions

View File

@ -29,11 +29,10 @@ function modpol.modules.consent:new_process(id, request_id, org_id)
setmetatable(process, modpol.modules.consent) setmetatable(process, modpol.modules.consent)
modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id) modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id)
-- modpol.orgs.get_org(self.org_id):add_pending_action('luke', self.id, 'approve')
local p_org = modpol.orgs.get_org(org_id) local p_org = modpol.orgs.get_org(org_id)
for i, member in ipairs(p_org.members) do for i, member in ipairs(p_org.members) do
p_org:add_pending_action(id, member, 'approve') p_org:add_pending_action(id, member)
end end
return process return process
@ -68,7 +67,7 @@ function modpol.modules.consent:approve(user, decision)
self.total_votes = self.total_votes + 1 self.total_votes = self.total_votes + 1
local p_org = modpol.orgs.get_org(self.org_id) local p_org = modpol.orgs.get_org(self.org_id)
p_org:remove_pending_action(self.id, user, "approve") p_org:remove_pending_action(self.id, user)
self:update_status() self:update_status()
end end

View File

@ -40,33 +40,25 @@ end
-- =========================== -- ===========================
-- adds a new pending action to the org's table -- adds a new pending action to the org's table
function modpol.orgs:add_pending_action(process_id, user, action) function modpol.orgs:add_pending_action(process_id, user)
-- adds tables if they don't exist already -- adds tables if they don't exist already
self.pending[user] = self.pending[user] or {} self.pending[user] = self.pending[user] or {}
self.pending[user][process_id] = self.pending[user][process_id] or {} -- flagging actual action
self.pending[user][process_id] = true
-- inserting actual action modpol.ocutil.log("Added pending action to " .. user .. " in process #" .. process_id)
table.insert(self.pending[user][process_id], action)
modpol.ocutil.log("Added pending action '" .. action .. "' to " .. user .. " in process #" .. process_id)
end end
-- ======================== -- ========================
-- removes a pending action from the org's table -- removes a pending action from the org's table
function modpol.orgs:remove_pending_action(process_id, user, action) function modpol.orgs:remove_pending_action(process_id, user)
-- cautiously checks if pending action exists before removing it
if self.pending[user] then if self.pending[user] then
if self.pending[user][process_id] then self.pending[user][process_id] = nil
-- searching for action to remove modpol.ocutil.log("Removed pending action from " .. user .. " in process #" .. process_id)
for i, a in pairs(self.pending[user][process_id]) do else
if a == action then modpol.ocutil.log("Could not remove pending action from " .. user .. " in process #" .. process_id)
self.pending[user][process_id][i] = nil
modpol.ocutil.log("Removed pending action '" .. action .. "' from " .. user .. " in process #" .. process_id)
return
end end
end
end
end
modpol.ocutil.log("Could not remove pending action '" .. action .. "' from " .. user .. " in process #" .. process_id)
end end
-- ===================== -- =====================