Skip to content

Commit

Permalink
Feat: Custom leaves (#1823)
Browse files Browse the repository at this point in the history
* crud custom leaves

* send custom leaves in index action

* review comments
  • Loading branch information
apoorv1316 authored May 21, 2024
1 parent e857d09 commit a518d19
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 11 deletions.
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
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
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
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
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
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.

0 comments on commit a518d19

Please sign in to comment.