Skip to content

Commit

Permalink
Merge branch 'develop' into prevent-editing-entries-added-to-draft-in…
Browse files Browse the repository at this point in the history
…voice
  • Loading branch information
apoorv1316 committed May 29, 2024
2 parents a81d92a + 826f133 commit 17f7754
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 38 deletions.
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
retriable (3.1.2)
rexml (3.2.6)
rexml (3.2.8)
strscan (>= 3.0.9)
rolify (6.0.1)
rspec-buildkite (0.1.6)
rspec-core (~> 3.0)
Expand Down Expand Up @@ -624,6 +625,7 @@ GEM
stripe (8.2.0)
strong_migrations (1.4.2)
activerecord (>= 5.2)
strscan (3.1.0)
thor (1.3.1)
tilt (2.0.11)
timeout (0.4.1)
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/internal_api/v1/custom_leaves_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class InternalApi::V1::CustomLeavesController < InternalApi::V1::ApplicationController
before_action :set_leave, only: [:update]

def update
authorize current_user, policy_class: LeaveWithLeaveTypesPolicy
CustomLeavesService.new(leave, update_params).process
render json: { notice: "Leaves updated successfully" }, status: :ok
end

private

def set_leave
@_leave ||= current_company.leaves.find_or_create_by(year: params[:year])
end

def update_params
params.require(:custom_leaves).permit(
add_custom_leaves: [:name, :color, :icon, :allocation_value,
:allocation_period, user_ids: [],
],
update_custom_leaves: [:id, :name, :color, :icon, :allocation_value,
:allocation_period, user_ids: [],
],
remove_custom_leaves: []
)
end
end
7 changes: 1 addition & 6 deletions app/controllers/internal_api/v1/employments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class InternalApi::V1::EmploymentsController < InternalApi::V1::ApplicationContr

def index
authorize Employment
render :index, locals: { users: users_with_not_client_role }, status: :ok
render :index, locals: { users: current_company.employees_without_client_role }, status: :ok
end

def show
Expand All @@ -28,9 +28,4 @@ def set_employment
def employment_params
params.require(:employment).permit(:designation, :employment_type, :joined_at, :resigned_at, :employee_id)
end

def users_with_not_client_role
users_with_client_role_ids = current_company.users.joins(:roles).where(roles: { name: "client" }).pluck(:id)
current_company.users.kept.where.not(id: users_with_client_role_ids)
end
end
2 changes: 1 addition & 1 deletion app/controllers/internal_api/v1/leaves_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class InternalApi::V1::LeavesController < InternalApi::V1::ApplicationController

def index
authorize Leave
leaves = current_company.leaves.includes([:leave_types])
leaves = current_company.leaves.includes([:leave_types, :custom_leaves])
render :index, locals: {
leaves:
}
Expand Down
10 changes: 9 additions & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ def billable_clients
end

def employees_without_client_role
users.with_kept_employments.joins(:roles).where.not(roles: { name: "client" }).distinct
user_ids_with_only_client_role = users.with_kept_employments
.joins(:roles)
.group("users.id, roles.resource_id, roles.resource_type")
.having("COUNT(roles.id) = 1 AND MAX(roles.name) = 'client' \
AND roles.resource_id = #{id} \
AND roles.resource_type = 'Company'")
.pluck("users.id")

users.with_kept_employments.where.not(id: user_ids_with_only_client_role).distinct
end
end
36 changes: 36 additions & 0 deletions app/models/custom_leave.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: custom_leaves
#
# id :bigint not null, primary key
# allocation_period :integer not null
# allocation_value :integer not null
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# leave_id :bigint not null
#
# Indexes
#
# index_custom_leaves_on_leave_id (leave_id)
#
# Foreign Keys
#
# fk_rails_... (leave_id => leaves.id)
#
class CustomLeave < ApplicationRecord
belongs_to :leave
has_many :custom_leave_users, dependent: :destroy
has_many :users, through: :custom_leave_users

enum allocation_period: {
days: 0,
weeks: 1,
months: 2
}

validates :name, :allocation_value, :allocation_period, presence: true
validates :allocation_value, numericality: { greater_than_or_equal_to: 1 }
end
26 changes: 26 additions & 0 deletions app/models/custom_leave_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: custom_leave_users
#
# id :bigint not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# custom_leave_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_custom_leave_users_on_custom_leave_id (custom_leave_id)
# index_custom_leave_users_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (custom_leave_id => custom_leaves.id)
# fk_rails_... (user_id => users.id)
#
class CustomLeaveUser < ApplicationRecord
belongs_to :custom_leave
belongs_to :user
end
21 changes: 12 additions & 9 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
#
# Table name: devices
#
# id :bigint not null, primary key
# device_type :string default("laptop")
# name :string
# serial_number :string
# specifications :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# user_id :bigint not null
# id :bigint not null, primary key
# device_type :string default("laptop")
# insurance_bought_date :date
# insurance_expiry_date :date
# is_insured :boolean default(FALSE)
# name :string
# serial_number :string
# specifications :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
Expand Down
6 changes: 1 addition & 5 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ def recipient_email_not_changed
end
end

