remove_child_org.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --- Remove child org
  2. -- A simple module that calls a process on an org to remove its child.
  3. -- @module remove_child_org
  4. local remove_child_org = {
  5. name = "Remove child org",
  6. slug = "remove_child_org",
  7. desc = "Removes a child org."
  8. }
  9. remove_child_org.data = {
  10. result = nil,
  11. child_to_remove = nil
  12. }
  13. remove_child_org.config = {
  14. approval_module = false
  15. }
  16. --- Removes a child org with consent
  17. -- @function remove_child_org:initiate
  18. -- @param result Callback if this module is embedded in other modules
  19. function remove_child_org:initiate(result)
  20. local children = {}
  21. for i,v in ipairs(self.org.children) do
  22. local child = modpol.orgs.get_org(v)
  23. if child then table.insert(children, child.name) end
  24. end
  25. -- Abort if no child orgs
  26. if #children == 0 then
  27. modpol.interactions.message(
  28. self.initiator,
  29. "Org has no children")
  30. if result then result() end
  31. self.org:delete_process(self.id)
  32. else
  33. self.data.result = result
  34. modpol.interactions.dropdown_query(
  35. self.initiator,
  36. "Choose a child of org "..
  37. self.org.name.." to remove:",
  38. children,
  39. function(input)
  40. self.data.child_to_remove = modpol.orgs.get_org(input)
  41. self:call_module(
  42. self.config.approval_module,
  43. self.initiator,
  44. {
  45. prompt = "Remove child org "..input.."?"
  46. },
  47. function()
  48. self:complete()
  49. end)
  50. modpol.interactions.org_dashboard(
  51. self.initiator, self.org.name)
  52. end)
  53. end
  54. end
  55. --- Complete the remove process
  56. -- @function remove_child_org:complete
  57. function remove_child_org:complete()
  58. modpol.interactions.message_org(
  59. self.initiator, self.data.child_to_remove.id,
  60. "Removing this org: " .. self.data.child_to_remove.name)
  61. modpol.interactions.message_org(
  62. self.initiator, self.org.id,
  63. "Removing child org of " .. self.org.name .. ": " ..
  64. self.data.child_to_remove.name)
  65. self.data.child_to_remove:delete()
  66. if self.data.result then self.data.result() end
  67. self.org:delete_process(self.id)
  68. end
  69. modpol.modules.remove_child_org = remove_child_org