diff --git a/app/assets/javascripts/login.js b/app/assets/javascripts/login.js new file mode 100644 index 0000000..70a2f44 --- /dev/null +++ b/app/assets/javascripts/login.js @@ -0,0 +1,43 @@ +$("#checkin_menu").show(); +$("#checkin").click( function(e){ + var username = $("#user_email").val(); + var password = $("#user_password").val(); + $.ajax({ + type: 'POST', + url: '/api/v1/checkin', + dataType: 'json', + contentType: 'application/json', + processData: false, + data: JSON.stringify({"username": username, "password": password }), + complete: function() { }, + success: function(data) { + alert("Checked IN!"); + $("#user_email").val(''); + $("#user_password").val(''); + }, + error: function(data,textStatus) { + alert( "Error: " + JSON.parse(data.responseText)["error"]); + } + }) +}); +$("#checkout").click( function(e){ + var username = $("#user_email").val(); + var password = $("#user_password").val(); + $.ajax({ + type: 'POST', + url: '/api/v1/checkout', + dataType: 'json', + contentType: 'application/json', + processData: false, + data: JSON.stringify({"username": username, "password": password }), + complete: function() { }, + success: function(data) { + alert("Checked OUT!"); + $("#user_email").val(''); + $("#user_password").val(''); + }, + error: function(data,textStatus) { + alert( "Error: " + JSON.parse(data.responseText)["error"]); + } + }) +}); diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 0000000..da644d0 --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -0,0 +1,23 @@ +class Api::V1::BaseController < ActionController::Base + respond_to :json + + before_filter :authenticate_user + + private + def authenticate_user + if params[:token] + @current_user = User.find_by_authentication_token(params[:token]) + else + user = User.find_for_database_authentication( :email => params[:username] ) + @current_user = user if user && user.valid_password?( params[:password] ) + end + unless @current_user + render :json => {:error => "Username/Password/Token invalid" }, :status => 403 + end + end + + def current_user + @current_user + end +end + diff --git a/app/controllers/api/v1/logs_controller.rb b/app/controllers/api/v1/logs_controller.rb new file mode 100644 index 0000000..498b6d2 --- /dev/null +++ b/app/controllers/api/v1/logs_controller.rb @@ -0,0 +1,20 @@ +class Api::V1::LogsController < Api::V1::BaseController + + def checkin + if @current_user.checked_in? + render :json => { "error" => "You are already checked in."}, :status => 404 and return + else + @current_user.checkin + render :nothing => true, :status => 204 and return + end + end + + def checkout + if !@current_user.checked_in? + render :json => { "error" => "You were not even checked in."}, :status => 404 and return + else + @current_user.checkout + render :nothing => true, :status => 204 and return + end + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 9b67e65..ce53d43 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,6 +36,8 @@ class User < ActiveRecord::Base user_role.to_s == role.to_s end +### TODO methods below probably belong somewhere else + def total_hours ActsAsLoggable::Log.where( :loggable_type => self.class.to_s, :loggable_id => self.id).sum { |l| (l.end_date - l.start_date)/3600 }.round(2) end @@ -49,4 +51,35 @@ class User < ActiveRecord::Base .sum { |l| (l.end_date - l.start_date)/3600 } .round(2) end + + def checked_in? + log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") + checked = logs.where( log_action_id: log_action.id). + where("start_date >= ?", Time.zone.now.beginning_of_day). + where("start_date = end_date") + !checked.empty? + end + + def checkin + log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") + time = Time.now + logs.create( logger_id: self.id, + logger_type: self.class.to_s, + start_date: time, + end_date: time, + log_action_id: log_action.id, + log_action_type: log_action.class.to_s) + save + puts logs.inspect + end + + def checkout + log_action = ::ActsAsLoggable::UserAction.find_by_action("CHECKIN") + checked = logs.where( log_action_id: log_action.id). + where("start_date >= ?", Time.zone.now.beginning_of_day). + where("start_date = end_date").first + checked.end_date = Time.now + checked.save + puts logs.inspect + end end diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 6acf013..d275e3f 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -1,6 +1,6 @@ <%= stylesheet_link_tag "bootstrap_and_overrides", :media => "all" %> -