process.lua 5.8 KB

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