Merge branch 'actions' into 'master'
adding pending actions control flow See merge request medlabboulder/modpol!25
This commit is contained in:
commit
0539b82d9a
@ -78,9 +78,9 @@ if (modpol.orgs.array) then
|
||||
if type(org) == 'table' then
|
||||
setmetatable(org, modpol.orgs)
|
||||
-- sets process metatable on load
|
||||
if type(org.processes) == 'table' then
|
||||
for id, process in ipairs(org.processes) do
|
||||
setmetatable(process, modpol.modules[process.type])
|
||||
for id, process in ipairs(org.processes) do
|
||||
if type(process) == 'table' then
|
||||
setmetatable(process, modpol.modules[process.type])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -8,6 +8,7 @@ modpol.modules.consent.__index = modpol.modules.consent
|
||||
function temp_consent_process()
|
||||
return {
|
||||
type = "consent",
|
||||
id = nil,
|
||||
org_id = nil,
|
||||
request_id = nil,
|
||||
total_votes = 0,
|
||||
@ -19,21 +20,31 @@ end
|
||||
|
||||
-- ===============================================
|
||||
-- function to create a new consent process to resolve a pending process
|
||||
function modpol.modules.consent:new_process(request_id, org_id)
|
||||
function modpol.modules.consent:new_process(id, request_id, org_id)
|
||||
local process = temp_consent_process()
|
||||
process.id = id
|
||||
process.request_id = request_id
|
||||
process.org_id = org_id
|
||||
|
||||
setmetatable(process, modpol.modules.consent)
|
||||
modpol.ocutil.log('Created new process 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)
|
||||
|
||||
for i, member in ipairs(p_org.members) do
|
||||
p_org:add_pending_action(id, member, 'approve')
|
||||
end
|
||||
|
||||
return process
|
||||
end
|
||||
|
||||
|
||||
-- =========================================
|
||||
-- function to delete a process, called when process finishes
|
||||
function modpol.modules.consent:delete()
|
||||
local process_org = modpol.orgs.get_org(self.org_id)
|
||||
process_org:wipe_pending_actions(self.id)
|
||||
process_org.processes[self.id] = "deleted"
|
||||
modpol.ocutil.log('Deleted process #' .. self.id)
|
||||
end
|
||||
@ -55,6 +66,9 @@ function modpol.modules.consent:approve(user, decision)
|
||||
end
|
||||
|
||||
self.total_votes = self.total_votes + 1
|
||||
|
||||
local p_org = modpol.orgs.get_org(self.org_id)
|
||||
p_org:remove_pending_action(self.id, user, "approve")
|
||||
|
||||
self:update_status()
|
||||
end
|
||||
@ -77,8 +91,4 @@ function modpol.modules.consent:update_status()
|
||||
else
|
||||
modpol.ocutil.log('Waiting for more votes...')
|
||||
end
|
||||
end
|
||||
|
||||
function modpol.modules.consent.pending_processes()
|
||||
|
||||
end
|
@ -14,6 +14,7 @@ function temp_org()
|
||||
policies = {},
|
||||
processes = {},
|
||||
requests = {},
|
||||
pending = {},
|
||||
members = {},
|
||||
parent = nil,
|
||||
children = {}
|
||||
|
@ -13,10 +13,6 @@ function modpol.orgs:create_process(process_type, request_id)
|
||||
return
|
||||
end
|
||||
|
||||
-- retrieving requested module
|
||||
local module = modpol.modules[process_type]
|
||||
local new_process = module:new_process(request_id, self.id)
|
||||
|
||||
-- linear search for empty process slots (lazy deletion)
|
||||
for k, v in ipairs(self.processes) do
|
||||
if v == 'deleted' then
|
||||
@ -28,17 +24,60 @@ function modpol.orgs:create_process(process_type, request_id)
|
||||
local index
|
||||
-- attempts to fill empty spots in list, otherwise appends to end
|
||||
if empty_index then
|
||||
self.processes[empty_index] = new_process
|
||||
index = empty_index
|
||||
else
|
||||
table.insert(self.processes, new_process)
|
||||
index = #self.processes
|
||||
index = #self.processes + 1
|
||||
end
|
||||
|
||||
new_process.id = index
|
||||
-- retrieving requested module
|
||||
local module = modpol.modules[process_type]
|
||||
local new_process = module:new_process(index, request_id, self.id)
|
||||
|
||||
self.processes[index] = new_process
|
||||
|
||||
return index
|
||||
end
|
||||
|
||||
-- ===========================
|
||||
-- adds a new pending action to the org's table
|
||||
function modpol.orgs:add_pending_action(process_id, user, action)
|
||||
-- adds tables if they don't exist already
|
||||
self.pending[user] = self.pending[user] or {}
|
||||
self.pending[user][process_id] = self.pending[user][process_id] or {}
|
||||
|
||||
-- inserting actual action
|
||||
table.insert(self.pending[user][process_id], action)
|
||||
modpol.ocutil.log("Added pending action '" .. action .. "' to " .. user .. " in process #" .. process_id)
|
||||
end
|
||||
|
||||
-- ========================
|
||||
-- removes a pending action from the org's table
|
||||
function modpol.orgs:remove_pending_action(process_id, user, action)
|
||||
-- cautiously checks if pending action exists before removing it
|
||||
if self.pending[user] then
|
||||
if self.pending[user][process_id] then
|
||||
-- searching for action to remove
|
||||
for i, a in pairs(self.pending[user][process_id]) do
|
||||
if a == action then
|
||||
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
|
||||
modpol.ocutil.log("Could not remove pending action '" .. action .. "' from " .. user .. " in process #" .. process_id)
|
||||
end
|
||||
|
||||
-- =====================
|
||||
-- removes all pending actions for a given process id from all users
|
||||
function modpol.orgs:wipe_pending_actions(process_id)
|
||||
for user in pairs(self.pending) do
|
||||
self.pending[user][process_id] = nil
|
||||
end
|
||||
modpol.ocutil.log("Removed all pending actions for process #" .. process_id)
|
||||
end
|
||||
|
||||
-- ===========================
|
||||
-- compares to requests to see if they are identical
|
||||
function modpol.orgs.comp_req(req1, req2)
|
||||
|
@ -33,13 +33,14 @@ new_request = {
|
||||
params = {"new_org"}
|
||||
}
|
||||
|
||||
request_id = modpol.instance:make_request(new_request)
|
||||
|
||||
modpol.instance:add_member('luke')
|
||||
modpol.instance:add_member('josh')
|
||||
modpol.instance:add_member('nathan')
|
||||
|
||||
request_id = modpol.instance:make_request(new_request)
|
||||
|
||||
for id, process in ipairs(modpol.instance.processes) do
|
||||
process:approve('luke', true)
|
||||
-- process:approve('luke', true)
|
||||
process:approve('josh', true)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user