added remove_org, get_org, and record functions to orgs

This commit is contained in:
Luke Miller 2021-04-05 22:39:20 -04:00
parent 9a0680b07e
commit 0a16d85be2

View File

@ -4,13 +4,26 @@ modpol.orgs =
list = {}
}
-- sets modpol.orgs as it's own callback
-- sets modpol.orgs as it's own fallback
modpol.orgs.__index = modpol.orgs
-- ==================================================
-- returns org when given its id
function modpol.orgs.get_org(id)
return modpol.orgs.list[id]
end
-- ===================================================
-- initializes the instance (root org)
-- can only be run once, as only one instance can exist
function modpol.orgs:init_instance()
local error_msg
if modpol.orgs.list[1] then
print('Error: instance has already been initialized')
return false
error_msg = 'Error: instance has already been initialized'
modpol.ocutil.log(error_msg)
return false, error_msg
end
local instance = {
@ -31,17 +44,57 @@ function modpol.orgs:init_instance()
return instance
end
function modpol.orgs:add_org(name)
if self.id == nil then
print('Error: add_org can only be run by a parent org')
-- =======================================================
-- records a log message to the modpol ledger
function modpol.orgs:record(msg, entry_type)
local entry = {
timestamp = '',
entry_type = nil,
action_msg = '',
org_name = '',
org_id = nil,
}
if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
entry.action_msg = msg
else
print('Error: msg must be a non empty string')
return false
end
if modpol.ocutil.str_empty(name) then
print('Error: name is required')
if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
entry.entry_type = entry_type
else
print('Error: entry_type must be a non empty string')
return false
end
entry.timestamp = os.time()
entry.org_id = self.id
entry.org_name = self.name
table.insert(modpol.ledger, entry)
modpol.store_data()
end
-- ==================================================
-- adds a new sub org to the org it is called on
-- ex: instance:add_org('town hall')
function modpol.orgs:add_org(name)
if self.id == nil then
error_msg = 'Error: add_org can only be called by another org'
modpol.ocutil.log(error_msg)
return false, error_msg
end
if modpol.ocutil.str_empty(name) then
error_msg = 'Error: org name is required'
modpol.ocutil.log(error_msg)
return false, error_msg
end
-- creating the child sub org
modpol.orgs.count = modpol.orgs.count + 1
local child_org = {
@ -49,11 +102,8 @@ function modpol.orgs:add_org(name)
name = name,
policies = {},
members = {},
ledger = {},
parent = self.id,
children = {},
properties = {},
old_names = {}
}
setmetatable(child_org, modpol.orgs)
@ -62,13 +112,37 @@ function modpol.orgs:add_org(name)
-- adding child to org list
modpol.orgs.list[child_org.id] = child_org
return child_org
end
-- ========================================
-- recursively deletes an org and its suborgs
-- note: "reason" param was removed, can be added back
function modpol.orgs:delete()
if self.id == 1 then
return false, 'Error: cannot delete instance'
end
if #self.children > 0 then
for i, child_id in pairs(self.children) do
local child = modpol.orgs.get_org(child_id)
print(child_id, child)
child:delete()
end
end
modpol.orgs.list[self.id] = nil
print('Removed ' .. self.name .. ': ' .. self.id)
end
function modpol.orgs:add_member(user)
table.insert(self.members, user)
end
function modpol.orgs:list_member()
print(self.members[1])
end
end