process.lua 5.8 KB

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