Commit 5ba52c44 authored by Matija Čupić's avatar Matija Čupić

Move callout dismissal to service

Moves the logic for dismissing a user callout into a service.
parent 333a697b
...@@ -4,8 +4,9 @@ class UserCalloutsController < ApplicationController ...@@ -4,8 +4,9 @@ class UserCalloutsController < ApplicationController
feature_category :navigation feature_category :navigation
def create def create
callout = current_user.find_or_initialize_callout(feature_name) callout = Users::DismissUserCalloutService.new(
callout.update(dismissed_at: Time.current) if callout.valid? container: nil, current_user: current_user, params: { feature_name: feature_name }
).execute
if callout.persisted? if callout.persisted?
respond_to do |format| respond_to do |format|
......
...@@ -15,13 +15,13 @@ module Mutations ...@@ -15,13 +15,13 @@ module Mutations
description: 'The user callout dismissed.' description: 'The user callout dismissed.'
def resolve(feature_name:) def resolve(feature_name:)
user_callout = current_user.find_or_initialize_callout(feature_name) callout = Users::DismissUserCalloutService.new(
container: nil, current_user: current_user, params: { feature_name: feature_name }
user_callout.update(dismissed_at: Time.current) if user_callout.valid? ).execute
errors = errors_on_object(user_callout) errors = errors_on_object(callout)
{ {
user_callout: user_callout, user_callout: callout,
errors: errors errors: errors
} }
end end
......
# frozen_string_literal: true
module Users
class DismissUserCalloutService < BaseContainerService
def execute
current_user.find_or_initialize_callout(params[:feature_name]).tap do |callout|
callout.update(dismissed_at: Time.current) if callout.valid?
end
end
end
end
...@@ -5487,7 +5487,7 @@ RSpec.describe User do ...@@ -5487,7 +5487,7 @@ RSpec.describe User do
subject(:find_or_initialize_callout) { user.find_or_initialize_callout(feature_name) } subject(:find_or_initialize_callout) { user.find_or_initialize_callout(feature_name) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:feature_name) { UserCallout.feature_names.keys.first } let(:feature_name) { UserCallout.feature_names.each_key.first }
context 'when callout exists' do context 'when callout exists' do
let!(:callout) { create(:user_callout, user: user, feature_name: feature_name) } let!(:callout) { create(:user_callout, user: user, feature_name: feature_name) }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Users::DismissUserCalloutService do
let(:user) { create(:user) }
let(:service) do
described_class.new(
container: nil, current_user: user, params: { feature_name: UserCallout.feature_names.each_key.first }
)
end
describe '#execute' do
subject(:execute) { service.execute }
it 'returns a user callout' do
expect(execute).to be_an_instance_of(UserCallout)
end
it 'sets the dismisse_at attribute to current time' do
freeze_time do
expect(execute).to have_attributes(dismissed_at: Time.current)
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