add attendance endpoint

This commit is contained in:
dibakor 2026-07-08 18:15:24 +06:00
parent bdcda8c1fb
commit a64aa85d33
6 changed files with 208 additions and 1 deletions

View File

@ -1,5 +1,6 @@

using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
@ -52,4 +53,29 @@ public class GetCustomersRequest : PagedRequest
[StringLength(10, MinimumLength = 1, ErrorMessage = "Employee number must be between 1 and 10 characters.")]
public string? EmployeeNumber { get; set; }
}
public class GetAttendanceByEmpRequest
{
[Required, NotNull, StringLength(maximumLength: 10, MinimumLength = 3, ErrorMessage = "EmployeeNumber must be between 3 and 10 characters.")]
public string EmployeeNumber { get; set; }
public DateTime? AttendanceDate { get; set; }
}
public class UpsertAttendanceByEmpRequest
{
[Required]
public DateTime AttendanceDate { get; set; }
[Required, NotNull, StringLength(maximumLength: 10, MinimumLength = 3, ErrorMessage = "EmployeeNumber must be between 3 and 10 characters.")]
public string EmployeeNumber { get; set; }
public DateTime? CheckInTime { get; set; }
public string? CheckInLatitude { get; set; }
public string? CheckInLongitude { get; set; }
public string? CheckInLocationName { get; set; }
public DateTime? CheckOutTime { get; set; }
public string? CheckOutLatitude { get; set; }
public string? CheckOutLongitude { get; set; }
public string? CheckOutLocationName { get; set; }
public bool IsCheckIn => CheckInTime.HasValue ? true : false;
public bool IsCheckOut => CheckOutTime.HasValue ? true : false;
}

View File

@ -131,4 +131,21 @@ public class GetCustomerResponse
public string SalesOrgCode { get; set; }
public string TeritoryCode { get; set; }
public string PlantCode { get; set; }
}
public class GetAttendanceByEmpResponse
{
public DateTime AttendanceDate { get; set; }
public string EmployeeNumber { get; set; }
public DateTime? CheckInTime { get; set; }
public string? CheckInLatitude { get; set; }
public string? CheckInLongitude { get; set; }
public string? CheckInLocationName { get; set; }
public DateTime? CheckOutTime { get; set; }
public string? CheckOutLatitude { get; set; }
public string? CheckOutLongitude { get; set; }
public string? CheckOutLocationName { get; set; }
public bool IsCheckIn => CheckInTime.HasValue ? true : false;
public bool IsCheckOut => CheckOutTime.HasValue ? true : false;
}

View File

@ -11,4 +11,7 @@ public interface IMobileMasterDataService
Task<PagedResult<GetBrandResponse>> GetBrandsAsync(GetBrandsRequest request);
Task<PagedResult<GetMaterialResponse>> GetMaterialsAsync(GetMaterialsRequest request);
Task<PagedResult<GetCustomerResponse>> GetCustomersAsync(GetCustomersRequest request);
Task<GetAttendanceByEmpResponse> GetAttendanceByEmpAsync(GetAttendanceByEmpRequest request);
Task<GetAttendanceByEmpResponse> UpsertAttendanceByEmpAsync(UpsertAttendanceByEmpRequest request);
}

View File

@ -114,6 +114,9 @@ public class MobileMasterDataService : IMobileMasterDataService
return response;
}
public async Task<PagedResult<GetMaterialResponse>> GetMaterialsAsync(GetMaterialsRequest request)
{
PagedResult<GetMaterialResponse> response = new();
@ -485,4 +488,107 @@ public class MobileMasterDataService : IMobileMasterDataService
return response;
}
public async Task<GetAttendanceByEmpResponse> GetAttendanceByEmpAsync(GetAttendanceByEmpRequest request)
{
GetAttendanceByEmpResponse response = null;
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@AttendanceDate", pType: SqlDbType.Date, pValue: request.AttendanceDate)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetAttendanceByEmployee", parameterValues: p))
{
if (dr.Read())
{
response = MapToResponse(dr);
}
dr.Close();
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
public async Task<GetAttendanceByEmpResponse> UpsertAttendanceByEmpAsync(UpsertAttendanceByEmpRequest request)
{
GetAttendanceByEmpResponse response = new();
try
{
using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode);
try
{
SqlParameter[] p =
[
SqlHelperExtension.CreateInParam(pName: "@AttendanceDate", pType: SqlDbType.Date, pValue: request.AttendanceDate),
SqlHelperExtension.CreateInParam(pName: "@EmployeeNumber", pType: SqlDbType.VarChar, pValue: request.EmployeeNumber),
SqlHelperExtension.CreateInParam(pName: "@CheckInTime", pType: SqlDbType.DateTime, pValue: request.CheckInTime),
SqlHelperExtension.CreateInParam(pName: "@CheckInLatitude", pType: SqlDbType.VarChar, pValue: request.CheckInLatitude),
SqlHelperExtension.CreateInParam(pName: "@CheckInLongitude", pType: SqlDbType.VarChar, pValue: request.CheckInLongitude),
SqlHelperExtension.CreateInParam(pName: "@CheckInLocationName", pType: SqlDbType.VarChar, pValue: request.CheckInLocationName),
SqlHelperExtension.CreateInParam(pName: "@CheckOutTime", pType: SqlDbType.DateTime, pValue: request.CheckOutTime),
SqlHelperExtension.CreateInParam(pName: "@CheckOutLatitude", pType: SqlDbType.VarChar, pValue: request.CheckOutLatitude),
SqlHelperExtension.CreateInParam(pName: "@CheckOutLongitude", pType: SqlDbType.VarChar, pValue: request.CheckOutLongitude),
SqlHelperExtension.CreateInParam(pName: "@CheckOutLocationName", pType: SqlDbType.VarChar, pValue: request.CheckOutLocationName)
];
using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.UpsertAttendanceByEmployee", parameterValues: p))
{
if (dr.Read())
{
response = MapToResponse(dr);
}
dr.Close();
}
tc.End();
}
catch (Exception ie)
{
tc?.HandleError();
throw DBCustomError.GenerateCustomError(ie);
}
}
catch (Exception ex)
{
throw;
}
return response;
}
private static GetAttendanceByEmpResponse MapToResponse(IDataReader dr)
{
return new GetAttendanceByEmpResponse
{
AttendanceDate = dr.GetDateTime(0),
EmployeeNumber = dr.GetString(1),
CheckInTime = dr.IsDBNull(2) ? null : dr.GetDateTime(2),
CheckInLatitude = dr.IsDBNull(3) ? null : dr.GetString(3),
CheckInLongitude = dr.IsDBNull(4) ? null : dr.GetString(4),
CheckInLocationName = dr.IsDBNull(5) ? null : dr.GetString(5),
CheckOutTime = dr.IsDBNull(6) ? null : dr.GetDateTime(6),
CheckOutLatitude = dr.IsDBNull(7) ? null : dr.GetString(7),
CheckOutLongitude = dr.IsDBNull(8) ? null : dr.GetString(8),
CheckOutLocationName = dr.IsDBNull(9) ? null : dr.GetString(9)
};
}
}

View File

@ -675,7 +675,8 @@ namespace OnlineSalesAutoCrop.CoreAPI.Services.Services.Systems
SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: user.UserId),
SqlHelperExtension.CreateInParam(pName: "@LoginId", pType: SqlDbType.VarChar, pValue: user.LoginId, size: 30),
SqlHelperExtension.CreateInParam(pName: "@IpAddress", pType: SqlDbType.VarChar, pValue: ipAddress, size: 20),
SqlHelperExtension.CreateInParam(pName: "@LoginTime", pType: SqlDbType.DateTime, pValue: DateTime.Now)
SqlHelperExtension.CreateInParam(pName: "@LoginTime", pType: SqlDbType.DateTime, pValue: DateTime.Now),
SqlHelperExtension.CreateInParam(pName: "@LogoutTime", pType: SqlDbType.DateTime, pValue: null)
];
_ = tc.ExecuteNonQuerySp(spName: "dbo.SaveAccessLog", parameterValues: p);

View File

@ -143,5 +143,59 @@ namespace OnlineSalesAutoCrop.CoreAPI.Controllers
return StatusCode(StatusCodes.Status500InternalServerError, MobileResponseBase<AppAuthUserResponse>.Failure(ex.InnerException != null ? ex.InnerException.Message : ex.Message, StatusCodes.Status500InternalServerError));
}
}
/// <summary>
/// Login using your credential data retrieve from SqlServer
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="request"></param>
/// <response code="200">If login successful Return Attendance List</response>
[HttpGet("Attendance")]
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(GetAttendanceByEmpResponse))]
public async Task<IActionResult> GetAttendance([FromQuery] GetAttendanceByEmpRequest request)
{
GetAttendanceByEmpResponse response = new();
try
{
response = await _service.GetAttendanceByEmpAsync(request);
return Ok(MobileResponseBase<GetAttendanceByEmpResponse>.Success(response));
}
catch (Exception ex)
{
string msg = $"Exception occur on Get Attendance endpoint with employee request - {request?.EmployeeNumber} -{request?.AttendanceDate}";
_logger.LogError(exception: ex, message: msg);
return StatusCode(StatusCodes.Status500InternalServerError, MobileResponseBase<AppAuthUserResponse>.Failure(ex.InnerException != null ? ex.InnerException.Message : ex.Message, StatusCodes.Status500InternalServerError));
}
}
/// <summary>
/// Login using your credential data retrieve from SqlServer
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="request"></param>
/// <response code="200">If login successful Return Attendance List</response>
[HttpPost("Attendance")]
[IgnoreAntiforgeryToken]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(GetAttendanceByEmpResponse))]
public async Task<IActionResult> UpsertAttendance([FromBody] UpsertAttendanceByEmpRequest request)
{
GetAttendanceByEmpResponse response = new();
try
{
response = await _service.UpsertAttendanceByEmpAsync(request);
return Ok(MobileResponseBase<GetAttendanceByEmpResponse>.Success(response));
}
catch (Exception ex)
{
string msg = $"Exception occur on Save Attendance endpoint with employee request - {request?.EmployeeNumber} -{request?.AttendanceDate}";
_logger.LogError(exception: ex, message: msg);
return StatusCode(StatusCodes.Status500InternalServerError, MobileResponseBase<AppAuthUserResponse>.Failure(ex.InnerException != null ? ex.InnerException.Message : ex.Message, StatusCodes.Status500InternalServerError));
}
}
}
}