Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.77 KB | None | 0 0
  1.  def create_product
  2.     product = Product.new();
  3.     product.name = params[:name]
  4.     product.description = params[:description]
  5.     product.logo = params[:logo]
  6.     product.team_id = params[:team_id]
  7.     product.initials = params[:initials]
  8.  
  9.     respond_to do |format|
  10.       if product.save
  11.         ceo_id = 0;
  12.         # Obtener user del ceo para enviarle la notificacion
  13.         auxList = CourseUser.where(:course => product.team.course)
  14.         auxList.each do |auxCU|  
  15.           if auxCU.rol == "CEO" # Es decir donde el rol es CEO
  16.             ceo_id = auxCU.user.id
  17.           end
  18.         end
  19.  
  20.         currUser = User.find(params[:user_id])
  21.  
  22.         ###############################
  23.         tokens = FcmToken.where(:user_id => ceo_id)
  24.  
  25.         if tokens.length > 0 && tokens[0] != nil
  26.           uri = URI('https://fcm.googleapis.com/fcm/send')
  27.           http = Net::HTTP.new(uri.host, uri.port)
  28.           http.use_ssl = true
  29.           req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json', 'Authorization' => 'key=AAAAe3BYdgo:APA91bF13EtVd07IZdv-9XTSATSwd-d1J1n2gKjVWpppTuz7Uj1R2hnwTCL3ioL4e7F4YVhU-iMzDI66Czu9mRT3A9sqQ-HVmb24wyda-lwEukaL7eCLjJHAvnEsi8foZ2_Bsh44wtN8'})
  30.  
  31.           req.body = {:to => tokens[0].token,
  32.            :notification => {:title => 'Han creado un producto en uno de tus equipos', :body => currUser.names + ' ha creado un producto en el equipo ' + product.team.name + ' del curso ' + product.team.course.name},
  33.             :data => {:type => 'NEW_PRODUCT', :user_id => currUser.id, :user_name => currUser.names, :course_id => product.team.course.id, :course_name => product.team.course.name, :team_id => product.team.id, :team_name => product.team.name, :product_id => product.id}}.to_json
  34.  
  35.           response = http.request(req)
  36.           ##############################
  37.         end
  38.  
  39.         # Verofocar si el producto esta basado en un prototipo
  40.         if params[:use_prototype] != nil && params[:use_prototype] == 'true'
  41.           prototype = Prototype.find(params[:prototype_id])
  42.  
  43.           # Obtener el compromiso de dicho prototipo y crear un compromiso para cada uno
  44.           commitment_prototypes = CommitmentPrototype.where(:prototype => prototype);
  45.           commitment_prototypes.each do |cp|
  46.             commitment = Commitment.new()
  47.             commitment.description = cp.description
  48.             commitment.deadline = cp.deadline
  49.             commitment.execution = 0
  50.             commitment.count = 0
  51.  
  52.             commitment.user = currUser.id
  53.             commitment.product = product
  54.  
  55.             # Validate if the user and the product were found
  56.             if currUser == nil
  57.                 format.json {render json: { info: "User not found",  status: :not_found}.to_json}
  58.             else  # usuario encontrado
  59.               if commitment.save
  60.                 format.html{redirect_to products_path , notice: "Product was created successfully"}
  61.                 format.json {render json: {product: product, status: :ok}.to_json}
  62.                 format.js
  63.               else  # error con commitment.save
  64.                 format.json {render json: { info: "Error creating commitment",  status: :unprocessable_entity}.to_json}
  65.               end
  66.             end
  67.           end # fin del commitent_prototypes.each
  68.         else  # No se uso prototipos
  69.           format.html{redirect_to products_path , notice: "Product was created successfully"}
  70.           format.json {render json: {product: product, status: :ok}.to_json}
  71.           format.js
  72.         end
  73.       else  # error con product.save
  74.           format.html { render "new", error: "The product was not created" }
  75.           format.json {render json: {product: product,  status: :unprocessable_entity}.to_json }
  76.           format.js
  77.       end
  78.     end
  79.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement