Le voci si possono modificare o eliminare se registrate per errore, e la ricerca non va più in errore SQL sul campo email. Co-authored-by: Cursor <cursoragent@cursor.com>
84 lines
2.5 KiB
Ruby
84 lines
2.5 KiB
Ruby
class ActivitiesController < ApplicationController
|
|
before_action :require_current_project!
|
|
before_action :set_organization
|
|
before_action :set_activity, only: %i[edit update destroy]
|
|
|
|
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
|
|
|
|
def edit
|
|
@page_title = "Modifica attività"
|
|
end
|
|
|
|
def update
|
|
if @activity.update(activity_params)
|
|
redirect_to @organization, notice: "Attività aggiornata."
|
|
else
|
|
@page_title = "Modifica attività"
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@activity.destroy!
|
|
redirect_to @organization, notice: "Attività eliminata."
|
|
end
|
|
|
|
private
|
|
|
|
def set_organization
|
|
@organization = Organization.find(params[:organization_id])
|
|
return if current_user.admin?
|
|
return if @organization.projects.merge(available_projects).exists?
|
|
|
|
redirect_to organizations_path, alert: "Organizzazione non disponibile per i tuoi progetti." and return
|
|
end
|
|
|
|
def set_activity
|
|
@activity = @organization.activities.find(params[: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
|