Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Project
- module Projects
- module Service
- class << self
- def create_from_request(params, account, user)
- quote_request_params = params[:quote_request]
- client_params = params[:client]
- client = find_project_client(client_params, account, user)
- raise Exception if client.blank?
- create_project client, quote_request_params, account, user
- end
- def create_project(client, quote_request, account, user)
- json_request = quote_request.to_json
- client_name = client.try(:contacts).try(:first).try(:email) || client.try(:name)
- project_params = {
- name: "Developer Project - #{client_name}",
- description: nil,
- clientable: {
- clientable_type: client.class,
- id: client.id
- },
- budget: quote_request[:price].to_h[:to],
- currency: 'pln',
- assignes: [],
- quote_request: json_request
- }
- ::Project::Projects::Manager.create project_params, account, user
- end
- def find_project_client(client_params, account, user)
- nip = client_params[:nip]
- if nip.present?
- 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)
- company.blank? ? Companies::Manager.find_by_gus_and_create(nip, account, user) : company
- else
- 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)
- person_params = {
- first_name: client_params[:first_name],
- last_name: client_params[:last_name],
- contacts: [{ phone: client_params[:phone], email: client_params[:email] }]
- }
- person.blank? ? People::Manager.create(person_params, account, user) : person
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement