diff --git a/app/controllers/api/submissions_controller.rb b/app/controllers/api/submissions_controller.rb index 9251f691..9e39199b 100644 --- a/app/controllers/api/submissions_controller.rb +++ b/app/controllers/api/submissions_controller.rb @@ -132,8 +132,8 @@ module Api include: { submitters: { only: %i[id slug uuid name email phone completed_at opened_at sent_at - created_at updated_at], - methods: %i[status] }, + created_at updated_at external_id], + methods: %i[status application_key] }, template: { only: %i[id name created_at updated_at] }, created_by_user: { only: %i[id email first_name last_name] } } @@ -146,7 +146,7 @@ module Api { message: %i[subject body], submitters: [[:send_email, :send_sms, :completed_redirect_url, :uuid, :name, :email, :role, - :completed, :phone, :application_key, + :completed, :phone, :application_key, :external_id, { values: {}, readonly_fields: [], message: %i[subject body], fields: [%i[name default_value title description readonly validation_pattern invalid_message]] }]] diff --git a/app/controllers/api/submitters_controller.rb b/app/controllers/api/submitters_controller.rb index dbefd551..3853ea54 100644 --- a/app/controllers/api/submitters_controller.rb +++ b/app/controllers/api/submitters_controller.rb @@ -7,7 +7,8 @@ module Api def index submitters = Submitters.search(@submitters, params[:q]) - submitters = submitters.where(application_key: params[:application_key]) if params[:application_key].present? + submitters = submitters.where(external_id: params[:application_key]) if params[:application_key].present? + submitters = submitters.where(external_id: params[:external_id]) if params[:external_id].present? submitters = submitters.where(submission_id: params[:submission_id]) if params[:submission_id].present? submitters = paginate( @@ -69,7 +70,8 @@ module Api submitter_params = params.key?(:submitter) ? params.require(:submitter) : params submitter_params.permit( - :send_email, :send_sms, :uuid, :name, :email, :role, :completed, :phone, :application_key, + :send_email, :send_sms, :uuid, :name, :email, :role, + :completed, :phone, :application_key, :external_id, { values: {}, readonly_fields: [], message: %i[subject body], fields: [%i[name default_value readonly validation_pattern invalid_message]] } ) @@ -82,7 +84,8 @@ module Api submitter.phone = attrs[:phone].to_s.gsub(/[^0-9+]/, '') if attrs.key?(:phone) submitter.values = submitter.values.merge(attrs[:values].to_unsafe_h) if attrs[:values].present? submitter.completed_at = attrs[:completed] ? Time.current : submitter.completed_at - submitter.application_key = attrs[:application_key] if attrs.key?(:application_key) + submitter.external_id = attrs[:application_key] if attrs.key?(:application_key) + submitter.external_id = attrs[:external_id] if attrs.key?(:external_id) assign_submission_fields(submitter.submission) assign_preferences(submitter, attrs) diff --git a/app/controllers/api/templates_clone_controller.rb b/app/controllers/api/templates_clone_controller.rb index 7537274d..ebba87bb 100644 --- a/app/controllers/api/templates_clone_controller.rb +++ b/app/controllers/api/templates_clone_controller.rb @@ -7,11 +7,13 @@ module Api def create authorize!(:manage, @template) - cloned_template = Templates::Clone.call(@template, - author: current_user, - name: params[:name], - application_key: params[:application_key], - folder_name: params[:folder_name]) + cloned_template = Templates::Clone.call( + @template, + author: current_user, + name: params[:name], + external_id: params[:external_id].presence || params[:application_key], + folder_name: params[:folder_name] + ) cloned_template.source = :api cloned_template.save! diff --git a/app/controllers/api/templates_controller.rb b/app/controllers/api/templates_controller.rb index 8329fbe8..9624262f 100644 --- a/app/controllers/api/templates_controller.rb +++ b/app/controllers/api/templates_controller.rb @@ -5,11 +5,7 @@ module Api load_and_authorize_resource :template def index - templates = Templates.search(@templates, params[:q]) - - templates = params[:archived] ? templates.archived : templates.active - templates = templates.where(application_key: params[:application_key]) if params[:application_key].present? - templates = templates.joins(:folder).where(folder: { name: params[:folder] }) if params[:folder].present? + templates = filter_templates(@templates, params) templates = paginate(templates.preload(:author, documents_attachments: :blob)) @@ -49,8 +45,19 @@ module Api private + def filter_templates(templates, params) + templates = Templates.search(templates, params[:q]) + templates = params[:archived] ? templates.archived : templates.active + templates = templates.where(external_id: params[:application_key]) if params[:application_key].present? + templates = templates.where(external_id: params[:external_id]) if params[:external_id].present? + templates = templates.joins(:folder).where(folder: { name: params[:folder] }) if params[:folder].present? + + templates + end + def serialize_params { + methods: %i[application_key], include: { author: { only: %i[id email first_name last_name] }, documents: { only: %i[id uuid], methods: %i[url preview_image_url filename] } } } diff --git a/app/models/submitter.rb b/app/models/submitter.rb index 7017b0cd..64a523a2 100644 --- a/app/models/submitter.rb +++ b/app/models/submitter.rb @@ -4,23 +4,23 @@ # # Table name: submitters # -# id :bigint not null, primary key -# application_key :string -# completed_at :datetime -# email :string -# ip :string -# name :string -# opened_at :datetime -# phone :string -# preferences :text not null -# sent_at :datetime -# slug :string not null -# ua :string -# uuid :string not null -# values :text not null -# created_at :datetime not null -# updated_at :datetime not null -# submission_id :bigint not null +# id :bigint not null, primary key +# completed_at :datetime +# email :string +# ip :string +# name :string +# opened_at :datetime +# phone :string +# preferences :text not null +# sent_at :datetime +# slug :string not null +# ua :string +# uuid :string not null +# values :text not null +# created_at :datetime not null +# updated_at :datetime not null +# external_id :string +# submission_id :bigint not null # # Indexes # @@ -62,6 +62,10 @@ class Submitter < ApplicationRecord end end + def application_key + external_id + end + def friendly_name if name.present? && email.present? && email.exclude?(',') %("#{name.delete('"')}" <#{email}>) diff --git a/app/models/template.rb b/app/models/template.rb index 78262887..7b3d7f1b 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -4,20 +4,20 @@ # # Table name: templates # -# id :bigint not null, primary key -# application_key :string -# archived_at :datetime -# fields :text not null -# name :string not null -# schema :text not null -# slug :string not null -# source :text not null -# submitters :text not null -# created_at :datetime not null -# updated_at :datetime not null -# account_id :bigint not null -# author_id :bigint not null -# folder_id :bigint not null +# id :bigint not null, primary key +# archived_at :datetime +# fields :text not null +# name :string not null +# schema :text not null +# slug :string not null +# source :text not null +# submitters :text not null +# created_at :datetime not null +# updated_at :datetime not null +# account_id :bigint not null +# author_id :bigint not null +# external_id :string +# folder_id :bigint not null # # Indexes # @@ -61,6 +61,10 @@ class Template < ApplicationRecord scope :active, -> { where(archived_at: nil) } scope :archived, -> { where.not(archived_at: nil) } + def application_key + external_id + end + private def maybe_set_default_folder diff --git a/db/migrate/20240131212010_rename_application_key_to_external_id.rb b/db/migrate/20240131212010_rename_application_key_to_external_id.rb new file mode 100644 index 00000000..5b6489d2 --- /dev/null +++ b/db/migrate/20240131212010_rename_application_key_to_external_id.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class RenameApplicationKeyToExternalId < ActiveRecord::Migration[7.1] + def change + rename_column :templates, :application_key, :external_id + rename_column :submitters, :application_key, :external_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 7eeabae8..2903b6c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_20_192055) do +ActiveRecord::Schema[7.1].define(version: 2024_01_31_212010) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -171,7 +171,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_20_192055) do t.datetime "updated_at", null: false t.string "name" t.string "phone" - t.string "application_key" + t.string "external_id" t.text "preferences", null: false t.index ["email"], name: "index_submitters_on_email" t.index ["slug"], name: "index_submitters_on_slug", unique: true @@ -202,7 +202,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_20_192055) do t.datetime "updated_at", null: false t.text "source", null: false t.bigint "folder_id", null: false - t.string "application_key" + t.string "external_id" t.index ["account_id"], name: "index_templates_on_account_id" t.index ["author_id"], name: "index_templates_on_author_id" t.index ["folder_id"], name: "index_templates_on_folder_id" diff --git a/lib/submissions/create_from_submitters.rb b/lib/submissions/create_from_submitters.rb index be8525de..615ec375 100644 --- a/lib/submissions/create_from_submitters.rb +++ b/lib/submissions/create_from_submitters.rb @@ -135,7 +135,7 @@ module Submissions email:, phone: attrs[:phone].to_s.gsub(/[^0-9+]/, ''), name: attrs[:name], - application_key: attrs[:application_key], + external_id: attrs[:external_id].presence || attrs[:application_key], completed_at: attrs[:completed] ? Time.current : nil, sent_at: mark_as_sent && email.present? && is_order_sent ? Time.current : nil, values: attrs[:values] || {}, diff --git a/lib/submitters/serialize_for_api.rb b/lib/submitters/serialize_for_api.rb index aa261158..7d8ec48f 100644 --- a/lib/submitters/serialize_for_api.rb +++ b/lib/submitters/serialize_for_api.rb @@ -18,9 +18,9 @@ module Submitters serialize_params = { include: {}, - only: %i[id slug uuid name email phone completed_at application_key + only: %i[id slug uuid name email phone completed_at external_id opened_at sent_at created_at updated_at], - methods: %i[status] + methods: %i[status application_key] } serialize_params[:include][:template] = { only: %i[id name created_at updated_at] } if with_template diff --git a/lib/submitters/serialize_for_webhook.rb b/lib/submitters/serialize_for_webhook.rb index 26a117e9..4a8eafd8 100644 --- a/lib/submitters/serialize_for_webhook.rb +++ b/lib/submitters/serialize_for_webhook.rb @@ -16,7 +16,8 @@ module Submitters submitter_name = (submitter.submission.template_submitters || submitter.submission.template.submitters).find { |e| e['uuid'] == submitter.uuid }['name'] - submitter.as_json(include: [template: { only: %i[id name created_at updated_at] }]) + submitter.as_json(methods: %i[application_key], + include: [template: { only: %i[id name external_id created_at updated_at] }]) .except('uuid', 'values', 'slug') .merge('values' => values, 'documents' => documents, diff --git a/lib/templates/clone.rb b/lib/templates/clone.rb index 278e2247..70b6576a 100644 --- a/lib/templates/clone.rb +++ b/lib/templates/clone.rb @@ -4,12 +4,12 @@ module Templates module Clone module_function - def call(original_template, author:, application_key: nil, name: nil, folder_name: nil) + def call(original_template, author:, external_id: nil, name: nil, folder_name: nil) original_template_account = original_template.account template = original_template_account.templates.new - template.application_key = application_key + template.external_id = external_id template.author = author template.name = name || "#{original_template.name} (Clone)"