decidim-restfull-proposals 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/decidim/api/rest_full/components/proposal_components_controller.rb +91 -0
  3. data/app/controllers/decidim/api/rest_full/draft_proposals/draft_proposals_controller.rb +116 -0
  4. data/app/controllers/decidim/api/rest_full/proposals/proposal_extended_data_controller.rb +31 -0
  5. data/app/controllers/decidim/api/rest_full/proposals/proposals_controller.rb +172 -0
  6. data/app/controllers/decidim/api/rest_full/vote_proposals/vote_proposals_controller.rb +90 -0
  7. data/app/jobs/decidim/rest_full/proposals/proposal_webhook_job.rb +67 -0
  8. data/app/models/decidim/rest_full/proposals/proposal_application_id.rb +14 -0
  9. data/app/serializers/decidim/api/rest_full/proposals/draft_proposal_serializer.rb +44 -0
  10. data/app/serializers/decidim/api/rest_full/proposals/proposal_component_serializer.rb +232 -0
  11. data/app/serializers/decidim/api/rest_full/proposals/proposal_serializer.rb +113 -0
  12. data/app/serializers/decidim/api/rest_full/proposals/proposal_state_serializer.rb +39 -0
  13. data/app/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer.rb +54 -0
  14. data/app/serializers/decidim/api/rest_full/proposals/vote_proposal_serializer.rb +27 -0
  15. data/app/services/decidim/rest_full/proposals/draft_proposals_operations.rb +248 -0
  16. data/app/services/decidim/rest_full/proposals/proposal_vote_eligibility.rb +106 -0
  17. data/app/services/decidim/rest_full/proposals/proposal_votes_operations.rb +173 -0
  18. data/app/services/decidim/rest_full/proposals/vote_proposals_operations.rb +114 -0
  19. data/config/locales/decidim_rest_full_proposals.en.yml +26 -0
  20. data/lib/decidim/rest_full/http_cache/proposal_show_fingerprint.rb +77 -0
  21. data/lib/decidim/rest_full/proposals/engine.rb +129 -0
  22. data/lib/decidim/rest_full/proposals/proposal_client_id_override.rb +18 -0
  23. data/lib/decidim/rest_full/proposals/proposals_controller_override.rb +28 -0
  24. data/lib/decidim/rest_full/proposals/ransackers.rb +84 -0
  25. data/lib/decidim/rest_full/proposals/test_definitions.rb +7 -0
  26. data/lib/decidim/rest_full/test/definitions/draft_proposal.rb +101 -0
  27. data/lib/decidim/rest_full/test/definitions/proposal.rb +115 -0
  28. data/lib/decidim/rest_full/test/definitions/proposal_component_schema.rb +110 -0
  29. data/lib/decidim/rest_full/test/definitions/proposals.rb +4 -0
  30. data/lib/decidim/rest_full/test/definitions/tags/draft_proposals_tag.rb +26 -0
  31. data/lib/decidim/rest_full/test/definitions/tags/proposal_tag.rb +32 -0
  32. data/lib/decidim/rest_full/test/definitions/vote_proposal.rb +87 -0
  33. data/lib/decidim-restfull-proposals.rb +17 -0
  34. data/spec/jobs/decidim/rest_full/proposals/proposal_webhook_job_spec.rb +66 -0
  35. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_index_spec.rb +149 -0
  36. data/spec/requests/decidim/api/rest_full/components/proposal_components_controller_show_spec.rb +140 -0
  37. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_create_spec.rb +116 -0
  38. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_delete_spec.rb +64 -0
  39. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_publish_spec.rb +132 -0
  40. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_show_spec.rb +72 -0
  41. data/spec/requests/decidim/api/rest_full/draft_proposals/draft_proposals_controller_update_spec.rb +307 -0
  42. data/spec/requests/decidim/api/rest_full/proposals/proposal_extended_data_controller_spec.rb +80 -0
  43. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_index_spec.rb +275 -0
  44. data/spec/requests/decidim/api/rest_full/proposals/proposals_controller_show_spec.rb +187 -0
  45. data/spec/requests/decidim/api/rest_full/scenarios/draft_publish_async_increments_count_and_lists_newest_spec.rb +144 -0
  46. data/spec/requests/decidim/api/rest_full/scenarios/participatory_process_phase_change_webhook_and_component_meta_spec.rb +130 -0
  47. data/spec/requests/decidim/api/rest_full/scenarios/proposals_published_at_desc_prev_next_chain_spec.rb +72 -0
  48. data/spec/requests/decidim/api/rest_full/scenarios/search_vote_enabled_impersonate_and_vote_spec.rb +88 -0
  49. data/spec/requests/decidim/api/rest_full/vote_proposals/vote_proposals_controller_spec.rb +231 -0
  50. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_chatbot_menu_spec.rb +147 -0
  51. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_phases_spec.rb +88 -0
  52. data/spec/serializers/decidim/api/rest_full/proposals/proposal_component_serializer_votes_spec.rb +103 -0
  53. data/spec/serializers/decidim/api/rest_full/proposals/proposal_serializer_can_vote_spec.rb +86 -0
  54. data/spec/serializers/decidim/api/rest_full/proposals/proposal_webhook_serializer_spec.rb +63 -0
  55. data/spec/services/decidim/rest_full/proposals/proposal_vote_eligibility_spec.rb +120 -0
  56. metadata +132 -0
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ # Shared imperative logic for draft proposals (sync controller + async ApiJob).
7
+ class DraftProposalsOperations
8
+ SPACE_MANIFEST_MODELS = Decidim::Api::RestFull::ApplicationController::SPACE_MANIFEST_MODELS
9
+
10
+ def initialize(execution_context, params)
11
+ @ctx = execution_context
12
+ @params = params.is_a?(ActionController::Parameters) ? params : ActionController::Parameters.new(params)
13
+ end
14
+
15
+ def create!
16
+ require_user!
17
+ component_id = @params.require(:data).require(:component_id)
18
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Draft Proposal already exists for this component" if collection.find_by(decidim_component_id: component_id)
19
+
20
+ draft_proposal = create_new_draft(component_id)
21
+ form = form_for(draft_proposal)
22
+ serialize_draft(draft_proposal, form)
23
+ end
24
+
25
+ def update!
26
+ require_user!
27
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Draft Proposal Not Found" unless draft_record
28
+
29
+ payload = data
30
+ return serialize_draft(draft_record.reload, form_for(draft_record)) if payload.keys.empty?
31
+
32
+ move_translations_to_current_locale!(draft_record)
33
+ form = form_for(draft_record)
34
+ apply_payload_to_form(form, payload)
35
+ validate_form_for_update!(form, payload.keys.map(&:to_s))
36
+ overlay_payload!(draft_record, form, payload)
37
+ serialize_draft(draft_record.reload, form_for(draft_record.reload))
38
+ end
39
+
40
+ def destroy!
41
+ require_user!
42
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Draft Proposal Not Found" unless draft_record
43
+
44
+ serialized_draft = serialize_draft(draft_record, form_for(draft_record), publishable: false)
45
+ draft_record.destroy!
46
+ serialized_draft
47
+ end
48
+
49
+ def publish!
50
+ require_user!
51
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Draft Proposal Not Found" unless draft_record
52
+
53
+ form = form_for(draft_record)
54
+ raise Decidim::RestFull::Core::ApiException::BadRequest.from_form(form) unless form.valid?
55
+
56
+ draft_record.update!(published_at: Time.now.utc)
57
+ published = nil
58
+ exec_ctx = @ctx
59
+ Decidim::Proposals::PublishProposal.call(draft_record, current_user) do
60
+ on(:ok) do
61
+ published = Decidim::Api::RestFull::Proposals::ProposalSerializer.new(
62
+ draft_record,
63
+ params: {
64
+ locales: available_locales,
65
+ host: organization.host,
66
+ act_as: exec_ctx.act_as,
67
+ client_id: exec_ctx.client_id
68
+ }
69
+ ).serializable_hash
70
+ end
71
+ on(:invalid) do
72
+ raise Decidim::RestFull::Core::ApiException::BadRequest.from_form(form) unless form.valid?
73
+ end
74
+ end
75
+ published
76
+ end
77
+
78
+ private
79
+
80
+ attr_reader :ctx
81
+
82
+ delegate :organization, :current_user, :act_as, :client_id, :available_locales, to: :ctx
83
+
84
+ def participatory_space_visibility
85
+ @participatory_space_visibility ||= Decidim::RestFull::ParticipatorySpaceVisibility.new(organization:, act_as:)
86
+ end
87
+
88
+ delegate :in_visible_spaces, :visible_spaces, :visible_scope_for, :space_class_from_name, to: :participatory_space_visibility
89
+
90
+ def require_user!
91
+ u = current_user
92
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User required" unless u
93
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User blocked" if u.blocked_at
94
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User locked" if u.locked_at
95
+ end
96
+
97
+ def filter_for_context(query)
98
+ components_filters = Decidim::Component.all
99
+ if @params.has_key?(:space_manifest)
100
+ components_filters = components_filters.where(participatory_space_type: space_class_from_name(@params.require(:space_manifest)))
101
+ components_filters = components_filters.where(participatory_space_id: @params.require(:space_id)) if @params.has_key?(:space_id)
102
+ end
103
+ query.where(decidim_component_id: in_visible_spaces(components_filters).ids)
104
+ end
105
+
106
+ def available_space_manifest_names
107
+ SPACE_MANIFEST_MODELS.keys.select do |m|
108
+ name = SPACE_MANIFEST_MODELS[m]
109
+ name.present? && Object.const_defined?(name)
110
+ end
111
+ end
112
+
113
+ def model_class
114
+ Decidim::Proposals::Proposal.joins(:coauthorships)
115
+ end
116
+
117
+ def collection
118
+ query = filter_for_context(model_class)
119
+ query.where("published_at is NULL AND decidim_coauthorships.decidim_author_id = ?", act_as.id)
120
+ end
121
+
122
+ def draft_record
123
+ @draft_record ||= collection.joins(:rest_full_application).where(rest_full_application: { api_client_id: client_id }).find_by(id: @params.require(:id))
124
+ end
125
+
126
+ def create_new_draft(component_id)
127
+ component = in_visible_spaces(Decidim::Component.all).find(component_id)
128
+ raise Decidim::RestFull::Core::ApiException::BadRequest, I18n.t("decidim.proposals.new.limit_reached").to_s if limit_reached?(component)
129
+
130
+ proposal = Decidim::Proposals::Proposal.new(component:, published_at: nil)
131
+ raise ::ActiveRecord::RecordNotSaved, "Could not add new draft" unless proposal.save!(validate: false)
132
+
133
+ coauthorship = proposal.coauthorships.build(author: current_user)
134
+ proposal.coauthorships << coauthorship
135
+ proposal.rest_full_application = Decidim::RestFull::Proposals::ProposalApplicationId.new(proposal_id: proposal.id, api_client_id: client_id)
136
+ raise ::ActiveRecord::RecordNotSaved, "Could not save draft relationships" unless proposal.save!(validate: false)
137
+
138
+ proposal
139
+ end
140
+
141
+ def limit_reached?(component)
142
+ proposal_limit = component.settings.proposal_limit
143
+ return false if proposal_limit.zero?
144
+
145
+ query = model_class.where(component:)
146
+ current_user_proposals_count = query.where("published_at IS NOT NULL AND decidim_coauthorships.decidim_author_id = ?", act_as.id).count
147
+ current_user_proposals_count >= proposal_limit
148
+ end
149
+
150
+ def data
151
+ @data ||= begin
152
+ data_payload = if @params.has_key?(:data)
153
+ @params[:data].permit!.to_h
154
+ else
155
+ {}
156
+ end
157
+ data_payload.select { |k| allowed_data_keys.include? k }
158
+ end
159
+ end
160
+
161
+ def allowed_data_keys
162
+ %w(title body)
163
+ end
164
+
165
+ def form_for(resource)
166
+ Decidim::Proposals::ProposalForm.from_model(resource).with_context(
167
+ current_organization: organization,
168
+ current_component: resource.component
169
+ )
170
+ end
171
+
172
+ def serialize_draft(draft_proposal, form, publishable: nil)
173
+ pub = publishable.nil? ? form.valid? : publishable
174
+ Decidim::Api::RestFull::Proposals::DraftProposalSerializer.new(
175
+ draft_proposal,
176
+ params: {
177
+ only: [],
178
+ locales: [current_locale.to_sym],
179
+ host: organization.host,
180
+ publishable: pub,
181
+ fields: allowed_data_keys
182
+ }
183
+ ).serializable_hash
184
+ end
185
+
186
+ def current_locale
187
+ @ctx.current_locale
188
+ end
189
+
190
+ def apply_payload_to_form(form, payload)
191
+ sanitizer = Rails::Html::FullSanitizer.new
192
+ allowed_data_keys.each do |field_name|
193
+ next unless payload_includes?(payload, field_name)
194
+
195
+ form.public_send(:"#{field_name}=", sanitizer.sanitize(payload_fetch(payload, field_name).to_s))
196
+ end
197
+ end
198
+
199
+ def validate_form_for_update!(form, update_keys)
200
+ form.valid?
201
+ update_errors = form.errors.select { |err| update_keys.include?(err.attribute.to_s) }
202
+ return if update_errors.empty?
203
+
204
+ raise Decidim::RestFull::Core::ApiException::BadRequest.from_errors(update_errors)
205
+ end
206
+
207
+ # Collapse title and body onto the resolved locale (user locale, or the
208
+ # organization default when user.locale is nil). Other locale keys are dropped.
209
+ # The stored string is copied as-is. Payload fields are applied afterwards.
210
+ def move_translations_to_current_locale!(draft_proposal)
211
+ locale = current_locale.to_s
212
+ allowed_data_keys.each do |field_name|
213
+ value = stored_translation(draft_proposal, field_name, locale)
214
+ draft_proposal.public_send(:"#{field_name}=", value.nil? ? {} : { locale => value })
215
+ end
216
+ end
217
+
218
+ def stored_translation(draft_proposal, field_name, locale)
219
+ raw = draft_proposal.public_send(field_name)
220
+ hash = raw.is_a?(Hash) ? raw.stringify_keys : {}
221
+ return hash[locale] if hash[locale].present?
222
+
223
+ hash.each_value.find(&:present?)
224
+ end
225
+
226
+ def overlay_payload!(draft_proposal, form, payload)
227
+ locale = current_locale.to_s
228
+ allowed_data_keys.each do |field_name|
229
+ next unless payload_includes?(payload, field_name)
230
+
231
+ draft_proposal.public_send(:"#{field_name}=", { locale => form.public_send(field_name) })
232
+ end
233
+ draft_proposal.save(validate: false)
234
+ end
235
+
236
+ def payload_includes?(payload, field_name)
237
+ payload.has_key?(field_name) || payload.has_key?(field_name.to_sym)
238
+ end
239
+
240
+ def payload_fetch(payload, field_name)
241
+ return payload[field_name] if payload.has_key?(field_name)
242
+
243
+ payload[field_name.to_sym]
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ # Shared vote eligibility for proposal / proposal-component REST meta.
7
+ # Reuses Decidim Awesome VotesByProposalStatus when that feature is available.
8
+ class ProposalVoteEligibility
9
+ class << self
10
+ def voting_enabled?(component)
11
+ settings = component.current_settings.to_h
12
+ settings[:votes_enabled] && !settings[:votes_blocked]
13
+ end
14
+
15
+ def awesome_votes_enabled_by_status(component)
16
+ return false unless awesome_status_feature?
17
+
18
+ ActiveModel::Type::Boolean.new.cast(
19
+ component.current_settings.try(:awesome_votes_enabled_by_status)
20
+ ) || false
21
+ end
22
+
23
+ def awesome_votes_enabled_states(component)
24
+ return [] unless awesome_status_feature?
25
+
26
+ Array(component.current_settings.try(:awesome_votes_enabled_states)).compact_blank.map(&:to_s)
27
+ end
28
+
29
+ def status_filter(component)
30
+ return NullStatusFilter.new unless awesome_status_feature?
31
+
32
+ Decidim::DecidimAwesome::Proposals::VotesByProposalStatus.new(component.current_settings)
33
+ end
34
+
35
+ def proposal_can_vote?(proposal)
36
+ return false unless proposal.published?
37
+ return false if proposal.withdrawn?
38
+ return false unless voting_enabled?(proposal.component)
39
+
40
+ filter = status_filter(proposal.component)
41
+ if filter.active?
42
+ filter.allowed?(proposal)
43
+ else
44
+ !proposal.rejected?
45
+ end
46
+ end
47
+
48
+ def votable_scope(component)
49
+ base = ::Decidim::Proposals::Proposal
50
+ .where(decidim_component_id: component.id)
51
+ .published
52
+ .not_withdrawn
53
+
54
+ filter = status_filter(component)
55
+ return base.except_rejected unless filter.active?
56
+
57
+ tokens = filter.allowed_tokens
58
+ return base.none if tokens.blank?
59
+
60
+ state_table = ::Decidim::Proposals::ProposalState.table_name
61
+ base.left_joins(:proposal_state).where(
62
+ "#{state_table}.token IN (:tokens) OR (#{state_table}.id IS NULL AND :includes_not_answered)",
63
+ tokens:,
64
+ includes_not_answered: tokens.include?("not_answered")
65
+ )
66
+ end
67
+
68
+ def unvoted_voteable_proposals_exist?(component, user)
69
+ base = votable_scope(component)
70
+ return false unless base.exists?
71
+
72
+ voted_ids = ::Decidim::Proposals::ProposalVote
73
+ .where(decidim_author_id: user.id)
74
+ .distinct
75
+ .pluck(:decidim_proposal_id)
76
+
77
+ return true if voted_ids.blank?
78
+
79
+ base.where.not(id: voted_ids).exists?
80
+ end
81
+
82
+ def awesome_status_feature?
83
+ Decidim::Toggle.gem_present?("decidim-decidim_awesome") &&
84
+ defined?(::Decidim::DecidimAwesome) &&
85
+ ::Decidim::DecidimAwesome.enabled?(:votes_by_proposal_status)
86
+ end
87
+ end
88
+
89
+ # Inactive stand-in when Awesome / votes_by_proposal_status is unavailable.
90
+ class NullStatusFilter
91
+ def active?
92
+ false
93
+ end
94
+
95
+ def allowed?(_proposal)
96
+ true
97
+ end
98
+
99
+ def allowed_tokens
100
+ []
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ # Shared logic for casting a proposal vote (sync + async).
7
+ class ProposalVotesOperations
8
+ def initialize(execution_context, params)
9
+ @ctx = execution_context
10
+ @params = params.is_a?(ActionController::Parameters) ? params : ActionController::Parameters.new(params)
11
+ end
12
+
13
+ def create!
14
+ require_user!
15
+ validate_component_votes!
16
+ validate_vote_preconditions!
17
+ serialized = cast_vote!
18
+ return serialize_proposal_with_vote(serialized) if include_full_proposal?
19
+
20
+ serialized
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :ctx
26
+
27
+ delegate :organization, :current_user, :available_locales, to: :ctx
28
+
29
+ def act_as
30
+ current_user
31
+ end
32
+
33
+ def participatory_space_visibility
34
+ @participatory_space_visibility ||= Decidim::RestFull::ParticipatorySpaceVisibility.new(organization:, act_as:)
35
+ end
36
+
37
+ delegate :in_visible_spaces, :visible_spaces, :visible_scope_for, :space_class_from_name, to: :participatory_space_visibility
38
+
39
+ def require_user!
40
+ u = current_user
41
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User required" unless u
42
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User blocked" if u.blocked_at
43
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User locked" if u.locked_at
44
+ end
45
+
46
+ def validate_component_votes!
47
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Vote are disabled" unless proposal_component.current_settings[:votes_enabled]
48
+ end
49
+
50
+ def validate_vote_preconditions!
51
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Already voted" if voted?
52
+
53
+ validate_weight!
54
+ end
55
+
56
+ def validate_weight!
57
+ if weight.negative?
58
+ raise Decidim::RestFull::Core::ApiException::BadRequest,
59
+ "Weight: error in the weight field. Negative weight not supported"
60
+ end
61
+ if weight.zero? && !support_abstention?
62
+ raise Decidim::RestFull::Core::ApiException::BadRequest,
63
+ "Weight: error in the weight field. Abstention not supported"
64
+ end
65
+ return unless !support_weight? && weight > 1
66
+
67
+ raise Decidim::RestFull::Core::ApiException::BadRequest,
68
+ "Weight: error in the weight field. Weight not supported"
69
+ end
70
+
71
+ def cast_vote!
72
+ serialized = nil
73
+ Decidim::Proposals::VoteProposal.call(proposal, current_user) do
74
+ on(:ok) do |proposal_vote|
75
+ proposal_vote.weight = weight if support_weight?
76
+ proposal_vote.save! if proposal_vote.changed?
77
+ serialized = serialize_vote(proposal_vote)
78
+ end
79
+ on(:invalid) do
80
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Vote is invalid"
81
+ end
82
+ end
83
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Vote is invalid" if serialized.nil?
84
+
85
+ serialized
86
+ end
87
+
88
+ def support_weight?
89
+ awesome? && voting_manifest
90
+ end
91
+
92
+ def support_abstention?
93
+ awesome? && proposal_component.settings[:voting_cards_show_abstain]
94
+ end
95
+
96
+ def voting_manifest
97
+ @voting_manifest ||= proposal_component.settings[:awesome_voting_manifest]
98
+ end
99
+
100
+ def weight
101
+ @weight ||= @params.require(:data).require(:weight).to_i
102
+ end
103
+
104
+ def awesome?
105
+ Decidim::Toggle.gem_present?("decidim-decidim_awesome") && Decidim::DecidimAwesome.enabled?(:weighted_proposal_voting)
106
+ end
107
+
108
+ def voted?
109
+ proposal_votes.exists?
110
+ end
111
+
112
+ def proposal_votes
113
+ proposal.votes.where(decidim_author_id: current_user.id)
114
+ end
115
+
116
+ def proposal
117
+ @proposal ||= collection.find(@params.require(:proposal_id))
118
+ end
119
+
120
+ def proposal_component
121
+ @proposal_component ||= proposal.component
122
+ end
123
+
124
+ def model_class
125
+ Decidim::Proposals::Proposal.published
126
+ end
127
+
128
+ def collection
129
+ filter_for_context(model_class)
130
+ end
131
+
132
+ def filter_for_context(query)
133
+ components_filters = Decidim::Component.all
134
+ if @params.has_key?(:space_manifest)
135
+ components_filters = components_filters.where(participatory_space_type: space_class_from_name(@params.require(:space_manifest)))
136
+ components_filters = components_filters.where(participatory_space_id: @params.require(:space_id)) if @params.has_key?(:space_id)
137
+ end
138
+ visible_ids = in_visible_spaces(components_filters).select(:id)
139
+ query.where(decidim_component_id: visible_ids)
140
+ end
141
+
142
+ def include_full_proposal?
143
+ ActiveModel::Type::Boolean.new.cast(@params[:include_proposal])
144
+ end
145
+
146
+ def serialize_vote(vote)
147
+ Decidim::Api::RestFull::Proposals::VoteProposalSerializer.new(
148
+ vote,
149
+ params: {
150
+ locales: available_locales,
151
+ host: organization.host,
152
+ act_as: current_user
153
+ }
154
+ ).serializable_hash
155
+ end
156
+
157
+ def serialize_proposal_with_vote(_vote_payload)
158
+ proposal.reload
159
+ Decidim::Api::RestFull::Proposals::ProposalSerializer.new(
160
+ proposal,
161
+ params: {
162
+ only: [],
163
+ locales: available_locales,
164
+ host: organization.host,
165
+ act_as: current_user,
166
+ client_id: @ctx.client_id
167
+ }
168
+ ).serializable_hash
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ # CRUD for Decidim::Proposals::ProposalVote (synchronous API).
7
+ class VoteProposalsOperations
8
+ def initialize(execution_context, params)
9
+ @ctx = execution_context
10
+ @params = params.is_a?(ActionController::Parameters) ? params : ActionController::Parameters.new(params)
11
+ end
12
+
13
+ def create!
14
+ ProposalVotesOperations.new(@ctx, @params).create!
15
+ end
16
+
17
+ def destroy!
18
+ require_user!
19
+ vote = find_vote!
20
+ raise Decidim::RestFull::Core::ApiException::Forbidden, "Cannot remove another user's vote" unless vote.decidim_author_id == current_user.id
21
+
22
+ serialized = serialize_vote(vote)
23
+ Decidim::Proposals::UnvoteProposal.call(vote.proposal, current_user) do
24
+ on(:ok) { return serialized }
25
+ on(:invalid) { raise Decidim::RestFull::Core::ApiException::BadRequest, "Vote could not be removed" }
26
+ end
27
+ end
28
+
29
+ def index_scope
30
+ scope = Decidim::Proposals::ProposalVote
31
+ .joins(:proposal)
32
+ .where(decidim_proposals_proposals: { decidim_component_id: visible_component_ids })
33
+ .merge(Decidim::Proposals::Proposal.published)
34
+ apply_vote_filters(scope)
35
+ end
36
+
37
+ def find_vote!
38
+ scope = Decidim::Proposals::ProposalVote
39
+ .joins(:proposal)
40
+ .where(decidim_proposals_proposals: { decidim_component_id: visible_component_ids })
41
+ scope.find(resource_id)
42
+ rescue ActiveRecord::RecordNotFound
43
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Vote not found"
44
+ end
45
+
46
+ private
47
+
48
+ attr_reader :ctx, :params
49
+
50
+ delegate :organization, :current_user, :available_locales, to: :ctx
51
+
52
+ def require_user!
53
+ u = current_user
54
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User required" unless u
55
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User blocked" if u.blocked_at
56
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User locked" if u.locked_at
57
+ end
58
+
59
+ def resource_id
60
+ (@params[:id] || @params["id"]).to_s
61
+ end
62
+
63
+ def participatory_space_visibility
64
+ @participatory_space_visibility ||= Decidim::RestFull::ParticipatorySpaceVisibility.new(
65
+ organization:,
66
+ act_as: current_user
67
+ )
68
+ end
69
+
70
+ delegate :in_visible_spaces, :space_class_from_name, to: :participatory_space_visibility
71
+
72
+ def visible_component_ids
73
+ components = Decidim::Component.where(manifest_name: "proposals")
74
+ if params.has_key?(:space_manifest)
75
+ components = components.where(participatory_space_type: space_class_from_name(params.require(:space_manifest)))
76
+ components = components.where(participatory_space_id: params.require(:space_id)) if params.has_key?(:space_id)
77
+ end
78
+ in_visible_spaces(components).select(:id)
79
+ end
80
+
81
+ def apply_vote_filters(scope)
82
+ filter = params[:filter] || params["filter"]
83
+ return scope unless filter
84
+
85
+ filter = filter.to_unsafe_h.stringify_keys if filter.respond_to?(:to_unsafe_h)
86
+ filter = filter.stringify_keys
87
+
88
+ if (author_id = filter["creator_id"] || filter["author_id"])
89
+ scope = scope.where(decidim_author_id: author_id)
90
+ end
91
+ scope = scope.where(decidim_proposal_id: filter["proposal_id"]) if filter["proposal_id"]
92
+ if (component_id = filter["component_id"])
93
+ scope = scope.where(decidim_proposals_proposals: { decidim_component_id: component_id })
94
+ end
95
+ if (space_id = filter["participatory_space_id"])
96
+ scope = scope.joins(proposal: :component).where(decidim_components: { participatory_space_id: space_id })
97
+ end
98
+ scope
99
+ end
100
+
101
+ def serialize_vote(vote)
102
+ Decidim::Api::RestFull::Proposals::VoteProposalSerializer.new(
103
+ vote,
104
+ params: {
105
+ locales: available_locales,
106
+ host: organization.host,
107
+ act_as: current_user
108
+ }
109
+ ).serializable_hash
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,26 @@
1
+ ---
2
+ en:
3
+ decidim:
4
+ rest_full:
5
+ models:
6
+ api_client:
7
+ fields:
8
+ scope_proposals: 'Proposal: manage proposals'
9
+ permission:
10
+ draft_proposal_creation:
11
+ succeeded: 'Draft proposal created: send draft proposal details'
12
+ draft_proposal_update:
13
+ succeeded: 'Draft proposal updated: send draft proposal details'
14
+ proposal_creation:
15
+ succeeded: 'Proposal created: send proposal details'
16
+ proposal_state_change:
17
+ succeeded: 'Proposal state changed: send proposal details'
18
+ proposal_update:
19
+ succeeded: 'Proposal updated: send proposal details'
20
+ proposals:
21
+ draft: 'draft: create, update, publish and see a draft proposal'
22
+ extended_data:
23
+ read: 'read extended data: can read proposal extended data'
24
+ update: 'write extended data: update and remove proposal extended data'
25
+ read: 'read: list and details of a proposal'
26
+ vote: 'vote: vote on proposals'