def users_not_with_client_role
company.employments.kept.joins(user: :roles).where.not(roles: { name: "client" })
end

def send_invitation_mail
user_already_exists = User.exists?(email: recipient_email)

company_details = {
name: company.name,
logo: company.company_logo,
employee_count: users_not_with_client_role.count
employee_count: company.employees_without_client_role.count
}

sender_details = {
Expand Down
1 change: 1 addition & 0 deletions app/models/leave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Leave < ApplicationRecord
belongs_to :company

has_many :leave_types, class_name: "LeaveType", dependent: :destroy
has_many :custom_leaves, class_name: "CustomLeave", dependent: :destroy
has_many :timeoff_entries, through: :leave_types

validates :year, presence: true,
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def initialize(msg = "Spam User Login")
has_many :clients, through: :projects
has_many :client_members, dependent: :destroy
has_many :timeoff_entries, dependent: :destroy
has_many :custom_leave_users
has_many :custom_leaves, through: :custom_leave_users, source: :custom_leave
has_many :carryovers

rolify strict: true
Expand Down
8 changes: 1 addition & 7 deletions app/services/clients/index_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def process
{
client_details:,
total_minutes:,
overdue_outstanding_amount:,
users_not_in_client_members:
overdue_outstanding_amount:
}
end

Expand Down Expand Up @@ -59,10 +58,5 @@ def total_minutes
def overdue_outstanding_amount
current_company.overdue_and_outstanding_and_draft_amount
end

def users_not_in_client_members
users_with_client_role = current_company.users.includes(:roles).where(roles: { name: "client" })
users_with_client_role.where.not(id: current_company.client_members.pluck(:user_id))
end
end
end
45 changes: 45 additions & 0 deletions app/services/custom_leaves_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class CustomLeavesService
attr_reader :leave, :params

def initialize(leave, params)
@leave = leave
@params = params
end

def process
ActiveRecord::Base.transaction do
add_custom_leaves
update_custom_leaves
remove_custom_leaves
end
end

private

def add_custom_leaves
return if params[:add_custom_leaves].blank?

params[:add_custom_leaves].each do |custom_leave|
leave.custom_leaves.create!(custom_leave)
end
end

def update_custom_leaves
return if params[:update_custom_leaves].blank?

params[:update_custom_leaves].each do |update_custom_leave|
custom_leave = leave.custom_leaves.find(update_custom_leave[:id])
next unless custom_leave

custom_leave.update!(update_custom_leave)
end
end

def remove_custom_leaves
return if params[:remove_custom_leaves].blank?

leave.custom_leaves.where(id: params[:remove_custom_leaves]).destroy_all
end
end
7 changes: 1 addition & 6 deletions app/services/reports/time_entries/report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def filter_options
if get_filters
@_filter_options ||= {
clients: current_company.clients.includes([:logo_attachment]).order(:name),
team_members: users_not_client_role.order(:first_name),
team_members: current_company.employees_without_client_role.order(:first_name),
projects: current_company.projects.as_json(only: [:id, :name])
}
end
Expand Down Expand Up @@ -120,11 +120,6 @@ def active_time_entries
{ discarded_at: nil }
end

def users_not_client_role
users_with_client_role_ids = current_company.users.joins(:roles).where(roles: { name: "client" }).pluck(:id)
current_company.users.where.not(id: users_with_client_role_ids)
end

def pagination_details
{
pages: @reports.total_pages,
Expand Down
10 changes: 10 additions & 0 deletions app/views/internal_api/v1/leaves/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ json.leaves leaves do |leave|
json.allocation_frequency leave_type.allocation_frequency
json.carry_forward_days leave_type.carry_forward_days
end
json.custom_leaves leave.custom_leaves do |custom_leave|
json.id custom_leave.id
json.name custom_leave.name
json.allocation_value custom_leave.allocation_value
json.allocation_period custom_leave.allocation_period
json.users custom_leave.users do |user|
json.id user.id
json.full_name user.full_name
end
end
end
2 changes: 1 addition & 1 deletion config/routes/internal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
resources :timeoff_entries, except: [:new, :edit]

patch "leave_with_leave_type/:year", to: "leave_with_leave_types#update", as: :update_leave_with_leave_types

patch "custom_leaves/:year", to: "custom_leaves#update"
match "*path", to: "application#not_found", via: :all, constraints: lambda { |req|
req.path.exclude?("rails/active_storage") && req.path.include?("internal_api")
}
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20240426050940_create_custom_leaves.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class CreateCustomLeaves < ActiveRecord::Migration[7.1]
def change
create_table :custom_leaves do |t|
t.string :name, null: false
t.integer :allocation_value, null: false
t.integer :allocation_period, null: false
t.references :leave, null: false, foreign_key: true

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240426053100_create_custom_leave_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateCustomLeaveUsers < ActiveRecord::Migration[7.1]
def change
create_table :custom_leave_users do |t|
t.references :custom_leave, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
22 changes: 22 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 17f7754

Please sign in to comment.