significant progress! a working process from beginning to end can be seen in test/org_req_test.lua

This commit is contained in:
Luke Miller
2021-05-06 14:13:02 -04:00
parent a7ba7605d3
commit 797d0bebb8
5 changed files with 66 additions and 12 deletions

View File

@ -78,8 +78,9 @@ if (modpol.orgs.array) then
if type(org) == 'table' then if type(org) == 'table' then
setmetatable(org, modpol.orgs) setmetatable(org, modpol.orgs)
-- sets process metatable on load -- sets process metatable on load
print(org.modules)
for id, process in ipairs(org.processes) do for id, process in ipairs(org.processes) do
setmetatable(process, org.modules[process.type]) setmetatable(process, modpol.modules[process.type])
end end
end end
end end

View File

@ -12,6 +12,7 @@ function temp_consent_process()
org_id = nil, org_id = nil,
request_id = nil, request_id = nil,
total_votes = 0, total_votes = 0,
majority_to_pass = 0.51,
votes_yes = {}, votes_yes = {},
votes_no = {} votes_no = {}
} }
@ -33,6 +34,11 @@ end
-- ====================================================== -- ======================================================
-- function for users to vote on a pending request -- function for users to vote on a pending request
function modpol.modules.consent:approve(user, decision) function modpol.modules.consent:approve(user, decision)
if not modpol.orgs.get_org(self.org_id):has_member(user) then
modpol.ocutil.log('Error in consent:approve -> user not a member of the org')
return
end
if decision then if decision then
table.insert(self.votes_yes, user) table.insert(self.votes_yes, user)
modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id) modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id)
@ -43,15 +49,24 @@ function modpol.modules.consent:approve(user, decision)
self.total_votes = self.total_votes + 1 self.total_votes = self.total_votes + 1
self:update_status()
end end
-- =================================================== -- ===================================================
-- determines whether process has finished and resolves request if it has (unfinished) -- determines whether process has finished and resolves request if it has (unfinished)
-- function modpol.modules.consent.call_vote_check(process) function modpol.modules.consent:update_status()
-- if votes_yes > to_pass then local process_org = modpol.orgs.get_org(self.org_id)
-- call_success() local eligible_voters = process_org:get_member_count()
-- elseif votes_no > to_pass then local votes_needed = math.ceil(self.majority_to_pass * eligible_voters)
-- call_failure()
-- end if #self.votes_yes >= votes_needed then
-- end modpol.ocutil.log('Request #' .. self.request_id .. ' passes')
process_org:resolve_request(self.request_id, true)
elseif #self.votes_no >= votes_needed then
modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass')
process_org:resolve_request(self.request_id, false)
else
modpol.ocutil.log('Waiting for more votes...')
end
end

View File

@ -271,7 +271,8 @@ end
function modpol.orgs:list_member() function modpol.orgs:list_member()
local str local str
for k, v in ipairs(self.members) do for k, v in ipairs(self.members) do
if str then -- checking to see if member name is valid
if str and str ~= '' then
str = str .. '\n' .. v str = str .. '\n' .. v
else else
str = v str = v
@ -280,6 +281,18 @@ function modpol.orgs:list_member()
return str return str
end end
-- ==============================
-- because member list uses lazy deletion, using #org.members will not show an accurate number
function modpol.orgs:get_member_count()
local count = 0
for k, v in ipairs(self.members) do
-- the empty string represents a deleted member in the members list
if v ~= '' then
count = count + 1
end
end
return count
end
-- ==================================== -- ====================================
-- adds a new policy to the policy table -- adds a new policy to the policy table
-- must define the policy type, process associated with it, and whether the request must be made by an org member -- must define the policy type, process associated with it, and whether the request must be made by an org member

View File

@ -67,6 +67,28 @@ function modpol.orgs:list_request()
return str return str
end end
-- ===============================
-- if the request was approved, the associated function is called, otherwise it is deleted
function modpol.orgs:resolve_request(request_id, approve)
if approve then
local request = self.requests[request_id]
local p = request.params
if request.type == "add_org" then
self:add_org(p[1])
elseif request.type == "delete" then
self:delete()
elseif request.type == "add_member" then
self:add_member(p[1])
elseif request.type == "remove_member" then
self:remove_member(p[1])
end
end
self.requests[request_id] = "deleted"
end
-- ================================ -- ================================
-- tries to make a request to the org -- tries to make a request to the org
function modpol.orgs:make_request(request) function modpol.orgs:make_request(request)

View File

@ -3,17 +3,20 @@ dofile('../modpol.lua');
modpol.orgs.reset() modpol.orgs.reset()
test_org = modpol.instance:add_org('test_org') test_org = modpol.instance:add_org('test_org')
test_org:add_member('luke')
test_org:add_member('nathan')
test_org:set_policy("add_member", nil, false); test_org:set_policy("add_member", nil, false);
new_request = { new_request = {
user = "lukvmil", user = "josh",
type = "add_member", type = "add_member",
params = {"lukvmil"} params = {"josh"}
} }
request_id = test_org:make_request(new_request) request_id = test_org:make_request(new_request)
process_id = test_org:create_process("consent", request_id) process_id = test_org:create_process("consent", request_id)
process = test_org.processes[process_id] process = test_org.processes[process_id]
process:approve("luke", true) process:approve("luke", true)
process:approve("nathan", true)