Skip to content

Commit

Permalink
Fix: Fix the vacation balance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Feb 8, 2024
1 parent 6660970 commit bc18e5d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 101 deletions.
2 changes: 1 addition & 1 deletion client/public/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
window.env = {
SERVER_DOMAIN_NAME_API: 'https://cshrserver.gent01.qa.grid.tf/api', // Added for testing, this is a staging API
SERVER_DOMAIN_NAME_API: 'http://127.0.0.1:8000/api', // Added for testing, this is a staging API
};
1 change: 0 additions & 1 deletion client/src/components/personalInformation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

<script lang="ts">
import { computed, ref } from 'vue'
import { onMounted } from 'vue'
export default {
name: 'personalInformation',
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/vacationBalance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
name: 'vacationBalance',
props: ['balance'],

setup(props) {
setup() {
const vacationInfoHeaders = ['Annuals', 'Emergencies', 'Excuses']

return {
Expand Down
49 changes: 25 additions & 24 deletions client/src/views/ProfileView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<v-col cols="12" sm="12" md="8" class="pa-2 border rounded position-relative ma-2">
<div>
<personalInformation :user="user.state.value" />
<vacationBalance :balance="balance.state.value" v-if="showBalance" />
<!-- v-if="showBalance" -->
<vacationBalance :balance="balance.state.value" />
</div>
</v-col>
</v-row>
Expand Down Expand Up @@ -53,33 +54,33 @@ export default defineComponent({
{ immediate: false }
)
const showBalance = computed(() => {
if (storedUser.value?.fullUser) {
if (
user.state.value &&
storedUser.value?.fullUser &&
user.state.value.id === storedUser.value?.fullUser.id
) {
return true
} else if (storedUser.value?.fullUser && storedUser.value?.fullUser.user_type === 'Admin') {
if (user.state.value.reporting_to) {
if (
user.state.value.reporting_to.includes(storedUser.value?.fullUser.id) &&
user.state.value.location.name === storedUser.value?.fullUser.location.name
) {
return true
}
}
return false
}
}
return false
})
// const showBalance = computed(() => {
// if (storedUser.value?.fullUser) {
// if (
// user.state.value &&
// storedUser.value?.fullUser &&
// user.state.value.id === storedUser.value?.fullUser.id
// ) {
// return true
// } else if (storedUser.value?.fullUser && storedUser.value?.fullUser.user_type === 'Admin') {
// if (user.state.value.reporting_to) {
// if (
// user.state.value.reporting_to.includes(storedUser.value?.fullUser.id) &&
// user.state.value.location.name === storedUser.value?.fullUser.location.name
// ) {
// return true
// }
// }
// return false
// }
// }
// return false
// })
return {
user,
balance,
showBalance,
// showBalance,
vacationBalance,
personalInformation,
profileImage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.9 on 2024-02-08 10:04

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("cshr", "0001_initial"),
]

operations = [
migrations.RemoveField(
model_name="vacation",
name="actual_days",
),
migrations.RemoveField(
model_name="vacationbalance",
name="actual_balance",
),
migrations.AddField(
model_name="vacationbalance",
name="office_vacation_balance",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="cshr.officevacationbalance",
),
),
]
6 changes: 1 addition & 5 deletions server/cshr/models/vacations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class Vacation(Requests):
from_date = models.DateField()
end_date = models.DateField()
change_log = models.JSONField(default=list)
actual_days = models.IntegerField(default=0)
# taked_from_old_balance = models.BooleanField(default=False)

def ___str__(self):
return self.reason
Expand All @@ -53,9 +51,7 @@ class VacationBalance(models.Model):
annual_leaves = models.IntegerField()
emergency_leaves = models.IntegerField()
leave_excuses = models.IntegerField()
actual_balance = models.JSONField(default=dict, null=True)
# date = models.DateField(auto_now=True)
# old_balance = models.JSONField(default=dict, null=True)
office_vacation_balance = models.ForeignKey("OfficeVacationBalance", on_delete=models.SET_NULL, null=True)

def __str__(self):
return str(self.user.email)
Expand Down
25 changes: 12 additions & 13 deletions server/cshr/serializers/vacations.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,49 +189,48 @@ def get_user(self, obj: VacationBalance):
def get_sick_leaves(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("sick_leaves") - obj.sick_leaves,
"all": obj.actual_balance.get("sick_leaves"),
"reserved": obj.sick_leaves,
"all": obj.office_vacation_balance.sick_leaves,
}
return UserBalanceVlaueSerializer(value).data

def get_compensation(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("compensation") - obj.compensation,
"all": obj.actual_balance.get("compensation"),
"reserved": obj.compensation,
"all": obj.office_vacation_balance.compensation,
}
return UserBalanceVlaueSerializer(value).data

def get_unpaid(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("unpaid") - obj.unpaid,
"all": obj.actual_balance.get("unpaid"),
"reserved": obj.unpaid,
"all": obj.office_vacation_balance.unpaid,
}
return UserBalanceVlaueSerializer(value).data

def get_annual_leaves(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("annual_leaves") - obj.annual_leaves,
"all": obj.actual_balance.get("annual_leaves"),
"reserved": obj.annual_leaves,
"all": obj.office_vacation_balance.annual_leaves,
}
return UserBalanceVlaueSerializer(value).data

def get_emergency_leaves(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("emergency_leaves")
- obj.emergency_leaves,
"all": obj.actual_balance.get("emergency_leaves"),
"reserved": obj.emergency_leaves,
"all": obj.office_vacation_balance.emergency_leaves,
}
return UserBalanceVlaueSerializer(value).data

def get_leave_excuses(self, obj: VacationBalance):
"""This method returns the actual user balance values."""
value = {
"reserved": obj.actual_balance.get("leave_excuses") - obj.leave_excuses,
"all": obj.actual_balance.get("leave_excuses"),
"reserved": obj.leave_excuses,
"all": obj.office_vacation_balance.leave_excuses,
}
return UserBalanceVlaueSerializer(value).data

Expand Down
14 changes: 0 additions & 14 deletions server/cshr/services/vacations.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,6 @@ def filter_user_vacations(user: User) -> Vacation:
return Vacation.objects.filter(applying_user=user).order_by("created_at")


def update_user_actual_balance(user_balance: VacationBalance) -> VacationBalance:
"""Update user actual balance field with the current balance."""
user_balance.actual_balance = {
"annual_leaves": user_balance.annual_leaves,
"sick_leaves": user_balance.sick_leaves,
"compensation": 100,
"unpaid": 100,
"emergency_leaves": user_balance.emergency_leaves,
"leave_excuses": user_balance.leave_excuses,
}
user_balance.save()
return user_balance


def send_vacation_to_calendar(vacation: Vacation) -> Dict[str, Any]:
from cshr.services.landing_page import (
LandingPageClassNameEnum,
Expand Down
43 changes: 20 additions & 23 deletions server/cshr/utils/vacation_balance_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,37 @@ def __init__(self):
self.file_path = os.path.join(f"{self.abspath}/vacation_balance.json")

def check(self, user) -> VacationBalance:
try:
balance = VacationBalance.objects.get(user=user)
for field in balance._meta.fields:
actual_value: int = getattr(balance, field.name)
if (
type(actual_value) is dict
and len(actual_value) > 0
and field.name != "actual_balance"
):
for k, v in actual_value.items():
balance_attr: int = getattr(balance, k)
if balance_attr + v < 365:
setattr(balance, k, balance_attr + v)
return balance
except VacationBalance.DoesNotExist:
balance = VacationBalance.objects.filter(user=user)
if(len(balance) == 0):
return self.create_new_balance(user)
balance = balance[0]
return balance


def create_new_balance_values(self, user: User) -> Dict:
# this help to divide to get the total days based on joining date
month: int = 12 - user.created_at.month
balance = OfficeVacationBalance.objects.get(
office_balance = OfficeVacationBalance.objects.filter(
year=datetime.datetime.now().year, location=user.location
)

annual_leaves: int = round(balance.annual_leaves / 12 * month)
leave_excuses: int = round(balance.leave_excuses / 12 * month)
emergency_leaves: int = round(balance.emergency_leaves / 12 * month)
if(len(office_balance) == 0):
office_balance = OfficeVacationBalance.objects.create(
year=datetime.datetime.now().year, location=user.location
)
else:
office_balance = office_balance[0]

annual_leaves: int = round(office_balance.annual_leaves / 12 * month)
leave_excuses: int = round(office_balance.leave_excuses / 12 * month)
emergency_leaves: int = round(office_balance.emergency_leaves / 12 * month)
calculated_values = {
"annual_leaves": annual_leaves,
"leave_excuses": leave_excuses,
"emergency_leaves": emergency_leaves,
"sick_leaves": balance.sick_leaves,
"unpaid": balance.unpaid,
"compensation": balance.compensation,
"sick_leaves": office_balance.sick_leaves,
"unpaid": office_balance.unpaid,
"compensation": office_balance.compensation,
}
return calculated_values

Expand All @@ -73,7 +70,7 @@ def create_new_balance(self, user: User) -> VacationBalance:
emergency_leaves=values["emergency_leaves"],
leave_excuses=values["leave_excuses"],
unpaid=values["unpaid"],
actual_balance=values,
office_vacation_balance = OfficeVacationBalance.objects.get(location__id = user.location.id)
)

return balance
Expand Down
19 changes: 0 additions & 19 deletions server/cshr/views/vacations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
get_vacation_by_id,
get_all_vacations,
send_vacation_to_calendar,
update_user_actual_balance,
)
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.request import Request
Expand Down Expand Up @@ -103,22 +102,6 @@ def put(self, request: Request) -> Response:
for user in users_in_office:
v.check(user)

reason = serializer.validated_data.get("reason")
add_value = serializer.validated_data.get("value")

balances = VacationBalance.objects.filter(user__in=users_in_office)
for obj in balances:
if hasattr(obj, reason):
old_value = obj.actual_balance.get(reason)
obj.actual_balance[reason] = old_value + add_value
# setattr(obj, reason, old_value + add_value)
obj.save()
else:
return CustomResponse.bad_request(
message="Unknown reason.",
data=serializer.data,
)

return CustomResponse.success(
message="Successfully updated balance values.",
data=serializer.data,
Expand Down Expand Up @@ -714,8 +697,6 @@ def put(self, request: Request) -> CustomResponse:
balance.save()

v.check(balance.user)
user_balance = get_balance_by_user(balance.user)
update_user_actual_balance(user_balance)

return CustomResponse.success(
message="Successfully updated user balance",
Expand Down

0 comments on commit bc18e5d

Please sign in to comment.