Class: Dscf::Marketplace::RfqResponseService

Inherits:
Object
  • Object
show all
Defined in:
app/services/dscf/marketplace/rfq_response_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_user) ⇒ RfqResponseService

Returns a new instance of RfqResponseService.



4
5
6
# File 'app/services/dscf/marketplace/rfq_response_service.rb', line 4

def initialize(current_user)
  @current_user = current_user
end

Instance Method Details

#respond_to_rfq(rfq, params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/dscf/marketplace/rfq_response_service.rb', line 8

def respond_to_rfq(rfq, params = {})
  return {success: false, errors: [ "RFQ not found or invalid" ]}  unless rfq.present? && (rfq.draft? || rfq.sent?)

  business = Dscf::Core::Business.find_by(user: @current_user)
  return {success: false, errors: [ "No business associated with user" ]} unless business.present?

  quotation = Dscf::Marketplace::Quotation.new(
    request_for_quotation: rfq,
    business: business,
    status: :draft,
    notes: params[:notes] || "",
    valid_until: params[:valid_until] || 7.days.from_now,
    delivery_date: params[:delivery_date] || (params[:valid_until] || 7.days.from_now).to_date
  )

  if quotation.save
    create_quotation_items(quotation, params[:quotation_items_attributes] || [])
    quotation.update_total_price!
    rfq.update(status: :responded) if rfq.sent?
    {success: true, quotation: quotation}
  else
    {success: false, errors: quotation.errors.full_messages}
  end
end