Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_product
- product = Product.new();
- product.name = params[:name]
- product.description = params[:description]
- product.logo = params[:logo]
- product.team_id = params[:team_id]
- product.initials = params[:initials]
- respond_to do |format|
- if product.save
- ceo_id = 0;
- # Obtener user del ceo para enviarle la notificacion
- auxList = CourseUser.where(:course => product.team.course)
- auxList.each do |auxCU|
- if auxCU.rol == "CEO" # Es decir donde el rol es CEO
- ceo_id = auxCU.user.id
- end
- end
- currUser = User.find(params[:user_id])
- ###############################
- tokens = FcmToken.where(:user_id => ceo_id)
- if tokens.length > 0 && tokens[0] != nil
- uri = URI('https://fcm.googleapis.com/fcm/send')
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true
- req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json', 'Authorization' => 'key=AAAAe3BYdgo:APA91bF13EtVd07IZdv-9XTSATSwd-d1J1n2gKjVWpppTuz7Uj1R2hnwTCL3ioL4e7F4YVhU-iMzDI66Czu9mRT3A9sqQ-HVmb24wyda-lwEukaL7eCLjJHAvnEsi8foZ2_Bsh44wtN8'})
- req.body = {:to => tokens[0].token,
- :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},
- :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
- response = http.request(req)
- ##############################
- end
- # Verofocar si el producto esta basado en un prototipo
- if params[:use_prototype] != nil && params[:use_prototype] == 'true'
- prototype = Prototype.find(params[:prototype_id])
- # Obtener el compromiso de dicho prototipo y crear un compromiso para cada uno
- commitment_prototypes = CommitmentPrototype.where(:prototype => prototype);
- commitment_prototypes.each do |cp|
- commitment = Commitment.new()
- commitment.description = cp.description
- commitment.deadline = cp.deadline
- commitment.execution = 0
- commitment.count = 0
- commitment.user = currUser.id
- commitment.product = product
- # Validate if the user and the product were found
- if currUser == nil
- format.json {render json: { info: "User not found", status: :not_found}.to_json}
- else # usuario encontrado
- if commitment.save
- format.html{redirect_to products_path , notice: "Product was created successfully"}
- format.json {render json: {product: product, status: :ok}.to_json}
- format.js
- else # error con commitment.save
- format.json {render json: { info: "Error creating commitment", status: :unprocessable_entity}.to_json}
- end
- end
- end # fin del commitent_prototypes.each
- else # No se uso prototipos
- format.html{redirect_to products_path , notice: "Product was created successfully"}
- format.json {render json: {product: product, status: :ok}.to_json}
- format.js
- end
- else # error con product.save
- format.html { render "new", error: "The product was not created" }
- format.json {render json: {product: product, status: :unprocessable_entity}.to_json }
- format.js
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement