Skip to content

Commit 3b55e10

Browse files
committed
Allow ExCS admin to set starter project identifier
To achieve this, I've had to remove the explicit *overriding* of `Project#identifier` in `Project::Create.build_project` when the user is an Experience CS admin. Note that the `Project#check_unique_not_null` check which is triggered on a `before_validation` callback will generate an identifier if none is set in `Project::Create.build_project`
1 parent 6319dd3 commit 3b55e10

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

app/controllers/api/projects_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def show
2727
end
2828

2929
def create
30-
result = Project::Create.call(project_hash: project_params)
30+
result = Project::Create.call(project_hash: project_params, current_user:)
3131

3232
if result.success?
3333
@project = result[:project]

app/graphql/mutations/create_project.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def resolve(**input)
1313
components: input[:components]&.map(&:to_h)
1414
)
1515

16-
response = Project::Create.call(project_hash:)
16+
response = Project::Create.call(project_hash:, current_user: context[:current_user])
1717
raise GraphQL::ExecutionError, response[:error] unless response.success?
1818

1919
{ project: response[:project] }

lib/concepts/project/operations/create.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
class Project
44
class Create
55
class << self
6-
def call(project_hash:)
6+
def call(project_hash:, current_user:)
77
response = OperationResponse.new
8-
response[:project] = build_project(project_hash)
8+
response[:project] = build_project(project_hash, current_user)
99
response[:project].save!
1010
response
1111
rescue StandardError => e
@@ -16,9 +16,9 @@ def call(project_hash:)
1616

1717
private
1818

19-
def build_project(project_hash)
20-
identifier = PhraseIdentifier.generate
21-
new_project = Project.new(project_hash.except(:components).merge(identifier:))
19+
def build_project(project_hash, current_user)
20+
project_hash[:identifier] = PhraseIdentifier.generate unless current_user&.experience_cs_admin?
21+
new_project = Project.new(project_hash.except(:components))
2222
new_project.components.build(project_hash[:components])
2323
new_project
2424
end

spec/concepts/project/create_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
require 'rails_helper'
44

55
RSpec.describe Project::Create, type: :unit do
6-
subject(:create_project) { described_class.call(project_hash:) }
6+
subject(:create_project) { described_class.call(project_hash:, current_user:) }
77

8-
let(:user_id) { 'e0675b6c-dc48-4cd6-8c04-0f7ac05af51a' }
8+
let(:current_user) { create(:user) }
9+
let(:user_id) { current_user.id }
910

1011
before do
1112
mock_phrase_generation
@@ -16,7 +17,7 @@
1617
let(:project_hash) { ActionController::Parameters.new({}).merge(user_id:) }
1718

1819
context 'with valid content' do
19-
subject(:create_project_with_content) { described_class.call(project_hash:) }
20+
subject(:create_project_with_content) { described_class.call(project_hash:, current_user:) }
2021

2122
let(:project_hash) do
2223
{

spec/features/project/creating_a_project_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
expect(response).to have_http_status(:created)
3030
end
3131

32-
it 'generates an identifier for the project' do
33-
post('/api/projects', headers:, params:)
32+
it 'generates an identifier for the project even if another identifier is specified' do
33+
params_with_identifier = { project: { identifier: 'test-identifier', components: [] } }
34+
post('/api/projects', headers:, params: params_with_identifier)
3435
data = JSON.parse(response.body, symbolize_names: true)
3536

3637
expect(data[:identifier]).to eq(generated_identifier)
@@ -213,6 +214,7 @@
213214
let(:params) do
214215
{
215216
project: {
217+
identifier: 'test-project',
216218
name: 'Test Project',
217219
locale: 'fr',
218220
project_type: Project::Types::SCRATCH,
@@ -230,6 +232,13 @@
230232
expect(response).to have_http_status(:created)
231233
end
232234

235+
it 'sets the project identifier to the specified (not the generated) value' do
236+
post('/api/projects', headers:, params:)
237+
data = JSON.parse(response.body, symbolize_names: true)
238+
239+
expect(data[:identifier]).to eq('test-project')
240+
end
241+
233242
it 'sets the project name to the specified value' do
234243
post('/api/projects', headers:, params:)
235244
data = JSON.parse(response.body, symbolize_names: true)

0 commit comments

Comments
 (0)