initial commit

This commit is contained in:
2026-07-21 14:08:10 +07:00
commit f7cf118326
533 changed files with 41762 additions and 0 deletions
@@ -0,0 +1,81 @@
using Indotalent.ConfigBackEnd.Exceptions;
using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class CreateLeaveBalanceRequest
{
public string EmployeeId { get; set; } = string.Empty;
public string LeaveCategoryId { get; set; } = string.Empty;
public int Entitlement { get; set; }
public int Used { get; set; }
public int Pending { get; set; }
public int Remaining { get; set; }
public int Year { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime ValidTo { get; set; }
public int CarryForward { get; set; }
}
public class CreateLeaveBalanceResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
}
public record CreateLeaveBalanceCommand(CreateLeaveBalanceRequest Data) : IRequest<CreateLeaveBalanceResponse>;
public class CreateLeaveBalanceHandler : IRequestHandler<CreateLeaveBalanceCommand, CreateLeaveBalanceResponse>
{
private readonly AppDbContext _context;
public CreateLeaveBalanceHandler(AppDbContext context) => _context = context;
public async Task<CreateLeaveBalanceResponse> Handle(CreateLeaveBalanceCommand request, CancellationToken cancellationToken)
{
var isExists = await _context.LeaveBalance
.AnyAsync(x => x.EmployeeId == request.Data.EmployeeId &&
x.LeaveCategoryId == request.Data.LeaveCategoryId &&
x.Year == request.Data.Year, cancellationToken);
if (isExists)
{
throw new AlreadyExistsException("Leave Balance", $"{request.Data.EmployeeId} for Year {request.Data.Year}");
}
var entityName = nameof(Data.Entities.LeaveBalance);
var autoNo = await _context.GenerateAutoNumberAsync(
entityName: entityName,
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
ct: cancellationToken
);
var entity = new Data.Entities.LeaveBalance
{
AutoNumber = autoNo,
EmployeeId = request.Data.EmployeeId,
LeaveCategoryId = request.Data.LeaveCategoryId,
Entitlement = request.Data.Entitlement,
Used = request.Data.Used,
Pending = request.Data.Pending,
Remaining = request.Data.Remaining,
Year = request.Data.Year,
ValidFrom = request.Data.ValidFrom,
ValidTo = request.Data.ValidTo,
CarryForward = request.Data.CarryForward
};
_context.LeaveBalance.Add(entity);
await _context.SaveChangesAsync(cancellationToken);
return new CreateLeaveBalanceResponse
{
Id = entity.Id,
AutoNumber = entity.AutoNumber
};
}
}
@@ -0,0 +1,16 @@
using FluentValidation;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class CreateLeaveBalanceValidator : AbstractValidator<CreateLeaveBalanceRequest>
{
public CreateLeaveBalanceValidator()
{
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
RuleFor(x => x.LeaveCategoryId).NotEmpty().WithMessage("Leave Category is required");
RuleFor(x => x.Year).GreaterThan(2000);
RuleFor(x => x.Entitlement).GreaterThanOrEqualTo(0);
RuleFor(x => x.ValidFrom).NotEmpty();
RuleFor(x => x.ValidTo).NotEmpty().GreaterThanOrEqualTo(x => x.ValidFrom);
}
}
@@ -0,0 +1,27 @@
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public record DeleteLeaveBalanceByIdRequest(string Id);
public record DeleteLeaveBalanceByIdCommand(DeleteLeaveBalanceByIdRequest Data) : IRequest<bool>;
public class DeleteLeaveBalanceByIdHandler : IRequestHandler<DeleteLeaveBalanceByIdCommand, bool>
{
private readonly AppDbContext _context;
public DeleteLeaveBalanceByIdHandler(AppDbContext context) => _context = context;
public async Task<bool> Handle(DeleteLeaveBalanceByIdCommand request, CancellationToken cancellationToken)
{
var entity = await _context.LeaveBalance
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
if (entity == null) return false;
_context.LeaveBalance.Remove(entity);
return await _context.SaveChangesAsync(cancellationToken) > 0;
}
}
@@ -0,0 +1,67 @@
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class GetLeaveBalanceByIdResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public string? EmployeeId { get; set; }
public string? EmployeeName { get; set; }
public string? LeaveCategoryId { get; set; }
public string? LeaveCategoryName { get; set; }
public int Entitlement { get; set; }
public int Used { get; set; }
public int Pending { get; set; }
public int Remaining { get; set; }
public int Year { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime ValidTo { get; set; }
public int CarryForward { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
}
public record GetLeaveBalanceByIdQuery(string Id) : IRequest<GetLeaveBalanceByIdResponse?>;
public class GetLeaveBalanceByIdHandler : IRequestHandler<GetLeaveBalanceByIdQuery, GetLeaveBalanceByIdResponse?>
{
private readonly AppDbContext _context;
public GetLeaveBalanceByIdHandler(AppDbContext context) => _context = context;
public async Task<GetLeaveBalanceByIdResponse?> Handle(GetLeaveBalanceByIdQuery request, CancellationToken cancellationToken)
{
return await _context.LeaveBalance
.AsNoTracking()
.Include(x => x.Employee)
.Include(x => x.LeaveCategory)
.Where(x => x.Id == request.Id)
.Select(x => new GetLeaveBalanceByIdResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
EmployeeId = x.EmployeeId,
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
LeaveCategoryId = x.LeaveCategoryId,
LeaveCategoryName = x.LeaveCategory != null ? x.LeaveCategory.Name : string.Empty,
Entitlement = x.Entitlement,
Used = x.Used,
Pending = x.Pending,
Remaining = x.Remaining,
Year = x.Year,
ValidFrom = x.ValidFrom,
ValidTo = x.ValidTo,
CarryForward = x.CarryForward,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
UpdatedAt = x.UpdatedAt,
UpdatedBy = x.UpdatedBy
})
.FirstOrDefaultAsync(cancellationToken);
}
}
@@ -0,0 +1,58 @@
using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class GetLeaveBalanceListResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public string? EmployeeId { get; set; }
public string? EmployeeName { get; set; }
public string? EmployeeCode { get; set; }
public string? LeaveCategoryId { get; set; }
public string? LeaveCategoryName { get; set; }
public int Entitlement { get; set; }
public int Used { get; set; }
public int Pending { get; set; }
public int Remaining { get; set; }
public int Year { get; set; }
}
public record GetLeaveBalanceListQuery() : IRequest<List<GetLeaveBalanceListResponse>>;
public class GetLeaveBalanceListHandler : IRequestHandler<GetLeaveBalanceListQuery, List<GetLeaveBalanceListResponse>>
{
private readonly AppDbContext _context;
public GetLeaveBalanceListHandler(AppDbContext context) => _context = context;
public async Task<List<GetLeaveBalanceListResponse>> Handle(GetLeaveBalanceListQuery request, CancellationToken cancellationToken)
{
return await _context.LeaveBalance
.AsNoTracking()
.NotDeletedOnly()
.Include(x => x.Employee)
.Include(x => x.LeaveCategory)
.OrderByDescending(x => x.Year)
.ThenBy(x => x.Employee!.FirstName)
.Select(x => new GetLeaveBalanceListResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
EmployeeId = x.EmployeeId,
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
EmployeeCode = x.Employee != null ? $"{x.Employee.Code}" : string.Empty,
LeaveCategoryId = x.LeaveCategoryId,
LeaveCategoryName = x.LeaveCategory != null ? x.LeaveCategory.Name : string.Empty,
Entitlement = x.Entitlement,
Used = x.Used,
Pending = x.Pending,
Remaining = x.Remaining,
Year = x.Year
})
.ToListAsync(cancellationToken);
}
}
@@ -0,0 +1,40 @@
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public record SyncLeaveBalanceCommand() : IRequest<bool>;
public class SyncLeaveBalanceHandler : IRequestHandler<SyncLeaveBalanceCommand, bool>
{
private readonly AppDbContext _context;
public SyncLeaveBalanceHandler(AppDbContext context) => _context = context;
public async Task<bool> Handle(SyncLeaveBalanceCommand request, CancellationToken cancellationToken)
{
var balances = await _context.LeaveBalance.ToListAsync(cancellationToken);
if (!balances.Any()) return true;
var allRequests = await _context.LeaveRequest
.AsNoTracking()
.ToListAsync(cancellationToken);
foreach (var balance in balances)
{
var empRequests = allRequests
.Where(x => x.EmployeeId == balance.EmployeeId &&
x.LeaveCategoryId == balance.LeaveCategoryId &&
x.StartDate.Year == balance.Year)
.ToList();
balance.Used = (int)empRequests.Where(x => x.Status == "Approved").Sum(x => x.Days);
balance.Pending = (int)empRequests.Where(x => x.Status == "Pending").Sum(x => x.Days);
balance.Remaining = (balance.Entitlement + balance.CarryForward) - balance.Used;
}
return await _context.SaveChangesAsync(cancellationToken) > 0;
}
}
@@ -0,0 +1,61 @@
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class UpdateLeaveBalanceRequest : CreateLeaveBalanceRequest
{
public string? Id { get; set; }
public string? EmployeeName { get; set; }
public string? LeaveCategoryName { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
}
public class UpdateLeaveBalanceResponse
{
public string? Id { get; set; }
public bool Success { get; set; }
}
public record UpdateLeaveBalanceCommand(UpdateLeaveBalanceRequest Data) : IRequest<UpdateLeaveBalanceResponse>;
public class UpdateLeaveBalanceHandler : IRequestHandler<UpdateLeaveBalanceCommand, UpdateLeaveBalanceResponse>
{
private readonly AppDbContext _context;
public UpdateLeaveBalanceHandler(AppDbContext context) => _context = context;
public async Task<UpdateLeaveBalanceResponse> Handle(UpdateLeaveBalanceCommand request, CancellationToken cancellationToken)
{
var entity = await _context.LeaveBalance
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
if (entity == null)
{
return new UpdateLeaveBalanceResponse { Id = request.Data.Id, Success = false };
}
entity.EmployeeId = request.Data.EmployeeId;
entity.LeaveCategoryId = request.Data.LeaveCategoryId;
entity.Entitlement = request.Data.Entitlement;
entity.Used = request.Data.Used;
entity.Pending = request.Data.Pending;
entity.Remaining = request.Data.Remaining;
entity.Year = request.Data.Year;
entity.ValidFrom = request.Data.ValidFrom;
entity.ValidTo = request.Data.ValidTo;
entity.CarryForward = request.Data.CarryForward;
await _context.SaveChangesAsync(cancellationToken);
return new UpdateLeaveBalanceResponse
{
Id = entity.Id,
Success = true
};
}
}
@@ -0,0 +1,15 @@
using FluentValidation;
namespace Indotalent.Features.Leave.LeaveBalance.Cqrs;
public class UpdateLeaveBalanceValidator : AbstractValidator<UpdateLeaveBalanceRequest>
{
public UpdateLeaveBalanceValidator()
{
RuleFor(x => x.Id).NotEmpty().WithMessage("ID is required for update");
RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("Employee is required");
RuleFor(x => x.LeaveCategoryId).NotEmpty().WithMessage("Leave Category is required");
RuleFor(x => x.Year).GreaterThan(2000);
RuleFor(x => x.Entitlement).GreaterThanOrEqualTo(0);
}
}