2024-11-13 20:44:01 +01:00
|
|
|
|
using FirmTracker_Server.Entities;
|
2024-11-26 22:12:02 +01:00
|
|
|
|
using FirmTracker_Server.nHibernate;
|
2024-12-12 19:48:08 +01:00
|
|
|
|
using static NHibernate.Engine.Query.CallableParser;
|
|
|
|
|
using FirmTracker_Server.Models;
|
2024-11-13 20:44:01 +01:00
|
|
|
|
|
2024-11-26 22:12:02 +01:00
|
|
|
|
public class WorkdayRepository
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
public void StartWorkday(int userId)
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
using (var transaction = session.BeginTransaction())
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
try
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
// Check if there is an existing workday that hasn't been stopped yet
|
|
|
|
|
var ongoingWorkday = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.UserId == userId && w.EndTime == null)
|
|
|
|
|
.OrderByDescending(w => w.StartTime)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (ongoingWorkday != null)
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
// If there is an ongoing workday, throw an exception or return a specific message
|
|
|
|
|
throw new Exception("Previous workday wasn't stopped yet.");
|
|
|
|
|
}
|
2024-11-13 20:44:01 +01:00
|
|
|
|
|
2024-11-26 22:12:02 +01:00
|
|
|
|
// Fetch the user entity
|
|
|
|
|
var user = session.Get<User>(userId);
|
|
|
|
|
if (user == null) throw new Exception("User not found");
|
2024-11-13 20:44:01 +01:00
|
|
|
|
|
2024-11-26 22:12:02 +01:00
|
|
|
|
// Create a new workday if there is no ongoing one
|
|
|
|
|
var workday = new Workday
|
|
|
|
|
{
|
|
|
|
|
StartTime = DateTime.Now,
|
2024-12-04 23:35:36 +01:00
|
|
|
|
User = user,
|
|
|
|
|
Absence = ""
|
2024-11-26 22:12:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
session.Save(workday);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw new Exception("An error occurred while starting the workday", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 23:35:36 +01:00
|
|
|
|
|
|
|
|
|
public void AddAbsence(int userId, string absenceType, DateTime startTime, DateTime endTime)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
using (var transaction = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var user = session.Get<User>(userId);
|
|
|
|
|
if (user == null) throw new Exception("User not found");
|
|
|
|
|
|
|
|
|
|
// Create a new workday entry for the absence
|
|
|
|
|
var workday = new Workday
|
|
|
|
|
{
|
|
|
|
|
User = user,
|
|
|
|
|
StartTime = startTime,
|
|
|
|
|
EndTime = endTime,
|
|
|
|
|
Absence = absenceType // Store the absence type as a string
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
session.Save(workday);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw new Exception("An error occurred while adding the absence", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-26 22:12:02 +01:00
|
|
|
|
|
|
|
|
|
public bool StopWorkday(int userId)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
using (var transaction = session.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var workday = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.UserId == userId && w.EndTime == null)
|
|
|
|
|
.OrderByDescending(w => w.StartTime)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (workday == null)
|
2024-11-13 20:44:01 +01:00
|
|
|
|
{
|
2024-11-26 22:12:02 +01:00
|
|
|
|
return false; // No ongoing workday found
|
2024-11-13 20:44:01 +01:00
|
|
|
|
}
|
2024-11-26 22:12:02 +01:00
|
|
|
|
|
|
|
|
|
workday.EndTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
session.Update(workday);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw new Exception("An error occurred while stopping the workday", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Workday> GetWorkdaysByUser(string email)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var workdays = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.Email == email)
|
|
|
|
|
.Select(w => new Workday
|
|
|
|
|
{
|
|
|
|
|
Id = w.Id,
|
|
|
|
|
StartTime = w.StartTime,
|
|
|
|
|
EndTime = w.EndTime ?? DateTime.Today.AddHours(17),
|
|
|
|
|
WorkedHours = (w.EndTime ?? DateTime.Today.AddHours(17)) - w.StartTime,
|
2024-12-04 23:35:36 +01:00
|
|
|
|
Absence = w.Absence,
|
2024-11-26 22:12:02 +01:00
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-12-12 19:48:08 +01:00
|
|
|
|
foreach (var workday in workdays)
|
|
|
|
|
{
|
|
|
|
|
if(workday.Absence!="")
|
|
|
|
|
{
|
|
|
|
|
workday.WorkedHours = TimeSpan.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return workdays;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("An error occurred while fetching workdays", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public DayDetailsDto GetDayDetails(string mail, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Fetch workdays for the specified user on the given date
|
|
|
|
|
var startOfDay = date.Date;
|
|
|
|
|
var endOfDay = startOfDay.AddDays(1);
|
|
|
|
|
|
|
|
|
|
var workdays = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.Email == mail && w.StartTime >= startOfDay && w.StartTime < endOfDay)
|
|
|
|
|
.Select(w => new Workday
|
|
|
|
|
{
|
|
|
|
|
StartTime = w.StartTime,
|
|
|
|
|
EndTime = w.EndTime ?? DateTime.Today.AddHours(17),
|
|
|
|
|
Absence = w.Absence,
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
TimeSpan totalWorkedHours = TimeSpan.Zero;
|
|
|
|
|
|
|
|
|
|
// Calculate total worked hours and adjust if there's an absence
|
|
|
|
|
foreach (var workday in workdays)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(workday.Absence))
|
|
|
|
|
{
|
|
|
|
|
totalWorkedHours += workday.WorkedHours;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DayDetailsDto
|
|
|
|
|
{
|
|
|
|
|
Email = mail,
|
|
|
|
|
Date = date,
|
|
|
|
|
TotalWorkedHours = totalWorkedHours.ToString(@"hh\:mm\:ss"),
|
|
|
|
|
WorkdayDetails = workdays
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("An error occurred while fetching the day's details", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public DayDetailsLoggedUserDto GetDayDetailsForLoggedUser(int userId, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Fetch workdays for the specified user on the given date
|
|
|
|
|
var startOfDay = date.Date;
|
|
|
|
|
var endOfDay = startOfDay.AddDays(1);
|
|
|
|
|
|
|
|
|
|
var workdays = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.UserId == userId && w.StartTime >= startOfDay && w.StartTime < endOfDay)
|
|
|
|
|
.Select(w => new Workday
|
|
|
|
|
{
|
|
|
|
|
StartTime = w.StartTime,
|
|
|
|
|
EndTime = w.EndTime ?? DateTime.Today.AddHours(17),
|
|
|
|
|
Absence = w.Absence,
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
TimeSpan totalWorkedHours = TimeSpan.Zero;
|
|
|
|
|
|
|
|
|
|
// Calculate total worked hours and adjust if there's an absence
|
|
|
|
|
foreach (var workday in workdays)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(workday.Absence))
|
|
|
|
|
{
|
|
|
|
|
totalWorkedHours += workday.WorkedHours;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new DayDetailsLoggedUserDto
|
|
|
|
|
{
|
|
|
|
|
UserId = userId,
|
|
|
|
|
Date = date,
|
|
|
|
|
TotalWorkedHours = totalWorkedHours.ToString(@"hh\:mm\:ss"),
|
|
|
|
|
WorkdayDetails = workdays
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("An error occurred while fetching the day's details", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public List<Workday> GetWorkdaysByLoggedUser(string userId)
|
|
|
|
|
{
|
|
|
|
|
using (var session = SessionFactory.OpenSession())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int parsedUserId = Int32.Parse(userId);
|
|
|
|
|
var workdays = session.Query<Workday>()
|
|
|
|
|
.Where(w => w.User.UserId == parsedUserId)
|
|
|
|
|
.Select(w => new Workday
|
|
|
|
|
{
|
|
|
|
|
Id = w.Id,
|
|
|
|
|
StartTime = w.StartTime,
|
|
|
|
|
EndTime = w.EndTime ?? DateTime.Today.AddHours(17),
|
|
|
|
|
WorkedHours = (w.EndTime ?? DateTime.Today.AddHours(17)) - w.StartTime,
|
|
|
|
|
Absence = w.Absence,
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2024-11-26 22:12:02 +01:00
|
|
|
|
return workdays;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("An error occurred while fetching workdays", ex);
|
2024-11-13 20:44:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|