discourse/plugins/automation/lib/discourse_automation/triggers/user_updated.rb
Osama Sayegh 4733369f71
FEATURE: Add 'New users only' option to user_updated trigger (#26648)
This commit adds a new option to the `user_updated` trigger of the automation plugin to only trigger an automation for new users that join after the automation is enabled.

Internal topic: t/125829/9.
2024-04-16 21:13:11 +03:00

35 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class DiscourseAutomation::Triggerable
USER_UPDATED = "user_updated"
end
DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::USER_UPDATED) do
field :custom_fields, component: :custom_fields
field :user_profile, component: :user_profile
field :once_per_user, component: :boolean
field :new_users_only, component: :boolean
validate do
has_triggers = has_trigger_field?(:custom_fields) && has_trigger_field?(:user_profile)
custom_fields = trigger_field(:custom_fields)["value"]
user_profile = trigger_field(:user_profile)["value"]
if has_triggers && custom_fields.blank? && user_profile.blank?
errors.add(
:base,
I18n.t("discourse_automation.triggerables.errors.custom_fields_or_user_profile_required"),
)
false
else
true
end
end
placeholder do |fields, automation|
custom_fields = automation.trigger_field("custom_fields")["value"] || []
user_profile = automation.trigger_field("user_profile")["value"] || []
custom_fields + user_profile
end
end