decidim-restfull-forms 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.
- checksums.yaml +7 -0
- data/app/concerns/decidim/api/rest_full/forms/controller_helpers.rb +66 -0
- data/app/controllers/decidim/api/rest_full/forms/answer_options_controller.rb +76 -0
- data/app/controllers/decidim/api/rest_full/forms/answers_controller.rb +85 -0
- data/app/controllers/decidim/api/rest_full/forms/questionnaire_responses_controller.rb +51 -0
- data/app/controllers/decidim/api/rest_full/forms/questionnaires_controller.rb +64 -0
- data/app/controllers/decidim/api/rest_full/forms/questions_controller.rb +90 -0
- data/app/controllers/decidim/api/rest_full/forms/submission_requests_controller.rb +39 -0
- data/app/serializers/decidim/api/rest_full/forms/answer_option_serializer.rb +17 -0
- data/app/serializers/decidim/api/rest_full/forms/answer_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/forms/application_serializer.rb +22 -0
- data/app/serializers/decidim/api/rest_full/forms/question_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/forms/questionnaire_response_serializer.rb +37 -0
- data/app/serializers/decidim/api/rest_full/forms/questionnaire_serializer.rb +75 -0
- data/app/serializers/decidim/api/rest_full/forms/submission_request_serializer.rb +37 -0
- data/app/services/decidim/rest_full/forms/answers_operations.rb +180 -0
- data/app/services/decidim/rest_full/forms/authoring_operations.rb +153 -0
- data/app/services/decidim/rest_full/forms/error_payload_builder.rb +40 -0
- data/app/services/decidim/rest_full/forms/locale_resolver.rb +86 -0
- data/app/services/decidim/rest_full/forms/questionnaire_json_forms_builder.rb +136 -0
- data/app/services/decidim/rest_full/forms/questionnaire_scope.rb +140 -0
- data/app/services/decidim/rest_full/forms/response_builder.rb +69 -0
- data/app/services/decidim/rest_full/forms/submission_bundle.rb +75 -0
- data/config/locales/decidim_rest_full_forms.en.yml +18 -0
- data/lib/decidim/rest_full/forms/decidim_032_compat.rb +62 -0
- data/lib/decidim/rest_full/forms/engine.rb +97 -0
- data/lib/decidim/rest_full/forms/test/definitions/forms_errors.rb +32 -0
- data/lib/decidim/rest_full/forms/test/definitions/locale_meta.rb +50 -0
- data/lib/decidim/rest_full/forms/test/definitions/question.rb +83 -0
- data/lib/decidim/rest_full/forms/test/definitions/question_create_request.rb +94 -0
- data/lib/decidim/rest_full/forms/test/definitions/questionnaire.rb +47 -0
- data/lib/decidim/rest_full/forms/test/definitions/questionnaire_response.rb +41 -0
- data/lib/decidim/rest_full/forms/test/definitions/request_bodies.rb +88 -0
- data/lib/decidim/rest_full/forms/test/definitions/submission_request.rb +44 -0
- data/lib/decidim/rest_full/forms/test/definitions/tags/forms_tag.rb +30 -0
- data/lib/decidim/rest_full/forms/test_definitions.rb +11 -0
- data/lib/decidim-restfull-forms.rb +14 -0
- data/spec/requests/decidim/api/rest_full/forms/answers_controller_create_spec.rb +68 -0
- data/spec/requests/decidim/api/rest_full/forms/answers_controller_sync_spec.rb +67 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaire_responses_controller_show_spec.rb +55 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_index_spec.rb +43 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_show_spec.rb +46 -0
- data/spec/requests/decidim/api/rest_full/forms/questionnaires_controller_update_spec.rb +107 -0
- data/spec/requests/decidim/api/rest_full/forms/questions_controller_spec.rb +166 -0
- metadata +141 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9d1b33139618860c0007c0240a6de93f809f6c27c289976b8442eeebba827e36
|
|
4
|
+
data.tar.gz: 74782840cdbc0f68fb06f53d7affdac9843149333e954ef231481f346c203f6a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9eda74c251e2a18f676ac74afc13cdd6e9c62f11538d5adff0ab3322fc3c1939e288ffdfa1605100aef4e14998de28fb93d2c75baab8cf74e06ea6205b940203
|
|
7
|
+
data.tar.gz: 54668c3e490fea72fc4fd3bf8074b324f8299a01a18fe435f66f29be2991c5b27ec84cc3f3e804394c7e1c3c0b4778d760ad4a9602092be75b8755587eaa24da
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
module ControllerHelpers
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def questionnaire_scope
|
|
13
|
+
@questionnaire_scope ||= Decidim::RestFull::Forms::QuestionnaireScope.new(
|
|
14
|
+
organization: current_organization,
|
|
15
|
+
visibility: participatory_space_visibility
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def locale_resolver
|
|
20
|
+
@locale_resolver ||= Decidim::RestFull::Forms::LocaleResolver.new(
|
|
21
|
+
organization: current_organization,
|
|
22
|
+
user: current_user,
|
|
23
|
+
params:,
|
|
24
|
+
accept_language: request.headers["Accept-Language"]
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def locale_meta
|
|
29
|
+
locale_resolver.meta_hash
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def api_context
|
|
33
|
+
visibility = participatory_space_visibility
|
|
34
|
+
headers = request.headers
|
|
35
|
+
request_params = params
|
|
36
|
+
Decidim::RestFull::ApiExecutionContext.from_controller(self).tap do |ctx|
|
|
37
|
+
ctx.define_singleton_method(:visibility) { visibility }
|
|
38
|
+
ctx.define_singleton_method(:request_headers) { headers }
|
|
39
|
+
ctx.define_singleton_method(:params) { request_params }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def filter_hash
|
|
44
|
+
fp = params[:filter]
|
|
45
|
+
fp.respond_to?(:to_unsafe_h) ? fp.to_unsafe_h.stringify_keys : {}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def questionnaire_show_json(questionnaire)
|
|
49
|
+
projection = Decidim::RestFull::Forms::QuestionnaireJsonFormsBuilder.new(
|
|
50
|
+
questionnaire,
|
|
51
|
+
locale: locale_meta[:locale],
|
|
52
|
+
organization: current_organization,
|
|
53
|
+
host: current_organization.host
|
|
54
|
+
).build
|
|
55
|
+
Decidim::RestFull::Forms::ResponseBuilder.questionnaire_show(
|
|
56
|
+
questionnaire,
|
|
57
|
+
projection:,
|
|
58
|
+
locale_meta:,
|
|
59
|
+
host: current_organization.host
|
|
60
|
+
)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class AnswerOptionsController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
10
|
+
|
|
11
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
12
|
+
before_action { ability.authorize! :manage, ::Decidim::Forms::Question }
|
|
13
|
+
|
|
14
|
+
def index
|
|
15
|
+
question = find_question!
|
|
16
|
+
scoped = question.answer_options.order(:id)
|
|
17
|
+
records, page_meta = paginate_collection(scoped)
|
|
18
|
+
payload = Decidim::Api::RestFull::Forms::AnswerOptionSerializer.new(
|
|
19
|
+
records
|
|
20
|
+
).serializable_hash
|
|
21
|
+
payload[:meta] = page_meta.merge(locale_meta)
|
|
22
|
+
render_json_with_conditional_get(
|
|
23
|
+
payload,
|
|
24
|
+
fingerprint: collection_fingerprint_for(scoped, extra: question.id)
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create
|
|
29
|
+
enqueue_rest_full_api_job!("forms/answer_options#create")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def create_sync
|
|
33
|
+
option = Decidim::RestFull::SyncRunner.call do
|
|
34
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).create_answer_option!
|
|
35
|
+
end
|
|
36
|
+
render json: Decidim::RestFull::Forms::ResponseBuilder.answer_option(option).merge(meta: locale_meta),
|
|
37
|
+
status: :created
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def update
|
|
41
|
+
enqueue_rest_full_api_job!("forms/answer_options#update")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def update_sync
|
|
45
|
+
option = Decidim::RestFull::SyncRunner.call do
|
|
46
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).update_answer_option!
|
|
47
|
+
end
|
|
48
|
+
render json: Decidim::RestFull::Forms::ResponseBuilder.answer_option(option).merge(meta: locale_meta)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def destroy
|
|
52
|
+
enqueue_rest_full_api_job!("forms/answer_options#destroy")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def destroy_sync
|
|
56
|
+
Decidim::RestFull::SyncRunner.call do
|
|
57
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).destroy_answer_option!
|
|
58
|
+
end
|
|
59
|
+
head :no_content
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def find_question!
|
|
65
|
+
qid = filter_hash["question_id"] || params[:question_id]
|
|
66
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "filter[question_id] required" if qid.blank?
|
|
67
|
+
|
|
68
|
+
question = Decidim::Forms::Question.find(qid)
|
|
69
|
+
questionnaire_scope.find!(question.decidim_questionnaire_id)
|
|
70
|
+
question
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class AnswersController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
10
|
+
|
|
11
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
12
|
+
before_action { ability.authorize! :submit, ::Decidim::Forms::Questionnaire }
|
|
13
|
+
|
|
14
|
+
def index
|
|
15
|
+
ability.authorize! :read, ::Decidim::Forms::Answer
|
|
16
|
+
relation = answers_corpus
|
|
17
|
+
relation = apply_answer_filters(relation)
|
|
18
|
+
scoped = relation.order(created_at: :desc, id: :desc)
|
|
19
|
+
records, page_meta = paginate_collection(scoped)
|
|
20
|
+
payload = Decidim::RestFull::Forms::ResponseBuilder.answer_index(records, locale_meta:)
|
|
21
|
+
payload[:meta] = page_meta.merge(locale_meta)
|
|
22
|
+
render_json_with_conditional_get(payload, fingerprint: collection_fingerprint_for(scoped))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create
|
|
26
|
+
enqueue_forms_answer_job!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def create_sync
|
|
30
|
+
result = Decidim::RestFull::SyncRunner.call do
|
|
31
|
+
Decidim::RestFull::Forms::AnswersOperations.new(api_context, request.params).create!
|
|
32
|
+
end
|
|
33
|
+
bundle = Decidim::RestFull::Forms::SubmissionBundle.find!(
|
|
34
|
+
result[:questionnaire_response_id],
|
|
35
|
+
organization: current_organization,
|
|
36
|
+
visibility: participatory_space_visibility
|
|
37
|
+
)
|
|
38
|
+
json = Decidim::RestFull::Forms::ResponseBuilder.questionnaire_response(
|
|
39
|
+
bundle,
|
|
40
|
+
locale_meta:,
|
|
41
|
+
host: current_organization.host
|
|
42
|
+
)
|
|
43
|
+
render json:, status: :created, location: json.dig(:data, :links, :self, :href)
|
|
44
|
+
rescue Decidim::RestFull::Forms::ValidationError => e
|
|
45
|
+
render json: e.payload, status: unprocessable_status
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def enqueue_forms_answer_job!
|
|
51
|
+
token = doorkeeper_token
|
|
52
|
+
raise Decidim::RestFull::Core::ApiException::Unauthorized, "The access token is invalid" unless token&.accessible?
|
|
53
|
+
|
|
54
|
+
job = Decidim::RestFull::ApiJob.compat_create!(
|
|
55
|
+
decidim_organization_id: current_organization.id,
|
|
56
|
+
doorkeeper_access_token_id: token.id,
|
|
57
|
+
oauth_application_id: token.application_id,
|
|
58
|
+
resource_owner_id: token.resource_owner_id,
|
|
59
|
+
command_key: "forms/answers#create",
|
|
60
|
+
status: "pending",
|
|
61
|
+
payload: rest_full_api_job_payload
|
|
62
|
+
)
|
|
63
|
+
Decidim::RestFull::ExecuteApiJobJob.perform_later(job.id)
|
|
64
|
+
|
|
65
|
+
body = Decidim::RestFull::Forms::ResponseBuilder.submission_request(job, host: current_organization.host)
|
|
66
|
+
render json: body,
|
|
67
|
+
status: :accepted,
|
|
68
|
+
location: body.dig(:data, :links, :self, :href),
|
|
69
|
+
headers: { "Retry-After" => "2" }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def answers_corpus
|
|
73
|
+
qids = questionnaire_scope.base_relation.pluck(:id)
|
|
74
|
+
Decidim::Forms::Answer.where(decidim_questionnaire_id: qids)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def apply_answer_filters(relation)
|
|
78
|
+
relation = relation.where(decidim_questionnaire_id: filter_hash["questionnaire_id"]) if filter_hash["questionnaire_id"].present?
|
|
79
|
+
relation
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionnaireResponsesController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
10
|
+
|
|
11
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
12
|
+
|
|
13
|
+
def show
|
|
14
|
+
ability.authorize! :read, ::Decidim::Forms::Answer
|
|
15
|
+
bundle = Decidim::RestFull::Forms::SubmissionBundle.find!(
|
|
16
|
+
params.require(:id),
|
|
17
|
+
organization: current_organization,
|
|
18
|
+
visibility: participatory_space_visibility
|
|
19
|
+
)
|
|
20
|
+
payload = Decidim::RestFull::Forms::ResponseBuilder.questionnaire_response(
|
|
21
|
+
bundle,
|
|
22
|
+
locale_meta:,
|
|
23
|
+
host: current_organization.host
|
|
24
|
+
)
|
|
25
|
+
anchor = Decidim::Forms::Answer.find_by(id: bundle.anchor_id)
|
|
26
|
+
render_json_with_conditional_get(
|
|
27
|
+
payload,
|
|
28
|
+
fingerprint: anchor ? resource_fingerprint_for(anchor) : nil
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def destroy
|
|
33
|
+
enqueue_rest_full_api_job!("forms/questionnaire_responses#destroy")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def destroy_sync
|
|
37
|
+
ability.authorize! :destroy, ::Decidim::Forms::Answer
|
|
38
|
+
Decidim::RestFull::SyncRunner.call do
|
|
39
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).destroy_questionnaire_response!
|
|
40
|
+
end
|
|
41
|
+
head :no_content
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def update_forbidden
|
|
45
|
+
head :method_not_allowed, allow: "GET, HEAD, DELETE"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionnairesController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
10
|
+
|
|
11
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
12
|
+
before_action { authorize_read! }
|
|
13
|
+
|
|
14
|
+
def index
|
|
15
|
+
relation = questionnaire_scope.filter(questionnaire_scope.base_relation, filter_hash)
|
|
16
|
+
scoped = relation.order(updated_at: :desc, id: :desc)
|
|
17
|
+
records, meta = paginate_collection(scoped)
|
|
18
|
+
payload = Decidim::RestFull::Forms::ResponseBuilder.questionnaire_index(
|
|
19
|
+
records,
|
|
20
|
+
locale_meta:,
|
|
21
|
+
host: current_organization.host
|
|
22
|
+
)
|
|
23
|
+
payload[:meta] = meta
|
|
24
|
+
render_json_with_conditional_get(payload, fingerprint: collection_fingerprint_for(scoped))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def show
|
|
28
|
+
questionnaire = questionnaire_scope.find!(params.require(:id))
|
|
29
|
+
render_json_with_conditional_get(
|
|
30
|
+
questionnaire_show_json(questionnaire),
|
|
31
|
+
fingerprint: resource_fingerprint_for(questionnaire)
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def update
|
|
36
|
+
enqueue_rest_full_api_job!("forms/questionnaires#update")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def update_sync
|
|
40
|
+
questionnaire = Decidim::RestFull::SyncRunner.call do
|
|
41
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).update_questionnaire!
|
|
42
|
+
end
|
|
43
|
+
render_json_with_conditional_get(
|
|
44
|
+
questionnaire_show_json(questionnaire),
|
|
45
|
+
fingerprint: resource_fingerprint_for(questionnaire)
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def authorize_read!
|
|
52
|
+
return ability.authorize! :read, ::Decidim::Forms::Questionnaire if ability.can?(:read, ::Decidim::Forms::Questionnaire)
|
|
53
|
+
|
|
54
|
+
ability.authorize! :read, ::Decidim::Surveys::Survey
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def authorize_manage!
|
|
58
|
+
ability.authorize! :manage, ::Decidim::Forms::Question
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionsController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
10
|
+
|
|
11
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
12
|
+
before_action { ability.authorize! :manage, ::Decidim::Forms::Question }
|
|
13
|
+
|
|
14
|
+
def index
|
|
15
|
+
questionnaire = questionnaire_for_filter
|
|
16
|
+
scoped = questionnaire.questions.order(:position, :id)
|
|
17
|
+
records, page_meta = paginate_collection(scoped)
|
|
18
|
+
payload = Decidim::RestFull::Forms::ResponseBuilder.questions(
|
|
19
|
+
records,
|
|
20
|
+
locale_meta:,
|
|
21
|
+
host: current_organization.host
|
|
22
|
+
)
|
|
23
|
+
payload[:meta] = page_meta.merge(locale_meta)
|
|
24
|
+
render_json_with_conditional_get(
|
|
25
|
+
payload,
|
|
26
|
+
fingerprint: collection_fingerprint_for(scoped, extra: questionnaire.id)
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def show
|
|
31
|
+
question = find_question!
|
|
32
|
+
render_json_with_conditional_get(
|
|
33
|
+
Decidim::RestFull::Forms::ResponseBuilder.question(question).merge(meta: locale_meta),
|
|
34
|
+
fingerprint: resource_fingerprint_for(question)
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def create
|
|
39
|
+
enqueue_rest_full_api_job!("forms/questions#create")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def create_sync
|
|
43
|
+
question = Decidim::RestFull::SyncRunner.call do
|
|
44
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).create_question!
|
|
45
|
+
end
|
|
46
|
+
render json: Decidim::RestFull::Forms::ResponseBuilder.question(question).merge(meta: locale_meta),
|
|
47
|
+
status: :created
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def update
|
|
51
|
+
enqueue_rest_full_api_job!("forms/questions#update")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def update_sync
|
|
55
|
+
question = Decidim::RestFull::SyncRunner.call do
|
|
56
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).update_question!
|
|
57
|
+
end
|
|
58
|
+
render json: Decidim::RestFull::Forms::ResponseBuilder.question(question).merge(meta: locale_meta)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def destroy
|
|
62
|
+
enqueue_rest_full_api_job!("forms/questions#destroy")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def destroy_sync
|
|
66
|
+
Decidim::RestFull::SyncRunner.call do
|
|
67
|
+
Decidim::RestFull::Forms::AuthoringOperations.new(api_context, request.params).destroy_question!
|
|
68
|
+
end
|
|
69
|
+
head :no_content
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def questionnaire_for_filter
|
|
75
|
+
qid = filter_hash["questionnaire_id"] || params[:questionnaire_id]
|
|
76
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "filter[questionnaire_id] required" if qid.blank?
|
|
77
|
+
|
|
78
|
+
questionnaire_scope.find!(qid)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def find_question!
|
|
82
|
+
Decidim::Forms::Question.find(params.require(:id)).tap do |q|
|
|
83
|
+
questionnaire_scope.find!(q.decidim_questionnaire_id)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class SubmissionRequestsController < Decidim::Api::RestFull::ApplicationController
|
|
8
|
+
include ControllerHelpers
|
|
9
|
+
|
|
10
|
+
before_action { doorkeeper_authorize! :surveys }
|
|
11
|
+
|
|
12
|
+
def show
|
|
13
|
+
job = Decidim::RestFull::ApiJob.find_by(
|
|
14
|
+
id: params.require(:id),
|
|
15
|
+
decidim_organization_id: current_organization.id,
|
|
16
|
+
command_key: "forms/answers#create"
|
|
17
|
+
)
|
|
18
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Submission request not found" unless job
|
|
19
|
+
|
|
20
|
+
r = job.result.is_a?(Hash) ? job.result.stringify_keys : {}
|
|
21
|
+
rv = r["return_value"] || r["data"] || {}
|
|
22
|
+
rv = rv.stringify_keys if rv.is_a?(Hash)
|
|
23
|
+
result_id = rv["questionnaire_response_id"]
|
|
24
|
+
payload = Decidim::RestFull::Forms::ResponseBuilder.submission_request(
|
|
25
|
+
job,
|
|
26
|
+
host: current_organization.host,
|
|
27
|
+
result_id:
|
|
28
|
+
)
|
|
29
|
+
render_json_with_conditional_get(
|
|
30
|
+
payload,
|
|
31
|
+
fingerprint: resource_fingerprint_for(job),
|
|
32
|
+
headers: job.status == "pending" ? { "Retry-After" => "2" } : {}
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class AnswerOptionSerializer < ApplicationSerializer
|
|
8
|
+
set_type :answer_options
|
|
9
|
+
|
|
10
|
+
attribute :body
|
|
11
|
+
|
|
12
|
+
belongs_to :question, &:question
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class AnswerSerializer < ApplicationSerializer
|
|
8
|
+
set_type :answers
|
|
9
|
+
|
|
10
|
+
attributes :body
|
|
11
|
+
|
|
12
|
+
attribute :question_id do |answer|
|
|
13
|
+
answer.decidim_question_id.to_s
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
belongs_to :questionnaire, &:questionnaire
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class ApplicationSerializer < Decidim::Api::RestFull::Core::ApplicationSerializer
|
|
8
|
+
def self.translate_field(field, locale)
|
|
9
|
+
return nil unless field.is_a?(Hash)
|
|
10
|
+
|
|
11
|
+
loc = locale.to_s.tr("-", "_").to_sym
|
|
12
|
+
field[loc] || field[locale] || field.values.compact.first
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.api_prefix(host)
|
|
16
|
+
"https://#{host}/api"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionSerializer < ApplicationSerializer
|
|
8
|
+
set_type :questions
|
|
9
|
+
|
|
10
|
+
attributes :position, :mandatory, :body, :description, :max_choices, :max_characters
|
|
11
|
+
|
|
12
|
+
attribute :question_type do |question|
|
|
13
|
+
Decidim::RestFull::Forms::Decidim032Compat.api_question_type(question.question_type)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
belongs_to :questionnaire, &:questionnaire
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionnaireResponseSerializer < ApplicationSerializer
|
|
8
|
+
set_type :questionnaire_response
|
|
9
|
+
|
|
10
|
+
set_id(&:id)
|
|
11
|
+
|
|
12
|
+
attribute :answers, &:answers_map
|
|
13
|
+
|
|
14
|
+
attribute :client_ip, &:ip_hash
|
|
15
|
+
|
|
16
|
+
meta do |bundle, params|
|
|
17
|
+
locale_meta = params[:locale_meta] || {}
|
|
18
|
+
locale_meta.merge(
|
|
19
|
+
user: bundle.user ? { id: bundle.user.id.to_s, type: "user" } : nil,
|
|
20
|
+
created_at: bundle.created_at&.iso8601
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
link :self do |bundle, params|
|
|
25
|
+
{
|
|
26
|
+
href: "#{api_prefix(params[:host])}/questionnaire_responses/#{bundle.id}",
|
|
27
|
+
rel: "self",
|
|
28
|
+
meta: { action_method: "GET" }
|
|
29
|
+
}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
belongs_to :questionnaire, record_type: :questionnaires, &:questionnaire
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Forms
|
|
7
|
+
class QuestionnaireSerializer < ApplicationSerializer
|
|
8
|
+
set_type :questionnaires
|
|
9
|
+
|
|
10
|
+
attribute :title do |questionnaire, params|
|
|
11
|
+
translate_field(questionnaire.title, params[:locale])
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
attribute :description do |questionnaire, params|
|
|
15
|
+
translate_field(questionnaire.description, params[:locale])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
attribute :schema, if: proc { |_, params| params[:projection].present? } do |_, params|
|
|
19
|
+
params[:projection][:schema]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
attribute :ui, if: proc { |_, params| params[:projection].present? } do |_, params|
|
|
23
|
+
params[:projection][:ui]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
attribute :updated_at do |questionnaire|
|
|
27
|
+
questionnaire.updated_at&.iso8601
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
meta do |_questionnaire, params|
|
|
31
|
+
base = params[:locale_meta] || {}
|
|
32
|
+
if params[:projection].present?
|
|
33
|
+
base.merge(submission: params[:projection][:meta])
|
|
34
|
+
else
|
|
35
|
+
base
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
link :self do |questionnaire, params|
|
|
40
|
+
prefix = api_prefix(params[:host])
|
|
41
|
+
{
|
|
42
|
+
href: "#{prefix}/questionnaires/#{questionnaire.id}",
|
|
43
|
+
rel: "self",
|
|
44
|
+
meta: { action_method: "GET" }
|
|
45
|
+
}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
link :submit do |questionnaire, params|
|
|
49
|
+
{
|
|
50
|
+
href: "#{api_prefix(params[:host])}/answers",
|
|
51
|
+
rel: "submit",
|
|
52
|
+
meta: { action_method: "POST", questionnaire_id: questionnaire.id.to_s }
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
link :submit_sync do |_, params|
|
|
57
|
+
{
|
|
58
|
+
href: "#{api_prefix(params[:host])}/answers/sync",
|
|
59
|
+
rel: "submit_sync",
|
|
60
|
+
meta: { action_method: "POST" }
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
link :questions do |questionnaire, params|
|
|
65
|
+
{
|
|
66
|
+
href: "#{api_prefix(params[:host])}/questions?filter[questionnaire_id]=#{questionnaire.id}",
|
|
67
|
+
rel: "questions",
|
|
68
|
+
meta: { action_method: "GET" }
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|