Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.49 KB | None | 0 0
  1. module Project
  2.   module Projects
  3.     module Service
  4.       class << self
  5.  
  6.         def create_from_request(params, account, user)
  7.           quote_request_params = params[:quote_request]
  8.           client_params = params[:client]
  9.           client = find_project_client(client_params, account, user)
  10.           raise Exception if client.blank?
  11.           create_project client, quote_request_params, account, user
  12.         end
  13.  
  14.         def create_project(client, quote_request, account, user)
  15.           json_request = quote_request.to_json
  16.           client_name = client.try(:contacts).try(:first).try(:email) || client.try(:name)
  17.           project_params = {
  18.             name: "Developer Project - #{client_name}",
  19.             description: nil,
  20.             clientable: {
  21.               clientable_type: client.class,
  22.               id: client.id
  23.             },
  24.             budget: quote_request[:price].to_h[:to],
  25.             currency: 'pln',
  26.             assignes: [],
  27.             quote_request: json_request
  28.           }
  29.           ::Project::Projects::Manager.create project_params, account, user
  30.         end
  31.  
  32.         def find_project_client(client_params, account, user)
  33.           nip = client_params[:nip]
  34.           if nip.present?
  35.             company = Company.joins("LEFT JOIN (fields f INNER JOIN fields_strings fs ON f.field_id = fs.id AND field_type = 'Fields::String' AND fs.key = 'nip') ON f.fieldable_type = 'Company' AND f.fieldable_id = companies.id AND f.field_type = 'Fields::String'").where("fs.value ILIKE ?", "%#{client_params[:nip]}%").try(:first)
  36.             company.blank? ? Companies::Manager.find_by_gus_and_create(nip, account, user) : company
  37.           else
  38.             person = Person.joins("INNER JOIN contacts ON contacts.contactable_id = people.id AND contacts.contactable_type= 'Person'").joins("LEFT JOIN (fields f INNER JOIN fields_strings fs ON f.field_id = fs.id AND field_type = 'Fields::String' AND fs.key = 'email') ON f.fieldable_type = 'Contact' AND f.fieldable_id = contacts.id AND f.field_type = 'Fields::String'").where("fs.value ILIKE ?", "%#{client_params[:email]}%").try(:first)
  39.             person_params = {
  40.               first_name: client_params[:first_name],
  41.               last_name: client_params[:last_name],
  42.               contacts: [{ phone: client_params[:phone], email: client_params[:email] }]
  43.             }
  44.             person.blank? ? People::Manager.create(person_params, account, user) : person
  45.           end
  46.         end
  47.       end
  48.     end
  49.   end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement