liquid_feedback_frontend

view env/util/add_support.lua @ 1500:71f54c43d7cb

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

Impressum / About Us