Commit 647c7987 authored by Nicolas Dular's avatar Nicolas Dular

Record when namespace created a subscription

This adds the first action for namespace onboarding, where we record
when a namespace created a new subscription.
parent 9e1a552f
......@@ -2,4 +2,20 @@
class NamespaceOnboardingAction < ApplicationRecord
belongs_to :namespace
ACTIONS = {
subscription_created: 1
}.freeze
enum action: ACTIONS
class << self
def completed?(namespace, action)
where(namespace: namespace, action: action).exists?
end
def create_action(namespace, action)
NamespaceOnboardingAction.safe_find_or_create_by(namespace: namespace, action: action)
end
end
end
......@@ -22,7 +22,11 @@ module Subscriptions
billing_email = customer_data[:email]
token = customer_data[:authentication_token]
client.create_subscription(create_subscription_params, billing_email, token)
response = client.create_subscription(create_subscription_params, billing_email, token)
NamespaceOnboardingAction.create_action(@group, :subscription_created) if response[:success]
response
end
private
......
......@@ -49,7 +49,11 @@ RSpec.describe Subscriptions::CreateService do
end
it 'creates a subscription with the returned authentication token' do
expect(client).to receive(:create_subscription).with(anything, customer_email, 'token')
expect(client)
.to receive(:create_subscription)
.with(anything, customer_email, 'token')
.and_return(success: true, data: { success: true, subscription_id: 'xxx' })
subject.execute
end
......@@ -61,6 +65,12 @@ RSpec.describe Subscriptions::CreateService do
it 'returns the response hash' do
expect(subject.execute).to eq(success: false, data: { errors: 'failed to create subscription' })
end
it 'does not create a namespace onboarding action' do
subject.execute
expect(NamespaceOnboardingAction.completed?(group, :subscription_created)).to eq(false)
end
end
context 'when successfully creating a subscription' do
......@@ -91,6 +101,12 @@ RSpec.describe Subscriptions::CreateService do
subject.execute
end
it 'creates a namespace onboarding action' do
subject.execute
expect(NamespaceOnboardingAction.completed?(group, :subscription_created)).to eq(true)
end
end
end
end
# frozen_string_literal: true
FactoryBot.define do
factory :namespace_onboarding_action do
namespace
action { :subscription_created }
end
end
......@@ -3,5 +3,45 @@
require 'spec_helper'
RSpec.describe NamespaceOnboardingAction do
let(:namespace) { build(:namespace) }
it { is_expected.to belong_to :namespace }
describe '.completed?' do
let(:action) { :subscription_created }
subject { described_class.completed?(namespace, action) }
context 'action created for the namespace' do
before do
create(:namespace_onboarding_action, namespace: namespace, action: action)
end
it { is_expected.to eq(true) }
end
context 'action created for another namespace' do
before do
create(:namespace_onboarding_action, namespace: build(:namespace), action: action)
end
it { is_expected.to eq(false) }
end
end
describe '.create_action' do
let(:action) { :subscription_created }
subject(:create_action) { described_class.create_action(namespace, action) }
it 'creates the action for the namespace just once' do
expect { create_action }.to change { count_namespace_actions }.by(1)
expect { create_action }.to change { count_namespace_actions }.by(0)
end
def count_namespace_actions
described_class.where(namespace: namespace, action: action).count
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