liquid_feedback_frontend

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/env/util/add_support.lua	Tue Mar 24 16:59:32 2020 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +function util.add_support(initiative_id, draft_id)
     1.5 +
     1.6 +  local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec()
     1.7 +
     1.8 +  -- TODO important m1 selectors returning result _SET_!
     1.9 +  local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
    1.10 +
    1.11 +  if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then
    1.12 +    slot.put_into("error", _"No voting rights.")
    1.13 +    return false
    1.14 +  end
    1.15 +
    1.16 +  if issue.closed then
    1.17 +    slot.put_into("error", _"This issue is already closed.")
    1.18 +    return false
    1.19 +  elseif issue.fully_frozen then
    1.20 +    slot.put_into("error", _"Voting for this issue has already begun.")
    1.21 +    return false
    1.22 +  elseif 
    1.23 +    (issue.half_frozen and issue.phase_finished) or
    1.24 +    (not issue.accepted and issue.phase_finished) 
    1.25 +  then
    1.26 +    slot.put_into("error", _"Current phase is already closed.")
    1.27 +    return false
    1.28 +  end
    1.29 +
    1.30 +  if initiative.revoked then
    1.31 +    slot.put_into("error", _"This initiative is revoked")
    1.32 +    return false
    1.33 +  end
    1.34 +
    1.35 +  local member = app.session.member
    1.36 +
    1.37 +  local supporter = Supporter:by_pk(initiative.id, member.id)
    1.38 +
    1.39 +  local last_draft = Draft:new_selector()
    1.40 +    :add_where{ "initiative_id = ?", initiative.id }
    1.41 +    :add_order_by("id DESC")
    1.42 +    :limit(1)
    1.43 +    :single_object_mode()
    1.44 +    :exec()
    1.45 +    
    1.46 +  if draft_id and draft_id ~= last_draft.id then
    1.47 +    slot.select("error", function()
    1.48 +      ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" }
    1.49 +    end)
    1.50 +    return false
    1.51 +  end
    1.52 +
    1.53 +  if not supporter then
    1.54 +    supporter = Supporter:new()
    1.55 +    supporter.member_id = member.id
    1.56 +    supporter.initiative_id = initiative.id
    1.57 +    supporter.draft_id = last_draft.id
    1.58 +    supporter:save()
    1.59 +  elseif supporter.draft_id ~= last_draft.id then
    1.60 +    supporter.draft_id = last_draft.id
    1.61 +    supporter:save()
    1.62 +    slot.put_into("notice", _"Your support has been updated to the latest draft")
    1.63 +  else
    1.64 +    slot.put_into("notice", _"You are already supporting the latest draft")
    1.65 +  end
    1.66 +
    1.67 +  return true
    1.68 +
    1.69 +end
    1.70 +

Impressum / About Us