decidim-chatbot 0.0.1
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/.erb_lint.yml +2134 -0
- data/.github/workflows/website.yml +57 -0
- data/.gitignore +13 -0
- data/.gitlab-ci.yml +204 -0
- data/.node-version +1 -0
- data/.nvmrc +1 -0
- data/.rspec +2 -0
- data/.rubocop.yml +17 -0
- data/.ruby-version +1 -0
- data/.simplecov +36 -0
- data/CONTRIBUTING.md +54 -0
- data/Gemfile +50 -0
- data/Gemfile.lock +1013 -0
- data/LICENSE.md +661 -0
- data/README.md +62 -0
- data/Rakefile +54 -0
- data/app/commands/decidim/chatbot/admin/destroy_item.rb +25 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_exit.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_login.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_propose_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_blogs.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_read_proposals.rb +12 -0
- data/app/commands/decidim/chatbot/admin/menu_items/update_vote_on_proposal.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_manual.rb +12 -0
- data/app/commands/decidim/chatbot/admin/notifications/update_phase.rb +12 -0
- data/app/commands/decidim/chatbot/admin/persist_item.rb +47 -0
- data/app/commands/decidim/chatbot/admin/reorder_items.rb +51 -0
- data/app/commands/decidim/chatbot/admin/send_test_notification.rb +74 -0
- data/app/commands/decidim/chatbot/admin/unsubscribe_user.rb +35 -0
- data/app/commands/decidim/chatbot/admin/update_chatbot.rb +17 -0
- data/app/commands/decidim/chatbot/notifications/deliver.rb +30 -0
- data/app/commands/decidim/chatbot/notifications/enqueue_phase.rb +67 -0
- data/app/commands/decidim/chatbot/notifications/send.rb +115 -0
- data/app/commands/decidim/chatbot/whatsapp/create_from_phone.rb +95 -0
- data/app/commands/decidim/chatbot/whatsapp/sync_phone.rb +56 -0
- data/app/controllers/decidim/api/rest_full/chatbot/base_controller.rb +52 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_menu_items_controller.rb +49 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbot_users_controller.rb +46 -0
- data/app/controllers/decidim/api/rest_full/chatbot/chatbots_controller.rb +40 -0
- data/app/controllers/decidim/chatbot/admin/chatbot_edit.rb +85 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/generals_controller.rb +43 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/menu_items_controller.rb +32 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/notifications_controller.rb +92 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users/filterable.rb +33 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/users_controller.rb +53 -0
- data/app/controllers/decidim/chatbot/admin/chatbots/whatsapps_controller.rb +59 -0
- data/app/controllers/decidim/chatbot/admin/chatbots_controller.rb +20 -0
- data/app/controllers/decidim/chatbot/admin/items_crud.rb +183 -0
- data/app/forms/decidim/chatbot/admin/chatbot_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/config_form.rb +20 -0
- data/app/forms/decidim/chatbot/admin/menu_item_form.rb +80 -0
- data/app/forms/decidim/chatbot/admin/menu_items/component_id_form.rb +60 -0
- data/app/forms/decidim/chatbot/admin/menu_items/exit_form.rb +24 -0
- data/app/forms/decidim/chatbot/admin/menu_items/login_form.rb +35 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/propose_proposals_labels.rb +53 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_blogs_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_content_labels.rb +51 -0
- data/app/forms/decidim/chatbot/admin/menu_items/read_proposals_form.rb +23 -0
- data/app/forms/decidim/chatbot/admin/menu_items/vote_on_proposal_form.rb +123 -0
- data/app/forms/decidim/chatbot/admin/notification_item_form.rb +111 -0
- data/app/forms/decidim/chatbot/admin/notifications/manual_form.rb +13 -0
- data/app/forms/decidim/chatbot/admin/notifications/phase_form.rb +79 -0
- data/app/forms/decidim/chatbot/admin/notifications/template_fields.rb +60 -0
- data/app/forms/decidim/chatbot/admin/notifications/test_form.rb +179 -0
- data/app/helpers/decidim/chatbot/admin/items_helper.rb +44 -0
- data/app/jobs/decidim/chatbot/chatbot_webhook_job.rb +59 -0
- data/app/jobs/decidim/chatbot/send_notification_job.rb +33 -0
- data/app/models/decidim/chatbot/chatbot.rb +47 -0
- data/app/models/decidim/chatbot/item.rb +99 -0
- data/app/packs/.keep +0 -0
- data/app/presenters/decidim/chatbot/admin_log/chatbot_presenter.rb +42 -0
- data/app/presenters/decidim/chatbot/admin_log/item_presenter.rb +32 -0
- data/app/queries/decidim/chatbot/admin/proposal_vote_mode.rb +41 -0
- data/app/queries/decidim/chatbot/whatsapp_users.rb +58 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer.rb +113 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_notification_serializer.rb +19 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_serializer.rb +66 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_identity_serializer.rb +23 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer.rb +28 -0
- data/app/serializers/decidim/api/rest_full/chatbot/chatbot_webhook_serializer.rb +21 -0
- data/app/services/decidim/chatbot/chatbot_webhook_handler.rb +40 -0
- data/app/services/decidim/chatbot/notifications/bindings.rb +87 -0
- data/app/services/decidim/chatbot/whatsapp/body_parameters.rb +29 -0
- data/app/services/decidim/chatbot/whatsapp/graph_client.rb +136 -0
- data/app/services/decidim/chatbot/whatsapp/status_mapper.rb +93 -0
- data/app/services/decidim/chatbot/whatsapp/template_manager_url.rb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/_menu_preview.html.erb +49 -0
- data/app/views/decidim/chatbot/admin/chatbots/generals/edit.html.erb +79 -0
- data/app/views/decidim/chatbot/admin/chatbots/index.html.erb +50 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/_form.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/edit.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/index.html.erb +43 -0
- data/app/views/decidim/chatbot/admin/chatbots/menu_items/new.html.erb +12 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_form.html.erb +4 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_manual_fields.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/_phase_fields.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/edit.html.erb +3 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/index.html.erb +40 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/new.html.erb +11 -0
- data/app/views/decidim/chatbot/admin/chatbots/notifications/test.html.erb +35 -0
- data/app/views/decidim/chatbot/admin/chatbots/tabs/_whatsapp.html.erb +47 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/_filters.html.erb +23 -0
- data/app/views/decidim/chatbot/admin/chatbots/users/index.html.erb +56 -0
- data/app/views/decidim/chatbot/admin/chatbots/whatsapps/show.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/_item_form.html.erb +64 -0
- data/app/views/decidim/chatbot/admin/shared/_row_actions.html.erb +28 -0
- data/app/views/decidim/chatbot/admin/shared/_tabs_menu.html.erb +13 -0
- data/app/views/decidim/chatbot/admin/shared/_type_fields.html.erb +2 -0
- data/app/views/decidim/chatbot/admin/shared/_type_picker.html.erb +15 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_component_id.html.erb +9 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_exit.html.erb +5 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_login.html.erb +8 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_manual.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_phase.html.erb +46 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_propose_proposals.html.erb +10 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_blogs.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_read_proposals.html.erb +16 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/_vote_on_proposal.html.erb +26 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_abstention_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_vote_label.html.erb +7 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_votes_closed_warning.html.erb +6 -0
- data/app/views/decidim/chatbot/admin/shared/type_fields/vote/_shows_weight_labels.html.erb +15 -0
- data/bin/check +13 -0
- data/bin/ci-setup +38 -0
- data/bin/gen-node-client +112 -0
- data/bin/gen-openapi +248 -0
- data/bin/i18n-tasks +27 -0
- data/bin/postversion +17 -0
- data/config/i18n-tasks.yml +38 -0
- data/config/locales/decidim_chatbot.en.yml +323 -0
- data/config/locales/decidim_chatbot.fr.yml +29 -0
- data/contrib/decidim-node-client/.gitignore +4 -0
- data/contrib/decidim-node-client/.npmignore +1 -0
- data/contrib/decidim-node-client/.openapi-generator/FILES +8 -0
- data/contrib/decidim-node-client/.openapi-generator/VERSION +1 -0
- data/contrib/decidim-node-client/.openapi-generator-ignore +13 -0
- data/contrib/decidim-node-client/.yarnrc.yml +5 -0
- data/contrib/decidim-node-client/README.md +37 -0
- data/contrib/decidim-node-client/api.ts +14193 -0
- data/contrib/decidim-node-client/base.ts +70 -0
- data/contrib/decidim-node-client/common.ts +185 -0
- data/contrib/decidim-node-client/configuration.ts +138 -0
- data/contrib/decidim-node-client/git_push.sh +57 -0
- data/contrib/decidim-node-client/index.ts +16 -0
- data/contrib/decidim-node-client/package.json +49 -0
- data/contrib/decidim-node-client/packup.config.ts +7 -0
- data/contrib/decidim-node-client/tsconfig.json +25 -0
- data/contrib/decidim-node-client/yarn.lock +2367 -0
- data/crowdin.yml +15 -0
- data/db/migrate/20260725120000_create_decidim_chatbot_chatbots.rb +20 -0
- data/db/migrate/20260822120000_create_decidim_chatbot_items.rb +20 -0
- data/db/migrate/20260824120000_add_menu_and_error_copy_to_decidim_chatbot_chatbots.rb +13 -0
- data/db/migrate/20260824140000_add_enabled_to_decidim_chatbot_items.rb +7 -0
- data/decidim-chatbot.gemspec +40 -0
- data/docker-compose.ci.yml +59 -0
- data/docker-compose.yml +47 -0
- data/lib/decidim/chatbot/admin_engine.rb +67 -0
- data/lib/decidim/chatbot/admin_menu.rb +41 -0
- data/lib/decidim/chatbot/default_manifests.rb +91 -0
- data/lib/decidim/chatbot/engine.rb +107 -0
- data/lib/decidim/chatbot/manifest.rb +31 -0
- data/lib/decidim/chatbot/menu_item_label_keys.rb +26 -0
- data/lib/decidim/chatbot/registry.rb +42 -0
- data/lib/decidim/chatbot/rest_full_ability.rb +24 -0
- data/lib/decidim/chatbot/settings_tab.rb +18 -0
- data/lib/decidim/chatbot/test_definitions.rb +352 -0
- data/lib/decidim/chatbot/variable_interpolator.rb +30 -0
- data/lib/decidim/chatbot/version.rb +21 -0
- data/lib/decidim/chatbot/webhook_publisher.rb +29 -0
- data/lib/decidim/chatbot.rb +41 -0
- data/lib/decidim-chatbot.rb +3 -0
- data/lib/tasks/chatbot.rake +64 -0
- data/openapi/openapi.json +37801 -0
- data/openapitools.json +7 -0
- data/package.json +22 -0
- data/prettier.config.js +15 -0
- data/spec/commands/decidim/chatbot/admin/destroy_item_spec.rb +24 -0
- data/spec/commands/decidim/chatbot/admin/persist_item_spec.rb +80 -0
- data/spec/commands/decidim/chatbot/admin/reorder_items_spec.rb +53 -0
- data/spec/commands/decidim/chatbot/admin/send_test_notification_spec.rb +105 -0
- data/spec/commands/decidim/chatbot/admin/unsubscribe_user_spec.rb +46 -0
- data/spec/commands/decidim/chatbot/admin/update_chatbot_spec.rb +56 -0
- data/spec/commands/decidim/chatbot/notifications/enqueue_phase_spec.rb +59 -0
- data/spec/commands/decidim/chatbot/notifications/send_spec.rb +101 -0
- data/spec/commands/decidim/chatbot/whatsapp/create_from_phone_spec.rb +122 -0
- data/spec/commands/decidim/chatbot/whatsapp/sync_phone_spec.rb +110 -0
- data/spec/controllers/decidim/chatbot/admin/chatbots_controller_spec.rb +25 -0
- data/spec/factories/chatbot.rb +44 -0
- data/spec/forms/decidim/chatbot/admin/chatbot_form_spec.rb +41 -0
- data/spec/forms/decidim/chatbot/admin/config_form_spec.rb +30 -0
- data/spec/forms/decidim/chatbot/admin/menu_item_form_spec.rb +49 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_form_spec.rb +54 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/component_id_forms_spec.rb +512 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/exit_form_spec.rb +39 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/login_form_spec.rb +101 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/propose_proposals_form_prefill_spec.rb +26 -0
- data/spec/forms/decidim/chatbot/admin/menu_items/read_blogs_form_prefill_spec.rb +22 -0
- data/spec/forms/decidim/chatbot/admin/notifications/manual_form_spec.rb +130 -0
- data/spec/forms/decidim/chatbot/admin/notifications/phase_form_spec.rb +79 -0
- data/spec/forms/decidim/chatbot/admin/notifications/test_form_spec.rb +172 -0
- data/spec/jobs/decidim/chatbot/chatbot_webhook_job_spec.rb +47 -0
- data/spec/jobs/decidim/chatbot/send_notification_job_spec.rb +53 -0
- data/spec/lib/decidim/chatbot/engine_spec.rb +44 -0
- data/spec/lib/decidim/chatbot/registry_spec.rb +30 -0
- data/spec/lib/decidim/chatbot/settings_tab_spec.rb +21 -0
- data/spec/lib/decidim/chatbot/variable_interpolator_spec.rb +34 -0
- data/spec/lib/decidim/chatbot/whatsapp/status_mapper_spec.rb +64 -0
- data/spec/lib/decidim/chatbot_spec.rb +59 -0
- data/spec/models/decidim/chatbot/chatbot_spec.rb +23 -0
- data/spec/models/decidim/chatbot/item_spec.rb +88 -0
- data/spec/presenters/decidim/chatbot/admin_log/chatbot_presenter_spec.rb +64 -0
- data/spec/queries/decidim/chatbot/admin/proposal_vote_mode_spec.rb +74 -0
- data/spec/queries/decidim/chatbot/whatsapp_users_spec.rb +57 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_menu_items_controller_spec.rb +344 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbot_users_controller_spec.rb +140 -0
- data/spec/requests/decidim/api/rest_full/chatbot/chatbots_controller_spec.rb +195 -0
- data/spec/requests/decidim/chatbot/admin/chatbots/notifications_controller_spec.rb +44 -0
- data/spec/requests/decidim/chatbot/admin/tenant_isolation_spec.rb +133 -0
- data/spec/requests/decidim/chatbot/admin/unsubscribe_user_spec.rb +28 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_menu_item_serializer_spec.rb +321 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_serializer_spec.rb +49 -0
- data/spec/serializers/decidim/api/rest_full/chatbot/chatbot_user_serializer_spec.rb +40 -0
- data/spec/services/decidim/chatbot/chatbot_webhook_handler_spec.rb +43 -0
- data/spec/services/decidim/chatbot/notifications/bindings_spec.rb +86 -0
- data/spec/services/decidim/chatbot/whatsapp/body_parameters_spec.rb +22 -0
- data/spec/services/decidim/chatbot/whatsapp/graph_client_spec.rb +205 -0
- data/spec/services/decidim/chatbot/whatsapp/template_manager_url_spec.rb +33 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/support/allow_all_hosts.rb +9 -0
- data/spec/support/chatbot_admin.rb +16 -0
- data/spec/support/decidim_factories.rb +7 -0
- data/spec/support/decidim_system_factories.rb +3 -0
- data/spec/support/devise.rb +5 -0
- data/spec/support/rack_test_system_driver.rb +43 -0
- data/spec/support/whatsapp_graph.rb +15 -0
- data/spec/swagger_helper.rb +47 -0
- data/spec/system/decidim/chatbot/admin/admin_can_send_manual_when_ready_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_configures_vote_on_proposal_menu_item_spec.rb +84 -0
- data/spec/system/decidim/chatbot/admin/admin_confirms_send_now_recipient_count_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_menu_item_with_demo_field_spec.rb +65 -0
- data/spec/system/decidim/chatbot/admin/admin_creates_phase_and_manual_notifications_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_org_processes_in_phase_select_spec.rb +27 -0
- data/spec/system/decidim/chatbot/admin/admin_does_not_see_other_organization_chatbots_spec.rb +25 -0
- data/spec/system/decidim/chatbot/admin/admin_is_redirected_when_chatbot_is_disabled_spec.rb +28 -0
- data/spec/system/decidim/chatbot/admin/admin_lists_organization_chatbots_spec.rb +36 -0
- data/spec/system/decidim/chatbot/admin/admin_manual_notification_sends_with_nickname_spec.rb +68 -0
- data/spec/system/decidim/chatbot/admin/admin_navigates_chatbot_main_tabs_spec.rb +52 -0
- data/spec/system/decidim/chatbot/admin/admin_phase_notification_sends_on_step_change_spec.rb +79 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_blog_component_spec.rb +205 -0
- data/spec/system/decidim/chatbot/admin/admin_picks_published_participatory_space_spec.rb +45 -0
- data/spec/system/decidim/chatbot/admin/admin_reorders_chatbot_menu_items_spec.rb +29 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_menu_item_faded_on_the_table_spec.rb +23 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_disabled_notification_item_faded_on_the_table_spec.rb +40 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_notification_facts_and_frozen_template_name_spec.rb +71 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_propose_proposals_defaults_prefilled_spec.rb +119 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_type_specific_fields_spec.rb +89 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_facts_and_can_sync_spec.rb +48 -0
- data/spec/system/decidim/chatbot/admin/admin_sees_whatsapp_template_edit_link_spec.rb +37 -0
- data/spec/system/decidim/chatbot/admin/admin_tests_notification_spec.rb +125 -0
- data/spec/system/decidim/chatbot/admin/admin_unchecks_enabled_on_a_menu_item_spec.rb +30 -0
- data/spec/system/decidim/chatbot/admin/admin_unsubscribes_whatsapp_user_from_chatbot_spec.rb +35 -0
- data/spec/system/decidim/chatbot/admin/admin_updates_welcome_message_from_general_tab_spec.rb +84 -0
- data/spec/system/decidim/chatbot/system/admin_updates_organization_chatbot_settings_spec.rb +33 -0
- data/spec/tasks/chatbot_create_spec.rb +99 -0
- data/spec/tasks/chatbot_sync_spec.rb +90 -0
- data/website/.node-version +1 -0
- data/website/.nvmrc +1 -0
- data/website/docs/developer/_category_.json +4 -0
- data/website/docs/developer/contribute.md +87 -0
- data/website/docs/index.md +42 -0
- data/website/docs/integrate/_category_.json +4 -0
- data/website/docs/integrate/index.md +214 -0
- data/website/docusaurus.config.ts +93 -0
- data/website/package.json +37 -0
- data/website/sidebars.ts +7 -0
- data/website/src/css/custom.css +37 -0
- data/website/static/api-docs.html +2218 -0
- data/website/static/img/logo.svg +1 -0
- data/website/static/openapi.json +37801 -0
- data/website/tsconfig.json +6 -0
- data/website/yarn.lock +9782 -0
- data/yarn.lock +1003 -0
- metadata +424 -0
data/openapitools.json
ADDED
data/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "decidim-chatbot",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Decidim chatbot module",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@decidim/prettier-config": "0.32",
|
|
8
|
+
"@openapitools/openapi-generator-cli": "^2.15.3",
|
|
9
|
+
"prettier": "^2.3.2"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"format": "prettier --write \"./app/packs/**/*.{js,scss,css}\" --no-error-on-unmatched-pattern",
|
|
13
|
+
"format:check": "prettier --check \"./app/packs/**/*.{js,scss,css}\" --no-error-on-unmatched-pattern",
|
|
14
|
+
"gen:openapi-spec": "bin/gen-openapi",
|
|
15
|
+
"gen:openapi-spec:check": "bin/gen-openapi --check",
|
|
16
|
+
"gen:node-client": "bin/gen-node-client --validate --input openapi/openapi.json",
|
|
17
|
+
"docs:compile_re_doc": "yarn openapi-generator-cli validate -i website/static/openapi.json && docker run --rm -v \"${PWD}:/workspace\" -w /workspace node:22-bookworm-slim bash -lc 'npm install -g @redocly/cli@1.25.11 >/dev/null && redocly build-docs website/static/openapi.json -o website/static/api-docs.html'",
|
|
18
|
+
"docs:build": "yarn docs:compile_re_doc && cd website && yarn build"
|
|
19
|
+
},
|
|
20
|
+
"license": "AGPL-3.0-or-later",
|
|
21
|
+
"packageManager": "yarn@1.22.22"
|
|
22
|
+
}
|
data/prettier.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const decidimPrettier = require("@decidim/prettier-config");
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
...decidimPrettier,
|
|
5
|
+
overrides: [
|
|
6
|
+
...(decidimPrettier.overrides || []),
|
|
7
|
+
{
|
|
8
|
+
files: "*.scss",
|
|
9
|
+
options: {
|
|
10
|
+
parser: "scss",
|
|
11
|
+
singleQuote: false
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Decidim::Chatbot::Admin::DestroyItem do
|
|
6
|
+
let(:item) { create(:chatbot_item) }
|
|
7
|
+
|
|
8
|
+
it "destroys the item" do
|
|
9
|
+
expect { described_class.new(item).call }.to broadcast(:ok)
|
|
10
|
+
expect(Decidim::Chatbot::Item.find_by(id: item.id)).to be_nil
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "publishes a menu_updated notification for menu items" do
|
|
14
|
+
chatbot = item.chatbot
|
|
15
|
+
expect(Decidim::Chatbot::WebhookPublisher).to receive(:menu_updated).with(chatbot)
|
|
16
|
+
described_class.new(item).call
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "does not publish for notification-scope items" do
|
|
20
|
+
notification_item = create(:chatbot_item, :notification)
|
|
21
|
+
expect(Decidim::Chatbot::WebhookPublisher).not_to receive(:menu_updated)
|
|
22
|
+
described_class.new(notification_item).call
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Decidim::Chatbot::Admin::PersistItem do
|
|
6
|
+
subject(:command) { described_class.new(form, item) }
|
|
7
|
+
|
|
8
|
+
let(:organization) { create(:organization) }
|
|
9
|
+
let(:chatbot) { create(:chatbot, organization:, whatsapp_phone_id: "999") }
|
|
10
|
+
let(:item) { chatbot.items.new(scope: "notification", position: 1) }
|
|
11
|
+
let(:form) do
|
|
12
|
+
Decidim::Chatbot::Admin::Notifications::ManualForm.from_params(
|
|
13
|
+
{
|
|
14
|
+
manifest_name: "manual",
|
|
15
|
+
template_id_en: "1649483436030166",
|
|
16
|
+
variables_json_en: '{"name":"Hi"}'
|
|
17
|
+
}
|
|
18
|
+
).with_context(current_organization: organization)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
before { enable_chatbot!(organization) }
|
|
22
|
+
|
|
23
|
+
it "persists a notification without calling Graph" do
|
|
24
|
+
expect(Decidim::Chatbot::Whatsapp::GraphClient).not_to receive(:new)
|
|
25
|
+
expect(Decidim::Chatbot::WebhookPublisher).not_to receive(:menu_updated)
|
|
26
|
+
expect { command.call }.to broadcast(:ok)
|
|
27
|
+
expect(item).to be_persisted
|
|
28
|
+
expect(item.settings["template_id"]["en"]).to eq("1649483436030166")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "stores enabled false on a menu item" do
|
|
32
|
+
menu_item = chatbot.items.new(scope: "menu", position: 1)
|
|
33
|
+
menu_form = Decidim::Chatbot::Admin::MenuItems::LoginForm.from_params(
|
|
34
|
+
{
|
|
35
|
+
manifest_name: "login",
|
|
36
|
+
title_en: "Sign in",
|
|
37
|
+
redirect_path: "/users/sign_in",
|
|
38
|
+
response_label_en: "Open {{login_url}}",
|
|
39
|
+
enabled: "0"
|
|
40
|
+
}
|
|
41
|
+
).with_context(current_organization: organization)
|
|
42
|
+
|
|
43
|
+
described_class.new(menu_form, menu_item).call
|
|
44
|
+
expect(menu_item.reload).not_to be_enabled
|
|
45
|
+
expect(menu_item.position).to eq(1)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it "keeps the stored position when the title changes" do
|
|
49
|
+
menu_item = create(:chatbot_item, chatbot:, position: 2, title: { "en" => "Old title" })
|
|
50
|
+
menu_form = Decidim::Chatbot::Admin::MenuItems::LoginForm.from_params(
|
|
51
|
+
{
|
|
52
|
+
manifest_name: "login",
|
|
53
|
+
title_en: "New title",
|
|
54
|
+
redirect_path: "/users/sign_in",
|
|
55
|
+
response_label_en: "Open {{login_url}}",
|
|
56
|
+
enabled: "1"
|
|
57
|
+
}
|
|
58
|
+
).with_context(current_organization: organization)
|
|
59
|
+
|
|
60
|
+
described_class.new(menu_form, menu_item).call
|
|
61
|
+
expect(menu_item.reload.position).to eq(2)
|
|
62
|
+
expect(menu_item.title["en"]).to eq("New title")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "publishes a menu_updated notification for menu items" do
|
|
66
|
+
menu_item = chatbot.items.new(scope: "menu", position: 1)
|
|
67
|
+
menu_form = Decidim::Chatbot::Admin::MenuItems::LoginForm.from_params(
|
|
68
|
+
{
|
|
69
|
+
manifest_name: "login",
|
|
70
|
+
title_en: "Sign in",
|
|
71
|
+
redirect_path: "/users/sign_in",
|
|
72
|
+
response_label: "Open {{login_url}}",
|
|
73
|
+
enabled: "1"
|
|
74
|
+
}
|
|
75
|
+
).with_context(current_organization: organization)
|
|
76
|
+
|
|
77
|
+
expect(Decidim::Chatbot::WebhookPublisher).to receive(:menu_updated).with(chatbot)
|
|
78
|
+
described_class.new(menu_form, menu_item).call
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Chatbot
|
|
7
|
+
module Admin
|
|
8
|
+
describe ReorderItems do
|
|
9
|
+
subject { described_class.new(items, order) }
|
|
10
|
+
|
|
11
|
+
let(:organization) { create(:organization) }
|
|
12
|
+
let(:chatbot) { create(:chatbot, organization:) }
|
|
13
|
+
let!(:first) { create(:chatbot_item, chatbot:, position: 0, title: { "en" => "A" }) }
|
|
14
|
+
let!(:second) { create(:chatbot_item, chatbot:, position: 1, title: { "en" => "B" }) }
|
|
15
|
+
let(:items) { chatbot.items.for_scope(:menu) }
|
|
16
|
+
let(:order) { [second.id, first.id] }
|
|
17
|
+
|
|
18
|
+
it "reorders scoped items" do
|
|
19
|
+
expect { subject.call }.to broadcast(:ok)
|
|
20
|
+
expect(first.reload.position).to eq(1)
|
|
21
|
+
expect(second.reload.position).to eq(0)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "publishes a menu_updated notification" do
|
|
25
|
+
expect(Decidim::Chatbot::WebhookPublisher).to receive(:menu_updated).with(chatbot)
|
|
26
|
+
subject.call
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "when the order includes a foreign id" do
|
|
30
|
+
let(:other) { create(:chatbot_item) }
|
|
31
|
+
let(:order) { [other.id, first.id] }
|
|
32
|
+
|
|
33
|
+
it "broadcasts invalid and does not move rows" do
|
|
34
|
+
expect { subject.call }.to broadcast(:invalid)
|
|
35
|
+
expect(first.reload.position).to eq(0)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
context "when the order includes an item from another organization" do
|
|
40
|
+
let(:other_org) { create(:organization) }
|
|
41
|
+
let(:foreign) { create(:chatbot_item, chatbot: create(:chatbot, organization: other_org)) }
|
|
42
|
+
let(:order) { [foreign.id, first.id] }
|
|
43
|
+
|
|
44
|
+
it "broadcasts invalid and does not move the foreign row" do
|
|
45
|
+
expect { subject.call }.to broadcast(:invalid)
|
|
46
|
+
expect(first.reload.position).to eq(0)
|
|
47
|
+
expect(foreign.reload.position).to eq(0)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Chatbot
|
|
7
|
+
module Admin
|
|
8
|
+
describe SendTestNotification do
|
|
9
|
+
subject(:command) { described_class.new(form, client:) }
|
|
10
|
+
|
|
11
|
+
let(:organization) { create(:organization, available_locales: %w(en fr)) }
|
|
12
|
+
let(:chatbot) { create(:chatbot, organization:, whatsapp_phone_id: "phone-1") }
|
|
13
|
+
let(:item) do
|
|
14
|
+
create(
|
|
15
|
+
:chatbot_item,
|
|
16
|
+
chatbot:,
|
|
17
|
+
scope: "notification",
|
|
18
|
+
manifest_name: "manual",
|
|
19
|
+
settings: {
|
|
20
|
+
"template_id" => { "en" => "1649483436030166" },
|
|
21
|
+
"variables" => { "en" => { "city" => "Nairobi" } }
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
let(:form) do
|
|
26
|
+
Notifications::TestForm.from_params(
|
|
27
|
+
phone_number: "+15551234567",
|
|
28
|
+
values: { "city" => "Mombasa" }
|
|
29
|
+
).with_context(current_organization: organization, item:)
|
|
30
|
+
end
|
|
31
|
+
let(:client) { instance_double(Decidim::Chatbot::Whatsapp::GraphClient) }
|
|
32
|
+
|
|
33
|
+
before do
|
|
34
|
+
allow(client).to receive(:send_template).and_return("messages" => [])
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "sends the template to the entered phone only" do
|
|
38
|
+
expect { command.call }.to broadcast(:ok)
|
|
39
|
+
expect(client).to have_received(:send_template).with(
|
|
40
|
+
"phone-1",
|
|
41
|
+
to: "15551234567",
|
|
42
|
+
template_id: "1649483436030166",
|
|
43
|
+
parameters: [{ type: "text", parameter_name: "city", text: "Mombasa" }]
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "sends the selected locale template" do
|
|
48
|
+
item.update!(
|
|
49
|
+
settings: item.settings.merge("template_id" => { "en" => "1649483436030166", "fr" => "1649483436030167" })
|
|
50
|
+
)
|
|
51
|
+
form.locale = "fr"
|
|
52
|
+
expect { command.call }.to broadcast(:ok)
|
|
53
|
+
expect(client).to have_received(:send_template).with(
|
|
54
|
+
"phone-1",
|
|
55
|
+
to: "15551234567",
|
|
56
|
+
template_id: "1649483436030167",
|
|
57
|
+
parameters: [{ type: "text", parameter_name: "city", text: "Mombasa" }]
|
|
58
|
+
)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "broadcasts invalid when the form is invalid" do
|
|
62
|
+
form.phone_number = ""
|
|
63
|
+
expect { command.call }.to broadcast(:invalid)
|
|
64
|
+
expect(client).not_to have_received(:send_template)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "when Graph raises" do
|
|
68
|
+
before { allow(client).to receive(:send_template).and_raise(StandardError, "boom") }
|
|
69
|
+
|
|
70
|
+
it "broadcasts graph_error without class prefix" do
|
|
71
|
+
expect { command.call }.to broadcast(:invalid, :graph_error, "boom")
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
context "when Graph raises Meta JSON" do
|
|
76
|
+
let(:graph_body) do
|
|
77
|
+
'Graph API 400: {"error":{"message":"Template name is required","code":100,"type":"OAuthException"}}'
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
before { allow(client).to receive(:send_template).and_raise(RuntimeError, graph_body) }
|
|
81
|
+
|
|
82
|
+
it "broadcasts the Meta error message" do
|
|
83
|
+
expect { command.call }.to broadcast(:invalid, :graph_error, "Template name is required")
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context "when no client is passed and credentials are missing" do
|
|
88
|
+
subject(:command) { described_class.new(form) }
|
|
89
|
+
|
|
90
|
+
before do
|
|
91
|
+
Decidim::Toggle.save_config!(
|
|
92
|
+
organization,
|
|
93
|
+
Decidim::Chatbot::MODULE_NAME,
|
|
94
|
+
{ "enabled" => true, "access_token" => "", "business_id" => "" }
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "broadcasts missing_credentials" do
|
|
99
|
+
expect { command.call }.to broadcast(:invalid, :missing_credentials)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Chatbot
|
|
7
|
+
module Admin
|
|
8
|
+
describe UnsubscribeUser do
|
|
9
|
+
subject { described_class.new(user, chatbot) }
|
|
10
|
+
|
|
11
|
+
let(:organization) { create(:organization) }
|
|
12
|
+
let(:chatbot) { create(:chatbot, organization:) }
|
|
13
|
+
let(:user) do
|
|
14
|
+
create(
|
|
15
|
+
:user,
|
|
16
|
+
organization:,
|
|
17
|
+
extended_data: {
|
|
18
|
+
"whatsapp_phone_number" => "+1555",
|
|
19
|
+
"keep" => "yes",
|
|
20
|
+
"whatsapp_chatbot_999_subscribed" => true
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "sets only the chatbot subscription flag to false" do
|
|
26
|
+
expect { subject.call }.to broadcast(:ok)
|
|
27
|
+
user.reload
|
|
28
|
+
expect(user.extended_data["whatsapp_phone_number"]).to eq("+1555")
|
|
29
|
+
expect(user.extended_data["keep"]).to eq("yes")
|
|
30
|
+
expect(user.extended_data["whatsapp_chatbot_999_subscribed"]).to be(true)
|
|
31
|
+
expect(user.extended_data["whatsapp_chatbot_#{chatbot.id}_subscribed"]).to be(false)
|
|
32
|
+
expect(user.extended_data[chatbot.subscription_key]).to be(false)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context "when the user is on another organization" do
|
|
36
|
+
let(:user) { create(:user, extended_data: { "whatsapp_phone_number" => "+1555" }) }
|
|
37
|
+
|
|
38
|
+
it "broadcasts invalid" do
|
|
39
|
+
expect { subject.call }.to broadcast(:invalid)
|
|
40
|
+
expect(user.reload.extended_data[chatbot.subscription_key]).to be_nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
module Decidim
|
|
6
|
+
module Chatbot
|
|
7
|
+
module Admin
|
|
8
|
+
describe UpdateChatbot do
|
|
9
|
+
subject { described_class.new(form, chatbot) }
|
|
10
|
+
|
|
11
|
+
let(:organization) { create(:organization) }
|
|
12
|
+
let(:user) { create(:user, :admin, :confirmed, organization:) }
|
|
13
|
+
let(:chatbot) do
|
|
14
|
+
create(
|
|
15
|
+
:chatbot,
|
|
16
|
+
organization:,
|
|
17
|
+
name: { "en" => "Old name" },
|
|
18
|
+
welcome_message: { "en" => "Old welcome" }
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
let(:form) do
|
|
22
|
+
ChatbotForm.from_params(
|
|
23
|
+
chatbot: {
|
|
24
|
+
welcome_message_en: "New welcome",
|
|
25
|
+
menu_intro_en: "What next?",
|
|
26
|
+
menu_connector_en: "or",
|
|
27
|
+
error_404_en: "Not found",
|
|
28
|
+
error_502_en: "Upstream failed",
|
|
29
|
+
error_401_en: "Not authorized"
|
|
30
|
+
}
|
|
31
|
+
).with_context(current_organization: organization, current_user: user)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "updates copy and keeps the WhatsApp-synced name" do
|
|
35
|
+
expect { subject.call }.to broadcast(:ok).and change(Decidim::ActionLog, :count).by(1)
|
|
36
|
+
expect(chatbot.reload.name["en"]).to eq("Old name")
|
|
37
|
+
expect(chatbot.welcome_message["en"]).to eq("New welcome")
|
|
38
|
+
expect(chatbot.menu_intro["en"]).to eq("What next?")
|
|
39
|
+
expect(chatbot.menu_connector["en"]).to eq("or")
|
|
40
|
+
expect(chatbot.error_404["en"]).to eq("Not found")
|
|
41
|
+
expect(chatbot.error_502["en"]).to eq("Upstream failed")
|
|
42
|
+
expect(chatbot.error_401["en"]).to eq("Not authorized")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "publishes a settings_updated webhook" do
|
|
46
|
+
expect(Decidim::Chatbot::ChatbotWebhookJob).to receive(:perform_later).with(
|
|
47
|
+
"chatbot.settings_updated.succeeded",
|
|
48
|
+
chatbot.id,
|
|
49
|
+
organization.id
|
|
50
|
+
)
|
|
51
|
+
subject.call
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Decidim::Chatbot::Notifications::EnqueuePhase do
|
|
6
|
+
let(:organization) { create(:organization) }
|
|
7
|
+
let(:other) { create(:organization) }
|
|
8
|
+
let(:process) { create(:participatory_process, organization:) }
|
|
9
|
+
let(:step) { create(:participatory_process_step, participatory_process: process) }
|
|
10
|
+
let(:chatbot) { create(:chatbot, organization:) }
|
|
11
|
+
let!(:admin) { create(:user, :admin, organization:) }
|
|
12
|
+
let!(:ready) do
|
|
13
|
+
create(
|
|
14
|
+
:chatbot_item,
|
|
15
|
+
chatbot:,
|
|
16
|
+
scope: "notification",
|
|
17
|
+
manifest_name: "phase",
|
|
18
|
+
settings: {
|
|
19
|
+
"participatory_space_gid" => process.to_gid.to_s,
|
|
20
|
+
"variables" => { "en" => {} },
|
|
21
|
+
"template_id" => { "en" => "1649483436030166" }
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
let!(:foreign_item) do
|
|
26
|
+
create(
|
|
27
|
+
:chatbot_item,
|
|
28
|
+
chatbot: create(:chatbot, organization: other),
|
|
29
|
+
scope: "notification",
|
|
30
|
+
manifest_name: "phase",
|
|
31
|
+
settings: {
|
|
32
|
+
"participatory_space_gid" => process.to_gid.to_s,
|
|
33
|
+
"variables" => { "en" => {} },
|
|
34
|
+
"template_id" => { "en" => "1649483436030166" }
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "enqueues only matching items in the step organization" do
|
|
40
|
+
allow(Decidim::Chatbot::SendNotificationJob).to receive(:perform_later)
|
|
41
|
+
described_class.call(step)
|
|
42
|
+
expect(Decidim::Chatbot::SendNotificationJob).to have_received(:perform_later).with(ready.id, anything)
|
|
43
|
+
expect(Decidim::Chatbot::SendNotificationJob).not_to have_received(:perform_later).with(foreign_item.id, anything)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "does not enqueue a disabled matching item" do
|
|
47
|
+
ready.update!(enabled: false)
|
|
48
|
+
allow(Decidim::Chatbot::SendNotificationJob).to receive(:perform_later)
|
|
49
|
+
described_class.call(step)
|
|
50
|
+
expect(Decidim::Chatbot::SendNotificationJob).not_to have_received(:perform_later)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "does not enqueue when the template name is blank" do
|
|
54
|
+
ready.update!(settings: ready.settings.merge("template_id" => { "en" => "" }))
|
|
55
|
+
allow(Decidim::Chatbot::SendNotificationJob).to receive(:perform_later)
|
|
56
|
+
described_class.call(step)
|
|
57
|
+
expect(Decidim::Chatbot::SendNotificationJob).not_to have_received(:perform_later)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Decidim::Chatbot::Notifications::Send do
|
|
6
|
+
subject(:command) { described_class.new(item, actor, client:) }
|
|
7
|
+
|
|
8
|
+
let(:organization) { create(:organization) }
|
|
9
|
+
let(:other) { create(:organization) }
|
|
10
|
+
let(:chatbot) { create(:chatbot, organization:, whatsapp_phone_id: "phone-1") }
|
|
11
|
+
let(:actor) { create(:user, :admin, organization:) }
|
|
12
|
+
let(:client) { instance_double(Decidim::Chatbot::Whatsapp::GraphClient, send_template: { "messages" => [] }) }
|
|
13
|
+
let(:item) do
|
|
14
|
+
create(
|
|
15
|
+
:chatbot_item,
|
|
16
|
+
chatbot:,
|
|
17
|
+
scope: "notification",
|
|
18
|
+
manifest_name: "manual",
|
|
19
|
+
settings: {
|
|
20
|
+
"template_id" => { "en" => "1649483436030166" },
|
|
21
|
+
"variables" => { "en" => { "user_name" => "%{user_name}" } }
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
let!(:subscribed) do
|
|
26
|
+
create(:user, organization:, name: "Ada", extended_data: { "whatsapp_phone_number" => "+15550001" })
|
|
27
|
+
end
|
|
28
|
+
let!(:unsubscribed) do
|
|
29
|
+
create(
|
|
30
|
+
:user,
|
|
31
|
+
organization:,
|
|
32
|
+
extended_data: { "whatsapp_phone_number" => "+15550002", chatbot.subscription_key => false }
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
let!(:foreign) do
|
|
36
|
+
create(:user, organization: other, extended_data: { "whatsapp_phone_number" => "+15550003" })
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "sends only to subscribed org users and logs counts" do
|
|
40
|
+
expect { command.call }.to broadcast(:ok)
|
|
41
|
+
expect(client).to have_received(:send_template).once.with(
|
|
42
|
+
"phone-1",
|
|
43
|
+
to: "15550001",
|
|
44
|
+
template_id: "1649483436030166",
|
|
45
|
+
parameters: [{ type: "text", parameter_name: "user_name", text: "Ada" }]
|
|
46
|
+
)
|
|
47
|
+
log = Decidim::ActionLog.order(:id).last
|
|
48
|
+
expect(log.action).to eq("send")
|
|
49
|
+
expect(log.resource).to eq(item)
|
|
50
|
+
expect(log.extra["user"]).to be_present
|
|
51
|
+
expect(log.extra["user"]["name"]).to eq(actor.name)
|
|
52
|
+
expect(log.extra["recipients_count"] || log.extra.dig("extra", "recipients_count")).to eq(1)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "sends when the template name is present without APPROVED" do
|
|
56
|
+
expect { command.call }.to broadcast(:ok)
|
|
57
|
+
expect(client).to have_received(:send_template)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "skips when the item is disabled" do
|
|
61
|
+
item.update!(enabled: false)
|
|
62
|
+
expect { command.call }.to broadcast(:invalid)
|
|
63
|
+
expect(client).not_to have_received(:send_template)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "skips when the template name is blank" do
|
|
67
|
+
item.update!(settings: item.settings.merge("template_id" => { "en" => "" }))
|
|
68
|
+
expect { command.call }.to broadcast(:invalid)
|
|
69
|
+
expect(client).not_to have_received(:send_template)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "is invalid when Graph credentials are missing" do
|
|
73
|
+
expect { described_class.new(item, actor).call }.to broadcast(:invalid)
|
|
74
|
+
expect(client).not_to have_received(:send_template)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "counts Graph failures without aborting the broadcast" do
|
|
78
|
+
allow(client).to receive(:send_template).and_raise(StandardError, "boom")
|
|
79
|
+
expect { command.call }.to broadcast(:ok)
|
|
80
|
+
log = Decidim::ActionLog.order(:id).last
|
|
81
|
+
expect(log.extra["failures_count"] || log.extra.dig("extra", "failures_count")).to eq(1)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "sends the recipient locale name and variable keys to GraphClient" do
|
|
85
|
+
subscribed.update!(locale: "es")
|
|
86
|
+
item.update!(
|
|
87
|
+
settings: item.settings.merge(
|
|
88
|
+
"template_id" => { "en" => "1649483436030166", "es" => "1649483436030168" },
|
|
89
|
+
"variables" => { "es" => { "user_name" => "Hola %{user_name}" }, "en" => { "user_name" => "Hi %{user_name}" } }
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
expect { command.call }.to broadcast(:ok)
|
|
94
|
+
expect(client).to have_received(:send_template).with(
|
|
95
|
+
"phone-1",
|
|
96
|
+
to: "15550001",
|
|
97
|
+
template_id: "1649483436030168",
|
|
98
|
+
parameters: [{ type: "text", parameter_name: "user_name", text: "Hola Ada" }]
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
end
|