Browse Source

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

Luke Miller 3 years ago
parent
commit
797d0bebb8
5 changed files with 66 additions and 12 deletions
  1. 2 1
      modpol/modpol.lua
  2. 22 7
      modpol/modules/consent.lua
  3. 14 1
      modpol/orgs/base.lua
  4. 22 0
      modpol/orgs/requests.lua
  5. 6 3
      modpol/tests/org_req_test.lua

+ 2 - 1
modpol/modpol.lua

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

+ 22 - 7
modpol/modules/consent.lua

@@ -12,6 +12,7 @@ function temp_consent_process()
         org_id = nil,
         request_id = nil,
         total_votes = 0,
+        majority_to_pass = 0.51,
         votes_yes = {},
         votes_no = {}
     }
@@ -33,6 +34,11 @@ end
 -- ======================================================
 -- function for users to vote on a pending request
 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
         table.insert(self.votes_yes, user)
         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:update_status()
 end
 
 -- ===================================================
 -- determines whether process has finished and resolves request if it has (unfinished)
--- function modpol.modules.consent.call_vote_check(process)
---     if votes_yes > to_pass then
---         call_success()
---     elseif votes_no > to_pass then
---         call_failure()
---     end
--- end
+function modpol.modules.consent:update_status()
+    local process_org = modpol.orgs.get_org(self.org_id)
+    local eligible_voters = process_org:get_member_count()
+    local votes_needed = math.ceil(self.majority_to_pass * eligible_voters)
+    
+    if #self.votes_yes >= votes_needed then
+        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
 

+ 14 - 1
modpol/orgs/base.lua

@@ -271,7 +271,8 @@ end
 function modpol.orgs:list_member()
     local str
     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
         else 
             str = v
@@ -280,6 +281,18 @@ function modpol.orgs:list_member()
     return str
 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
 -- must define the policy type, process associated with it, and whether the request must be made by an org member

+ 22 - 0
modpol/orgs/requests.lua

@@ -67,6 +67,28 @@ function modpol.orgs:list_request()
     return str
 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
 function modpol.orgs:make_request(request)

+ 6 - 3
modpol/tests/org_req_test.lua

@@ -3,17 +3,20 @@ dofile('../modpol.lua');
 modpol.orgs.reset()
 
 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);
 
 new_request = {
-    user = "lukvmil",
+    user = "josh",
     type = "add_member",
-    params = {"lukvmil"}
+    params = {"josh"}
 }
 
 request_id = test_org:make_request(new_request)
 
 process_id = test_org:create_process("consent", request_id)
 process = test_org.processes[process_id]
-process:approve("luke", true)
+process:approve("luke", true)
+process:approve("nathan", true)