Commit 40c2cb59 authored by minahilnichols's avatar minahilnichols

Move creation of trial or lead to a separate service

parent 48582035
......@@ -5,15 +5,13 @@ import RegistrationForm from 'ee/registrations/components/company_form.vue';
export default () => {
const el = document.querySelector('#js-company-registration-form');
const { submitPath, trial, firstName, lastName, role, jtbd, comment } = el.dataset;
const { submitPath, trial, role, jtbd, comment } = el.dataset;
return new Vue({
el,
apolloProvider,
provide: {
submitPath,
firstName,
lastName,
role,
jtbd,
comment,
......
......@@ -28,7 +28,7 @@ export default {
CountryOrRegionSelector,
RegistrationTrialToggle,
},
inject: ['submitPath', 'firstName', 'lastName', 'role', 'jtbd', 'comment'],
inject: ['submitPath', 'role', 'jtbd', 'comment'],
props: {
trial: {
type: Boolean,
......@@ -84,8 +84,11 @@ export default {
</script>
<template>
<gl-form :action="createLeadPath" method="post">
<gl-form :action="submitPath" method="post">
<input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
<input :value="role" type="hidden" name="role" data-testid="role" />
<input :value="jtbd" type="hidden" name="jtbd" data-testid="jtbd" />
<input :value="comment" type="hidden" name="comment" data-testid="comment" />
<gl-form-text class="gl-font-base gl-text-gray-400 gl-pb-3">{{ descriptionText }}</gl-form-text>
<div class="gl-display-flex gl-flex-direction-column gl-sm-flex-direction-row gl-mt-5">
<gl-form-group
......
......@@ -12,16 +12,17 @@ module Registrations
end
def create
if Gitlab::Utils.to_boolean(params[:trial])
result = GitlabSubscriptions::CreateLeadService.new.execute({ trial_user: company_params })
redirect_to(new_users_sign_up_groups_project_path(trial_onboarding_flow: true)) && return if result[:success]
else
result = GitlabSubscriptions::CreateHandRaiseLeadService.new.execute(company_params)
redirect_to(new_users_sign_up_groups_project_path(skip_trial: true)) && return if result[:success]
end
result = GitlabSubscriptions::CreateTrialOrLeadService.new.execute(
user: current_user,
params: permitted_params
)
if result[:success]
redirect_to new_users_sign_up_groups_project_path(redirect_param)
else
render :new
end
end
private
......@@ -31,22 +32,28 @@ module Registrations
redirect_to new_trial_registration_path, alert: I18n.t('devise.failure.unauthenticated')
end
def company_params
params.permit(:first_name, :last_name, :company_name, :company_size, :phone_number,
:country, :state, :website_url, :glm_content, :glm_source)
.merge(extra_params)
def permitted_params
params.permit(
:company_name,
:company_size,
:phone_number,
:country,
:state,
:website_url,
# previous step(s) data
:role,
:jtbd,
:comment,
:trial
)
end
def extra_params
{
work_email: current_user.email,
uid: current_user.id,
provider: 'gitlab',
setup_for_company: current_user.setup_for_company,
skip_email_confirmation: true,
gitlab_com_trial: true,
newsletter_segment: current_user.email_opted_in
}
def redirect_param
if params[:trial] == 'true'
{ trial_onboarding_flow: true }
else
{ skip_trial: true }
end
end
end
end
......@@ -29,12 +29,10 @@ module EE
{
submit_path: users_sign_up_company_path(glm_params),
trial: params[:trial],
first_name: current_user.first_name,
last_name: current_user.last_name,
role: params[:role],
jtbd: params[:jtbd],
comment: params[:comment]
}.merge(params.slice(:first_name, :last_name).to_unsafe_h.symbolize_keys)
}
end
def should_ask_company_question?
......
# frozen_string_literal: true
module GitlabSubscriptions
class CreateTrialOrLeadService
def execute(user:, params:)
params = params.merge(hardcoded_values).merge(user_values(user))
response = if Gitlab::Utils.to_boolean(params[:trial])
client.generate_trial(trial_user: params)
else
client.generate_hand_raise_lead(params)
end
if response[:success]
ServiceResponse.success
else
ServiceResponse.error(message: response.dig(:data, :errors))
end
end
private
def hardcoded_values
{
provider: 'gitlab',
skip_email_confirmation: true,
gitlab_com_trial: true
}
end
def user_values(user)
{
uid: user.id,
first_name: user.first_name,
last_name: user.last_name,
work_email: user.email,
setup_for_company: user.setup_for_company,
newsletter_segment: user.email_opted_in
}
end
def client
Gitlab::SubscriptionPortal::Client
end
end
end
......@@ -49,10 +49,9 @@ RSpec.describe Registrations::CompanyController do
it { is_expected.to render_template(:new) }
end
end
describe '#create' do
using RSpec::Parameterized::TableSyntax
context 'on success' do
let(:params) do
{
company_name: 'GitLab',
......@@ -60,56 +59,42 @@ RSpec.describe Registrations::CompanyController do
phone_number: '+1 23 456-78-90',
country: 'US',
state: 'CA',
website_url: 'gitlab.com',
work_email: user.email,
uid: user.id,
provider: 'gitlab',
setup_for_company: user.setup_for_company,
skip_email_confirmation: true,
gitlab_com_trial: true,
newsletter_segment: user.email_opted_in
website_url: 'gitlab.com'
}
end
let(:hand_raise_params) { ActionController::Parameters.new(params).permit! }
let(:lead_params) { { trial_user: hand_raise_params } }
let(:trial_onboarding_flow) { new_users_sign_up_groups_project_path(trial_onboarding_flow: true) }
let(:skip_trial) { new_users_sign_up_groups_project_path(skip_trial: true) }
where(:trial, :expected_params, :post_service, :redirect_query) do
true | ref(:lead_params) | GitlabSubscriptions::CreateLeadService | ref(:trial_onboarding_flow)
false | ref(:hand_raise_params) | GitlabSubscriptions::CreateHandRaiseLeadService | ref(:skip_trial)
context 'on success' do
where(:trial, :redirect_query) do
'true' | { trial_onboarding_flow: true }
'false' | { skip_trial: true }
end
with_them do
it 'calls the correct service' do
expect_next_instance_of(post_service) do |service|
expect(service).to receive(:execute).with(expected_params).and_return({ success: true })
it 'creates trial or lead and redirects to the corect path' do
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service|
expect(service).to receive(:execute).with({
user: user,
params: ActionController::Parameters.new(params.merge({ trial: trial })).permit!
}).and_return({ success: true })
end
post_params = params.merge(trial: trial)
post :create, params: post_params
post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:redirect)
expect(response).to redirect_to(redirect_query)
expect(response).to redirect_to(new_users_sign_up_groups_project_path(redirect_query))
end
end
end
context 'on failure' do
where(:trial, :post_service) do
true | GitlabSubscriptions::CreateLeadService
false | GitlabSubscriptions::CreateHandRaiseLeadService
end
where(trial: %w[true false])
with_them do
it 'calls the correct service' do
expect_next_instance_of(post_service) do |service|
it 'renders company page :new' do
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service|
expect(service).to receive(:execute).and_return(ServiceResponse.error(message: 'failed'))
end
post :create, params: { trial: trial }
post :create, params: params.merge({ trial: trial })
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:new)
end
......
......@@ -18,33 +18,22 @@ RSpec.describe 'Company Information', :js do
end
context 'send company information to create lead' do
it 'with all required fields' do
trial_user_params = {
"company_name" => 'GitLab',
"company_size" => '1 - 99'.delete(' '),
"phone_number" => '+1 23 456-78-90',
"country" => 'US',
"state" => 'CA',
"work_email" => user.email,
"uid" => user.id,
"setup_for_company" => user.setup_for_company,
"skip_email_confirmation" => true,
"gitlab_com_trial" => true,
"provider" => "gitlab",
"newsletter_segment" => user.email_opted_in,
"website_url" => 'gitlab.com'
using RSpec::Parameterized::TableSyntax
let(:params) do
{
company_name: 'GitLab',
company_size: '1-99',
phone_number: '+1 23 456-78-90',
country: 'US',
state: 'CA',
website_url: 'gitlab.com',
role: '',
jtbd: '',
comment: ''
}
end
let(:params) { ActionController::Parameters.new(trial_user_params).permit! }
let(:trial_params) { { trial_user: params } }
fill_in 'company_name', with: 'GitLab'
select '1 - 99', from: 'company_size'
select 'United States of America', from: 'country'
select 'California', from: 'state'
fill_in 'website_url', with: 'gitlab.com'
fill_in 'phone_number', with: '+1 23 456-78-90'
where(trial: %w[true false])
with_them do
it 'proceeds to next step' do
......@@ -56,10 +45,13 @@ RSpec.describe 'Company Information', :js do
fill_in 'phone_number', with: '+1 23 456-78-90'
# defaults to trial off, click to turn on
click_button class: 'gl-toggle' if trial
click_button class: 'gl-toggle' if Gitlab::Utils.to_boolean(trial)
expect_next_instance_of(post_service) do |service|
expect(service).to receive(:execute).with(post_params).and_return({ success: true })
expect_next_instance_of(GitlabSubscriptions::CreateTrialOrLeadService) do |service|
expect(service).to receive(:execute).with({
user: user,
params: ActionController::Parameters.new(params.merge({ trial: trial })).permit!
}).and_return({ success: true })
end
click_button 'Continue'
......
......@@ -18,8 +18,6 @@ describe('RegistrationForm', () => {
localVue,
provide: {
submitPath: SUBMIT_PATH,
firstName: 'Joe',
lastName: 'Doe',
role: 'Software Engineer',
jtbd: 'Jobs to be done',
comment: 'A comment',
......@@ -76,8 +74,6 @@ describe('RegistrationForm', () => {
it.each`
testid | value
${'first_name'} | ${'Joe'}
${'last_name'} | ${'Doe'}
${'role'} | ${'Software Engineer'}
${'jtbd'} | ${'Jobs to be done'}
${'comment'} | ${'A comment'}
......
......@@ -61,44 +61,15 @@ RSpec.describe EE::TrialHelper do
double('User', first_name: '_first_name_', last_name: '_last_name_')
end
let(:extra_params) do
{
first_name: '_params_first_name_',
last_name: '_params_last_name_'
}
end
let(:params) do
ActionController::Parameters.new(extra_params.merge(glm_source: '_glm_source_', glm_content: '_glm_content_'))
end
before do
allow(helper).to receive(:params).and_return(params)
allow(helper).to receive(:current_user).and_return(user)
end
it 'provides expected form data' do
keys = extra_params.keys + [:submit_path, :trial, :role, :jtbd, :comment]
keys = [:submit_path, :trial, :role, :jtbd, :comment]
expect(helper.create_company_form_data.keys.map(&:to_sym)).to match_array(keys)
end
it 'allows overriding data with params' do
expect(helper.create_company_form_data).to match(a_hash_including(extra_params))
end
context 'when params are empty' do
let(:extra_params) { {} }
it 'uses the values from current user' do
current_user_attributes = {
first_name: user.first_name,
last_name: user.last_name
}
expect(helper.create_company_form_data).to match(a_hash_including(current_user_attributes))
end
end
end
describe '#should_ask_company_question?' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSubscriptions::CreateTrialOrLeadService do
let(:user) { build(:user) }
describe '#execute' do
using RSpec::Parameterized::TableSyntax
where(:trial, :service) do
'true' | :generate_trial
'false' | :generate_hand_raise_lead
end
with_them do
it 'successfully creates a trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: true })
result = described_class.new.execute(**{ user: user, params: { trial: trial } })
expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be true
end
it 'error while creating trial or lead' do
allow(Gitlab::SubscriptionPortal::Client).to receive(service).and_return({ success: false })
result = described_class.new.execute(**{ user: user, params: { trial: trial } })
expect(result.is_a?(ServiceResponse)).to be true
expect(result.success?).to be false
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