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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c678bfb79c0d935d23476cece83d6a19c55eb99730383fe662771c162af378f6
4
+ data.tar.gz: d39876d334e1210d383ac8db5d424673b930a47440697f59d37c0b375f5081bf
5
+ SHA512:
6
+ metadata.gz: 4e09c21348de62fd2787439fe6e1afd90daa46212cce4e7f32ccdabfa2d97f8c80f005a6ce4bd8a4129f212ebef89ca33fc0e2c691b47ea724aed0d28f36ff5b
7
+ data.tar.gz: 33a379ac8019cd4fbd0353d8ac8d812221e1b6b14cd68cc81c866f7db86e08d4de9ddf58b16d86a8b840cd05b9f2950f94c6d45e9a41fd13675fa21888de1ab7
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module Components
7
+ class ProposalComponentsController < Decidim::Api::RestFull::Components::ComponentsController
8
+ before_action { doorkeeper_authorize! :public }
9
+ before_action { ability.authorize! :read, ::Decidim::Component }
10
+
11
+ def index
12
+ query = collection.reorder(nil).order(id: :asc).ransack(params[:filter]).result
13
+ scoped = in_visible_spaces(query)
14
+ records, meta = paginate_collection(scoped)
15
+ data = records.map do |component|
16
+ Decidim::Api::RestFull::Proposals::ProposalComponentSerializer.new(
17
+ component,
18
+ params: {
19
+ only: [],
20
+ locales: available_locales,
21
+ host: current_organization.host,
22
+ act_as:,
23
+ client_id:
24
+ }
25
+ ).serializable_hash[:data]
26
+ end
27
+ render_json_with_conditional_get({ data:, meta: }, fingerprint: collection_fingerprint_for(scoped))
28
+ end
29
+
30
+ def show
31
+ resource_id = params.require(:id).to_i
32
+ match = collection.find(resource_id)
33
+ payload = Decidim::Api::RestFull::Proposals::ProposalComponentSerializer.new(
34
+ match,
35
+ params: {
36
+ only: [],
37
+ locales: available_locales,
38
+ host: current_organization.host,
39
+ act_as:,
40
+ client_id:
41
+ }
42
+ ).serializable_hash
43
+ render_json_with_conditional_get(payload, fingerprint: resource_fingerprint_for(match))
44
+ end
45
+
46
+ protected
47
+
48
+ def voted?
49
+ proposal_votes.exists?
50
+ end
51
+
52
+ def last_vote
53
+ proposal_votes.last
54
+ end
55
+
56
+ def proposal_votes
57
+ proposal.votes.where(decidim_author_id: current_user.id)
58
+ end
59
+
60
+ def proposal
61
+ @proposal ||= collection.find(params.require(:resource_id))
62
+ end
63
+
64
+ def proposal_component
65
+ @proposal_component ||= proposal.component
66
+ end
67
+
68
+ def order_columns
69
+ %w(rand published_at)
70
+ end
71
+
72
+ def default_order_column
73
+ "published_at"
74
+ end
75
+
76
+ def component_manifest
77
+ "proposals"
78
+ end
79
+
80
+ def model_class
81
+ Decidim::Component
82
+ end
83
+
84
+ def collection
85
+ model_class.where(manifest_name: "proposals")
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module DraftProposals
7
+ # CRUD for draft proposals (unpublished). Uses Decidim ProposalForm for validation.
8
+ # Allowed writable fields: title, body (see allowed_data_keys). Update moves
9
+ # stored strings onto the current locale, validates the payload, then overlays it.
10
+ class DraftProposalsController < Decidim::Api::RestFull::Core::ResourcesController
11
+ include Decidim::Api::RestFull::AsyncApiJobEnqueuing
12
+
13
+ before_action { doorkeeper_authorize! :proposals }
14
+ before_action { ability.authorize! :draft, ::Decidim::Proposals::Proposal }
15
+
16
+ before_action { require_user! }
17
+
18
+ def show
19
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Draft Proposal Not Found" unless draft
20
+
21
+ form = form_for(draft)
22
+ payload = Decidim::Api::RestFull::Proposals::DraftProposalSerializer.new(
23
+ draft,
24
+ params: {
25
+ only: [],
26
+ locales: [current_locale.to_sym],
27
+ host: current_organization.host,
28
+ publishable: form.valid?,
29
+ fields: allowed_data_keys
30
+ }
31
+ ).serializable_hash
32
+ render_json_with_conditional_get(payload, fingerprint: resource_fingerprint_for(draft))
33
+ end
34
+
35
+ def create
36
+ enqueue_rest_full_api_job!("draft_proposals#create")
37
+ end
38
+
39
+ def create_sync
40
+ render json: (Decidim::RestFull::SyncRunner.call do
41
+ Decidim::RestFull::Proposals::DraftProposalsOperations.new(api_execution_context, params).create!
42
+ end)
43
+ end
44
+
45
+ def publish
46
+ enqueue_rest_full_api_job!("draft_proposals#publish")
47
+ end
48
+
49
+ def publish_sync
50
+ render json: (Decidim::RestFull::SyncRunner.call do
51
+ Decidim::RestFull::Proposals::DraftProposalsOperations.new(api_execution_context, params).publish!
52
+ end)
53
+ end
54
+
55
+ def update
56
+ enqueue_rest_full_api_job!("draft_proposals#update")
57
+ end
58
+
59
+ def update_sync
60
+ render json: (Decidim::RestFull::SyncRunner.call do
61
+ Decidim::RestFull::Proposals::DraftProposalsOperations.new(api_execution_context, params).update!
62
+ end)
63
+ end
64
+
65
+ def destroy
66
+ enqueue_rest_full_api_job!("draft_proposals#destroy")
67
+ end
68
+
69
+ def destroy_sync
70
+ render json: (Decidim::RestFull::SyncRunner.call do
71
+ Decidim::RestFull::Proposals::DraftProposalsOperations.new(api_execution_context, params).destroy!
72
+ end)
73
+ end
74
+
75
+ private
76
+
77
+ def form_for(resource)
78
+ Decidim::Proposals::ProposalForm.from_model(resource).with_context(
79
+ current_organization:,
80
+ current_component: resource.component
81
+ )
82
+ end
83
+
84
+ def order_columns
85
+ ["rand"]
86
+ end
87
+
88
+ def default_order_column
89
+ "rand"
90
+ end
91
+
92
+ def component_manifest
93
+ "proposals"
94
+ end
95
+
96
+ def model_class
97
+ Decidim::Proposals::Proposal.joins(:coauthorships)
98
+ end
99
+
100
+ def collection
101
+ query = filter_for_context(model_class)
102
+ query.where("published_at is NULL AND decidim_coauthorships.decidim_author_id = ?", act_as.id)
103
+ end
104
+
105
+ def draft
106
+ @draft ||= collection.joins(:rest_full_application).where(rest_full_application: { api_client_id: client_id }).find(params.require(:id))
107
+ end
108
+
109
+ def allowed_data_keys
110
+ %w(title body)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module Proposals
7
+ class ProposalExtendedDataController < Decidim::Api::RestFull::Core::ResourcesController
8
+ include Decidim::Api::RestFull::ExtendedDataEndpoints
9
+
10
+ before_action { doorkeeper_authorize! :proposals }
11
+ before_action { ability.authorize! :read, ::Decidim::Proposals::Proposal }
12
+
13
+ private
14
+
15
+ def extended_data_resource
16
+ @extended_data_resource ||= begin
17
+ proposal = filter_for_context(Decidim::Proposals::Proposal).find_by(id: params.require(:id))
18
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Proposal not found" unless proposal
19
+
20
+ proposal
21
+ end
22
+ end
23
+
24
+ def extended_data_subject_class
25
+ ::Decidim::Proposals::Proposal
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module Proposals
7
+ class ProposalsController < Decidim::Api::RestFull::Core::ResourcesController
8
+ before_action { doorkeeper_authorize! :proposals }
9
+ before_action { ability.authorize! :read, ::Decidim::Proposals::Proposal }
10
+ class CurrentUser < ActiveSupport::CurrentAttributes
11
+ attribute :user
12
+ end
13
+ before_action { CurrentUser.user = current_user }
14
+ def index
15
+ authorize_extended_data_filter!(::Decidim::Proposals::Proposal)
16
+ query = collection.ransack(params[:filter])
17
+
18
+ results = query.result
19
+ scoped = ordered(results).includes(:component)
20
+ records, meta = paginate_collection(scoped)
21
+ payload = Decidim::Api::RestFull::Proposals::ProposalSerializer.new(
22
+ records,
23
+ params: {
24
+ only: [],
25
+ locales: available_locales,
26
+ host: current_organization.host,
27
+ client_id:,
28
+ act_as:,
29
+ includes_extended: can?(:read_extended_data, ::Decidim::Proposals::Proposal)
30
+ }
31
+ ).serializable_hash
32
+ payload[:meta] = meta
33
+ fp = Decidim::RestFull::Core::HttpCache::CollectionFingerprint.for_request(
34
+ self,
35
+ relation: index_fingerprint_relation(scoped)
36
+ )
37
+ render_json_with_conditional_get(payload, fingerprint: fp)
38
+ end
39
+
40
+ def show
41
+ @resource = find_proposal!
42
+ render_json_with_conditional_get(
43
+ serialize_proposal_show(@resource),
44
+ fingerprint: Decidim::RestFull::HttpCache::ProposalShowFingerprint.for_request(self, @resource)
45
+ )
46
+ end
47
+
48
+ protected
49
+
50
+ def order_columns
51
+ %w(rand published_at)
52
+ end
53
+
54
+ def default_order_column
55
+ "published_at"
56
+ end
57
+
58
+ def default_order_direction
59
+ "desc"
60
+ end
61
+
62
+ def component_manifest
63
+ "proposals"
64
+ end
65
+
66
+ def serialize_proposal_show(resource)
67
+ pagination = proposal_pagination_meta
68
+ Decidim::Api::RestFull::Proposals::ProposalSerializer.new(
69
+ resource,
70
+ params: show_serializer_params(pagination)
71
+ ).serializable_hash
72
+ end
73
+
74
+ def serialized_show
75
+ serialize_proposal_show(find_proposal!)
76
+ end
77
+
78
+ def show_serializer_params(pagination)
79
+ {
80
+ only: [],
81
+ locales: available_locales,
82
+ host: current_organization.host,
83
+ client_id:,
84
+ act_as:
85
+ }.merge(pagination)
86
+ end
87
+
88
+ def find_proposal!
89
+ proposal = collection.find_by("decidim_proposals_proposals.id" => resource_id)
90
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Proposal Not Found" unless proposal
91
+
92
+ proposal
93
+ end
94
+
95
+ def proposal_pagination_meta
96
+ match = proposal_pagination_match
97
+ bounds = collection_pagination_bounds(collection)
98
+ {
99
+ first: bounds[:first],
100
+ last: bounds[:last],
101
+ next: match&.next_id,
102
+ prev: match&.previous_id,
103
+ count: bounds[:count]
104
+ }
105
+ end
106
+
107
+ def proposal_pagination_match
108
+ # SoftDeletable/paranoia must not wrap the window subquery alias.
109
+ Decidim::Proposals::Proposal.unscoped
110
+ .select(proposal_pagination_select)
111
+ .from(proposal_pagination_subquery)
112
+ .find_by("decidim_proposals_proposals.id = ? ", resource_id)
113
+ end
114
+
115
+ def proposal_pagination_select
116
+ <<~SQL.squish
117
+ decidim_proposals_proposals.id,
118
+ decidim_proposals_proposals.previous_id as previous_id,
119
+ decidim_proposals_proposals.next_id as next_id
120
+ SQL
121
+ end
122
+
123
+ def proposal_pagination_subquery
124
+ "(#{proposal_windowed_query}) AS decidim_proposals_proposals"
125
+ end
126
+
127
+ def proposal_windowed_query
128
+ ordered(paginated_base_scope.ransack(params[:filter]).result).to_sql
129
+ end
130
+
131
+ def paginated_base_scope
132
+ collection.select(
133
+ "decidim_proposals_proposals.id",
134
+ "decidim_proposals_proposals.decidim_component_id",
135
+ "decidim_proposals_proposals.published_at",
136
+ "LAG(decidim_proposals_proposals.id) OVER (ORDER BY #{order_string}) AS previous_id",
137
+ "LEAD(decidim_proposals_proposals.id) OVER (ORDER BY #{order_string}) AS next_id"
138
+ )
139
+ end
140
+
141
+ def model_class
142
+ Decidim::Proposals::Proposal.joins(:coauthorships)
143
+ end
144
+
145
+ def collection
146
+ query = filter_for_context(model_class)
147
+ query = query.where(decidim_component_id: params.require(:component_id)) if params.has_key? :component_id
148
+ table = Decidim::Proposals::Proposal.table_name
149
+ Rails.logger.warn "draft filtered out as filtering votes" if act_as.nil? && vote_weight_filtered?
150
+ if act_as.nil? || vote_weight_filtered?
151
+ query.where.not("#{table}.published_at" => nil)
152
+ else
153
+ query.where.not("#{table}.published_at" => nil)
154
+ .or(query.where("#{table}.published_at IS NULL AND decidim_coauthorships.decidim_author_id = ?", act_as.id))
155
+ end
156
+ end
157
+
158
+ def vote_weight_filtered?
159
+ return false unless params.has_key?(:filter)
160
+
161
+ params[:filter].to_unsafe_h.any? { |key, _value| key.to_s.start_with?("voted_weight") }
162
+ end
163
+
164
+ # Conditional GET must not re-run ORDER BY RANDOM() or other expensive sort keys.
165
+ def index_fingerprint_relation(scoped)
166
+ scoped.except(:order, :reorder)
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module VoteProposals
7
+ class VoteProposalsController < Decidim::Api::RestFull::ApplicationController
8
+ include Decidim::Api::RestFull::AsyncApiJobEnqueuing
9
+
10
+ before_action { doorkeeper_authorize! :proposals }
11
+ before_action :authorize_vote!, only: [:create, :create_sync]
12
+ before_action :authorize_unvote!, only: [:destroy]
13
+ before_action :authorize_read!, only: [:index, :show]
14
+ before_action :require_user!, only: [:create, :create_sync, :destroy]
15
+ before_action :validate_votes_enabled!, only: [:create, :create_sync]
16
+
17
+ def index
18
+ scoped = operations.index_scope.order(id: :asc)
19
+ records, meta = paginate_collection(scoped)
20
+ payload = Decidim::Api::RestFull::Proposals::VoteProposalSerializer.new(
21
+ records,
22
+ params: serializer_params
23
+ ).serializable_hash
24
+ payload[:meta] = meta
25
+ render_json_with_conditional_get(payload, fingerprint: collection_fingerprint_for(scoped))
26
+ end
27
+
28
+ def show
29
+ vote = operations.find_vote!
30
+ payload = Decidim::Api::RestFull::Proposals::VoteProposalSerializer.new(
31
+ vote,
32
+ params: serializer_params
33
+ ).serializable_hash
34
+ render_json_with_conditional_get(payload, fingerprint: resource_fingerprint_for(vote))
35
+ end
36
+
37
+ def create
38
+ enqueue_rest_full_api_job!("vote_proposals#create")
39
+ end
40
+
41
+ def create_sync
42
+ render json: Decidim::RestFull::SyncRunner.call { operations.create! }
43
+ end
44
+
45
+ def destroy
46
+ render json: Decidim::RestFull::SyncRunner.call { operations.destroy! }
47
+ end
48
+
49
+ private
50
+
51
+ def operations
52
+ Decidim::RestFull::Proposals::VoteProposalsOperations.new(api_execution_context, params)
53
+ end
54
+
55
+ def require_user!
56
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "User required" unless current_user
57
+ end
58
+
59
+ def validate_votes_enabled!
60
+ proposal_id = params[:proposal_id] || params.dig(:data, :proposal_id)
61
+ proposal = Decidim::Proposals::Proposal.published.find_by(id: proposal_id)
62
+ raise Decidim::RestFull::Core::ApiException::NotFound, "Proposal not found" unless proposal
63
+
64
+ raise Decidim::RestFull::Core::ApiException::BadRequest, "Vote are disabled" unless proposal.component.current_settings[:votes_enabled]
65
+ end
66
+
67
+ def authorize_read!
68
+ ability.authorize! :read, ::Decidim::Proposals::Proposal
69
+ end
70
+
71
+ def authorize_vote!
72
+ ability.authorize! :vote, ::Decidim::Proposals::Proposal
73
+ end
74
+
75
+ def authorize_unvote!
76
+ ability.authorize! :unvote, ::Decidim::Proposals::Proposal
77
+ end
78
+
79
+ def serializer_params
80
+ {
81
+ locales: available_locales,
82
+ host: current_organization.host,
83
+ act_as: current_user
84
+ }
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ class ProposalWebhookJob < ::Decidim::RestFull::ApplicationJob
7
+ def perform(event_name, proposal_id, organization_id)
8
+ organization = load_organization(organization_id)
9
+ return unless Decidim::RestFull::Core::ModuleAvailability.module_enabled?(organization, :proposals)
10
+
11
+ proposal = load_proposal(proposal_id)
12
+ permissions_for(event_name, organization).each do |permission|
13
+ dispatch_for_permission(permission, event_name, proposal, organization)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def load_proposal(proposal_id)
20
+ Decidim::Proposals::Proposal.find(proposal_id)
21
+ end
22
+
23
+ def load_organization(organization_id)
24
+ Decidim::Organization.find(organization_id)
25
+ end
26
+
27
+ def permissions_for(event_name, organization)
28
+ Decidim::RestFull::Core::Permission.where(permission: event_name, api_client: organization.api_clients)
29
+ end
30
+
31
+ def dispatch_for_permission(permission, event_name, proposal, organization)
32
+ serializer = ::Decidim::Api::RestFull::Proposals::ProposalWebhookSerializer.new(
33
+ proposal,
34
+ event_name:,
35
+ organization:
36
+ )
37
+ payload = build_payload(permission.api_client, serializer, organization)
38
+ return log_invalid_event(event_name, payload) unless payload.valid?
39
+
40
+ webhook_registrations_for(permission.api_client, event_name).each do |registration|
41
+ enqueue_webhook(registration, payload)
42
+ end
43
+ end
44
+
45
+ def build_payload(api_client, serializer, organization)
46
+ Decidim::RestFull::Core::WebhookEventForm.new(
47
+ **serializer.event_attributes
48
+ ).with_context(organization:, api_client:)
49
+ end
50
+
51
+ def log_invalid_event(event_name, payload)
52
+ Rails.logger.warn("Invalid event name: #{event_name}. #{payload.errors.full_messages.join(", ")}")
53
+ end
54
+
55
+ def webhook_registrations_for(api_client, event_name)
56
+ Decidim::RestFull::Core::WebhookRegistration.where(api_client_id: api_client.id).where(
57
+ "subscriptions @> ?", [event_name].to_json
58
+ )
59
+ end
60
+
61
+ def enqueue_webhook(webhook_registration, payload)
62
+ ::Decidim::RestFull::Core::WebhookJob.perform_later(webhook_registration, payload.as_json, payload.timestamp.to_s)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module RestFull
5
+ module Proposals
6
+ class ProposalApplicationId < ::ApplicationRecord
7
+ self.table_name = "proposal_application_ids"
8
+
9
+ belongs_to :api_client, class_name: "Decidim::RestFull::Core::ApiClient"
10
+ belongs_to :proposal, class_name: "Decidim::Proposals::Proposal"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Api
5
+ module RestFull
6
+ module Proposals
7
+ class DraftProposalSerializer < ProposalSerializer
8
+ attribute :errors do |proposal, params|
9
+ fields = params[:fields] || []
10
+ component = proposal.component
11
+ participatory_space = component.participatory_space
12
+ organization = participatory_space.organization
13
+ form = Decidim::Proposals::ProposalForm.from_model(proposal).with_context(
14
+ current_organization: organization,
15
+ current_participatory_space: participatory_space,
16
+ current_component: component
17
+ )
18
+ form.valid?
19
+ fields.to_h do |key|
20
+ key_sym = :"#{key}"
21
+ [key_sym, form.errors.select { |err| err.attribute == key_sym }.map(&:full_message)]
22
+ end
23
+ end
24
+
25
+ def self.default_meta(proposal)
26
+ scope = proposal.component.scope || proposal.participatory_space.scope
27
+ metas = {}
28
+ metas[:scope] = scope.id if scope
29
+ metas
30
+ end
31
+
32
+ meta do |proposal, params|
33
+ metas = default_meta(proposal)
34
+ metas[:publishable] = params[:publishable] if params.has_key? :publishable
35
+ metas[:client_id] = proposal.rest_full_application.api_client.client_id if proposal.rest_full_application
36
+ metas[:fields] = params[:fields] || []
37
+
38
+ metas
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end