Include autenticazione, progetti isolati, mail marketing HTML con SMTP, test A/B e editor WYSIWYG. Co-authored-by: Cursor <cursoragent@cursor.com>
56 lines
1.7 KiB
Ruby
56 lines
1.7 KiB
Ruby
class ActivitiesController < ApplicationController
|
|
before_action :set_organization, only: %i[create]
|
|
|
|
def create
|
|
@activity = @organization.activities.build(activity_params)
|
|
@activity.user = current_user
|
|
@activity.happened_at ||= Time.current
|
|
|
|
if @activity.save
|
|
maybe_update_pipeline_from_activity!
|
|
redirect_to @organization, notice: "Attività registrata."
|
|
else
|
|
redirect_to @organization, alert: @activity.errors.full_messages.to_sentence
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_organization
|
|
@organization = Organization.find(params[:organization_id])
|
|
end
|
|
|
|
def activity_params
|
|
params.require(:activity).permit(
|
|
:activity_type, :subject, :description, :happened_at, :contact_id, :opportunity_id
|
|
)
|
|
end
|
|
|
|
def maybe_update_pipeline_from_activity!
|
|
opportunity = @activity.opportunity || @organization.opportunities.open_stage.order(updated_at: :desc).first
|
|
return unless opportunity
|
|
|
|
stage_map = {
|
|
"email_sent" => "contacted",
|
|
"email_received" => "replied",
|
|
"call" => "contacted",
|
|
"demo" => "demo_trial",
|
|
"trial_started" => "demo_trial",
|
|
"first_use" => "first_use",
|
|
"proposal_sent" => "proposal",
|
|
"won" => "won",
|
|
"lost" => "lost"
|
|
}
|
|
target = stage_map[@activity.activity_type]
|
|
return unless target
|
|
return if opportunity.won? || opportunity.lost?
|
|
return if Catalog::PIPELINE_ORDER.index(opportunity.pipeline_stage).to_i >= Catalog::PIPELINE_ORDER.index(target).to_i
|
|
|
|
attrs = { pipeline_stage: target }
|
|
if target == "lost"
|
|
attrs[:lost_reason] = params[:lost_reason].presence || "other"
|
|
end
|
|
opportunity.move_to_stage!(target, lost_reason: attrs[:lost_reason], user: current_user)
|
|
end
|
|
end
|