Skip to content

Commit

Permalink
Handle carryover leaves using Carryover model (#1824)
Browse files Browse the repository at this point in the history
- This will be used to calculate previous year carryforward leaves count

Co-authored-by: Nishant Samel <nishant@saeloun.com>
  • Loading branch information
nisusam and Nishant Samel authored May 2, 2024
1 parent 7f63ebe commit b3d3b84
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 10 deletions.
40 changes: 40 additions & 0 deletions app/models/carryover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: carryovers
#
# id :bigint not null, primary key
# discarded_at :datetime
# duration :integer
# from_year :integer
# to_year :integer
# total_leave_balance :integer
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# leave_type_id :bigint not null
# user_id :bigint not null
#
# Indexes
#
# index_carryovers_on_company_id (company_id)
# index_carryovers_on_discarded_at (discarded_at)
# index_carryovers_on_leave_type_id (leave_type_id)
# index_carryovers_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (company_id => companies.id)
# fk_rails_... (leave_type_id => leave_types.id)
# fk_rails_... (user_id => users.id)
#
class Carryover < ApplicationRecord
include Discard::Model

belongs_to :user
belongs_to :company
belongs_to :leave_type

validates :from_year, :to_year, :total_leave_balance, :duration, presence: true
end
1 change: 1 addition & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Company < ApplicationRecord
has_many :timeoff_entries, through: :users
has_many :holidays, dependent: :destroy
has_many :holiday_infos, through: :holidays, dependent: :destroy
has_many :carryovers

resourcify

Expand Down
1 change: 1 addition & 0 deletions app/models/leave_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class LeaveType < ApplicationRecord
belongs_to :leave, class_name: "Leave"

has_many :timeoff_entries, dependent: :destroy
has_many :carryovers

validates :name, presence: true, format: { with: /\A[a-zA-Z\s]+\z/ }, length: { maximum: 20 }
validates :color, presence: true, uniqueness: { scope: :leave_id, message: "has already been taken for this leave" }
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ 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 :carryovers

rolify strict: true

Expand Down
18 changes: 9 additions & 9 deletions app/services/timeoff_entries/index_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ def user_joined_date
def calculate_previous_year_carryforward(leave_type)
return 0 unless leave_type

total_leave_type_days = calculate_total_duration(leave_type, previous_year)

timeoff_entries_duration = leave_type.timeoff_entries.kept.where(user_id:).sum(:duration)

net_duration = (total_leave_type_days * 8 * 60) - timeoff_entries_duration

carry_forward_duration = leave_type.carry_forward_days * 8 * 60

net_duration > carry_forward_duration ? carry_forward_duration : net_duration > 0 ? net_duration : 0
last_year_carryover =
Carryover.find_by(
user_id:,
company_id: current_company.id,
leave_type_id: leave_type.id,
from_year: previous_year
)

last_year_carryover && (last_year_carryover.duration > 0) ? last_year_carryover.duration : 0
end
end
end
19 changes: 19 additions & 0 deletions db/migrate/20240429142938_create_carryovers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class CreateCarryovers < ActiveRecord::Migration[7.1]
def change
create_table :carryovers do |t|
t.references :user, null: false, foreign_key: true
t.references :company, null: false, foreign_key: true
t.references :leave_type, null: false, foreign_key: true
t.integer :from_year
t.integer :to_year
t.integer :total_leave_balance
t.integer :duration
t.datetime :discarded_at

t.timestamps
end
add_index :carryovers, :discarded_at
end
end
22 changes: 21 additions & 1 deletion db/schema.rb

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

14 changes: 14 additions & 0 deletions spec/factories/carryovers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

FactoryBot.define do
factory :carryover do
user
company
leave_type
from_year { 2023 }
to_year { 2024 }
total_leave_balance { 10000 } # in minutes
duration { 2400 } # in minutes
discarded_at { "2024-04-29 19:59:39" }
end
end
20 changes: 20 additions & 0 deletions spec/models/carryover_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Carryover, type: :model do
subject { build(:carryover) }

describe "Associations" do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:company) }
it { is_expected.to belong_to(:leave_type) }
end

describe "Validations" do
it { is_expected.to validate_presence_of(:from_year) }
it { is_expected.to validate_presence_of(:to_year) }
it { is_expected.to validate_presence_of(:total_leave_balance) }
it { is_expected.to validate_presence_of(:duration) }
end
end

0 comments on commit b3d3b84

Please sign in to comment.