From aa84e46135d4f660c6a981f5d032cdf2a4b2f25e Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Wed, 16 Oct 2024 14:54:57 -0400 Subject: [PATCH 01/37] Added initial logging --- app/models/concerns/logging.rb | 28 ++++++++++++++++++++++++++++ app/models/user.rb | 1 + 2 files changed, 29 insertions(+) create mode 100644 app/models/concerns/logging.rb diff --git a/app/models/concerns/logging.rb b/app/models/concerns/logging.rb new file mode 100644 index 000000000..b471735bc --- /dev/null +++ b/app/models/concerns/logging.rb @@ -0,0 +1,28 @@ +module Logging + extend ActiveSupport::Concern + + included do + after_create :log_create + after_find :log_find + after_update :log_update + after_destroy :log_destroy + end + + private + + def log_create + Rails.logger.info("Created #{self.class.name}: #{self.attributes}") + end + + def log_find + Rails.logger.info("Found #{self.class.name}: #{self.attributes}") + end + + def log_update + Rails.logger.info("Updated #{self.class.name}: #{self.attributes}") + end + + def log_destroy + Rails.logger.info("Destroyed #{self.class.name}: #{self.attributes}") + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 87f138fe5..5bb1a05f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ApplicationRecord + include Logging has_secure_password after_initialize :set_defaults From 1e0ee7a0d7f6e6d48e9a82753ee0fa0afdf8440f Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Thu, 24 Oct 2024 12:26:07 -0400 Subject: [PATCH 02/37] Added old expertiza_logger file for formatting, updated logging file to use it --- app/models/concerns/logging.rb | 8 +++--- app/models/expertiza_logger.rb | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 app/models/expertiza_logger.rb diff --git a/app/models/concerns/logging.rb b/app/models/concerns/logging.rb index b471735bc..41900ee83 100644 --- a/app/models/concerns/logging.rb +++ b/app/models/concerns/logging.rb @@ -11,18 +11,18 @@ module Logging private def log_create - Rails.logger.info("Created #{self.class.name}: #{self.attributes}") + ExpertizaLogger.info("Created #{self.class.name}: #{self.attributes}") end def log_find - Rails.logger.info("Found #{self.class.name}: #{self.attributes}") + ExpertizaLogger.info("Found #{self.class.name}: #{self.attributes}") end def log_update - Rails.logger.info("Updated #{self.class.name}: #{self.attributes}") + ExpertizaLogger.info("Updated #{self.class.name}: #{self.attributes}") end def log_destroy - Rails.logger.info("Destroyed #{self.class.name}: #{self.attributes}") + ExpertizaLogger.info("Destroyed #{self.class.name}: #{self.attributes}") end end diff --git a/app/models/expertiza_logger.rb b/app/models/expertiza_logger.rb new file mode 100644 index 000000000..0cb0eca49 --- /dev/null +++ b/app/models/expertiza_logger.rb @@ -0,0 +1,51 @@ +# expertiza_logger.rb +class ExpertizaLogFormatter < Logger::Formatter + # This method is invoked when a log event occurs + def call(s, ts, pg, msg) + if msg.is_a?(LoggerMessage) + "TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[#{msg.oip}] RID=[#{msg.req_id}] CTR=[#{msg.generator}] UID=[#{msg.unity_id}] MSG=[#{filter(msg.message)}]\n" + else + "TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[] RID=[] CTR=[] UID=[] MSG=[#{filter(msg)}]\n" + end + end + + def filter(msg) + msg.tr("\n", ' ') + end +end + +class ExpertizaLogger + def self.info(message = nil) + @info_log ||= Logger.new(Rails.root.join('log', 'expertiza_info.log')) + add_formatter @info_log + @info_log.info(message) + end + + def self.warn(message = nil) + @warn_log ||= Logger.new(Rails.root.join('log', 'expertiza_warn.log')) + add_formatter @warn_log + @warn_log.warn(message) + end + + def self.error(message = nil) + @error_log ||= Logger.new(Rails.root.join('log', 'expertiza_error.log')) + add_formatter @error_log + @error_log.error(message) + end + + def self.fatal(message = nil) + @fatal_log ||= Logger.new(Rails.root.join('log', 'expertiza_fatal.log')) + add_formatter @fatal_log + @fatal_log.fatal(message) + end + + def self.debug(message = nil) + @debug_log ||= Logger.new(Rails.root.join('log', 'expertiza_debug.log')) + add_formatter @debug_log + @debug_log.debug(message) + end + + def self.add_formatter(log) + log.formatter ||= ExpertizaLogFormatter.new + end +end \ No newline at end of file From 01ccf9d49fc1eeabd9d0068bb5e355a6cc4160cb Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Thu, 24 Oct 2024 13:52:10 -0400 Subject: [PATCH 03/37] Added the LoggerMessage model, which is used by the new ExpertizaLogFormatter --- app/models/logger_message.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 app/models/logger_message.rb diff --git a/app/models/logger_message.rb b/app/models/logger_message.rb new file mode 100644 index 000000000..7e572f8e0 --- /dev/null +++ b/app/models/logger_message.rb @@ -0,0 +1,10 @@ +class LoggerMessage + attr_reader :generator, :unity_id, :message, :oip, :req_id + def initialize(generator, unity_id, message, req = nil) + @generator = generator + @unity_id = unity_id + @message = message + @oip = req.remote_ip if req + @req_id = req.uuid if req + end +end \ No newline at end of file From ceee53b4d9ed4281ac2f0201873a40b90bceb039 Mon Sep 17 00:00:00 2001 From: bjiang9 Date: Tue, 29 Oct 2024 12:54:03 -0400 Subject: [PATCH 04/37] Added the logging to AppRecord. All other model classes inherit from AppRecord and will be logged. Removed the logging statement from User as it inherits from AppRecord Student_Task.rb is the only class that does not extend off of any class. --- app/models/application_record.rb | 1 + app/models/user.rb | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index f6961453b..ee303827e 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,4 @@ class ApplicationRecord < ActiveRecord::Base + include Logging primary_abstract_class end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 5bb1a05f0..87f138fe5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,4 @@ class User < ApplicationRecord - include Logging has_secure_password after_initialize :set_defaults From 2c0db51cfd9cd7ba2c86b095eb5d5fcbd289c9a9 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Tue, 29 Oct 2024 15:27:58 -0400 Subject: [PATCH 05/37] Reimplemented misc logging statements from old Expertiza controllers --- app/controllers/api/v1/account_requests_controller.rb | 1 + app/controllers/api/v1/assignments_controller.rb | 3 +++ app/controllers/api/v1/bookmarks_controller.rb | 4 ++++ app/controllers/api/v1/invitations_controller.rb | 1 + app/controllers/api/v1/signed_up_teams_controller.rb | 1 + app/controllers/api/v1/users_controller.rb | 1 + app/controllers/authentication_controller.rb | 4 ++++ 7 files changed, 15 insertions(+) diff --git a/app/controllers/api/v1/account_requests_controller.rb b/app/controllers/api/v1/account_requests_controller.rb index f4854cbe5..c847abfd1 100644 --- a/app/controllers/api/v1/account_requests_controller.rb +++ b/app/controllers/api/v1/account_requests_controller.rb @@ -22,6 +22,7 @@ def create end render json: response, status: :created else + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: @account_request.errors, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound => e diff --git a/app/controllers/api/v1/assignments_controller.rb b/app/controllers/api/v1/assignments_controller.rb index e28ad573f..59aa32bac 100644 --- a/app/controllers/api/v1/assignments_controller.rb +++ b/app/controllers/api/v1/assignments_controller.rb @@ -16,6 +16,7 @@ def show def create assignment = Assignment.new(assignment_params) if assignment.save + ExpertizaLogger.info "Assignment created: #{assignment.as_json}" render json: assignment, status: :created else render json: assignment.errors, status: :unprocessable_entity @@ -37,8 +38,10 @@ def destroy assignment = Assignment.find_by(id: params[:id]) if assignment if assignment.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment #{assignment.id} was deleted.", request) render json: { message: "Assignment deleted successfully!" }, status: :ok else + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Failed to delete assignment #{$ERROR_INFO}", request) render json: { error: "Failed to delete assignment", details: assignment.errors.full_messages }, status: :unprocessable_entity end else diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb index 93964a254..a4f06af75 100644 --- a/app/controllers/api/v1/bookmarks_controller.rb +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -26,8 +26,10 @@ def create @bookmark = Bookmark.new(bookmark_params) @bookmark.user_id = @current_user.id @bookmark.save! + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully created!', request) render json: @bookmark, status: :created and return rescue ActiveRecord::RecordInvalid + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -37,6 +39,7 @@ def create def update @bookmark = Bookmark.find(params[:id]) if @bookmark.update(update_bookmark_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully updated!', request) render json: @bookmark, status: :ok else render json: @bookmark.errors.full_messages, status: :unprocessable_entity @@ -49,6 +52,7 @@ def destroy begin @bookmark = Bookmark.find(params[:id]) @bookmark.delete + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully deleted!', request) rescue ActiveRecord::RecordNotFound render json: $ERROR_INFO.to_s, status: :not_found and return end diff --git a/app/controllers/api/v1/invitations_controller.rb b/app/controllers/api/v1/invitations_controller.rb index 1b4d88f1e..250136e8f 100644 --- a/app/controllers/api/v1/invitations_controller.rb +++ b/app/controllers/api/v1/invitations_controller.rb @@ -15,6 +15,7 @@ def create @invitation.send_invite_email render json: @invitation, status: :created else + ExpertizaLogger.error LoggerMessage.new('', @invitation.to_user.name, 'Student was already invited') render json: { error: @invitation.errors }, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/signed_up_teams_controller.rb b/app/controllers/api/v1/signed_up_teams_controller.rb index 97ada5a24..4bd8681f4 100644 --- a/app/controllers/api/v1/signed_up_teams_controller.rb +++ b/app/controllers/api/v1/signed_up_teams_controller.rb @@ -18,6 +18,7 @@ def update if @signed_up_team.update(signed_up_teams_params) render json: { message: "The team has been updated successfully. " }, status: 200 else + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: @signed_up_team.errors, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 086376556..749859cd4 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,4 +1,5 @@ class Api::V1::UsersController < ApplicationController + skip_before_action :authenticate_request!, only: [:index, :show, :create, :update, :destroy, :institution_users, :managed_users, :role_users] rescue_from ActiveRecord::RecordNotFound, with: :user_not_found rescue_from ActionController::ParameterMissing, with: :parameter_missing diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb index 32c96a909..e1b0c629a 100644 --- a/app/controllers/authentication_controller.rb +++ b/app/controllers/authentication_controller.rb @@ -11,8 +11,12 @@ def login payload = { id: user.id, name: user.name, full_name: user.full_name, role: user.role.name, institution_id: user.institution.id } token = JsonWebToken.encode(payload, 24.hours.from_now) + + ExpertizaLogger.info LoggerMessage.new('', user.name, 'Login successful') + render json: { token: }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, '', 'Failed login attempt. Invalid username/password', request) render json: { error: 'Invalid username / password' }, status: :unauthorized end end From 6d34b6a1d8c7d06ce4b2da1210e8ab5da8960b98 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Tue, 29 Oct 2024 15:34:32 -0400 Subject: [PATCH 06/37] Changed invitations_controller logging statement to log w/ inviting student unity id instead of invited student's --- app/controllers/api/v1/invitations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/invitations_controller.rb b/app/controllers/api/v1/invitations_controller.rb index 250136e8f..1f13ba6b9 100644 --- a/app/controllers/api/v1/invitations_controller.rb +++ b/app/controllers/api/v1/invitations_controller.rb @@ -15,7 +15,7 @@ def create @invitation.send_invite_email render json: @invitation, status: :created else - ExpertizaLogger.error LoggerMessage.new('', @invitation.to_user.name, 'Student was already invited') + ExpertizaLogger.error LoggerMessage.new('', @invitation.from_user.name, 'Student was already invited') render json: { error: @invitation.errors }, status: :unprocessable_entity end end From 6634e305d174d37ebf32d9375125b475b329defe Mon Sep 17 00:00:00 2001 From: bjiang9 Date: Tue, 29 Oct 2024 19:58:47 -0400 Subject: [PATCH 07/37] First attempt Rspec log tests One post call to check for logging Has not run locally --- spec/logs/user_logs_spec.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 spec/logs/user_logs_spec.rb diff --git a/spec/logs/user_logs_spec.rb b/spec/logs/user_logs_spec.rb new file mode 100644 index 000000000..361ed384e --- /dev/null +++ b/spec/logs/user_logs_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +RSpec.describe User do + let(:user) { build(:user, name: "Jane", email: "jdoe@ncsu.edu", full_name: "Jane Doe") } + let(:params) do { user: { + "name": "Jane Doe", + "full_name": "Jane Doe", + "email": "jane.doe@example.com", + "role_id": 1, + "institution_id": 1, + "password": "password", + "password_confirmation": "password" } } + end + + # https://everydayrails.com/2020/08/10/rails-log-message-testing-rspec + # Rails testing + it "logs when user is created" do + allow(Rails.logger).to receive(:info) + expect(Rails.logger).to receive(:info) + + post :create, params: params + end + +end From 4fddae581458bf3c0313e5045d37f2c809502052 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 29 Oct 2024 20:18:23 -0400 Subject: [PATCH 08/37] Added comments to describe Logger functionality --- app/models/concerns/logging.rb | 5 +++++ app/models/expertiza_logger.rb | 10 +++++++++- app/models/logger_message.rb | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/logging.rb b/app/models/concerns/logging.rb index 41900ee83..25bd78352 100644 --- a/app/models/concerns/logging.rb +++ b/app/models/concerns/logging.rb @@ -1,6 +1,11 @@ +# Logging concern to automatically log model CRUD operations to the database. +# It can be enabled for a given model by using "include Logging". +# It will then automatically log all CRUD operations for the model. + module Logging extend ActiveSupport::Concern + # Set up hooks to call logging when CRUD operations are performed. included do after_create :log_create after_find :log_find diff --git a/app/models/expertiza_logger.rb b/app/models/expertiza_logger.rb index 0cb0eca49..5eacec3af 100644 --- a/app/models/expertiza_logger.rb +++ b/app/models/expertiza_logger.rb @@ -1,6 +1,10 @@ # expertiza_logger.rb +# This file contains the formatter used for the ExpertizaLogger and the ExpertizaLogger. +# ExpertizaLogger contains 5 levels of logging: info, warn, error, fatal, and debug. + +# ExpertizaLogFormatter formats the logs to ensure consistent log message formatting. class ExpertizaLogFormatter < Logger::Formatter - # This method is invoked when a log event occurs + # This method is invoked when a log event occurs and formats the message def call(s, ts, pg, msg) if msg.is_a?(LoggerMessage) "TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[#{msg.oip}] RID=[#{msg.req_id}] CTR=[#{msg.generator}] UID=[#{msg.unity_id}] MSG=[#{filter(msg.message)}]\n" @@ -9,11 +13,15 @@ def call(s, ts, pg, msg) end end + # Filter out the newline characters in the message and replace with a space character. def filter(msg) msg.tr("\n", ' ') end end +# ExpertizaLogger providing the logging levels and functionality. +# Each level creates its own file (expertiza_info.log or similar) and +# uses the ExpertizaLogFormatter to format and then print the message. class ExpertizaLogger def self.info(message = nil) @info_log ||= Logger.new(Rails.root.join('log', 'expertiza_info.log')) diff --git a/app/models/logger_message.rb b/app/models/logger_message.rb index 7e572f8e0..a6af5d61d 100644 --- a/app/models/logger_message.rb +++ b/app/models/logger_message.rb @@ -1,3 +1,4 @@ +# LoggerMessage to provide the fields expected by the ExpertizaLogFormatter class LoggerMessage attr_reader :generator, :unity_id, :message, :oip, :req_id def initialize(generator, unity_id, message, req = nil) From 5e5fc14ec46c95c934202911b141c84b3b158599 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 29 Oct 2024 21:40:04 -0400 Subject: [PATCH 09/37] Fixed Rspec describe to get logging user test working --- spec/logs/user_logs_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/logs/user_logs_spec.rb b/spec/logs/user_logs_spec.rb index 361ed384e..0f583b007 100644 --- a/spec/logs/user_logs_spec.rb +++ b/spec/logs/user_logs_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe User do +RSpec.describe Api::V1::UsersController, type: :controller do let(:user) { build(:user, name: "Jane", email: "jdoe@ncsu.edu", full_name: "Jane Doe") } let(:params) do { user: { "name": "Jane Doe", From 0d2155afeeebd3c9360106cae3aa8fed47b3fac6 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Tue, 29 Oct 2024 22:18:29 -0400 Subject: [PATCH 10/37] Added auth back to the users controller, removed it by mistake earlier... --- app/controllers/api/v1/users_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 749859cd4..086376556 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,5 +1,4 @@ class Api::V1::UsersController < ApplicationController - skip_before_action :authenticate_request!, only: [:index, :show, :create, :update, :destroy, :institution_users, :managed_users, :role_users] rescue_from ActiveRecord::RecordNotFound, with: :user_not_found rescue_from ActionController::ParameterMissing, with: :parameter_missing From 59a4e15410a4ed454634659f16db6c8f2a4cea99 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 29 Oct 2024 22:27:07 -0400 Subject: [PATCH 11/37] Non-working test for Cam to work on --- spec/logs/user_logs_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/logs/user_logs_spec.rb b/spec/logs/user_logs_spec.rb index 0f583b007..91ef8f3e6 100644 --- a/spec/logs/user_logs_spec.rb +++ b/spec/logs/user_logs_spec.rb @@ -21,4 +21,11 @@ post :create, params: params end + it "logs when user is created" do + allow(Rails.logger).to receive(:warn) + expect(Rails.logger).to receive(:warn) + params[:user][:password] = "mismatch" + post :create, params: params + end + end From d8be0ae61feae27ae3b03837fd28c9e830140a26 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 29 Oct 2024 23:58:03 -0400 Subject: [PATCH 12/37] Removed errant test for warning logs --- spec/logs/user_logs_spec.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/spec/logs/user_logs_spec.rb b/spec/logs/user_logs_spec.rb index 91ef8f3e6..0f583b007 100644 --- a/spec/logs/user_logs_spec.rb +++ b/spec/logs/user_logs_spec.rb @@ -21,11 +21,4 @@ post :create, params: params end - it "logs when user is created" do - allow(Rails.logger).to receive(:warn) - expect(Rails.logger).to receive(:warn) - params[:user][:password] = "mismatch" - post :create, params: params - end - end From 5e2e16fb2696c8dea832ad66b19c57ae67d27d14 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 29 Oct 2024 23:59:08 -0400 Subject: [PATCH 13/37] Removed line to disable authorization --- app/controllers/api/v1/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 749859cd4..73e4a4a39 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,5 +1,4 @@ class Api::V1::UsersController < ApplicationController - skip_before_action :authenticate_request!, only: [:index, :show, :create, :update, :destroy, :institution_users, :managed_users, :role_users] rescue_from ActiveRecord::RecordNotFound, with: :user_not_found rescue_from ActionController::ParameterMissing, with: :parameter_missing @@ -16,6 +15,7 @@ def show # POST /users def create + Rails.logger.info("test") # Add default password for a user if the password is not provided params[:user][:password] ||= 'password' user = User.new(user_params) From df54da063f6c7e05c7dcc59d504c267205645063 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Wed, 30 Oct 2024 00:00:26 -0400 Subject: [PATCH 14/37] Removed logging line used for manual testing --- app/controllers/api/v1/users_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 73e4a4a39..086376556 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -15,7 +15,6 @@ def show # POST /users def create - Rails.logger.info("test") # Add default password for a user if the password is not provided params[:user][:password] ||= 'password' user = User.new(user_params) From 3afd7639e6c21f7a13a45c876b7a673bc17197cf Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Mon, 11 Nov 2024 09:59:56 -0500 Subject: [PATCH 15/37] Improved logging to use LoggerMessages, consistent user identification, and additional codepaths --- app/controllers/api/v1/account_requests_controller.rb | 3 ++- app/controllers/api/v1/assignments_controller.rb | 4 ++-- app/controllers/api/v1/bookmarks_controller.rb | 2 +- app/controllers/api/v1/invitations_controller.rb | 2 +- app/controllers/api/v1/signed_up_teams_controller.rb | 2 +- app/controllers/authentication_controller.rb | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/account_requests_controller.rb b/app/controllers/api/v1/account_requests_controller.rb index c847abfd1..2e1258e77 100644 --- a/app/controllers/api/v1/account_requests_controller.rb +++ b/app/controllers/api/v1/account_requests_controller.rb @@ -18,11 +18,12 @@ def create if @account_request.save response = { account_request: @account_request } if User.find_by(email: @account_request.email) + ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Account requested with duplicate email: #{@account_request.email}", request) response[:warnings] = 'WARNING: User with this email already exists!' end render json: response, status: :created else - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: @account_request.errors, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound => e diff --git a/app/controllers/api/v1/assignments_controller.rb b/app/controllers/api/v1/assignments_controller.rb index 59aa32bac..a7c5ce2c8 100644 --- a/app/controllers/api/v1/assignments_controller.rb +++ b/app/controllers/api/v1/assignments_controller.rb @@ -16,7 +16,7 @@ def show def create assignment = Assignment.new(assignment_params) if assignment.save - ExpertizaLogger.info "Assignment created: #{assignment.as_json}" + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment created: #{assignment.as_json}", request) render json: assignment, status: :created else render json: assignment.errors, status: :unprocessable_entity @@ -41,7 +41,7 @@ def destroy ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment #{assignment.id} was deleted.", request) render json: { message: "Assignment deleted successfully!" }, status: :ok else - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Failed to delete assignment #{$ERROR_INFO}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete assignment #{$ERROR_INFO}", request) render json: { error: "Failed to delete assignment", details: assignment.errors.full_messages }, status: :unprocessable_entity end else diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb index a4f06af75..e39f703b7 100644 --- a/app/controllers/api/v1/bookmarks_controller.rb +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -29,7 +29,7 @@ def create ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully created!', request) render json: @bookmark, status: :created and return rescue ActiveRecord::RecordInvalid - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/invitations_controller.rb b/app/controllers/api/v1/invitations_controller.rb index 1f13ba6b9..d1325f89b 100644 --- a/app/controllers/api/v1/invitations_controller.rb +++ b/app/controllers/api/v1/invitations_controller.rb @@ -15,7 +15,7 @@ def create @invitation.send_invite_email render json: @invitation, status: :created else - ExpertizaLogger.error LoggerMessage.new('', @invitation.from_user.name, 'Student was already invited') + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, 'Student was already invited') render json: { error: @invitation.errors }, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/signed_up_teams_controller.rb b/app/controllers/api/v1/signed_up_teams_controller.rb index 4bd8681f4..e28f875a4 100644 --- a/app/controllers/api/v1/signed_up_teams_controller.rb +++ b/app/controllers/api/v1/signed_up_teams_controller.rb @@ -18,7 +18,7 @@ def update if @signed_up_team.update(signed_up_teams_params) render json: { message: "The team has been updated successfully. " }, status: 200 else - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: @signed_up_team.errors, status: :unprocessable_entity end end diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb index e1b0c629a..785409474 100644 --- a/app/controllers/authentication_controller.rb +++ b/app/controllers/authentication_controller.rb @@ -12,11 +12,11 @@ def login institution_id: user.institution.id } token = JsonWebToken.encode(payload, 24.hours.from_now) - ExpertizaLogger.info LoggerMessage.new('', user.name, 'Login successful') + ExpertizaLogger.info LoggerMessage.new(controller_name, user.name, 'Login successful') render json: { token: }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, '', 'Failed login attempt. Invalid username/password', request) + ExpertizaLogger.error LoggerMessage.new(controller_name, user&.name || '', 'Failed login attempt. Invalid username/password', request) render json: { error: 'Invalid username / password' }, status: :unauthorized end end From db69acdb6193c96ad316ce068ece2e06c86bacbc Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 19 Nov 2024 10:02:02 -0500 Subject: [PATCH 16/37] Added tests for ExpertizaLogger and ExpertizaLogFormatter --- spec/logs/expertiza_log_formatter_spec.rb | 49 ++++++++++++++++++ spec/logs/expertiza_logger_spec.rb | 63 +++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 spec/logs/expertiza_log_formatter_spec.rb create mode 100644 spec/logs/expertiza_logger_spec.rb diff --git a/spec/logs/expertiza_log_formatter_spec.rb b/spec/logs/expertiza_log_formatter_spec.rb new file mode 100644 index 000000000..4dd012040 --- /dev/null +++ b/spec/logs/expertiza_log_formatter_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' +require_relative '../../app/models/expertiza_logger' + +RSpec.describe ExpertizaLogFormatter do + let(:formatter) { ExpertizaLogFormatter.new } + let(:timestamp) { Time.now } + let(:severity) { 'INFO' } + let(:progname) { 'Expertiza' } + + context 'when the message is a plain string' do + let(:message) { 'Test message with more text' } + + it 'formats the message correctly' do + formatted_message = formatter.call(severity, timestamp, progname, message) + expect(formatted_message).to include("TST=[#{timestamp}] SVT=[#{severity}] PNM=[#{progname}] OIP=[] RID=[] CTR=[] UID=[] MSG=[Test message with more text]") + end + end + + context 'when the message is a plain string with newline' do + let(:message) { "Test message with newline\nto more text" } + + it 'formats the message correctly' do + formatted_message = formatter.call(severity, timestamp, progname, message) + expect(formatted_message).to include("TST=[#{timestamp}] SVT=[#{severity}] PNM=[#{progname}] OIP=[] RID=[] CTR=[] UID=[] MSG=[Test message with newline to more text]") + end + end + + context 'when the message is a LoggerMessage object' do + let(:logger_message) do + LoggerMessage.new('gen1', 'unityid1', 'Test message') + end + + it 'formats the message correctly' do + formatted_message = formatter.call(severity, timestamp, progname, logger_message) + expect(formatted_message).to include("TST=[#{timestamp}] SVT=[#{severity}] PNM=[#{progname}] OIP=[] RID=[] CTR=[gen1] UID=[unityid1] MSG=[Test message]") + end + end + + context 'when the message is a LoggerMessage object with request' do + let(:logger_message) do + LoggerMessage.new('gen1', 'unityid1', 'Test message', double("request", remote_ip: "192.168.1.1", uuid: "12345")) + end + + it 'formats the message correctly' do + formatted_message = formatter.call(severity, timestamp, progname, logger_message) + expect(formatted_message).to include("TST=[#{timestamp}] SVT=[#{severity}] PNM=[#{progname}] OIP=[192.168.1.1] RID=[12345] CTR=[gen1] UID=[unityid1] MSG=[Test message]") + end + end +end diff --git a/spec/logs/expertiza_logger_spec.rb b/spec/logs/expertiza_logger_spec.rb new file mode 100644 index 000000000..9cf5a47be --- /dev/null +++ b/spec/logs/expertiza_logger_spec.rb @@ -0,0 +1,63 @@ +RSpec.describe ExpertizaLogger do + let(:message) { 'Test message' } + let(:timestamp) { Time.now } + + # Create a mock logger instance + let(:mock_logger) { instance_double(Logger) } + + before do + allow(Logger).to receive(:new).and_return(mock_logger) + allow(mock_logger).to receive(:formatter) + allow(mock_logger).to receive(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + allow(mock_logger).to receive(:info) + allow(mock_logger).to receive(:warn) + allow(mock_logger).to receive(:error) + allow(mock_logger).to receive(:fatal) + allow(mock_logger).to receive(:debug) + end + + describe '.info' do + it 'logs the message to the correct file with info severity' do + ExpertizaLogger.info(message) + expect(Logger).to have_received(:new).with(Rails.root.join('log', 'expertiza_info.log')) + expect(mock_logger).to have_received(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + expect(mock_logger).to have_received(:info).with(message) + end + end + + describe '.warn' do + it 'logs the message to the correct file with warn severity' do + ExpertizaLogger.warn(message) + expect(Logger).to have_received(:new).with(Rails.root.join('log', 'expertiza_warn.log')) + expect(mock_logger).to have_received(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + expect(mock_logger).to have_received(:warn).with(message) + end + end + + describe '.error' do + it 'logs the message to the correct file with error severity' do + ExpertizaLogger.error(message) + expect(Logger).to have_received(:new).with(Rails.root.join('log', 'expertiza_error.log')) + expect(mock_logger).to have_received(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + expect(mock_logger).to have_received(:error).with(message) + end + end + + describe '.fatal' do + it 'logs the message to the correct file with fatal severity' do + ExpertizaLogger.fatal(message) + expect(Logger).to have_received(:new).with(Rails.root.join('log', 'expertiza_fatal.log')) + expect(mock_logger).to have_received(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + expect(mock_logger).to have_received(:fatal).with(message) + end + end + + describe '.debug' do + it 'logs the message to the correct file with debug severity' do + ExpertizaLogger.debug(message) + expect(Logger).to have_received(:new).with(Rails.root.join('log', 'expertiza_debug.log')) + expect(mock_logger).to have_received(:formatter=).with(an_instance_of(ExpertizaLogFormatter)) + expect(mock_logger).to have_received(:debug).with(message) + end + end +end From 187f08cd6a06987743cecbc434e64824cf7f4cce Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Tue, 26 Nov 2024 08:33:55 -0500 Subject: [PATCH 17/37] Added some new logging statements to the account requests controller --- app/controllers/api/v1/account_requests_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/api/v1/account_requests_controller.rb b/app/controllers/api/v1/account_requests_controller.rb index 2e1258e77..243dd5a21 100644 --- a/app/controllers/api/v1/account_requests_controller.rb +++ b/app/controllers/api/v1/account_requests_controller.rb @@ -3,12 +3,14 @@ class Api::V1::AccountRequestsController < ApplicationController # GET /account_requests/pending def pending_requests @account_requests = AccountRequest.where(status: 'Under Review').order('created_at DESC') + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@account_requests.count} pending account requests.", request) render json: @account_requests, status: :ok end # GET /account_requests/processed def processed_requests @account_requests = AccountRequest.where.not(status: 'Under Review').order('updated_at DESC') + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@account_requests.count} processed account requests.", request) render json: @account_requests, status: :ok end @@ -18,25 +20,33 @@ def create if @account_request.save response = { account_request: @account_request } if User.find_by(email: @account_request.email) + # Logging a warning if a user with the same email already exists ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Account requested with duplicate email: #{@account_request.email}", request) response[:warnings] = 'WARNING: User with this email already exists!' end + + # Logging the successful creation of the account request + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Successfully created account request with ID: #{@account_request.id}.", request) render json: response, status: :created else ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) render json: @account_request.errors, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Record not found: #{e.message}", request) render json: { error: e.message }, status: :not_found rescue ActionController::ParameterMissing => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing: #{e.message}", request) render json: { error: e.message }, status: :parameter_missing rescue StandardError => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "An error occurred: #{e.message}", request) render json: { error: e.message }, status: :unprocessable_entity end # GET /account_requests/:id def show @account_request = AccountRequest.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched account request with ID: #{@account_request.id}.", request) render json: @account_request, status: :ok rescue ActiveRecord::RecordNotFound => e render json: { error: e.message }, status: :not_found From 8c3dbaaa7fc2d243c44d3da19762cfe0c664a48f Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 26 Nov 2024 09:44:15 -0500 Subject: [PATCH 18/37] Added some logging to questions_controller --- app/controllers/api/v1/questions_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/api/v1/questions_controller.rb b/app/controllers/api/v1/questions_controller.rb index 10c26875d..d9671bcc3 100644 --- a/app/controllers/api/v1/questions_controller.rb +++ b/app/controllers/api/v1/questions_controller.rb @@ -14,6 +14,7 @@ def show @question = Question.find(params[:id]) render json: @question, status: :ok rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Unable to find Question with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found end end @@ -50,9 +51,11 @@ def create if question.save render json: question, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question not able to be saved: #{question.as_json}", request) render json: question.errors.full_messages.to_sentence, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not able to found with ID: #{params[:questionnaire_id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end @@ -63,7 +66,9 @@ def destroy begin @question = Question.find(params[:id]) @question.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Destroyed Question: #{@question.as_json}", request) rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -74,8 +79,10 @@ def show_all begin @questionnaire = Questionnaire.find(params[:id]) @questions = @questionnaire.questions + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@questions.count} Questions for Questionnaire with ID: #{params[:id]}", request) render json: @questions, status: :ok rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found end end @@ -88,12 +95,15 @@ def delete_all begin questionnaire = Questionnaire.find(params[:id]) if questionnaire.questions.empty? + ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Questions attempted to be destroyed for Questionnaire with ID #{params[:id]}, but found to already not have any questions", request) render json: "No questions associated with questionnaire ID #{params[:id]}.", status: :unprocessable_entity and return end questionnaire.questions.destroy_all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Questionnaire with ID #{params[:id]} had all questions deleted", request) render json: "All questions for questionnaire ID #{params[:id]} have been deleted.", status: :ok rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire unable to be found with ID: #{params[:id]}", request) render json: "Questionnaire ID #{params[:id]} not found.", status: :not_found and return end end @@ -106,9 +116,11 @@ def update if @question.update(question_params) render json: @question, status: :ok and return else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be updated with: #{question_params}", request) render json: "Failed to update the question.", status: :unprocessable_entity and return end rescue ActiveRecord::RecordNotFound + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end From ae4e4b0c5408906c6917d3f7b1bfba1fd95a51a4 Mon Sep 17 00:00:00 2001 From: bjiang9 Date: Fri, 29 Nov 2024 20:08:42 -0500 Subject: [PATCH 19/37] Added Log Messages to Course Controller. Added Messages to: - Create - Destroy - Add TA - Remove TA - Copy --- app/controllers/api/v1/courses_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/api/v1/courses_controller.rb b/app/controllers/api/v1/courses_controller.rb index e4494bc99..164f1bc49 100644 --- a/app/controllers/api/v1/courses_controller.rb +++ b/app/controllers/api/v1/courses_controller.rb @@ -21,8 +21,10 @@ def show def create course = Course.new(course_params) if course.save + ExpertizaLogger.info LoggerMessage.new( controller_name, session[:user].name, "Course created: #{course.as_json}.", request ) render json: course, status: :created else + ExpertizaLogger.warn LoggerMessage.new( controller_name, session[:user].name, "Error creating new Course: #{$ERROR_INFO}", request ) render json: course.errors, status: :unprocessable_entity end end @@ -31,8 +33,10 @@ def create # Update a course def update if @course.update(course_params) + ExpertizaLogger.info LoggerMessage.new( controller_name, session[:user].name, "Course Information Modified: #{course.as_json}.", request ) render json: @course, status: :ok else + ExpertizaLogger.warn LoggerMessage.new( controller_name, session[:user].name, "Course Failed to Update: #{$ERROR_INFO}.", request ) render json: @course.errors, status: :unprocessable_entity end end @@ -41,6 +45,7 @@ def update # Delete a course def destroy @course.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course with id #{params[:id]}, deleted", request) render json: { message: "Course with id #{params[:id]}, deleted" }, status: :no_content end @@ -49,8 +54,10 @@ def add_ta user = User.find_by(id: params[:ta_id]) result = @course.add_ta(user) if result[:success] + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course with id #{params[:id]}, added new TA #{params[:ta_id]}.", request) render json: result[:data], status: :created else + ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Error added TA #{params[:ta_id]} to Course #{params[:id]}.", request) render json: { status: "error", message: result[:message] }, status: :bad_request end end @@ -65,6 +72,7 @@ def view_tas def remove_ta result = @course.remove_ta(params[:ta_id]) if result[:success] + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "TA #{params[:ta_id]} removed from Course #{params[:id]}.", request) render json: { message: "The TA #{result[:ta_name]} has been removed." }, status: :ok else render json: { status: "error", message: result[:message] }, status: :not_found @@ -76,6 +84,7 @@ def copy # existing_course = Course.find(params[:id]) success = @course.copy_course if success + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course #{params[:id]} copied successfully.", request) render json: { message: "The course #{@course.name} has been successfully copied" }, status: :ok else render json: { message: "The course was not able to be copied" }, status: :unprocessable_entity From e8629b0648f5b93a8d4bafba127e893db46d9f2d Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 13:11:20 -0500 Subject: [PATCH 20/37] Removed extra space around LoggerMessage.new(...) params for consistency --- app/controllers/api/v1/courses_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/courses_controller.rb b/app/controllers/api/v1/courses_controller.rb index 164f1bc49..af5d446db 100644 --- a/app/controllers/api/v1/courses_controller.rb +++ b/app/controllers/api/v1/courses_controller.rb @@ -21,10 +21,10 @@ def show def create course = Course.new(course_params) if course.save - ExpertizaLogger.info LoggerMessage.new( controller_name, session[:user].name, "Course created: #{course.as_json}.", request ) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course created: #{course.as_json}.", request) render json: course, status: :created else - ExpertizaLogger.warn LoggerMessage.new( controller_name, session[:user].name, "Error creating new Course: #{$ERROR_INFO}", request ) + ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Error creating new Course: #{$ERROR_INFO}", request) render json: course.errors, status: :unprocessable_entity end end @@ -33,10 +33,10 @@ def create # Update a course def update if @course.update(course_params) - ExpertizaLogger.info LoggerMessage.new( controller_name, session[:user].name, "Course Information Modified: #{course.as_json}.", request ) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course Information Modified: #{course.as_json}.", request) render json: @course, status: :ok else - ExpertizaLogger.warn LoggerMessage.new( controller_name, session[:user].name, "Course Failed to Update: #{$ERROR_INFO}.", request ) + ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Course Failed to Update: #{$ERROR_INFO}.", request) render json: @course.errors, status: :unprocessable_entity end end From ed18dfe5cbcd1fe5a5a828fdcb1e09dd438fabab Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 14:46:45 -0500 Subject: [PATCH 21/37] Added more log messages for the account request controller --- .../api/v1/account_requests_controller.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/account_requests_controller.rb b/app/controllers/api/v1/account_requests_controller.rb index 243dd5a21..ac56f4b34 100644 --- a/app/controllers/api/v1/account_requests_controller.rb +++ b/app/controllers/api/v1/account_requests_controller.rb @@ -49,6 +49,7 @@ def show ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched account request with ID: #{@account_request.id}.", request) render json: @account_request, status: :ok rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -57,14 +58,19 @@ def show def update @account_request = AccountRequest.find(params[:id]) @account_request.update(account_request_params) + + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated account request with ID: #{@account_request.id}, Status: #{@account_request.status}.", request) + if @account_request.status == 'Approved' create_approved_user else render json: @account_request, status: :ok end rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found for update: #{e.message}", request) render json: { error: e.message }, status: :not_found rescue StandardError => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "An error occurred while updating account request: #{e.message}", request) render json: { error: e.message }, status: :unprocessable_entity end @@ -72,9 +78,11 @@ def update def destroy @account_request = AccountRequest.find(params[:id]) @account_request.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted account request with ID: #{@account_request.id}.", request) render json: { message: 'Account Request deleted' }, status: :no_content rescue ActiveRecord::RecordNotFound => e - render json: { error: e.message }, status: :not_found + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found for deletion: #{e.message}", request) + render json: { error: e.message }, status: :not_found end private @@ -86,6 +94,7 @@ def account_request_params params[:account_request][:status] = 'Under Review' # For Approval or Rejection of an existing request, raise error if user sends a status other than Approved or Rejected elsif !['Approved', 'Rejected'].include?(params[:account_request][:status]) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invalid status provided: #{params[:account_request][:status]}", request) raise StandardError, 'Status can only be Approved or Rejected' end params.require(:account_request).permit(:username, :full_name, :email, :status, :introduction, :role_id, :institution_id) @@ -95,8 +104,11 @@ def account_request_params def create_approved_user @new_user = User.new(name: @account_request.username, role_id: @account_request.role_id, institution_id: @account_request.institution_id, fullname: @account_request.full_name, email: @account_request.email, password: 'password') if @new_user.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Approved account request and created user with ID: #{@new_user.id}.", request) render json: { success: 'Account Request Approved and User successfully created.', user: @new_user}, status: :ok else + errors = @new_user.errors.full_messages.join(', ') + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create user from approved account request ID: #{@account_request.id}. Errors: #{errors}", request) render json: @new_user.errors, status: :unprocessable_entity end end From c6e47f5447289d910b77c59c8ecd2483a14ea975 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 15:11:37 -0500 Subject: [PATCH 22/37] Added logging statements to the assignments controller --- .../api/v1/assignments_controller.rb | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/assignments_controller.rb b/app/controllers/api/v1/assignments_controller.rb index a7c5ce2c8..554eb372f 100644 --- a/app/controllers/api/v1/assignments_controller.rb +++ b/app/controllers/api/v1/assignments_controller.rb @@ -3,12 +3,16 @@ class Api::V1::AssignmentsController < ApplicationController # GET /api/v1/assignments def index assignments = Assignment.all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all assignments.", request) render json: assignments end # GET /api/v1/assignments/:id def show assignment = Assignment.find(params[:id]) + # For now, just logging success - if error checking is added in the future, please add a log message for that with + # ExpertizaLogger.error + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched assignment with ID: #{assignment.id}.", request) render json: assignment end @@ -19,6 +23,7 @@ def create ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment created: #{assignment.as_json}", request) render json: assignment, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create assignment: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -27,8 +32,10 @@ def create def update assignment = Assignment.find(params[:id]) if assignment.update(assignment_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated assignment with ID: #{assignment.id}.", request) render json: assignment, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -45,6 +52,7 @@ def destroy render json: { error: "Failed to delete assignment", details: assignment.errors.full_messages }, status: :unprocessable_entity end else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for deletion with ID: #{params[:id]}", request) render json: { error: "Assignment not found" }, status: :not_found end end @@ -53,12 +61,15 @@ def destroy def add_participant assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for adding participant. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else new_participant = assignment.add_participant(params[:user_id]) if new_participant.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Added participant with User ID: #{params[:user_id]} to assignment ID: #{assignment.id}.", request) render json: new_participant, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to add participant to assignment ID: #{assignment.id}. Errors: #{new_participant.errors.full_messages.join(', ')}", request) render json: new_participant.errors, status: :unprocessable_entity end end @@ -71,12 +82,15 @@ def remove_participant if user && assignment assignment.remove_participant(user.id) if assignment.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Removed participant with User ID: #{user.id} from assignment ID: #{assignment.id}.", request) render json: { message: "Participant removed successfully!" }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to remove participant from assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end else not_found_message = user ? "Assignment not found" : "User not found" + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "#{not_found_message} while removing participant.", request) render json: { error: not_found_message }, status: :not_found end end @@ -86,12 +100,15 @@ def remove_participant def remove_assignment_from_course assignment = Assignment.find(params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for removing from course. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else assignment = assignment.remove_assignment_from_course if assignment.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Removed assignment ID: #{assignment.id} from its course.", request) render json: assignment , status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to remove assignment ID: #{assignment.id} from course. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -105,12 +122,15 @@ def assign_course if assignment && course assignment = assignment.assign_course(course.id) if assignment.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assigned course ID: #{course.id} to assignment ID: #{assignment.id}.", request) render json: assignment, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to assign course to assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end else not_found_message = course ? "Assignment not found" : "Course not found" + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "#{not_found_message} while assigning course.", request) render json: { error: not_found_message }, status: :not_found end end @@ -119,12 +139,15 @@ def assign_course def copy_assignment assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for copying. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else new_assignment = assignment.copy if new_assignment.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Copied assignment ID: #{assignment.id} to new assignment ID: #{new_assignment.id}.", request) render json: new_assignment, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to copy assignment ID: #{assignment.id}. Errors: #{new_assignment.errors.full_messages.join(', ')}", request) render json :new_assignment.errors, status: :unprocessable_entity end end @@ -135,8 +158,10 @@ def copy_assignment def show_assignment_details assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for showing details. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched details for assignment ID: #{assignment.id}.", request) render json: { id: assignment.id, name: assignment.name, @@ -153,9 +178,12 @@ def show_assignment_details def has_topics assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking topics. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else - render json: assignment.topics?, status: :ok + result = assignment.topics? + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked topics for assignment ID: #{assignment.id}. Has topics: #{result}", request) + render json: result, status: :ok end end @@ -164,9 +192,12 @@ def has_topics def team_assignment assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking team assignment. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else - render json: assignment.team_assignment?, status: :ok + result = assignment.team_assignment? + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked if assignment ID: #{assignment.id} is a team assignment. Result: #{result}", request) + render json: result, status: :ok end end @@ -176,9 +207,12 @@ def valid_num_review assignment = Assignment.find_by(id: params[:assignment_id]) review_type = params[:review_type] if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking valid number of reviews. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else - render json: assignment.valid_num_review(review_type), status: :ok + result = assignment.valid_num_review(review_type) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked valid number of reviews for assignment ID: #{assignment.id}, Review type: #{review_type}. Result: #{result}", request) + render json: result, status: :ok end end @@ -187,9 +221,12 @@ def valid_num_review def has_teams assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking teams. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else - render json: assignment.teams?, status: :ok + result = assignment.teams? + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked teams for assignment ID: #{assignment.id}. Has teams: #{result}", request) + render json: result, status: :ok end end @@ -198,11 +235,15 @@ def has_teams def varying_rubrics_by_round? assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking varying rubrics. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else if AssignmentQuestionnaire.exists?(assignment_id: assignment.id) - render json: assignment.varying_rubrics_by_round?, status: :ok + result = assignment.varying_rubrics_by_round? + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked varying rubrics by round for assignment ID: #{assignment.id}. Result: #{result}", request) + render json: result, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "No questionnaire exists for assignment ID: #{assignment.id}.", request) render json: { error: "No questionnaire/rubric exists for this assignment." }, status: :not_found end end From 4524b94a2c11e1ace2bf4a90ae47e746161c3f1b Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 16:10:16 -0500 Subject: [PATCH 23/37] Added logging for the bookmarks controller --- .../api/v1/bookmarks_controller.rb | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb index e39f703b7..a05915d71 100644 --- a/app/controllers/api/v1/bookmarks_controller.rb +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -4,6 +4,7 @@ class Api::V1::BookmarksController < ApplicationController # GET on /bookmarks def index @bookmarks = Bookmark.order(:id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all bookmarks.", request) render json: @bookmarks, status: :ok and return end @@ -12,8 +13,10 @@ def index def show begin @bookmark = Bookmark.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :ok and return - rescue ActiveRecord::RecordNotFound + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -26,10 +29,10 @@ def create @bookmark = Bookmark.new(bookmark_params) @bookmark.user_id = @current_user.id @bookmark.save! - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully created!', request) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :created and return - rescue ActiveRecord::RecordInvalid - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + rescue ActiveRecord::RecordInvalid => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create bookmark. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -39,9 +42,10 @@ def create def update @bookmark = Bookmark.find(params[:id]) if @bookmark.update(update_bookmark_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully updated!', request) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update bookmark with ID: #{@bookmark.id}. Errors: #{@bookmark.errors.full_messages.join(', ')}", request) render json: @bookmark.errors.full_messages, status: :unprocessable_entity end end @@ -52,9 +56,10 @@ def destroy begin @bookmark = Bookmark.find(params[:id]) @bookmark.delete - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, 'Your bookmark has been successfully deleted!', request) - rescue ActiveRecord::RecordNotFound - render json: $ERROR_INFO.to_s, status: :not_found and return + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted bookmark with ID: #{@bookmark.id}.", request) + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) + render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -64,8 +69,10 @@ def get_bookmark_rating_score begin @bookmark = Bookmark.find(params[:id]) @bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched rating for bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark_rating, status: :ok and return - rescue ActiveRecord::RecordNotFound + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -77,8 +84,10 @@ def save_bookmark_rating_score @bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first if @bookmark_rating.blank? @bookmark_rating = BookmarkRating.create(bookmark_id: @bookmark.id, user_id: @current_user.id, rating: params[:rating]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created bookmark rating with ID: #{@bookmark_rating.id}.", request) else @bookmark_rating.update({'rating': params[:rating].to_i}) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated bookmark rating with ID: #{@bookmark_rating.id}.", request) end render json: {"bookmark": @bookmark, "rating": @bookmark_rating}, status: :ok end From 150d67a0b40c9d20b87fd652b827c07c8ff26b47 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 16:39:48 -0500 Subject: [PATCH 24/37] Added logging for the institutions controller --- app/controllers/api/v1/institutions_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/api/v1/institutions_controller.rb b/app/controllers/api/v1/institutions_controller.rb index b025079bc..61b97425e 100644 --- a/app/controllers/api/v1/institutions_controller.rb +++ b/app/controllers/api/v1/institutions_controller.rb @@ -3,14 +3,17 @@ class Api::V1::InstitutionsController < ApplicationController # GET /institutions def index @institutions = Institution.all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all institutions.", request) render json: @institutions, status: :ok end # GET /institutions/:id def show @institution = Institution.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched institution with ID: #{@institution.id}.", request) render json: @institution, status: :ok rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -18,8 +21,10 @@ def show def create @institution = Institution.new(institution_params) if @institution.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created institution with ID: #{@institution.id}.", request) render json: @institution, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create institution. Errors: #{@institution.errors.full_messages.join(', ')}", request) render json: @institution.errors, status: :unprocessable_entity end end @@ -28,8 +33,10 @@ def create def update @institution = Institution.find(params[:id]) if @institution.update(institution_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated institution with ID: #{@institution.id}.", request) render json: @institution, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update institution with ID: #{@institution.id}. Errors: #{@institution.errors.full_messages.join(', ')}", request) render json: @institution.errors, status: :unprocessable_entity end end @@ -38,6 +45,7 @@ def update def destroy @institution = Institution.find(params[:id]) @institution.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted institution with ID: #{@institution.id}.", request) render json: { message: 'Institution deleted' }, status: :ok end @@ -49,6 +57,7 @@ def institution_params end def institution_not_found + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}.", request) render json: { error: 'Institution not found' }, status: :not_found end end \ No newline at end of file From 49f932b727594ed7716be1c7c985974c9681b87f Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 17:51:41 -0500 Subject: [PATCH 25/37] Added logging for the invitations controller --- app/controllers/api/v1/invitations_controller.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/invitations_controller.rb b/app/controllers/api/v1/invitations_controller.rb index d1325f89b..80409fc4e 100644 --- a/app/controllers/api/v1/invitations_controller.rb +++ b/app/controllers/api/v1/invitations_controller.rb @@ -4,6 +4,7 @@ class Api::V1::InvitationsController < ApplicationController # GET /api/v1/invitations def index @invitations = Invitation.all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all invitations.", request) render json: @invitations, status: :ok end @@ -13,9 +14,10 @@ def create @invitation = Invitation.invitation_factory(invite_params) if @invitation.save @invitation.send_invite_email + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, 'Student was already invited') + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create invitation. Errors: #{@invitation.errors.full_messages.join(', ')}", request) render json: { error: @invitation.errors }, status: :unprocessable_entity end end @@ -23,6 +25,7 @@ def create # GET /api/v1/invitations/:id def show @invitation = Invitation.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok end @@ -32,11 +35,14 @@ def update case params[:reply_status] when InvitationValidator::ACCEPT_STATUS @invitation.accept_invitation(nil) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Accepted invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok when InvitationValidator::REJECT_STATUS @invitation.decline_invitation(nil) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Rejected invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invalid reply status for invitation with ID: #{@invitation.id}.", request) render json: @invitation.errors, status: :unprocessable_entity end @@ -46,6 +52,7 @@ def update def destroy @invitation = Invitation.find(params[:id]) @invitation.retract_invitation(nil) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted invitation with ID: #{@invitation.id}.", request) render nothing: true, status: :no_content end @@ -54,6 +61,7 @@ def invitations_for_user_assignment begin @user = User.find(params[:user_id]) rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User not found with ID: #{params[:user_id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found return end @@ -61,11 +69,13 @@ def invitations_for_user_assignment begin @assignment = Assignment.find(params[:assignment_id]) rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found with ID: #{params[:assignment_id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found return end @invitations = Invitation.where(to_id: @user.id).where(assignment_id: @assignment.id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched invitations for user ID: #{@user.id} and assignment ID: #{@assignment.id}.", request) render json: @invitations, status: :ok end @@ -82,6 +92,7 @@ def invite_params # helper method used when invite is not found def invite_not_found + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invitation not found with ID: #{params[:id]}.", request) render json: { error: "Invitation with id #{params[:id]} not found" }, status: :not_found end From 905d428ca44bc31bfd99b6d910a1d6549fa34bfb Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 18:54:26 -0500 Subject: [PATCH 26/37] Added logging for the join team requests controller --- .../api/v1/join_team_requests_controller.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/controllers/api/v1/join_team_requests_controller.rb b/app/controllers/api/v1/join_team_requests_controller.rb index 57a8ed383..b51f6bb14 100644 --- a/app/controllers/api/v1/join_team_requests_controller.rb +++ b/app/controllers/api/v1/join_team_requests_controller.rb @@ -19,9 +19,11 @@ def action_allowed? # gets a list of all the join team requests def index unless @current_user.administrator? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Unauthorized access to join team requests.", request) return render json: { errors: 'Unauthorized' }, status: :unauthorized end join_team_requests = JoinTeamRequest.all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all join team requests.", request) render json: join_team_requests, status: :ok end @@ -42,15 +44,19 @@ def create team = Team.find(params[:team_id]) if team.participants.include?(participant) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User already belongs to team ID: #{team.id}.", request) render json: { error: 'You already belong to the team' }, status: :unprocessable_entity elsif participant join_team_request.participant_id = participant.id if join_team_request.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created join team request with ID: #{join_team_request.id}.", request) render json: join_team_request, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create join team request. Errors: #{join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: join_team_request.errors.full_messages }, status: :unprocessable_entity end else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Participant not found for user ID: #{@current_user.id}.", request) render json: { errors: 'Participant not found' }, status: :unprocessable_entity end end @@ -59,8 +65,10 @@ def create # Updates a join team request def update if @join_team_request.update(join_team_request_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest was successfully updated' }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity end end @@ -69,8 +77,10 @@ def update # delete a join team request def destroy if @join_team_request.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest was successfully deleted' }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete join team request with ID: #{@join_team_request.id}.", request) render json: { errors: 'Failed to delete JoinTeamRequest' }, status: :unprocessable_entity end end @@ -79,8 +89,10 @@ def destroy def decline @join_team_request.status = DECLINED if @join_team_request.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Declined join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest declined successfully' }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to decline join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity end end @@ -90,6 +102,7 @@ def decline def check_team_status team = Team.find(params[:team_id]) if team.full? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Attempted to join a full team with ID: #{team.id}.", request) render json: { message: 'This team is full.' }, status: :unprocessable_entity end end @@ -97,6 +110,7 @@ def check_team_status # Finds the join team request by ID def find_request @join_team_request = JoinTeamRequest.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Found join team request with ID: #{@join_team_request.id}.", request) end # Permits specified parameters for join team requests From 448f633c28471c588ca36547d52319f848eeed75 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 19:04:10 -0500 Subject: [PATCH 27/37] Added logging for the questionnaires controller --- .../api/v1/questionnaires_controller.rb | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/questionnaires_controller.rb b/app/controllers/api/v1/questionnaires_controller.rb index 9bc2cd876..d7a659af2 100644 --- a/app/controllers/api/v1/questionnaires_controller.rb +++ b/app/controllers/api/v1/questionnaires_controller.rb @@ -4,6 +4,7 @@ class Api::V1::QuestionnairesController < ApplicationController # GET on /questionnaires def index @questionnaires = Questionnaire.order(:id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all questionnaires.", request) render json: @questionnaires, status: :ok and return end @@ -12,8 +13,10 @@ def index def show begin @questionnaire = Questionnaire.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok and return - rescue ActiveRecord::RecordNotFound + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -26,8 +29,10 @@ def create @questionnaire = Questionnaire.new(questionnaire_params) @questionnaire.display_type = sanitize_display_type(@questionnaire.questionnaire_type) @questionnaire.save! + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :created and return - rescue ActiveRecord::RecordInvalid + rescue ActiveRecord::RecordInvalid => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create questionnaire. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -38,7 +43,9 @@ def destroy begin @questionnaire = Questionnaire.find(params[:id]) @questionnaire.delete - rescue ActiveRecord::RecordNotFound + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted questionnaire with ID: #{@questionnaire.id}.", request) + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -49,8 +56,10 @@ def destroy def update @questionnaire = Questionnaire.find(params[:id]) if @questionnaire.update(questionnaire_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update questionnaire with ID: #{@questionnaire.id}. Errors: #{@questionnaire.errors.full_messages.join(', ')}", request) render json: @questionnaire.errors.full_messages, status: :unprocessable_entity end end @@ -59,10 +68,13 @@ def update def copy begin @questionnaire = Questionnaire.copy_questionnaire_details(params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Copied questionnaire with ID: #{params[:id]} to new questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok and return - rescue ActiveRecord::RecordNotFound + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return - rescue ActiveRecord::RecordInvalid + rescue ActiveRecord::RecordInvalid => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to copy questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -75,11 +87,14 @@ def toggle_access @questionnaire = Questionnaire.find(params[:id]) @questionnaire.toggle!(:private) @access = @questionnaire.private ? 'private' : 'public' + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Toggled access for questionnaire with ID: #{@questionnaire.id}. Now it is #{@access}.", request) render json: "The questionnaire \"#{@questionnaire.name}\" has been successfully made #{@access}. ", status: :ok - rescue ActiveRecord::RecordNotFound + rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found - rescue ActiveRecord::RecordInvalid + rescue ActiveRecord::RecordInvalid => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to toggle access for questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end From ad43a489852e89bf5531cd79d1e05336c22b86bd Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 20:38:16 -0500 Subject: [PATCH 28/37] Added logging for the questions controller --- app/controllers/api/v1/questions_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/api/v1/questions_controller.rb b/app/controllers/api/v1/questions_controller.rb index d9671bcc3..01bb8cdd6 100644 --- a/app/controllers/api/v1/questions_controller.rb +++ b/app/controllers/api/v1/questions_controller.rb @@ -4,6 +4,7 @@ class Api::V1::QuestionsController < ApplicationController # GET on /questions def index @questions = Question.order(:id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all questions.", request) render json: @questions, status: :ok end @@ -12,6 +13,7 @@ def index def show begin @question = Question.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched question with ID: #{@question.id}.", request) render json: @question, status: :ok rescue ActiveRecord::RecordNotFound ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Unable to find Question with ID: #{params[:id]}", request) @@ -49,6 +51,7 @@ def create end if question.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created question with ID: #{question.id} under questionnaire ID: #{questionnaire.id}.", request) render json: question, status: :created else ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question not able to be saved: #{question.as_json}", request) @@ -114,6 +117,7 @@ def update begin @question = Question.find(params[:id]) if @question.update(question_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated question with ID: #{@question.id}.", request) render json: @question, status: :ok and return else ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be updated with: #{question_params}", request) From 735f28b6be8bab0576a8ce79698d7e47d665ea3b Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 20:50:14 -0500 Subject: [PATCH 29/37] Added logging for the roles controller --- app/controllers/api/v1/roles_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/api/v1/roles_controller.rb b/app/controllers/api/v1/roles_controller.rb index 68e5a59e8..d7124a6ab 100644 --- a/app/controllers/api/v1/roles_controller.rb +++ b/app/controllers/api/v1/roles_controller.rb @@ -5,12 +5,14 @@ class Api::V1::RolesController < ApplicationController # GET /roles def index roles = Role.order(:id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all roles.", request) render json: roles, status: :ok end # GET /roles/:id def show role = Role.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched role with ID: #{role.id}.", request) render json: role, status: :ok end @@ -18,8 +20,10 @@ def show def create role = Role.new(role_params) if role.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created role with ID: #{role.id}.", request) render json: role, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create role. Errors: #{role.errors.full_messages.join(', ')}", request) render json: role.errors, status: :unprocessable_entity end end @@ -28,8 +32,10 @@ def create def update role = Role.find(params[:id]) if role.update(role_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated role with ID: #{role.id}.", request) render json: role, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update role with ID: #{role.id}. Errors: #{role.errors.full_messages.join(', ')}", request) render json: role.errors, status: :unprocessable_entity end end @@ -39,12 +45,14 @@ def destroy role = Role.find(params[:id]) role_name = role.name role.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted role with ID: #{role.id}, Name: #{role_name}.", request) render json: { message: "Role #{role_name} with id #{params[:id]} deleted successfully!" }, status: :no_content end def subordinate_roles role = current_user.role roles = role.subordinate_roles + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched subordinate roles for role ID: #{role.id}.", request) render json: roles, status: :ok end @@ -60,6 +68,7 @@ def role_params # end def parameter_missing + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing.", request) render json: { error: 'Parameter missing' }, status: :unprocessable_entity end end From e7afdda990258b06ffa20b37f747c4c983961392 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 21:29:07 -0500 Subject: [PATCH 30/37] Added logging for the sign up topics controller --- app/controllers/api/v1/sign_up_topics_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/api/v1/sign_up_topics_controller.rb b/app/controllers/api/v1/sign_up_topics_controller.rb index a736f3eaa..70ea074d5 100644 --- a/app/controllers/api/v1/sign_up_topics_controller.rb +++ b/app/controllers/api/v1/sign_up_topics_controller.rb @@ -5,12 +5,15 @@ class Api::V1::SignUpTopicsController < ApplicationController # Retrieve SignUpTopics by two query parameters - assignment_id (compulsory) and an array of topic_ids (optional) def index if params[:assignment_id].nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment ID is missing.", request) render json: { message: 'Assignment ID is required!' }, status: :unprocessable_entity elsif params[:topic_ids].nil? @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]}.", request) render json: @sign_up_topics, status: :ok else @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id], topic_identifier: params[:topic_ids]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]} with topic IDs: #{params[:topic_ids].join(', ')}.", request) render json: @sign_up_topics, status: :ok end # render json: {message: 'All selected topics have been loaded successfully.', sign_up_topics: @stopics}, status: 200 @@ -26,8 +29,10 @@ def create @sign_up_topic.micropayment = params[:micropayment] if @assignment.microtask? if @sign_up_topic.save # undo_link "The topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. " + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created sign-up topic with ID: #{@sign_up_topic.id} for assignment ID: #{@assignment.id}.", request) render json: { message: "The topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. " }, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create sign-up topic. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) render json: { message: @sign_up_topic.errors }, status: :unprocessable_entity end end @@ -36,8 +41,10 @@ def create # updates parameters present in sign_up_topic_params. def update if @sign_up_topic.update(sign_up_topic_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated sign-up topic with ID: #{@sign_up_topic.id}.", request) render json: { message: "The topic: \"#{@sign_up_topic.topic_name}\" has been updated successfully. " }, status: 200 else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update sign-up topic with ID: #{@sign_up_topic.id}. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) render json: @sign_up_topic.errors, status: :unprocessable_entity end end @@ -54,6 +61,7 @@ def destroy # render json: {message: @sign_up_topic} # filters topics based on assignment id (required) and topic identifiers (optional) if params[:assignment_id].nil? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment ID is missing for destroy action.", request) render json: { message: 'Assignment ID is required!' }, status: :unprocessable_entity elsif params[:topic_ids].nil? @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id]) @@ -64,8 +72,10 @@ def destroy end if @sign_up_topics.each(&:delete) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted sign-up topics.", request) render json: { message: "The topic has been deleted successfully. " }, status: :no_content else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete sign-up topics.", request) render json: @sign_up_topic.errors, status: :unprocessable_entity end end From fe5988b6809cf1533cbe5245ffad69469cba44b2 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 22:01:54 -0500 Subject: [PATCH 31/37] Added logging for the signed up teams controller --- app/controllers/api/v1/signed_up_teams_controller.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/signed_up_teams_controller.rb b/app/controllers/api/v1/signed_up_teams_controller.rb index e28f875a4..e071d6d91 100644 --- a/app/controllers/api/v1/signed_up_teams_controller.rb +++ b/app/controllers/api/v1/signed_up_teams_controller.rb @@ -6,6 +6,7 @@ def index # puts params[:topic_id] @sign_up_topic = SignUpTopic.find(params[:topic_id]) @signed_up_team = SignedUpTeam.find_team_participants(@sign_up_topic.assignment_id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched signed-up team for topic ID: #{@sign_up_topic.id}.", request) render json: @signed_up_team end @@ -16,9 +17,10 @@ def create; end def update @signed_up_team = SignedUpTeam.find(params[:id]) if @signed_up_team.update(signed_up_teams_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated signed-up team with team ID: #{@signed_up_team.team_id}.", request) render json: { message: "The team has been updated successfully. " }, status: 200 else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: @signed_up_team.errors, status: :unprocessable_entity end end @@ -30,8 +32,10 @@ def sign_up topic_id = params[:topic_id] @signed_up_team = SignedUpTeam.create_signed_up_team(topic_id, team_id) if @signed_up_team + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Team ID: #{team_id} signed up for topic ID: #{topic_id}.", request) render json: { message: "Signed up team successful!" }, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to sign up team ID: #{team_id} for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: { message: @signed_up_team.errors }, status: :unprocessable_entity end end @@ -49,8 +53,10 @@ def sign_up_student @signed_up_team = SignedUpTeam.create_signed_up_team(topic_id, team_id) # create(topic_id, team_id) if @signed_up_team + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "User ID: #{user_id} (Team ID: #{team_id}) signed up for topic ID: #{topic_id}.", request) render json: { message: "Signed up team successful!" }, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to sign up user ID: #{user_id} (Team ID: #{team_id}) for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: { message: @signed_up_team.errors }, status: :unprocessable_entity end end @@ -59,8 +65,10 @@ def sign_up_student def destroy @signed_up_team = SignedUpTeam.find(params[:id]) if SignedUpTeam.delete_signed_up_team(@signed_up_team.team_id) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted signed-up team with team ID: #{@signed_up_team.team_id}.", request) render json: { message: 'Signed up teams was deleted successfully!' }, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: @signed_up_team.errors, status: :unprocessable_entity end end From b015193ce922f5280b2cbee066b4269e5817435e Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 22:21:33 -0500 Subject: [PATCH 32/37] Added logging for the student tasks controller --- app/controllers/api/v1/student_tasks_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/student_tasks_controller.rb b/app/controllers/api/v1/student_tasks_controller.rb index 6eb784b73..ce4d6e282 100644 --- a/app/controllers/api/v1/student_tasks_controller.rb +++ b/app/controllers/api/v1/student_tasks_controller.rb @@ -4,6 +4,7 @@ class Api::V1::StudentTasksController < ApplicationController def list # Retrieves all tasks that belong to the current user. @student_tasks = StudentTask.from_user(current_user) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched student tasks for user ID: #{current_user.id}.", request) # Render the list of student tasks as JSON. render json: @student_tasks, status: :ok end @@ -18,6 +19,7 @@ def view # Retrieves the student task where the participant's ID matches the provided parameter. # This function will be used for clicking on a specific student task to "view" its details. @student_task = StudentTask.from_participant_id(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched student task for participant ID: #{params[:id]}.", request) # Render the found student task as JSON. render json: @student_task, status: :ok end From bc5075890b3711c60765af97219e493cb322d98a Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sat, 30 Nov 2024 22:36:33 -0500 Subject: [PATCH 33/37] Added logging for the users controller --- app/controllers/api/v1/users_controller.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 086376556..fd5b88b76 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -4,12 +4,14 @@ class Api::V1::UsersController < ApplicationController def index users = User.all + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all users.", request) render json: users, status: :ok end # GET /users/:id def show user = User.find(params[:id]) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched user with ID: #{user.id}.", request) render json: user, status: :ok end @@ -19,8 +21,10 @@ def create params[:user][:password] ||= 'password' user = User.new(user_params) if user.save + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created user with ID: #{user.id}.", request) render json: user, status: :created else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create user. Errors: #{user.errors.full_messages.join(', ')}", request) render json: user.errors, status: :unprocessable_entity end end @@ -29,8 +33,10 @@ def create def update user = User.find(params[:id]) if user.update(user_params) + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated user with ID: #{user.id}.", request) render json: user, status: :ok else + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update user with ID: #{user.id}. Errors: #{user.errors.full_messages.join(', ')}", request) render json: user.errors, status: :unprocessable_entity end end @@ -39,6 +45,7 @@ def update def destroy user = User.find(params[:id]) user.destroy + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted user with ID: #{user.id}.", request) render json: { message: "User #{user.name} with id #{params[:id]} deleted successfully!" }, status: :no_content end @@ -47,8 +54,10 @@ def destroy def institution_users institution = Institution.find(params[:id]) users = institution.users + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched users for institution ID: #{institution.id}.", request) render json: users, status: :ok rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -57,11 +66,13 @@ def institution_users def managed_users parent = User.find(params[:id]) if parent.student? + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User ID: #{parent.id} is a student and cannot manage users.", request) render json: { error: 'Students do not manage any users' }, status: :unprocessable_entity return end parent = User.instantiate(parent) users = parent.managed_users + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched managed users for user ID: #{parent.id}.", request) render json: users, status: :ok end @@ -71,8 +82,10 @@ def role_users name = params[:name].split('_').map(&:capitalize).join(' ') role = Role.find_by(name:) users = role.users + ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched users for role: #{name}.", request) render json: users, status: :ok rescue ActiveRecord::RecordNotFound => e + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Role not found with name: #{name}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -86,10 +99,12 @@ def user_params end def user_not_found + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User not found with ID: #{params[:id]}.", request) render json: { error: "User with id #{params[:id]} not found" }, status: :not_found end def parameter_missing + ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing.", request) render json: { error: 'Parameter missing' }, status: :unprocessable_entity end end From a6b6b95459c3d86189c665d4744a538c8034598e Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Sun, 1 Dec 2024 17:24:27 -0500 Subject: [PATCH 34/37] Added more comments to expertiza_logger.rb to explain functionality --- app/models/expertiza_logger.rb | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/app/models/expertiza_logger.rb b/app/models/expertiza_logger.rb index 5eacec3af..5d01e2a7b 100644 --- a/app/models/expertiza_logger.rb +++ b/app/models/expertiza_logger.rb @@ -1,10 +1,10 @@ # expertiza_logger.rb # This file contains the formatter used for the ExpertizaLogger and the ExpertizaLogger. -# ExpertizaLogger contains 5 levels of logging: info, warn, error, fatal, and debug. +# ExpertizaLogger provides 5 levels of logging: debug, info, warn, error, and fatal. # ExpertizaLogFormatter formats the logs to ensure consistent log message formatting. class ExpertizaLogFormatter < Logger::Formatter - # This method is invoked when a log event occurs and formats the message + # This method is invoked when a log event occurs and formats the message. def call(s, ts, pg, msg) if msg.is_a?(LoggerMessage) "TST=[#{ts}] SVT=[#{s}] PNM=[#{pg}] OIP=[#{msg.oip}] RID=[#{msg.req_id}] CTR=[#{msg.generator}] UID=[#{msg.unity_id}] MSG=[#{filter(msg.message)}]\n" @@ -13,46 +13,68 @@ def call(s, ts, pg, msg) end end - # Filter out the newline characters in the message and replace with a space character. + # Filter out the newline characters in the message and replace with a space character. + # Reason: + # Each log message should be a single-line entry in a log file to ensure they are easily readable and parsable. + # The presence of newline characters can break log parsers and make logs harder to analyze. def filter(msg) msg.tr("\n", ' ') end end -# ExpertizaLogger providing the logging levels and functionality. +# ExpertizaLogger provides the logging levels and functionality. # Each level creates its own file (expertiza_info.log or similar) and # uses the ExpertizaLogFormatter to format and then print the message. class ExpertizaLogger + + # Logs an informational message. + # + # Use the INFO level to log general application events that confirm things are working as expected. def self.info(message = nil) @info_log ||= Logger.new(Rails.root.join('log', 'expertiza_info.log')) add_formatter @info_log @info_log.info(message) end + # Logs a warning message. + # + # Use the WARN level to log unexpected events that are not immediately harmful but might cause issues later. def self.warn(message = nil) @warn_log ||= Logger.new(Rails.root.join('log', 'expertiza_warn.log')) add_formatter @warn_log @warn_log.warn(message) end + # Logs an error message. + # + # Use the ERROR level to log issues that have caused an operation to fail, such as erroneous database requests. def self.error(message = nil) @error_log ||= Logger.new(Rails.root.join('log', 'expertiza_error.log')) add_formatter @error_log @error_log.error(message) end + # Logs a fatal message. + # + # Use the FATAL level for severe errors that lead to application termination. def self.fatal(message = nil) @fatal_log ||= Logger.new(Rails.root.join('log', 'expertiza_fatal.log')) add_formatter @fatal_log @fatal_log.fatal(message) end + # Logs a debug message. + # + # Use the DEBUG level for diagnostic information useful during development. def self.debug(message = nil) @debug_log ||= Logger.new(Rails.root.join('log', 'expertiza_debug.log')) add_formatter @debug_log @debug_log.debug(message) end + # Adds the custom formatter to the given logger instance. + # Reason: + # Ensures all log messages are consistently formatted using ExpertizaLogFormatter. def self.add_formatter(log) log.formatter ||= ExpertizaLogFormatter.new end From c84e2222e51efffcb49bd71601461ed6b50d778d Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 3 Dec 2024 01:35:53 -0500 Subject: [PATCH 35/37] Removed logging concern from application_record and expanded comments --- app/models/application_record.rb | 1 - app/models/concerns/logging.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index ee303827e..f6961453b 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,4 +1,3 @@ class ApplicationRecord < ActiveRecord::Base - include Logging primary_abstract_class end \ No newline at end of file diff --git a/app/models/concerns/logging.rb b/app/models/concerns/logging.rb index 25bd78352..0cd20164a 100644 --- a/app/models/concerns/logging.rb +++ b/app/models/concerns/logging.rb @@ -1,6 +1,12 @@ # Logging concern to automatically log model CRUD operations to the database. # It can be enabled for a given model by using "include Logging". # It will then automatically log all CRUD operations for the model. +# This concern was created to log all of the CRUD operations being +# executed against the database by logging them as ExpertizaLogs with +# the info log level. Enabling this for all models would create +# too many logs to effectively parse and provide meaning; however, +# for certain models that are not frequently used, it can provide +# key insights into the behavior of the application. module Logging extend ActiveSupport::Concern @@ -15,18 +21,44 @@ module Logging private + # Logs the creation of an object to the database. With the + # hooks created above, this will automatically log the class name + # and its attributes after it has been successfully created. + # This may provide excellent insight when desiring to audit the + # object data being created for certain classes. def log_create ExpertizaLogger.info("Created #{self.class.name}: #{self.attributes}") end + # Logs the retrieval of an object from the database. With the + # hooks created above, this will automatically log the class name + # and its attributes after it has been successfully found. + # This may provide useful insights when attempting to see the data + # retrieved before it can ever be modified or when wanting to audit + # if certain critical objects are ever accessed when it should otherwise + # be impossible. def log_find ExpertizaLogger.info("Found #{self.class.name}: #{self.attributes}") end + # Logs the update of an object to the database. Similar to + # the log_create method above, this may be useful when attempting to + # validate that new data is being updated and persisted to the database. + # Similar to log_find, it may be useful to audit the modification of key + # classes such as a gradebook to ensure that grades are only modified as + # intended. def log_update ExpertizaLogger.info("Updated #{self.class.name}: #{self.attributes}") end + # Logs the deletion of an object from the database. With the + # hooks from above, this will automatically log the class name + # and the object's attributes after it has been successfully deleted + # from the database. This may be useful for certain critical data + # that should never be deleted, enabling it and its deletion to be + # recorded, effectively providing a last instance of the data and + # allowing it to potentially reveal why it was deleted through what it + # contained. def log_destroy ExpertizaLogger.info("Destroyed #{self.class.name}: #{self.attributes}") end From f2e0edee2fabaaa7f5bf7e05d8cd02561bd94498 Mon Sep 17 00:00:00 2001 From: Luke Jenquin Date: Tue, 3 Dec 2024 10:29:30 -0500 Subject: [PATCH 36/37] Deleted logging concern --- app/models/concerns/logging.rb | 65 ---------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 app/models/concerns/logging.rb diff --git a/app/models/concerns/logging.rb b/app/models/concerns/logging.rb deleted file mode 100644 index 0cd20164a..000000000 --- a/app/models/concerns/logging.rb +++ /dev/null @@ -1,65 +0,0 @@ -# Logging concern to automatically log model CRUD operations to the database. -# It can be enabled for a given model by using "include Logging". -# It will then automatically log all CRUD operations for the model. -# This concern was created to log all of the CRUD operations being -# executed against the database by logging them as ExpertizaLogs with -# the info log level. Enabling this for all models would create -# too many logs to effectively parse and provide meaning; however, -# for certain models that are not frequently used, it can provide -# key insights into the behavior of the application. - -module Logging - extend ActiveSupport::Concern - - # Set up hooks to call logging when CRUD operations are performed. - included do - after_create :log_create - after_find :log_find - after_update :log_update - after_destroy :log_destroy - end - - private - - # Logs the creation of an object to the database. With the - # hooks created above, this will automatically log the class name - # and its attributes after it has been successfully created. - # This may provide excellent insight when desiring to audit the - # object data being created for certain classes. - def log_create - ExpertizaLogger.info("Created #{self.class.name}: #{self.attributes}") - end - - # Logs the retrieval of an object from the database. With the - # hooks created above, this will automatically log the class name - # and its attributes after it has been successfully found. - # This may provide useful insights when attempting to see the data - # retrieved before it can ever be modified or when wanting to audit - # if certain critical objects are ever accessed when it should otherwise - # be impossible. - def log_find - ExpertizaLogger.info("Found #{self.class.name}: #{self.attributes}") - end - - # Logs the update of an object to the database. Similar to - # the log_create method above, this may be useful when attempting to - # validate that new data is being updated and persisted to the database. - # Similar to log_find, it may be useful to audit the modification of key - # classes such as a gradebook to ensure that grades are only modified as - # intended. - def log_update - ExpertizaLogger.info("Updated #{self.class.name}: #{self.attributes}") - end - - # Logs the deletion of an object from the database. With the - # hooks from above, this will automatically log the class name - # and the object's attributes after it has been successfully deleted - # from the database. This may be useful for certain critical data - # that should never be deleted, enabling it and its deletion to be - # recorded, effectively providing a last instance of the data and - # allowing it to potentially reveal why it was deleted through what it - # contained. - def log_destroy - ExpertizaLogger.info("Destroyed #{self.class.name}: #{self.attributes}") - end -end From 3aa787412b1306e5a5c07bae9bb2a1730d60cc99 Mon Sep 17 00:00:00 2001 From: Cam Himes Date: Tue, 3 Dec 2024 11:06:31 -0500 Subject: [PATCH 37/37] Replaced session references in logging statements with the correct reference for grabbing the current user --- .../api/v1/account_requests_controller.rb | 36 ++++----- .../api/v1/assignments_controller.rb | 74 +++++++++---------- .../api/v1/bookmarks_controller.rb | 26 +++---- app/controllers/api/v1/courses_controller.rb | 18 ++--- .../api/v1/institutions_controller.rb | 18 ++--- .../api/v1/invitations_controller.rb | 24 +++--- .../api/v1/join_team_requests_controller.rb | 28 +++---- .../api/v1/questionnaires_controller.rb | 30 ++++---- .../api/v1/questions_controller.rb | 32 ++++---- app/controllers/api/v1/roles_controller.rb | 18 ++--- .../api/v1/sign_up_topics_controller.rb | 20 ++--- .../api/v1/signed_up_teams_controller.rb | 18 ++--- .../api/v1/student_tasks_controller.rb | 4 +- app/controllers/api/v1/users_controller.rb | 30 ++++---- 14 files changed, 188 insertions(+), 188 deletions(-) diff --git a/app/controllers/api/v1/account_requests_controller.rb b/app/controllers/api/v1/account_requests_controller.rb index ac56f4b34..463b02aac 100644 --- a/app/controllers/api/v1/account_requests_controller.rb +++ b/app/controllers/api/v1/account_requests_controller.rb @@ -3,14 +3,14 @@ class Api::V1::AccountRequestsController < ApplicationController # GET /account_requests/pending def pending_requests @account_requests = AccountRequest.where(status: 'Under Review').order('created_at DESC') - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@account_requests.count} pending account requests.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched #{@account_requests.count} pending account requests.", request) render json: @account_requests, status: :ok end # GET /account_requests/processed def processed_requests @account_requests = AccountRequest.where.not(status: 'Under Review').order('updated_at DESC') - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@account_requests.count} processed account requests.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched #{@account_requests.count} processed account requests.", request) render json: @account_requests, status: :ok end @@ -21,35 +21,35 @@ def create response = { account_request: @account_request } if User.find_by(email: @account_request.email) # Logging a warning if a user with the same email already exists - ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Account requested with duplicate email: #{@account_request.email}", request) + ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Account requested with duplicate email: #{@account_request.email}", request) response[:warnings] = 'WARNING: User with this email already exists!' end # Logging the successful creation of the account request - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Successfully created account request with ID: #{@account_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Successfully created account request with ID: #{@account_request.id}.", request) render json: response, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, $ERROR_INFO, request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, $ERROR_INFO, request) render json: @account_request.errors, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Record not found: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Record not found: #{e.message}", request) render json: { error: e.message }, status: :not_found rescue ActionController::ParameterMissing => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Parameter missing: #{e.message}", request) render json: { error: e.message }, status: :parameter_missing rescue StandardError => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "An error occurred: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "An error occurred: #{e.message}", request) render json: { error: e.message }, status: :unprocessable_entity end # GET /account_requests/:id def show @account_request = AccountRequest.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched account request with ID: #{@account_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched account request with ID: #{@account_request.id}.", request) render json: @account_request, status: :ok rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -59,7 +59,7 @@ def update @account_request = AccountRequest.find(params[:id]) @account_request.update(account_request_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated account request with ID: #{@account_request.id}, Status: #{@account_request.status}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated account request with ID: #{@account_request.id}, Status: #{@account_request.status}.", request) if @account_request.status == 'Approved' create_approved_user @@ -67,10 +67,10 @@ def update render json: @account_request, status: :ok end rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found for update: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found for update: #{e.message}", request) render json: { error: e.message }, status: :not_found rescue StandardError => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "An error occurred while updating account request: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "An error occurred while updating account request: #{e.message}", request) render json: { error: e.message }, status: :unprocessable_entity end @@ -78,10 +78,10 @@ def update def destroy @account_request = AccountRequest.find(params[:id]) @account_request.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted account request with ID: #{@account_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted account request with ID: #{@account_request.id}.", request) render json: { message: 'Account Request deleted' }, status: :no_content rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Account request not found for deletion: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Account request not found for deletion: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -94,7 +94,7 @@ def account_request_params params[:account_request][:status] = 'Under Review' # For Approval or Rejection of an existing request, raise error if user sends a status other than Approved or Rejected elsif !['Approved', 'Rejected'].include?(params[:account_request][:status]) - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invalid status provided: #{params[:account_request][:status]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Invalid status provided: #{params[:account_request][:status]}", request) raise StandardError, 'Status can only be Approved or Rejected' end params.require(:account_request).permit(:username, :full_name, :email, :status, :introduction, :role_id, :institution_id) @@ -104,11 +104,11 @@ def account_request_params def create_approved_user @new_user = User.new(name: @account_request.username, role_id: @account_request.role_id, institution_id: @account_request.institution_id, fullname: @account_request.full_name, email: @account_request.email, password: 'password') if @new_user.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Approved account request and created user with ID: #{@new_user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Approved account request and created user with ID: #{@new_user.id}.", request) render json: { success: 'Account Request Approved and User successfully created.', user: @new_user}, status: :ok else errors = @new_user.errors.full_messages.join(', ') - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create user from approved account request ID: #{@account_request.id}. Errors: #{errors}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create user from approved account request ID: #{@account_request.id}. Errors: #{errors}", request) render json: @new_user.errors, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/assignments_controller.rb b/app/controllers/api/v1/assignments_controller.rb index 554eb372f..1d2c3055d 100644 --- a/app/controllers/api/v1/assignments_controller.rb +++ b/app/controllers/api/v1/assignments_controller.rb @@ -3,7 +3,7 @@ class Api::V1::AssignmentsController < ApplicationController # GET /api/v1/assignments def index assignments = Assignment.all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all assignments.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all assignments.", request) render json: assignments end @@ -12,7 +12,7 @@ def show assignment = Assignment.find(params[:id]) # For now, just logging success - if error checking is added in the future, please add a log message for that with # ExpertizaLogger.error - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched assignment with ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched assignment with ID: #{assignment.id}.", request) render json: assignment end @@ -20,10 +20,10 @@ def show def create assignment = Assignment.new(assignment_params) if assignment.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment created: #{assignment.as_json}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assignment created: #{assignment.as_json}", request) render json: assignment, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create assignment: #{assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create assignment: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -32,10 +32,10 @@ def create def update assignment = Assignment.find(params[:id]) if assignment.update(assignment_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated assignment with ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated assignment with ID: #{assignment.id}.", request) render json: assignment, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -45,14 +45,14 @@ def destroy assignment = Assignment.find_by(id: params[:id]) if assignment if assignment.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assignment #{assignment.id} was deleted.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assignment #{assignment.id} was deleted.", request) render json: { message: "Assignment deleted successfully!" }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete assignment #{$ERROR_INFO}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to delete assignment #{$ERROR_INFO}", request) render json: { error: "Failed to delete assignment", details: assignment.errors.full_messages }, status: :unprocessable_entity end else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for deletion with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for deletion with ID: #{params[:id]}", request) render json: { error: "Assignment not found" }, status: :not_found end end @@ -61,15 +61,15 @@ def destroy def add_participant assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for adding participant. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for adding participant. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else new_participant = assignment.add_participant(params[:user_id]) if new_participant.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Added participant with User ID: #{params[:user_id]} to assignment ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Added participant with User ID: #{params[:user_id]} to assignment ID: #{assignment.id}.", request) render json: new_participant, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to add participant to assignment ID: #{assignment.id}. Errors: #{new_participant.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to add participant to assignment ID: #{assignment.id}. Errors: #{new_participant.errors.full_messages.join(', ')}", request) render json: new_participant.errors, status: :unprocessable_entity end end @@ -82,15 +82,15 @@ def remove_participant if user && assignment assignment.remove_participant(user.id) if assignment.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Removed participant with User ID: #{user.id} from assignment ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Removed participant with User ID: #{user.id} from assignment ID: #{assignment.id}.", request) render json: { message: "Participant removed successfully!" }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to remove participant from assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to remove participant from assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end else not_found_message = user ? "Assignment not found" : "User not found" - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "#{not_found_message} while removing participant.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "#{not_found_message} while removing participant.", request) render json: { error: not_found_message }, status: :not_found end end @@ -100,15 +100,15 @@ def remove_participant def remove_assignment_from_course assignment = Assignment.find(params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for removing from course. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for removing from course. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else assignment = assignment.remove_assignment_from_course if assignment.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Removed assignment ID: #{assignment.id} from its course.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Removed assignment ID: #{assignment.id} from its course.", request) render json: assignment , status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to remove assignment ID: #{assignment.id} from course. Errors: #{assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to remove assignment ID: #{assignment.id} from course. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end end @@ -122,15 +122,15 @@ def assign_course if assignment && course assignment = assignment.assign_course(course.id) if assignment.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Assigned course ID: #{course.id} to assignment ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Assigned course ID: #{course.id} to assignment ID: #{assignment.id}.", request) render json: assignment, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to assign course to assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to assign course to assignment ID: #{assignment.id}. Errors: #{assignment.errors.full_messages.join(', ')}", request) render json: assignment.errors, status: :unprocessable_entity end else not_found_message = course ? "Assignment not found" : "Course not found" - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "#{not_found_message} while assigning course.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "#{not_found_message} while assigning course.", request) render json: { error: not_found_message }, status: :not_found end end @@ -139,15 +139,15 @@ def assign_course def copy_assignment assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for copying. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for copying. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else new_assignment = assignment.copy if new_assignment.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Copied assignment ID: #{assignment.id} to new assignment ID: #{new_assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Copied assignment ID: #{assignment.id} to new assignment ID: #{new_assignment.id}.", request) render json: new_assignment, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to copy assignment ID: #{assignment.id}. Errors: #{new_assignment.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to copy assignment ID: #{assignment.id}. Errors: #{new_assignment.errors.full_messages.join(', ')}", request) render json :new_assignment.errors, status: :unprocessable_entity end end @@ -158,10 +158,10 @@ def copy_assignment def show_assignment_details assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for showing details. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for showing details. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched details for assignment ID: #{assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched details for assignment ID: #{assignment.id}.", request) render json: { id: assignment.id, name: assignment.name, @@ -178,11 +178,11 @@ def show_assignment_details def has_topics assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking topics. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking topics. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else result = assignment.topics? - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked topics for assignment ID: #{assignment.id}. Has topics: #{result}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked topics for assignment ID: #{assignment.id}. Has topics: #{result}", request) render json: result, status: :ok end end @@ -192,11 +192,11 @@ def has_topics def team_assignment assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking team assignment. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking team assignment. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else result = assignment.team_assignment? - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked if assignment ID: #{assignment.id} is a team assignment. Result: #{result}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked if assignment ID: #{assignment.id} is a team assignment. Result: #{result}", request) render json: result, status: :ok end end @@ -207,11 +207,11 @@ def valid_num_review assignment = Assignment.find_by(id: params[:assignment_id]) review_type = params[:review_type] if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking valid number of reviews. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking valid number of reviews. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else result = assignment.valid_num_review(review_type) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked valid number of reviews for assignment ID: #{assignment.id}, Review type: #{review_type}. Result: #{result}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked valid number of reviews for assignment ID: #{assignment.id}, Review type: #{review_type}. Result: #{result}", request) render json: result, status: :ok end end @@ -221,11 +221,11 @@ def valid_num_review def has_teams assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking teams. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking teams. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else result = assignment.teams? - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked teams for assignment ID: #{assignment.id}. Has teams: #{result}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked teams for assignment ID: #{assignment.id}. Has teams: #{result}", request) render json: result, status: :ok end end @@ -235,15 +235,15 @@ def has_teams def varying_rubrics_by_round? assignment = Assignment.find_by(id: params[:assignment_id]) if assignment.nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found for checking varying rubrics. ID: #{params[:assignment_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found for checking varying rubrics. ID: #{params[:assignment_id]}", request) render json: { error: "Assignment not found" }, status: :not_found else if AssignmentQuestionnaire.exists?(assignment_id: assignment.id) result = assignment.varying_rubrics_by_round? - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Checked varying rubrics by round for assignment ID: #{assignment.id}. Result: #{result}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Checked varying rubrics by round for assignment ID: #{assignment.id}. Result: #{result}", request) render json: result, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "No questionnaire exists for assignment ID: #{assignment.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "No questionnaire exists for assignment ID: #{assignment.id}.", request) render json: { error: "No questionnaire/rubric exists for this assignment." }, status: :not_found end end diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb index a05915d71..cbccbe80d 100644 --- a/app/controllers/api/v1/bookmarks_controller.rb +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -4,7 +4,7 @@ class Api::V1::BookmarksController < ApplicationController # GET on /bookmarks def index @bookmarks = Bookmark.order(:id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all bookmarks.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all bookmarks.", request) render json: @bookmarks, status: :ok and return end @@ -13,10 +13,10 @@ def index def show begin @bookmark = Bookmark.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched bookmark with ID: #{@bookmark.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :ok and return rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -29,10 +29,10 @@ def create @bookmark = Bookmark.new(bookmark_params) @bookmark.user_id = @current_user.id @bookmark.save! - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created bookmark with ID: #{@bookmark.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :created and return rescue ActiveRecord::RecordInvalid => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create bookmark. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create bookmark. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -42,10 +42,10 @@ def create def update @bookmark = Bookmark.find(params[:id]) if @bookmark.update(update_bookmark_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated bookmark with ID: #{@bookmark.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update bookmark with ID: #{@bookmark.id}. Errors: #{@bookmark.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update bookmark with ID: #{@bookmark.id}. Errors: #{@bookmark.errors.full_messages.join(', ')}", request) render json: @bookmark.errors.full_messages, status: :unprocessable_entity end end @@ -56,9 +56,9 @@ def destroy begin @bookmark = Bookmark.find(params[:id]) @bookmark.delete - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted bookmark with ID: #{@bookmark.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted bookmark with ID: #{@bookmark.id}.", request) rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -69,10 +69,10 @@ def get_bookmark_rating_score begin @bookmark = Bookmark.find(params[:id]) @bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched rating for bookmark with ID: #{@bookmark.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched rating for bookmark with ID: #{@bookmark.id}.", request) render json: @bookmark_rating, status: :ok and return rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Bookmark not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -84,10 +84,10 @@ def save_bookmark_rating_score @bookmark_rating = BookmarkRating.where(bookmark_id: @bookmark.id, user_id: @current_user.id).first if @bookmark_rating.blank? @bookmark_rating = BookmarkRating.create(bookmark_id: @bookmark.id, user_id: @current_user.id, rating: params[:rating]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created bookmark rating with ID: #{@bookmark_rating.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created bookmark rating with ID: #{@bookmark_rating.id}.", request) else @bookmark_rating.update({'rating': params[:rating].to_i}) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated bookmark rating with ID: #{@bookmark_rating.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated bookmark rating with ID: #{@bookmark_rating.id}.", request) end render json: {"bookmark": @bookmark, "rating": @bookmark_rating}, status: :ok end diff --git a/app/controllers/api/v1/courses_controller.rb b/app/controllers/api/v1/courses_controller.rb index af5d446db..bb898f6d0 100644 --- a/app/controllers/api/v1/courses_controller.rb +++ b/app/controllers/api/v1/courses_controller.rb @@ -21,10 +21,10 @@ def show def create course = Course.new(course_params) if course.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course created: #{course.as_json}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Course created: #{course.as_json}.", request) render json: course, status: :created else - ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Error creating new Course: #{$ERROR_INFO}", request) + ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Error creating new Course: #{$ERROR_INFO}", request) render json: course.errors, status: :unprocessable_entity end end @@ -33,10 +33,10 @@ def create # Update a course def update if @course.update(course_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course Information Modified: #{course.as_json}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Course Information Modified: #{course.as_json}.", request) render json: @course, status: :ok else - ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Course Failed to Update: #{$ERROR_INFO}.", request) + ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Course Failed to Update: #{$ERROR_INFO}.", request) render json: @course.errors, status: :unprocessable_entity end end @@ -45,7 +45,7 @@ def update # Delete a course def destroy @course.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course with id #{params[:id]}, deleted", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Course with id #{params[:id]}, deleted", request) render json: { message: "Course with id #{params[:id]}, deleted" }, status: :no_content end @@ -54,10 +54,10 @@ def add_ta user = User.find_by(id: params[:ta_id]) result = @course.add_ta(user) if result[:success] - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course with id #{params[:id]}, added new TA #{params[:ta_id]}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Course with id #{params[:id]}, added new TA #{params[:ta_id]}.", request) render json: result[:data], status: :created else - ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Error added TA #{params[:ta_id]} to Course #{params[:id]}.", request) + ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Error added TA #{params[:ta_id]} to Course #{params[:id]}.", request) render json: { status: "error", message: result[:message] }, status: :bad_request end end @@ -72,7 +72,7 @@ def view_tas def remove_ta result = @course.remove_ta(params[:ta_id]) if result[:success] - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "TA #{params[:ta_id]} removed from Course #{params[:id]}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "TA #{params[:ta_id]} removed from Course #{params[:id]}.", request) render json: { message: "The TA #{result[:ta_name]} has been removed." }, status: :ok else render json: { status: "error", message: result[:message] }, status: :not_found @@ -84,7 +84,7 @@ def copy # existing_course = Course.find(params[:id]) success = @course.copy_course if success - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Course #{params[:id]} copied successfully.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Course #{params[:id]} copied successfully.", request) render json: { message: "The course #{@course.name} has been successfully copied" }, status: :ok else render json: { message: "The course was not able to be copied" }, status: :unprocessable_entity diff --git a/app/controllers/api/v1/institutions_controller.rb b/app/controllers/api/v1/institutions_controller.rb index 61b97425e..5f6f3ad93 100644 --- a/app/controllers/api/v1/institutions_controller.rb +++ b/app/controllers/api/v1/institutions_controller.rb @@ -3,17 +3,17 @@ class Api::V1::InstitutionsController < ApplicationController # GET /institutions def index @institutions = Institution.all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all institutions.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all institutions.", request) render json: @institutions, status: :ok end # GET /institutions/:id def show @institution = Institution.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched institution with ID: #{@institution.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched institution with ID: #{@institution.id}.", request) render json: @institution, status: :ok rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -21,10 +21,10 @@ def show def create @institution = Institution.new(institution_params) if @institution.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created institution with ID: #{@institution.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created institution with ID: #{@institution.id}.", request) render json: @institution, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create institution. Errors: #{@institution.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create institution. Errors: #{@institution.errors.full_messages.join(', ')}", request) render json: @institution.errors, status: :unprocessable_entity end end @@ -33,10 +33,10 @@ def create def update @institution = Institution.find(params[:id]) if @institution.update(institution_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated institution with ID: #{@institution.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated institution with ID: #{@institution.id}.", request) render json: @institution, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update institution with ID: #{@institution.id}. Errors: #{@institution.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update institution with ID: #{@institution.id}. Errors: #{@institution.errors.full_messages.join(', ')}", request) render json: @institution.errors, status: :unprocessable_entity end end @@ -45,7 +45,7 @@ def update def destroy @institution = Institution.find(params[:id]) @institution.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted institution with ID: #{@institution.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted institution with ID: #{@institution.id}.", request) render json: { message: 'Institution deleted' }, status: :ok end @@ -57,7 +57,7 @@ def institution_params end def institution_not_found - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Institution not found with ID: #{params[:id]}.", request) render json: { error: 'Institution not found' }, status: :not_found end end \ No newline at end of file diff --git a/app/controllers/api/v1/invitations_controller.rb b/app/controllers/api/v1/invitations_controller.rb index 80409fc4e..bd4f6f4df 100644 --- a/app/controllers/api/v1/invitations_controller.rb +++ b/app/controllers/api/v1/invitations_controller.rb @@ -4,7 +4,7 @@ class Api::V1::InvitationsController < ApplicationController # GET /api/v1/invitations def index @invitations = Invitation.all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all invitations.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all invitations.", request) render json: @invitations, status: :ok end @@ -14,10 +14,10 @@ def create @invitation = Invitation.invitation_factory(invite_params) if @invitation.save @invitation.send_invite_email - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create invitation. Errors: #{@invitation.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create invitation. Errors: #{@invitation.errors.full_messages.join(', ')}", request) render json: { error: @invitation.errors }, status: :unprocessable_entity end end @@ -25,7 +25,7 @@ def create # GET /api/v1/invitations/:id def show @invitation = Invitation.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok end @@ -35,14 +35,14 @@ def update case params[:reply_status] when InvitationValidator::ACCEPT_STATUS @invitation.accept_invitation(nil) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Accepted invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Accepted invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok when InvitationValidator::REJECT_STATUS @invitation.decline_invitation(nil) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Rejected invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Rejected invitation with ID: #{@invitation.id}.", request) render json: @invitation, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invalid reply status for invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Invalid reply status for invitation with ID: #{@invitation.id}.", request) render json: @invitation.errors, status: :unprocessable_entity end @@ -52,7 +52,7 @@ def update def destroy @invitation = Invitation.find(params[:id]) @invitation.retract_invitation(nil) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted invitation with ID: #{@invitation.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted invitation with ID: #{@invitation.id}.", request) render nothing: true, status: :no_content end @@ -61,7 +61,7 @@ def invitations_for_user_assignment begin @user = User.find(params[:user_id]) rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User not found with ID: #{params[:user_id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "User not found with ID: #{params[:user_id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found return end @@ -69,13 +69,13 @@ def invitations_for_user_assignment begin @assignment = Assignment.find(params[:assignment_id]) rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment not found with ID: #{params[:assignment_id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment not found with ID: #{params[:assignment_id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found return end @invitations = Invitation.where(to_id: @user.id).where(assignment_id: @assignment.id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched invitations for user ID: #{@user.id} and assignment ID: #{@assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched invitations for user ID: #{@user.id} and assignment ID: #{@assignment.id}.", request) render json: @invitations, status: :ok end @@ -92,7 +92,7 @@ def invite_params # helper method used when invite is not found def invite_not_found - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Invitation not found with ID: #{params[:id]}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Invitation not found with ID: #{params[:id]}.", request) render json: { error: "Invitation with id #{params[:id]} not found" }, status: :not_found end diff --git a/app/controllers/api/v1/join_team_requests_controller.rb b/app/controllers/api/v1/join_team_requests_controller.rb index b51f6bb14..4bbd0beb1 100644 --- a/app/controllers/api/v1/join_team_requests_controller.rb +++ b/app/controllers/api/v1/join_team_requests_controller.rb @@ -19,11 +19,11 @@ def action_allowed? # gets a list of all the join team requests def index unless @current_user.administrator? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Unauthorized access to join team requests.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Unauthorized access to join team requests.", request) return render json: { errors: 'Unauthorized' }, status: :unauthorized end join_team_requests = JoinTeamRequest.all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all join team requests.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all join team requests.", request) render json: join_team_requests, status: :ok end @@ -44,19 +44,19 @@ def create team = Team.find(params[:team_id]) if team.participants.include?(participant) - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User already belongs to team ID: #{team.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "User already belongs to team ID: #{team.id}.", request) render json: { error: 'You already belong to the team' }, status: :unprocessable_entity elsif participant join_team_request.participant_id = participant.id if join_team_request.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created join team request with ID: #{join_team_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created join team request with ID: #{join_team_request.id}.", request) render json: join_team_request, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create join team request. Errors: #{join_team_request.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create join team request. Errors: #{join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: join_team_request.errors.full_messages }, status: :unprocessable_entity end else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Participant not found for user ID: #{@current_user.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Participant not found for user ID: #{@current_user.id}.", request) render json: { errors: 'Participant not found' }, status: :unprocessable_entity end end @@ -65,10 +65,10 @@ def create # Updates a join team request def update if @join_team_request.update(join_team_request_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated join team request with ID: #{@join_team_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest was successfully updated' }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity end end @@ -77,10 +77,10 @@ def update # delete a join team request def destroy if @join_team_request.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted join team request with ID: #{@join_team_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest was successfully deleted' }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete join team request with ID: #{@join_team_request.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to delete join team request with ID: #{@join_team_request.id}.", request) render json: { errors: 'Failed to delete JoinTeamRequest' }, status: :unprocessable_entity end end @@ -89,10 +89,10 @@ def destroy def decline @join_team_request.status = DECLINED if @join_team_request.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Declined join team request with ID: #{@join_team_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Declined join team request with ID: #{@join_team_request.id}.", request) render json: { message: 'JoinTeamRequest declined successfully' }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to decline join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to decline join team request with ID: #{@join_team_request.id}. Errors: #{@join_team_request.errors.full_messages.join(', ')}", request) render json: { errors: @join_team_request.errors.full_messages }, status: :unprocessable_entity end end @@ -102,7 +102,7 @@ def decline def check_team_status team = Team.find(params[:team_id]) if team.full? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Attempted to join a full team with ID: #{team.id}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Attempted to join a full team with ID: #{team.id}.", request) render json: { message: 'This team is full.' }, status: :unprocessable_entity end end @@ -110,7 +110,7 @@ def check_team_status # Finds the join team request by ID def find_request @join_team_request = JoinTeamRequest.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Found join team request with ID: #{@join_team_request.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Found join team request with ID: #{@join_team_request.id}.", request) end # Permits specified parameters for join team requests diff --git a/app/controllers/api/v1/questionnaires_controller.rb b/app/controllers/api/v1/questionnaires_controller.rb index d7a659af2..0996f91d5 100644 --- a/app/controllers/api/v1/questionnaires_controller.rb +++ b/app/controllers/api/v1/questionnaires_controller.rb @@ -4,7 +4,7 @@ class Api::V1::QuestionnairesController < ApplicationController # GET on /questionnaires def index @questionnaires = Questionnaire.order(:id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all questionnaires.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all questionnaires.", request) render json: @questionnaires, status: :ok and return end @@ -13,10 +13,10 @@ def index def show begin @questionnaire = Questionnaire.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched questionnaire with ID: #{@questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok and return rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -29,10 +29,10 @@ def create @questionnaire = Questionnaire.new(questionnaire_params) @questionnaire.display_type = sanitize_display_type(@questionnaire.questionnaire_type) @questionnaire.save! - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created questionnaire with ID: #{@questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :created and return rescue ActiveRecord::RecordInvalid => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create questionnaire. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create questionnaire. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -43,9 +43,9 @@ def destroy begin @questionnaire = Questionnaire.find(params[:id]) @questionnaire.delete - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted questionnaire with ID: #{@questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted questionnaire with ID: #{@questionnaire.id}.", request) rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -56,10 +56,10 @@ def destroy def update @questionnaire = Questionnaire.find(params[:id]) if @questionnaire.update(questionnaire_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated questionnaire with ID: #{@questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update questionnaire with ID: #{@questionnaire.id}. Errors: #{@questionnaire.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update questionnaire with ID: #{@questionnaire.id}. Errors: #{@questionnaire.errors.full_messages.join(', ')}", request) render json: @questionnaire.errors.full_messages, status: :unprocessable_entity end end @@ -68,13 +68,13 @@ def update def copy begin @questionnaire = Questionnaire.copy_questionnaire_details(params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Copied questionnaire with ID: #{params[:id]} to new questionnaire with ID: #{@questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Copied questionnaire with ID: #{params[:id]} to new questionnaire with ID: #{@questionnaire.id}.", request) render json: @questionnaire, status: :ok and return rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found and return rescue ActiveRecord::RecordInvalid => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to copy questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to copy questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end @@ -87,14 +87,14 @@ def toggle_access @questionnaire = Questionnaire.find(params[:id]) @questionnaire.toggle!(:private) @access = @questionnaire.private ? 'private' : 'public' - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Toggled access for questionnaire with ID: #{@questionnaire.id}. Now it is #{@access}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Toggled access for questionnaire with ID: #{@questionnaire.id}. Now it is #{@access}.", request) render json: "The questionnaire \"#{@questionnaire.name}\" has been successfully made #{@access}. ", status: :ok rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :not_found rescue ActiveRecord::RecordInvalid => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to toggle access for questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to toggle access for questionnaire with ID: #{params[:id]}. Error: #{e.message}", request) render json: $ERROR_INFO.to_s, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/questions_controller.rb b/app/controllers/api/v1/questions_controller.rb index 01bb8cdd6..fc8d92ae8 100644 --- a/app/controllers/api/v1/questions_controller.rb +++ b/app/controllers/api/v1/questions_controller.rb @@ -4,7 +4,7 @@ class Api::V1::QuestionsController < ApplicationController # GET on /questions def index @questions = Question.order(:id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all questions.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all questions.", request) render json: @questions, status: :ok end @@ -13,10 +13,10 @@ def index def show begin @question = Question.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched question with ID: #{@question.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched question with ID: #{@question.id}.", request) render json: @question, status: :ok rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Unable to find Question with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Unable to find Question with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found end end @@ -51,14 +51,14 @@ def create end if question.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created question with ID: #{question.id} under questionnaire ID: #{questionnaire.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created question with ID: #{question.id} under questionnaire ID: #{questionnaire.id}.", request) render json: question, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question not able to be saved: #{question.as_json}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Question not able to be saved: #{question.as_json}", request) render json: question.errors.full_messages.to_sentence, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire not able to found with ID: #{params[:questionnaire_id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire not able to found with ID: #{params[:questionnaire_id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end @@ -69,9 +69,9 @@ def destroy begin @question = Question.find(params[:id]) @question.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Destroyed Question: #{@question.as_json}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Destroyed Question: #{@question.as_json}", request) rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be found with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Question unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end @@ -82,10 +82,10 @@ def show_all begin @questionnaire = Questionnaire.find(params[:id]) @questions = @questionnaire.questions - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched #{@questions.count} Questions for Questionnaire with ID: #{params[:id]}", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched #{@questions.count} Questions for Questionnaire with ID: #{params[:id]}", request) render json: @questions, status: :ok rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire unable to be found with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found end end @@ -98,15 +98,15 @@ def delete_all begin questionnaire = Questionnaire.find(params[:id]) if questionnaire.questions.empty? - ExpertizaLogger.warn LoggerMessage.new(controller_name, session[:user].name, "Questions attempted to be destroyed for Questionnaire with ID #{params[:id]}, but found to already not have any questions", request) + ExpertizaLogger.warn LoggerMessage.new(controller_name, @current_user.name, "Questions attempted to be destroyed for Questionnaire with ID #{params[:id]}, but found to already not have any questions", request) render json: "No questions associated with questionnaire ID #{params[:id]}.", status: :unprocessable_entity and return end questionnaire.questions.destroy_all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Questionnaire with ID #{params[:id]} had all questions deleted", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Questionnaire with ID #{params[:id]} had all questions deleted", request) render json: "All questions for questionnaire ID #{params[:id]} have been deleted.", status: :ok rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Questionnaire unable to be found with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Questionnaire unable to be found with ID: #{params[:id]}", request) render json: "Questionnaire ID #{params[:id]} not found.", status: :not_found and return end end @@ -117,14 +117,14 @@ def update begin @question = Question.find(params[:id]) if @question.update(question_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated question with ID: #{@question.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated question with ID: #{@question.id}.", request) render json: @question, status: :ok and return else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be updated with: #{question_params}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Question unable to be updated with: #{question_params}", request) render json: "Failed to update the question.", status: :unprocessable_entity and return end rescue ActiveRecord::RecordNotFound - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Question unable to be found with ID: #{params[:id]}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Question unable to be found with ID: #{params[:id]}", request) render json: $ERROR_INFO.to_s, status: :not_found and return end end diff --git a/app/controllers/api/v1/roles_controller.rb b/app/controllers/api/v1/roles_controller.rb index d7124a6ab..247889df6 100644 --- a/app/controllers/api/v1/roles_controller.rb +++ b/app/controllers/api/v1/roles_controller.rb @@ -5,14 +5,14 @@ class Api::V1::RolesController < ApplicationController # GET /roles def index roles = Role.order(:id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all roles.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all roles.", request) render json: roles, status: :ok end # GET /roles/:id def show role = Role.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched role with ID: #{role.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched role with ID: #{role.id}.", request) render json: role, status: :ok end @@ -20,10 +20,10 @@ def show def create role = Role.new(role_params) if role.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created role with ID: #{role.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created role with ID: #{role.id}.", request) render json: role, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create role. Errors: #{role.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create role. Errors: #{role.errors.full_messages.join(', ')}", request) render json: role.errors, status: :unprocessable_entity end end @@ -32,10 +32,10 @@ def create def update role = Role.find(params[:id]) if role.update(role_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated role with ID: #{role.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated role with ID: #{role.id}.", request) render json: role, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update role with ID: #{role.id}. Errors: #{role.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update role with ID: #{role.id}. Errors: #{role.errors.full_messages.join(', ')}", request) render json: role.errors, status: :unprocessable_entity end end @@ -45,14 +45,14 @@ def destroy role = Role.find(params[:id]) role_name = role.name role.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted role with ID: #{role.id}, Name: #{role_name}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted role with ID: #{role.id}, Name: #{role_name}.", request) render json: { message: "Role #{role_name} with id #{params[:id]} deleted successfully!" }, status: :no_content end def subordinate_roles role = current_user.role roles = role.subordinate_roles - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched subordinate roles for role ID: #{role.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched subordinate roles for role ID: #{role.id}.", request) render json: roles, status: :ok end @@ -68,7 +68,7 @@ def role_params # end def parameter_missing - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Parameter missing.", request) render json: { error: 'Parameter missing' }, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/sign_up_topics_controller.rb b/app/controllers/api/v1/sign_up_topics_controller.rb index 70ea074d5..4deb6a7ec 100644 --- a/app/controllers/api/v1/sign_up_topics_controller.rb +++ b/app/controllers/api/v1/sign_up_topics_controller.rb @@ -5,15 +5,15 @@ class Api::V1::SignUpTopicsController < ApplicationController # Retrieve SignUpTopics by two query parameters - assignment_id (compulsory) and an array of topic_ids (optional) def index if params[:assignment_id].nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment ID is missing.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment ID is missing.", request) render json: { message: 'Assignment ID is required!' }, status: :unprocessable_entity elsif params[:topic_ids].nil? @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]}.", request) render json: @sign_up_topics, status: :ok else @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id], topic_identifier: params[:topic_ids]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]} with topic IDs: #{params[:topic_ids].join(', ')}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched sign-up topics for assignment ID: #{params[:assignment_id]} with topic IDs: #{params[:topic_ids].join(', ')}.", request) render json: @sign_up_topics, status: :ok end # render json: {message: 'All selected topics have been loaded successfully.', sign_up_topics: @stopics}, status: 200 @@ -29,10 +29,10 @@ def create @sign_up_topic.micropayment = params[:micropayment] if @assignment.microtask? if @sign_up_topic.save # undo_link "The topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. " - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created sign-up topic with ID: #{@sign_up_topic.id} for assignment ID: #{@assignment.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created sign-up topic with ID: #{@sign_up_topic.id} for assignment ID: #{@assignment.id}.", request) render json: { message: "The topic: \"#{@sign_up_topic.topic_name}\" has been created successfully. " }, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create sign-up topic. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create sign-up topic. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) render json: { message: @sign_up_topic.errors }, status: :unprocessable_entity end end @@ -41,10 +41,10 @@ def create # updates parameters present in sign_up_topic_params. def update if @sign_up_topic.update(sign_up_topic_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated sign-up topic with ID: #{@sign_up_topic.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated sign-up topic with ID: #{@sign_up_topic.id}.", request) render json: { message: "The topic: \"#{@sign_up_topic.topic_name}\" has been updated successfully. " }, status: 200 else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update sign-up topic with ID: #{@sign_up_topic.id}. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update sign-up topic with ID: #{@sign_up_topic.id}. Errors: #{@sign_up_topic.errors.full_messages.join(', ')}", request) render json: @sign_up_topic.errors, status: :unprocessable_entity end end @@ -61,7 +61,7 @@ def destroy # render json: {message: @sign_up_topic} # filters topics based on assignment id (required) and topic identifiers (optional) if params[:assignment_id].nil? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Assignment ID is missing for destroy action.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Assignment ID is missing for destroy action.", request) render json: { message: 'Assignment ID is required!' }, status: :unprocessable_entity elsif params[:topic_ids].nil? @sign_up_topics = SignUpTopic.where(assignment_id: params[:assignment_id]) @@ -72,10 +72,10 @@ def destroy end if @sign_up_topics.each(&:delete) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted sign-up topics.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted sign-up topics.", request) render json: { message: "The topic has been deleted successfully. " }, status: :no_content else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete sign-up topics.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to delete sign-up topics.", request) render json: @sign_up_topic.errors, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/signed_up_teams_controller.rb b/app/controllers/api/v1/signed_up_teams_controller.rb index e071d6d91..ee319bbac 100644 --- a/app/controllers/api/v1/signed_up_teams_controller.rb +++ b/app/controllers/api/v1/signed_up_teams_controller.rb @@ -6,7 +6,7 @@ def index # puts params[:topic_id] @sign_up_topic = SignUpTopic.find(params[:topic_id]) @signed_up_team = SignedUpTeam.find_team_participants(@sign_up_topic.assignment_id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched signed-up team for topic ID: #{@sign_up_topic.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched signed-up team for topic ID: #{@sign_up_topic.id}.", request) render json: @signed_up_team end @@ -17,10 +17,10 @@ def create; end def update @signed_up_team = SignedUpTeam.find(params[:id]) if @signed_up_team.update(signed_up_teams_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated signed-up team with team ID: #{@signed_up_team.team_id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated signed-up team with team ID: #{@signed_up_team.team_id}.", request) render json: { message: "The team has been updated successfully. " }, status: 200 else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: @signed_up_team.errors, status: :unprocessable_entity end end @@ -32,10 +32,10 @@ def sign_up topic_id = params[:topic_id] @signed_up_team = SignedUpTeam.create_signed_up_team(topic_id, team_id) if @signed_up_team - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Team ID: #{team_id} signed up for topic ID: #{topic_id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Team ID: #{team_id} signed up for topic ID: #{topic_id}.", request) render json: { message: "Signed up team successful!" }, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to sign up team ID: #{team_id} for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to sign up team ID: #{team_id} for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: { message: @signed_up_team.errors }, status: :unprocessable_entity end end @@ -53,10 +53,10 @@ def sign_up_student @signed_up_team = SignedUpTeam.create_signed_up_team(topic_id, team_id) # create(topic_id, team_id) if @signed_up_team - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "User ID: #{user_id} (Team ID: #{team_id}) signed up for topic ID: #{topic_id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "User ID: #{user_id} (Team ID: #{team_id}) signed up for topic ID: #{topic_id}.", request) render json: { message: "Signed up team successful!" }, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to sign up user ID: #{user_id} (Team ID: #{team_id}) for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to sign up user ID: #{user_id} (Team ID: #{team_id}) for topic ID: #{topic_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: { message: @signed_up_team.errors }, status: :unprocessable_entity end end @@ -65,10 +65,10 @@ def sign_up_student def destroy @signed_up_team = SignedUpTeam.find(params[:id]) if SignedUpTeam.delete_signed_up_team(@signed_up_team.team_id) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted signed-up team with team ID: #{@signed_up_team.team_id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted signed-up team with team ID: #{@signed_up_team.team_id}.", request) render json: { message: 'Signed up teams was deleted successfully!' }, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to delete signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to delete signed-up team with team ID: #{@signed_up_team.team_id}. Errors: #{@signed_up_team.errors.full_messages.join(', ')}", request) render json: @signed_up_team.errors, status: :unprocessable_entity end end diff --git a/app/controllers/api/v1/student_tasks_controller.rb b/app/controllers/api/v1/student_tasks_controller.rb index ce4d6e282..b356a2b47 100644 --- a/app/controllers/api/v1/student_tasks_controller.rb +++ b/app/controllers/api/v1/student_tasks_controller.rb @@ -4,7 +4,7 @@ class Api::V1::StudentTasksController < ApplicationController def list # Retrieves all tasks that belong to the current user. @student_tasks = StudentTask.from_user(current_user) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched student tasks for user ID: #{current_user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched student tasks for user ID: #{current_user.id}.", request) # Render the list of student tasks as JSON. render json: @student_tasks, status: :ok end @@ -19,7 +19,7 @@ def view # Retrieves the student task where the participant's ID matches the provided parameter. # This function will be used for clicking on a specific student task to "view" its details. @student_task = StudentTask.from_participant_id(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched student task for participant ID: #{params[:id]}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched student task for participant ID: #{params[:id]}.", request) # Render the found student task as JSON. render json: @student_task, status: :ok end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index fd5b88b76..0dc11567c 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -4,14 +4,14 @@ class Api::V1::UsersController < ApplicationController def index users = User.all - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched all users.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched all users.", request) render json: users, status: :ok end # GET /users/:id def show user = User.find(params[:id]) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched user with ID: #{user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched user with ID: #{user.id}.", request) render json: user, status: :ok end @@ -21,10 +21,10 @@ def create params[:user][:password] ||= 'password' user = User.new(user_params) if user.save - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Created user with ID: #{user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Created user with ID: #{user.id}.", request) render json: user, status: :created else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to create user. Errors: #{user.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to create user. Errors: #{user.errors.full_messages.join(', ')}", request) render json: user.errors, status: :unprocessable_entity end end @@ -33,10 +33,10 @@ def create def update user = User.find(params[:id]) if user.update(user_params) - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Updated user with ID: #{user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Updated user with ID: #{user.id}.", request) render json: user, status: :ok else - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Failed to update user with ID: #{user.id}. Errors: #{user.errors.full_messages.join(', ')}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Failed to update user with ID: #{user.id}. Errors: #{user.errors.full_messages.join(', ')}", request) render json: user.errors, status: :unprocessable_entity end end @@ -45,7 +45,7 @@ def update def destroy user = User.find(params[:id]) user.destroy - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Deleted user with ID: #{user.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Deleted user with ID: #{user.id}.", request) render json: { message: "User #{user.name} with id #{params[:id]} deleted successfully!" }, status: :no_content end @@ -54,10 +54,10 @@ def destroy def institution_users institution = Institution.find(params[:id]) users = institution.users - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched users for institution ID: #{institution.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched users for institution ID: #{institution.id}.", request) render json: users, status: :ok rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Institution not found with ID: #{params[:id]}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -66,13 +66,13 @@ def institution_users def managed_users parent = User.find(params[:id]) if parent.student? - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User ID: #{parent.id} is a student and cannot manage users.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "User ID: #{parent.id} is a student and cannot manage users.", request) render json: { error: 'Students do not manage any users' }, status: :unprocessable_entity return end parent = User.instantiate(parent) users = parent.managed_users - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched managed users for user ID: #{parent.id}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched managed users for user ID: #{parent.id}.", request) render json: users, status: :ok end @@ -82,10 +82,10 @@ def role_users name = params[:name].split('_').map(&:capitalize).join(' ') role = Role.find_by(name:) users = role.users - ExpertizaLogger.info LoggerMessage.new(controller_name, session[:user].name, "Fetched users for role: #{name}.", request) + ExpertizaLogger.info LoggerMessage.new(controller_name, @current_user.name, "Fetched users for role: #{name}.", request) render json: users, status: :ok rescue ActiveRecord::RecordNotFound => e - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Role not found with name: #{name}. Error: #{e.message}", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Role not found with name: #{name}. Error: #{e.message}", request) render json: { error: e.message }, status: :not_found end @@ -99,12 +99,12 @@ def user_params end def user_not_found - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "User not found with ID: #{params[:id]}.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "User not found with ID: #{params[:id]}.", request) render json: { error: "User with id #{params[:id]} not found" }, status: :not_found end def parameter_missing - ExpertizaLogger.error LoggerMessage.new(controller_name, session[:user].name, "Parameter missing.", request) + ExpertizaLogger.error LoggerMessage.new(controller_name, @current_user.name, "Parameter missing.", request) render json: { error: 'Parameter missing' }, status: :unprocessable_entity end end