process.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. --- Process functions for orgs
  2. -- @module modpol.orgs.process
  3. --- Call modules
  4. -- @function modpol.orgs.call_module
  5. -- @param module_slug Same as module name
  6. -- @param intiator Initiator for module
  7. -- @param config Config for module
  8. -- @param result
  9. function modpol.orgs:call_module(module_slug, initiator, config, result, parent_id)
  10. if not modpol.modules[module_slug] then
  11. modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
  12. return
  13. end
  14. local index = #self.processes + 1
  15. local module = modpol.modules[module_slug]
  16. -- first applies any relevant org policies
  17. -- then overrides with the config values given on input
  18. local new_config = {}
  19. if modpol.util.num_pairs(module.config) > 0 then
  20. for k, v in pairs(module.config) do
  21. new_config[k] = v
  22. -- overrides with org policies
  23. if self.policies[module_slug]
  24. and self.policies[module_slug][k] then
  25. new_config[k] = self.policies[module_slug][k]
  26. end
  27. -- overrides with input settings
  28. if config and config[k] then
  29. new_config[k] = config[k]
  30. end
  31. end
  32. end
  33. -- setting default params
  34. local new_process = {
  35. metatable = {__index = module},
  36. initiator = initiator,
  37. org = self,
  38. id = index,
  39. parent_id = parent_id,
  40. children = {},
  41. config = modpol.util.copy_table(new_config),
  42. data = modpol.util.copy_table(module.data),
  43. slug = module_slug
  44. }
  45. -- call module wrapper for modules, passes its id to child process when called
  46. function new_process:call_module(module_slug, initiator, config, result)
  47. local child_id = self.org:call_module(module_slug, initiator, config, result, self.id)
  48. table.insert(self.children, child_id)
  49. end
  50. setmetatable(new_process, new_process.metatable)
  51. self.processes[index] = new_process
  52. self.processes[index]:initiate(result)
  53. local msg = "Initiating "..module_slug..
  54. " process id "..index.." in org "..self.name
  55. return index
  56. end
  57. --- Generic approval check for modules, able to call other modules
  58. -- @function modpol.orgs.approve
  59. -- @param approval_module Slug of module used to approve, or nil
  60. -- @param process The parent process of which this is part
  61. -- @param config Config for module
  62. -- @param result Function for what gets done if approved
  63. function modpol.orgs:approve(approval_module, process, config, result)
  64. if not approval_module then -- if nil then simply approve
  65. result()
  66. return
  67. elseif not modpol.modules[approval_module] then
  68. modpol.interactions.message(
  69. process.initiator,
  70. "Approval process failed: module " .. approval_module
  71. .. " does not exist.")
  72. modpol.interactions.org_dashboard(
  73. process.initiator, process.org.id)
  74. process.org:delete_process(process.id)
  75. return
  76. else
  77. -- call module
  78. modpol.orgs:call_module(
  79. approval_module,
  80. process.initiator,
  81. config,
  82. result, process.id)
  83. end
  84. end
  85. --- Get the root process of the given id
  86. -- @function modpol.orgs.get_root_process
  87. -- @param id
  88. -- @return root process
  89. function modpol.orgs:get_root_process(id)
  90. local process = self.processes[id]
  91. while (process.parent_id) do
  92. process = self.processes[process.parent_id]
  93. end
  94. return process
  95. end
  96. --- Delete the process given id, return to dashboard
  97. -- @function modpol.orgs.delete_process
  98. -- @param id
  99. function modpol.orgs:delete_process(id)
  100. local process = self.processes[id]
  101. if process and process ~= "deleted" then
  102. -- recursively deletes any children
  103. if #process.children > 0 then
  104. for i, child_id in pairs(process.children) do
  105. self:delete_process(child_id)
  106. end
  107. end
  108. local msg = "Deleting " .. self.processes[id].slug .. " process id "..id.." in org "..self.name
  109. modpol.ocutil.log(msg)
  110. self:record(msg, self.processes[id].slug)
  111. self:wipe_pending_actions(id)
  112. -- sets process to 'deleted' in process table
  113. self.processes[id] = 'deleted'
  114. end
  115. end
  116. --- Delete process tree by id
  117. -- @function modpol.orgs:delete_process_tree
  118. -- @param id Id of process tree
  119. function modpol.orgs:delete_process_tree(id)
  120. self:delete_process(self:get_root_process(id).id)
  121. end
  122. --- Add a new pending action
  123. -- @function modpol.orgs:add_pending_action
  124. -- @param process_id Process id
  125. -- @param user User adding the action
  126. -- @param callback
  127. function modpol.orgs:add_pending_action(process_id, user, callback)
  128. self.pending[user] = self.pending[user] or {}
  129. self.pending[user][process_id] = callback
  130. modpol.interactions.message(
  131. user, "New pending action in org "..self.name)
  132. end
  133. --- Remove a pending action
  134. -- @function modpol.orgs:remove_pending_action
  135. -- @param process_id Process id to be removed
  136. -- @param user
  137. function modpol.orgs:remove_pending_action(process_id, user)
  138. if self.pending[user] then
  139. self.pending[user][process_id] = nil
  140. end
  141. end
  142. --- Wipe all pending actions for process
  143. -- @function modpol.orgs:wipe_pending_actions
  144. -- @param process_id
  145. function modpol.orgs:wipe_pending_actions(process_id)
  146. for user in pairs(self.pending) do
  147. self.pending[user][process_id] = nil
  148. end
  149. end
  150. --- Check if there are pending actions for user
  151. -- @function modpol.orgs:has_pending_actions
  152. -- @param user User
  153. -- @return True if there are pending actions for a user, false if not
  154. function modpol.orgs:has_pending_actions(user)
  155. -- next() will return the next pair in a table
  156. -- if next() returns nil, the table is empty
  157. if not self.pending[user] then
  158. return false
  159. else
  160. if not next(self.pending[user]) then
  161. return false
  162. else
  163. return true
  164. end
  165. end
  166. end
  167. --- Interact a user with given process
  168. -- @function modpol.orgs:interact
  169. -- @param process_id
  170. -- @param user
  171. function modpol.orgs:interact(process_id, user)
  172. local process = self.processes[process_id]
  173. if self.pending[user] then
  174. local callback = self.pending[user][process_id]
  175. if callback and process ~= "deleted" then
  176. -- get data in case callback ends process
  177. local slug = self.processes[process_id].slug
  178. -- run callback
  179. process[callback](process, user)
  180. -- record org data
  181. local msg = "Updating "..slug..
  182. " process id "..process_id.." in org "..self.name
  183. self:record(msg, slug)
  184. end
  185. end
  186. end