# HG changeset patch # User bsw # Date 1585065572 -3600 # Node ID 71f54c43d7cb958fb5927cfd74ecb454d4e46dd0 # Parent f8a4260841df86978b910659469157f0a9921877 Added API support interface diff -r f8a4260841df -r 71f54c43d7cb app/main/api/support.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/main/api/support.lua Tue Mar 24 16:59:32 2020 +0100 @@ -0,0 +1,17 @@ +local initiative_id = param.get("initiative_id") + +local success = util.add_support(initiative_id) + +slot.set_layout(nil, "application/json") + +local r = json.array() + +if success then + r.status = "ok" +else + r.status = "error" +end + +slot.put_into("data", json.export(json.object{ result = r })) +slot.put_into("data", "\n") + diff -r f8a4260841df -r 71f54c43d7cb app/main/initiative/_action/add_support.lua --- a/app/main/initiative/_action/add_support.lua Tue Mar 24 16:50:16 2020 +0100 +++ b/app/main/initiative/_action/add_support.lua Tue Mar 24 16:59:32 2020 +0100 @@ -1,62 +1,4 @@ -local initiative = Initiative:new_selector():add_where{ "id = ?", param.get_id()}:single_object_mode():exec() - +local initiative_id = param.get_id() local draft_id = param.get("draft_id", atom.integer) --- TODO important m1 selectors returning result _SET_! -local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() - -if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then - return execute.view { module = "index", view = "403" } -end - -if issue.closed then - slot.put_into("error", _"This issue is already closed.") - return false -elseif issue.fully_frozen then - slot.put_into("error", _"Voting for this issue has already begun.") - return false -elseif - (issue.half_frozen and issue.phase_finished) or - (not issue.accepted and issue.phase_finished) -then - slot.put_into("error", _"Current phase is already closed.") - return false -end - -if initiative.revoked then - slot.put_into("error", _"This initiative is revoked") - return false -end - -local member = app.session.member - -local supporter = Supporter:by_pk(initiative.id, member.id) - -local last_draft = Draft:new_selector() - :add_where{ "initiative_id = ?", initiative.id } - :add_order_by("id DESC") - :limit(1) - :single_object_mode() - :exec() - -if draft_id and draft_id ~= last_draft.id then - slot.select("error", function() - ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } - end) - return false -end - -if not supporter then - supporter = Supporter:new() - supporter.member_id = member.id - supporter.initiative_id = initiative.id - supporter.draft_id = last_draft.id - supporter:save() -elseif supporter.draft_id ~= last_draft.id then - supporter.draft_id = last_draft.id - supporter:save() - slot.put_into("notice", _"Your support has been updated to the latest draft") -else - slot.put_into("notice", _"You are already supporting the latest draft") -end - +return util.add_support(initiative_id, draft_id) diff -r f8a4260841df -r 71f54c43d7cb env/request/router.lua --- a/env/request/router.lua Tue Mar 24 16:50:16 2020 +0100 +++ b/env/request/router.lua Tue Mar 24 16:59:32 2020 +0100 @@ -11,6 +11,7 @@ settings_info = true, settings = true, event = true, + support = true, embed_initiative = true } diff -r f8a4260841df -r 71f54c43d7cb env/util/add_support.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/env/util/add_support.lua Tue Mar 24 16:59:32 2020 +0100 @@ -0,0 +1,67 @@ +function util.add_support(initiative_id, draft_id) + + local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec() + + -- TODO important m1 selectors returning result _SET_! + local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() + + if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then + slot.put_into("error", _"No voting rights.") + return false + end + + if issue.closed then + slot.put_into("error", _"This issue is already closed.") + return false + elseif issue.fully_frozen then + slot.put_into("error", _"Voting for this issue has already begun.") + return false + elseif + (issue.half_frozen and issue.phase_finished) or + (not issue.accepted and issue.phase_finished) + then + slot.put_into("error", _"Current phase is already closed.") + return false + end + + if initiative.revoked then + slot.put_into("error", _"This initiative is revoked") + return false + end + + local member = app.session.member + + local supporter = Supporter:by_pk(initiative.id, member.id) + + local last_draft = Draft:new_selector() + :add_where{ "initiative_id = ?", initiative.id } + :add_order_by("id DESC") + :limit(1) + :single_object_mode() + :exec() + + if draft_id and draft_id ~= last_draft.id then + slot.select("error", function() + ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } + end) + return false + end + + if not supporter then + supporter = Supporter:new() + supporter.member_id = member.id + supporter.initiative_id = initiative.id + supporter.draft_id = last_draft.id + supporter:save() + elseif supporter.draft_id ~= last_draft.id then + supporter.draft_id = last_draft.id + supporter:save() + slot.put_into("notice", _"Your support has been updated to the latest draft") + else + slot.put_into("notice", _"You are already supporting the latest draft") + end + + return true + +end +