Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/api/v2/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class OrderProcessor < JSONAPI::Processor
def prepare_context
context[:template] = find_template
context[:template_attributes] = template_attributes unless context[:template].nil?
context[:project] = find_project
end

def find_project
project_uuid = params[:data][:attributes][:project_uuid]
return nil if project_uuid.nil?

project = Project.with_uuid(project_uuid).first
raise JSONAPI::Exceptions::InvalidFieldValue.new(:project_uuid, project_uuid) if project.nil?

project
end

def find_template
Expand Down
6 changes: 5 additions & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
serialize :item_options, coder: YAML

before_validation :set_study_from_aliquots, unless: :cross_study_allowed, if: :autodetect_studies
before_validation :set_project_from_aliquots, unless: :cross_project_allowed, if: :autodetect_projects
before_validation :set_project_from_aliquots, unless: :cross_project_allowed, if: [:autodetect_projects, :project_not_set]

Check failure on line 64 in app/models/order.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/LineLength: Line is too long. [124/120] (https://rubystyle.guide#max-line-length)

Check failure on line 64 in app/models/order.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Style/SymbolArray: Use `%i` or `%I` for an array of symbols. (https://rubystyle.guide#percent-i)

Check failure on line 64 in app/models/order.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/LineLength: Line is too long. [124/120] (https://rubystyle.guide#max-line-length)

Check failure on line 64 in app/models/order.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Style/SymbolArray: Use `%i` or `%I` for an array of symbols. (https://rubystyle.guide#percent-i)

def project_not_set
project.blank?
end

validates :study, presence: true, unless: :cross_study_allowed
validates :project, presence: true, unless: :cross_project_allowed
Expand Down
3 changes: 2 additions & 1 deletion app/models/submission_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# We could have use a Prototype Factory , and so just associate a name to existing submission
# but that doesn't work because the submission prototype doesn't pass the validation stage.
# Anyway that's basically a prototype factory
class SubmissionTemplate < ApplicationRecord

Check failure on line 6 in app/models/submission_template.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Metrics/ClassLength: Class has too many lines. [101/100]
include Uuid::Uuidable

validates :name, presence: true
Expand Down Expand Up @@ -54,8 +54,9 @@
end
end

def create_order!(attributes)
def create_order!(attributes, project = nil)
new_order(attributes).tap do |order|
order.project = project if project
yield(order) if block_given?
order.save!
end
Expand Down
9 changes: 8 additions & 1 deletion app/resources/api/v2/order_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
# For more information about JSON:API see the [JSON:API Specifications](https://jsonapi.org/format/)
# or refer to the [JSONAPI::Resources](http://jsonapi-resources.com/) package for Sequencescape's implementation.
class OrderResource < BaseResource

Check failure on line 56 in app/resources/api/v2/order_resource.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. (https://rubystyle.guide#empty-lines-around-bodies)

Check failure on line 56 in app/resources/api/v2/order_resource.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. (https://rubystyle.guide#empty-lines-around-bodies)

Check failure on line 57 in app/resources/api/v2/order_resource.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/EmptyLines: Extra blank line detected. (https://rubystyle.guide#two-or-more-empty-lines)

Check failure on line 57 in app/resources/api/v2/order_resource.rb

View workflow job for this annotation

GitHub Actions / Rubocop

Layout/EmptyLines: Extra blank line detected. (https://rubystyle.guide#two-or-more-empty-lines)
###
# Attributes
###
Expand All @@ -75,6 +77,11 @@
# @return [String] The UUID of this {Order}.
attribute :uuid, readonly: true

# @!attribute [w] project_uuid
# @return [String] The UUID of the project to associate with this Order on creation.
attribute :project_uuid, writeonly: true
attr_writer :project_uuid # Not stored, consumed by OrderProcessor.

###
# Relationships
###
Expand Down Expand Up @@ -131,7 +138,7 @@
def self.create(context)
return super if context[:template].nil?

order = context[:template].create_order!(context[:template_attributes])
order = context[:template].create_order!(context[:template_attributes], context[:project])
new(order, context)
end
end
Expand Down
Loading