remove_process.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. --- Remove a process
  2. -- @module remove_process
  3. local remove_process = {
  4. name = "Remove process",
  5. slug = "remove_process",
  6. desc = "User can remove own processes, approval required for those of others",
  7. hide = false;
  8. }
  9. remove_process.data = {
  10. }
  11. remove_process.config = {
  12. approval_module = "consent"
  13. }
  14. --- Initiate function
  15. -- @function remove_process:initiate
  16. -- @param result Callback if this module is embedded in other modules
  17. function remove_process:initiate(result)
  18. -- prepare process options
  19. local available_processes = {}
  20. for k,process in pairs(self.org.processes) do
  21. if process ~= "deleted" then
  22. available_processes[process.id] = process
  23. end
  24. end
  25. local process_list = {}
  26. local process_count = 0
  27. for k,v in pairs(available_processes) do
  28. local mine = ""
  29. if v.initiator == self.initiator then mine = "*" end
  30. table.insert(process_list,"["..v.id.."] "..v.slug..mine)
  31. process_count = process_count + 1
  32. end
  33. -- abort if no processes to remove
  34. if process_count == 0 then
  35. modpol.interactions.message(
  36. self.initiator, "Org has no modules")
  37. modpol.interactions.org_dashboard(
  38. self.initiator, self.org.id)
  39. if result then result() end
  40. self.org:delete_process(self.id)
  41. return
  42. end
  43. table.sort(process_list)
  44. -- now ask which to remove
  45. modpol.interactions.dropdown_query(
  46. self.initiator, "Choose a process to remove (* marks yours, no consent required):",
  47. process_list,
  48. function(process_choice)
  49. -- confirm choice
  50. local process_id = tonumber(
  51. string.match(process_choice, "%d+"))
  52. local process_mine = string.match(process_choice,
  53. "%*")
  54. modpol.interactions.binary_poll_user(
  55. self.initiator,
  56. "Confirm: Remove process \""..
  57. process_choice .. "\"?",
  58. function(input)
  59. if input == "Yes" then
  60. if process_mine then
  61. self.org:delete_process_tree(process_id)
  62. modpol.interactions.message(
  63. self.initiator,
  64. "Removed process: "..process_choice)
  65. modpol.interactions.org_dashboard(
  66. self.initiator, self.org.id)
  67. if result then result() end
  68. self.org:delete_process(self.id)
  69. else
  70. self:call_module(
  71. self.config.approval_module,
  72. self.initiator,
  73. {
  74. prompt =
  75. "Approve removal of process "
  76. ..process_choice.."?"
  77. },
  78. function(input)
  79. modpol.interactions.message_org(
  80. self.initiator,
  81. self.org.id,
  82. "Removing process: "..
  83. process_choice)
  84. self.org:delete_process_tree(process_id)
  85. modpol.interactions.org_dashboard(
  86. self.initiator, self.org.id)
  87. if result then result() end
  88. self.org:delete_process(self.id)
  89. end
  90. )
  91. end
  92. modpol.interactions.org_dashboard(
  93. self.initiator, self.org.id)
  94. else
  95. modpol.interactions.org_dashboard(
  96. self.initiator, self.org.id)
  97. if result then result() end
  98. self.org:delete_process(self.id)
  99. end
  100. end
  101. )
  102. end
  103. )
  104. end
  105. modpol.modules.remove_process = remove_process