Commit 3176a7ff authored by Aishwarya Subramanian's avatar Aishwarya Subramanian Committed by James Lopez

Add endpoint and service to create Leads

We need to create Leads when customers are requesting a trial for
GL.com. In order to do it we're going to use a service that will call a
custom endpoint on the subscription portal.
parent 94c2b462
......@@ -133,7 +133,7 @@ class GroupsController < Groups::ApplicationController
protected
def render_show_html
render 'groups/show'
render 'groups/show', locals: { trial: params[:trial] }
end
def render_details_html
......
......@@ -10,7 +10,7 @@ class TrialRegistrationsController < RegistrationsController
def create
super do |new_user|
new_user.system_hook_service.execute_hooks_for(new_user, :create)
new_user.system_hook_service.execute_hooks_for(new_user, :create) if new_user.persisted?
end
end
......
......@@ -4,6 +4,7 @@ class TrialsController < ApplicationController
before_action :check_if_gl_com
before_action :check_if_improved_trials_enabled
before_action :authenticate_user!
before_action :fetch_namespace, only: :apply
def new
end
......@@ -15,12 +16,22 @@ class TrialsController < ApplicationController
result = GitlabSubscriptions::CreateLeadService.new.execute({ trial_user: company_params })
if result[:success]
redirect_to select_namespace_trials_url
redirect_to select_trials_url
else
render :new
end
end
def apply
result = GitlabSubscriptions::ApplyTrialService.new.execute(apply_trial_params)
if result[:success]
redirect_to group_url(@namespace, { trial: true })
else
redirect_to select_trials_url
end
end
private
def authenticate_user!
......@@ -48,4 +59,19 @@ class TrialsController < ApplicationController
def check_if_improved_trials_enabled
render_404 unless Feature.enabled?(:improved_trial_signup)
end
def apply_trial_params
gl_com_params = { gitlab_com_trial: true, sync_to_gl: true }
{
trial_user: params.permit(:namespace_id).merge(gl_com_params),
uid: current_user.id
}
end
def fetch_namespace
@namespace = current_user.namespaces.find(params[:namespace_id])
render_404 unless @namespace
end
end
# frozen_string_literal: true
module GitlabSubscriptions
class ApplyTrialService
def execute(apply_trial_params)
response = client.generate_trial(apply_trial_params)
if response[:success]
{ success: true }
else
{ success: false, errors: response.dig(:data, :errors) }
end
end
end
private
def client
Gitlab::SubscriptionPortal::Client.new
end
end
......@@ -3,6 +3,7 @@
resources :trials, only: [:new] do
collection do
post :create_lead
get :select_namespace, action: :select
get :select
post :apply
end
end
......@@ -69,6 +69,14 @@ describe TrialRegistrationsController do
post :create, params: { user: user_params }
end
it 'does not trigger user_create event when data is invalid' do
user_params[:email] = ''
expect_any_instance_of(SystemHooksService).not_to receive(:execute_hooks_for).with(an_instance_of(User), :create)
post :create, params: { user: user_params }
end
end
end
end
......
......@@ -61,7 +61,7 @@ describe TrialsController do
it 'redirects user to Step 3' do
post :create_lead
expect(response).to redirect_to(select_namespace_trials_url)
expect(response).to redirect_to(select_trials_url)
end
end
......@@ -76,4 +76,42 @@ describe TrialsController do
end
end
end
describe '#select' do
it_behaves_like 'an authenticated endpoint', :get, :select
end
describe '#apply' do
let(:user) { create(:user) }
let(:namespace) { create(:namespace, owner_id: user.id, path: 'namespace-test') }
let(:apply_trial_result) { nil }
before do
sign_in(user)
expect_any_instance_of(GitlabSubscriptions::ApplyTrialService).to receive(:execute) do
{ success: apply_trial_result }
end
end
context 'on success' do
let(:apply_trial_result) { true }
it "redirects to group's path with the parameter trial as true" do
post :apply, params: { namespace_id: namespace.id }
expect(response).to redirect_to("/#{namespace.path}?trial=true")
end
end
context 'on failure' do
let(:apply_trial_result) { false }
it 'redirects to new select namespaces for trials path' do
post :apply, params: { namespace_id: namespace.id }
expect(response).to redirect_to(select_trials_path)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment