practicekea_backend/microservices/_layers/common/AttendanceService.cs

120 lines
4.3 KiB
C#
Raw Permalink Normal View History

2025-10-22 13:05:31 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
public class AttendanceService
{
private readonly OdooService _odooService;
public AttendanceService(OdooService odooService)
{
_odooService = odooService ?? throw new ArgumentNullException(nameof(odooService));
}
/// <summary>
/// Records attendance for an employee.
/// </summary>
/// <param name="attendanceData">The attendance data to record.</param>
/// <returns>ID of the created attendance record.</returns>
public async Task<int> RecordAttendanceAsync(object attendanceData)
{
if (attendanceData == null)
{
throw new ArgumentNullException(nameof(attendanceData), "Attendance data cannot be null.");
}
return await _odooService.CreateAsync("hr.attendance", attendanceData);
}
/// <summary>
/// Records attendance for an employee.
/// </summary>
/// <param name="attendanceData">The attendance data to record.</param>
/// <returns>ID of the created attendance record.</returns>
public async Task<int> MarkAsAbsentAsync(object absenceData)
{
if (absenceData == null)
{
throw new ArgumentNullException(nameof(absenceData), "Attendance data cannot be null.");
}
return await _odooService.CreateAsync("hr.attendance", absenceData);
}
// Method to delete attendance record for a given employee and date
public async Task<bool> DeleteAttendanceAsync(int employeeId, DateTime date)
{
// Prepare the filter data to search for the attendance record
var filterData = new
{
domain = new List<object>
{
new object[] { "employee_id", "=", employeeId },
new object[] { "check_in", ">=", date.Date.ToString("yyyy-MM-dd") + " 00:00:00" },
new object[] { "check_out", "<=", date.Date.ToString("yyyy-MM-dd") + " 23:59:59" }
}
};
// Search for the attendance record using the Odoo service
var attendanceRecords = await _odooService.SearchReadAttendanceAsync("hr.attendance", filterData);
if (attendanceRecords == null || !attendanceRecords.Any())
{
// No record found to delete
return false;
}
var attendanceRecord = attendanceRecords.FirstOrDefault();
var attendanceId = -1;
if (attendanceRecord != null)
{
// Cast attendanceRecord to JsonElement if it's not already
JsonElement recordElement = JsonSerializer.Deserialize<JsonElement>(attendanceRecord.ToString());
if (recordElement.ValueKind == JsonValueKind.Object && recordElement.TryGetProperty("id", out var idElement))
{
attendanceId = idElement.GetInt32(); // Get the ID as an integer
// Now use attendanceId for deletion or further operations
}
else
{
// Handle the case where the "id" property is missing or the structure is not as expected
throw new Exception("Attendance record does not contain 'id' property or is invalid.");
}
}
else
{
throw new Exception("No attendance records found.");
}
// Use the DeleteAsync method to delete the attendance record
var success = await _odooService.DeleteAsync("hr.attendance", attendanceId);
return success;
}
/// <summary>
/// Retrieves attendance records for employees within a specified date range.
/// </summary>
/// <param name="filterData">The filter criteria for retrieving attendance records.</param>
/// <returns>A list of attendance records.</returns>
/// <exception cref="ArgumentNullException">Thrown when filterData is null.</exception>
public async Task<List<object>> GetAttendanceAsync(object filterData)
{
if (filterData == null)
{
throw new ArgumentNullException(nameof(filterData), "Filter data cannot be null.");
}
// Use OdooService to perform the search_read request
return await _odooService.SearchReadAttendanceAsync("hr.attendance", filterData);
}
}