Skip to content

Commit

Permalink
Discard leaves and holidays (#1659)
Browse files Browse the repository at this point in the history
* discard leave and leave types with time off entries

* discard holidays and holiday infos with time off entries

* Updated time tracking controller and service

* no need to pass role with entries

* render only kept leaves

* fixed undefined project

* fixed the time tracking spec

* fixed time tracking specs

* fixed time tracking specs
  • Loading branch information
prasanthchaduvula authored Jan 31, 2024
1 parent 893ed70 commit 537cc8c
Show file tree
Hide file tree
Showing 32 changed files with 234 additions and 89 deletions.
2 changes: 1 addition & 1 deletion app/controllers/internal_api/v1/holidays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class InternalApi::V1::HolidaysController < ApplicationController
def index
authorize Holiday

holidays = current_company.holidays.includes([:holiday_infos])
holidays = current_company.holidays.kept.includes([:holiday_infos])
render :index, locals: { holidays: }, status: :ok
end

Expand Down
10 changes: 9 additions & 1 deletion app/controllers/internal_api/v1/time_tracking_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ def index
to = params[:to] || 1.month.since.end_of_month

data = TimeTrackingIndexService.new(current_user:, user: @user, company: current_company, from:, to:, year:).process
render json: data, status: :ok

render :index, locals: {
clients: data[:clients],
employees: data[:employees],
entries: data[:entries],
holiday_infos: data[:holiday_infos],
leave_types: data[:leave_types],
projects: data[:projects]
}, status: :ok
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def index
.during(params[:from], params[:to])
.includes(:user, :project, :client)
entries = TimesheetEntriesPresenter.new(timesheet_entries).group_snippets_by_work_date
entries[:currentUserRole] = current_user.primary_role current_company
render json: { entries: }, status: :ok
end

Expand Down
20 changes: 4 additions & 16 deletions app/javascript/src/components/TimeTracking/EntryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface props {
handleDeleteEntry: (id: number) => void; // eslint-disable-line
setEditEntryId: React.Dispatch<React.SetStateAction<number>>;
bill_status: string;
currentUserRole: string;
setNewEntryView: any;
handleDuplicate: any;
}
Expand Down Expand Up @@ -79,11 +78,10 @@ const EntryCard: React.FC<props> = ({
handleDeleteEntry,
setEditEntryId,
bill_status,
currentUserRole,
setNewEntryView,
handleDuplicate,
}) => {
const { isDesktop } = useUserContext();
const { isDesktop, companyRole } = useUserContext();

const handleCardClick = () => {
if (!isDesktop) {
Expand Down Expand Up @@ -163,19 +161,9 @@ const EntryCard: React.FC<props> = ({
<p className="mx-auto text-2xl xl:text-4xl">{minToHHMM(duration)}</p>
</div>
<div className="flex w-5/12 items-center justify-evenly">
{showDuplicateAction(
bill_status,
currentUserRole,
id,
handleDuplicate
)}
{showUpdateAction(bill_status, currentUserRole, id, setEditEntryId)}
{showDeleteAction(
bill_status,
currentUserRole,
id,
handleDeleteEntry
)}
{showDuplicateAction(bill_status, companyRole, id, handleDuplicate)}
{showUpdateAction(bill_status, companyRole, id, setEditEntryId)}
{showDeleteAction(bill_status, companyRole, id, handleDeleteEntry)}
</div>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion app/javascript/src/components/TimeTracking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ const TimeTracking: React.FC<Iprops> = ({ user, isAdminUser }) => {
/>
) : (
<EntryCard
currentUserRole={entryList["currentUserRole"]}
handleDeleteEntry={handleDeleteEntry}
handleDuplicate={handleDuplicate}
key={weekCounter}
Expand Down
15 changes: 4 additions & 11 deletions app/javascript/src/components/TimeoffEntries/EntryCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import {

const TimeoffEntryCard = ({
timeoffEntry,
currentUserRole,
leaveTypeDetails,
holidayDetails,
}: Iprops) => {
const { isDesktop } = useUserContext();
const { isDesktop, companyRole } = useUserContext();
const {
entryList,
setEditTimeoffEntryId,
Expand Down Expand Up @@ -140,19 +139,14 @@ const TimeoffEntryCard = ({
<div className="flex w-5/12 items-center justify-evenly">
{showDuplicateAction(
bill_status,
currentUserRole,
companyRole,
id,
handleDuplicateTimeoffEntry
)}
{showUpdateAction(
bill_status,
currentUserRole,
id,
handleEditBtnClick
)}
{showUpdateAction(bill_status, companyRole, id, handleEditBtnClick)}
{showDeleteAction(
bill_status,
currentUserRole,
companyRole,
id,
handleDeleteTimeoffEntry
)}
Expand All @@ -174,7 +168,6 @@ interface Iprops {
timeoffEntry: any;
leaveTypeDetails: any;
holidayDetails: any;
currentUserRole: string;
}

export default TimeoffEntryCard;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TimeoffForm from "../TimeoffForm";

const TimeoffEntryManager = ({
timeoffEntry,
currentUserRole,
leaveTypeDetails,
holidayDetails,
isDisplayEditTimeoffEntryForm,
Expand All @@ -22,7 +21,6 @@ const TimeoffEntryManager = ({
/>
) : (
<TimeoffEntryCard
currentUserRole={currentUserRole}
holidayDetails={holidayDetails}
key={timeoffEntry?.id}
leaveTypeDetails={leaveTypeDetails}
Expand All @@ -37,7 +35,6 @@ interface Iprops {
timeoffEntry: any;
leaveTypeDetails: any;
holidayDetails: any;
currentUserRole: string;
isDisplayEditTimeoffEntryForm: boolean;
}

Expand Down
20 changes: 4 additions & 16 deletions app/javascript/src/components/TimesheetEntries/EntryCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ const EntryCard = ({
handleDeleteEntry,
setEditEntryId,
bill_status,
currentUserRole,
setNewEntryView,
handleDuplicate,
}: Iprops) => {
const { isDesktop } = useUserContext();
const { isDesktop, companyRole } = useUserContext();

const handleCardClick = () => {
if (!isDesktop) {
Expand Down Expand Up @@ -70,19 +69,9 @@ const EntryCard = ({
<p className="mx-auto text-2xl xl:text-4xl">{minToHHMM(duration)}</p>
</div>
<div className="flex w-5/12 items-center justify-evenly">
{showDuplicateAction(
bill_status,
currentUserRole,
id,
handleDuplicate
)}
{showUpdateAction(bill_status, currentUserRole, id, setEditEntryId)}
{showDeleteAction(
bill_status,
currentUserRole,
id,
handleDeleteEntry
)}
{showDuplicateAction(bill_status, companyRole, id, handleDuplicate)}
{showUpdateAction(bill_status, companyRole, id, setEditEntryId)}
{showDeleteAction(bill_status, companyRole, id, handleDeleteEntry)}
</div>
</div>
</div>
Expand All @@ -98,7 +87,6 @@ interface Iprops {
handleDeleteEntry: (id: number) => void; // eslint-disable-line
setEditEntryId: React.Dispatch<React.SetStateAction<number>>;
bill_status: string;
currentUserRole: string;
setNewEntryView: any;
handleDuplicate: any;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const AddEntry = () => {
return setProjectId(0);
}

const selectedProject = projects[client].find(
const selectedProject = projects[client]?.find(
currentProject => currentProject.name === project
);
if (selectedProject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ const TimeEntryManager = () => {
<>
{entry?.type === "timesheet" && !entry?.leave_type_id ? (
<EntryCard
currentUserRole={entryList["currentUserRole"]}
handleDeleteEntry={handleDeleteEntry}
handleDuplicate={handleDuplicate}
key={weekCounter}
Expand All @@ -105,7 +104,6 @@ const TimeEntryManager = () => {
/>
) : (
<TimeoffEntryManager
currentUserRole={entryList["currentUserRole"]}
timeoffEntry={entry}
holidayDetails={
holidaysHashObj[entry?.holiday_info_id || null]
Expand Down
14 changes: 13 additions & 1 deletion app/models/holiday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: holidays
#
# id :bigint not null, primary key
# discarded_at :datetime
# enable_optional_holidays :boolean default(FALSE)
# holiday_types :string default([]), is an Array
# no_of_allowed_optional_holidays :integer
Expand All @@ -16,18 +17,29 @@
#
# Indexes
#
# index_holidays_on_company_id (company_id)
# index_holidays_on_company_id (company_id)
# index_holidays_on_discarded_at (discarded_at)
#
# Foreign Keys
#
# fk_rails_... (company_id => companies.id)
#
class Holiday < ApplicationRecord
include Discard::Model

has_many :holiday_infos, dependent: :destroy
belongs_to :company

enum time_period_optional_holidays: { per_quarter: 0, per_year: 1, per_month: 2, per_week: 3 }

validates :year, presence: true, uniqueness: { scope: :company_id }
validates :year, numericality: { only_integer: true, greater_than_or_equal_to: 1900, less_than_or_equal_to: 2099 }

after_discard :discard_holiday_infos

private

def discard_holiday_infos
holiday_infos.discard_all
end
end
28 changes: 19 additions & 9 deletions app/models/holiday_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
#
# Table name: holiday_infos
#
# id :bigint not null, primary key
# category :integer default("national"), not null
# date :date not null
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# holiday_id :bigint not null
# id :bigint not null, primary key
# category :integer default("national"), not null
# date :date not null
# discarded_at :datetime
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# holiday_id :bigint not null
#
# Indexes
#
# index_holiday_infos_on_holiday_id (holiday_id)
# index_holiday_infos_on_discarded_at (discarded_at)
# index_holiday_infos_on_holiday_id (holiday_id)
#
# Foreign Keys
#
# fk_rails_... (holiday_id => holidays.id)
#
class HolidayInfo < ApplicationRecord
include Discard::Model

belongs_to :holiday

has_many :timeoff_entries, inverse_of: :holiday_info
has_many :timeoff_entries, dependent: :destroy

enum category: { national: 0, optional: 1 }

Expand All @@ -35,6 +39,8 @@ class HolidayInfo < ApplicationRecord
validate :validate_holiday_category
validate :validate_year

after_discard :discard_timeoff_entries

scope :for_year_and_category, ->(year, category) {
joins(:holiday)
.where(holidays: { year: })
Expand All @@ -60,4 +66,8 @@ def validate_year
errors.add(:date, "must have the same year as the associated holiday")
end
end

def discard_timeoff_entries
timeoff_entries.discard_all
end
end
22 changes: 17 additions & 5 deletions app/models/leave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
#
# Table name: leaves
#
# id :bigint not null, primary key
# year :integer
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
# id :bigint not null, primary key
# discarded_at :datetime
# year :integer
# created_at :datetime not null
# updated_at :datetime not null
# company_id :bigint not null
#
# Indexes
#
# index_leaves_on_company_id (company_id)
# index_leaves_on_discarded_at (discarded_at)
# index_leaves_on_year_and_company_id (year,company_id) UNIQUE
#
# Foreign Keys
Expand All @@ -23,9 +25,19 @@
# frozen_string_literal: true

class Leave < ApplicationRecord
include Discard::Model

belongs_to :company
has_many :leave_types, class_name: "LeaveType", dependent: :destroy

validates :year, presence: true,
numericality: { greater_than_or_equal_to: 1000, less_than_or_equal_to: 9999 }

after_discard :discard_leave_types

private

def discard_leave_types
leave_types.discard_all
end
end
Loading

0 comments on commit 537cc8c

Please sign in to comment.