diff --git a/app/controllers/internal_api/v1/custom_leaves_controller.rb b/app/controllers/internal_api/v1/custom_leaves_controller.rb new file mode 100644 index 0000000000..1a6ca7b8ad --- /dev/null +++ b/app/controllers/internal_api/v1/custom_leaves_controller.rb @@ -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 diff --git a/app/controllers/internal_api/v1/leaves_controller.rb b/app/controllers/internal_api/v1/leaves_controller.rb index 6f05f405a7..23fe06f942 100644 --- a/app/controllers/internal_api/v1/leaves_controller.rb +++ b/app/controllers/internal_api/v1/leaves_controller.rb @@ -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: } diff --git a/app/models/custom_leave.rb b/app/models/custom_leave.rb new file mode 100644 index 0000000000..3c49427c7f --- /dev/null +++ b/app/models/custom_leave.rb @@ -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 diff --git a/app/models/custom_leave_user.rb b/app/models/custom_leave_user.rb new file mode 100644 index 0000000000..7c4fe8f4b9 --- /dev/null +++ b/app/models/custom_leave_user.rb @@ -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 diff --git a/app/models/device.rb b/app/models/device.rb index b2ed741a45..223e87d46c 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -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 # diff --git a/app/models/leave.rb b/app/models/leave.rb index 5690a892b0..7c2eefdca2 100644 --- a/app/models/leave.rb +++ b/app/models/leave.rb @@ -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, diff --git a/app/models/user.rb b/app/models/user.rb index 31291c2b68..f2b6b854fc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/services/custom_leaves_service.rb b/app/services/custom_leaves_service.rb new file mode 100644 index 0000000000..fb9d6a39db --- /dev/null +++ b/app/services/custom_leaves_service.rb @@ -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 diff --git a/app/views/internal_api/v1/leaves/index.json.jbuilder b/app/views/internal_api/v1/leaves/index.json.jbuilder index e5207525a5..b80d6b913f 100644 --- a/app/views/internal_api/v1/leaves/index.json.jbuilder +++ b/app/views/internal_api/v1/leaves/index.json.jbuilder @@ -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 diff --git a/config/routes/internal_api.rb b/config/routes/internal_api.rb index 169dfcf185..ed64750fec 100644 --- a/config/routes/internal_api.rb +++ b/config/routes/internal_api.rb @@ -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") } diff --git a/db/migrate/20240426050940_create_custom_leaves.rb b/db/migrate/20240426050940_create_custom_leaves.rb new file mode 100644 index 0000000000..eb4896589b --- /dev/null +++ b/db/migrate/20240426050940_create_custom_leaves.rb @@ -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 diff --git a/db/migrate/20240426053100_create_custom_leave_users.rb b/db/migrate/20240426053100_create_custom_leave_users.rb new file mode 100644 index 0000000000..c1b57d774e --- /dev/null +++ b/db/migrate/20240426053100_create_custom_leave_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 66a85e37c2..a7ec5d472f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -162,6 +162,25 @@ t.boolean "calendar_enabled", default: true end + create_table "custom_leave_users", force: :cascade do |t| + t.bigint "custom_leave_id", null: false + t.bigint "user_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["custom_leave_id"], name: "index_custom_leave_users_on_custom_leave_id" + t.index ["user_id"], name: "index_custom_leave_users_on_user_id" + end + + create_table "custom_leaves", force: :cascade do |t| + t.string "name", null: false + t.integer "allocation_value", null: false + t.integer "allocation_period", null: false + t.bigint "leave_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["leave_id"], name: "index_custom_leaves_on_leave_id" + end + create_table "data_migrations", primary_key: "version", id: :string, force: :cascade do |t| end @@ -556,6 +575,9 @@ add_foreign_key "client_members", "companies" add_foreign_key "client_members", "users" add_foreign_key "clients", "companies" + add_foreign_key "custom_leave_users", "custom_leaves", column: "custom_leave_id" + add_foreign_key "custom_leave_users", "users" + add_foreign_key "custom_leaves", "leaves", column: "leave_id" add_foreign_key "devices", "companies" add_foreign_key "devices", "users" add_foreign_key "employments", "companies"