liquid_feedback_frontend

annotate env/util/add_support.lua @ 1516:9d99a4f262a2

Fixed privilege check for support via API
author bsw
date Thu Aug 20 14:03:09 2020 +0200 (2020-08-20)
parents 71f54c43d7cb
children 87e2a58bef9e
rev   line source
bsw@1500 1 function util.add_support(initiative_id, draft_id)
bsw@1500 2
bsw@1500 3 local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec()
bsw@1500 4
bsw@1500 5 -- TODO important m1 selectors returning result _SET_!
bsw@1500 6 local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
bsw@1500 7
bsw@1516 8 local member = app.access_token.member or app.session.member
bsw@1516 9
bsw@1516 10 if not member:has_voting_right_for_unit_id(issue.area.unit_id) then
bsw@1500 11 slot.put_into("error", _"No voting rights.")
bsw@1500 12 return false
bsw@1500 13 end
bsw@1500 14
bsw@1500 15 if issue.closed then
bsw@1500 16 slot.put_into("error", _"This issue is already closed.")
bsw@1500 17 return false
bsw@1500 18 elseif issue.fully_frozen then
bsw@1500 19 slot.put_into("error", _"Voting for this issue has already begun.")
bsw@1500 20 return false
bsw@1500 21 elseif
bsw@1500 22 (issue.half_frozen and issue.phase_finished) or
bsw@1500 23 (not issue.accepted and issue.phase_finished)
bsw@1500 24 then
bsw@1500 25 slot.put_into("error", _"Current phase is already closed.")
bsw@1500 26 return false
bsw@1500 27 end
bsw@1500 28
bsw@1500 29 if initiative.revoked then
bsw@1500 30 slot.put_into("error", _"This initiative is revoked")
bsw@1500 31 return false
bsw@1500 32 end
bsw@1500 33
bsw@1500 34 local member = app.session.member
bsw@1500 35
bsw@1500 36 local supporter = Supporter:by_pk(initiative.id, member.id)
bsw@1500 37
bsw@1500 38 local last_draft = Draft:new_selector()
bsw@1500 39 :add_where{ "initiative_id = ?", initiative.id }
bsw@1500 40 :add_order_by("id DESC")
bsw@1500 41 :limit(1)
bsw@1500 42 :single_object_mode()
bsw@1500 43 :exec()
bsw@1500 44
bsw@1500 45 if draft_id and draft_id ~= last_draft.id then
bsw@1500 46 slot.select("error", function()
bsw@1500 47 ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" }
bsw@1500 48 end)
bsw@1500 49 return false
bsw@1500 50 end
bsw@1500 51
bsw@1500 52 if not supporter then
bsw@1500 53 supporter = Supporter:new()
bsw@1500 54 supporter.member_id = member.id
bsw@1500 55 supporter.initiative_id = initiative.id
bsw@1500 56 supporter.draft_id = last_draft.id
bsw@1500 57 supporter:save()
bsw@1500 58 elseif supporter.draft_id ~= last_draft.id then
bsw@1500 59 supporter.draft_id = last_draft.id
bsw@1500 60 supporter:save()
bsw@1500 61 slot.put_into("notice", _"Your support has been updated to the latest draft")
bsw@1500 62 else
bsw@1500 63 slot.put_into("notice", _"You are already supporting the latest draft")
bsw@1500 64 end
bsw@1500 65
bsw@1500 66 return true
bsw@1500 67
bsw@1500 68 end
bsw@1500 69

Impressum / About Us