[CLAUDE] Workflow: LeaveBalance business logic — trừ phép khi duyệt + số dư (Phase 11 P11-B)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 4m8s

Số dư phép theo (User × LeaveType × Year) + trừ tự động khi đơn nghỉ duyệt cuối.
Policy: cho phép vượt số dư (âm) + cảnh báo (anh main chốt), tích hợp vào trang đơn nghỉ.

Schema (Mig 42 AddLeaveBalances — pure additive, 1 bảng):
- LeaveBalance: UserId + LeaveTypeId + Year + EntitledDays + UsedDays + AdjustmentDays.
  UNIQUE (UserId,LeaveTypeId,Year), FK LeaveType Restrict, decimal(5,2).
  Remaining = Entitled + Adjustment − Used (computed, không store).

Deduction hook (ApproveLeaveRequestHandler nhánh terminal DaDuyet — exactly-once):
- Upsert LeaveBalance(RequesterUserId, LeaveTypeId, StartDate.Year), auto-create từ
  LeaveType.DaysPerYear, UsedDays += NumDays. Guard Status!=DaGuiDuyet chặn re-approve.

FK invariant guard (em main thêm sau test reveal FK risk):
- Create + UpdateDraft validate LeaveTypeId tồn tại (AnyAsync) → ConflictException.
  Đóng cửa vào — bogus type không thể tới deduction FK insert (tránh 500 kẹt đơn).

CQRS LeaveBalanceFeatures.cs: GetMy (self, lazy merge active LeaveType) + GetUser (admin)
  + AdjustLeaveBalance (admin upsert carry-over). Controller [Authorize] + admin Roles=Admin.
Embed: GetLeaveRequestByIdHandler trả balance NGƯỜI TẠO (approver xem thấy đúng).
FE: WorkflowAppDetailPage ×2 — block "Số dư phép" + cảnh báo vượt khi kind=leave (SHA256 identical).

Tests (+11, 130→154 PASS): deduction single/multi-level/accumulate/negative-allowed/
  reject-return-no-deduct + lazy-merge + adjust upsert + Create guard bogus→Conflict.
  Cũng repair 2 test S42 terminal FK-fail (template BuildLeave +seed LeaveType).

Verify: build 0 error · 154 test · FE ×2 · reviewer Max PASS (deduction exactly-once +
  FK invariant fully closed, 2 minor concurrency/comment defer).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-05-30 11:10:44 +07:00
parent 0db5e1fdc9
commit 82d7fcff4d
21 changed files with 7356 additions and 10 deletions

View File

@ -125,6 +125,9 @@ public class ApplicationDbContext
// Phase 10.4 G-P1 (Mig 40 — S38) — Chấm công web GPS.
public DbSet<Attendance> Attendances => Set<Attendance>();
// Phase 11 P11-B Wave 1 (Mig 42) — Số dư phép theo năm.
public DbSet<LeaveBalance> LeaveBalances => Set<LeaveBalance>();
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Hrm;
namespace SolutionErp.Infrastructure.Persistence.Configurations;
// EF Mig 42 P11-B Wave 1 (Phase 11) — Số dư phép theo năm.
// FK LeaveType WithMany() Restrict (catalog không cascade). UNIQUE composite
// (UserId, LeaveTypeId, Year). HRM no global HasQueryFilter — list query
// MUST .Where(!IsDeleted) thủ công ở handler.
public class LeaveBalanceConfiguration : IEntityTypeConfiguration<LeaveBalance>
{
public void Configure(EntityTypeBuilder<LeaveBalance> e)
{
e.ToTable("LeaveBalances");
e.Property(x => x.EntitledDays).HasPrecision(5, 2);
e.Property(x => x.UsedDays).HasPrecision(5, 2);
e.Property(x => x.AdjustmentDays).HasPrecision(5, 2);
e.HasOne(x => x.LeaveType)
.WithMany()
.HasForeignKey(x => x.LeaveTypeId)
.OnDelete(DeleteBehavior.Restrict);
e.HasIndex(x => new { x.UserId, x.LeaveTypeId, x.Year }).IsUnique();
e.HasIndex(x => x.UserId);
}
}

View File

@ -0,0 +1,68 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SolutionErp.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddLeaveBalances : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "LeaveBalances",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
UserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
LeaveTypeId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Year = table.Column<int>(type: "int", nullable: false),
EntitledDays = table.Column<decimal>(type: "decimal(5,2)", precision: 5, scale: 2, nullable: false),
UsedDays = table.Column<decimal>(type: "decimal(5,2)", precision: 5, scale: 2, nullable: false),
AdjustmentDays = table.Column<decimal>(type: "decimal(5,2)", precision: 5, scale: 2, nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
UpdatedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
DeletedBy = table.Column<Guid>(type: "uniqueidentifier", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_LeaveBalances", x => x.Id);
table.ForeignKey(
name: "FK_LeaveBalances_LeaveTypes_LeaveTypeId",
column: x => x.LeaveTypeId,
principalTable: "LeaveTypes",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_LeaveBalances_LeaveTypeId",
table: "LeaveBalances",
column: "LeaveTypeId");
migrationBuilder.CreateIndex(
name: "IX_LeaveBalances_UserId",
table: "LeaveBalances",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_LeaveBalances_UserId_LeaveTypeId_Year",
table: "LeaveBalances",
columns: new[] { "UserId", "LeaveTypeId", "Year" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "LeaveBalances");
}
}
}

View File

@ -2553,6 +2553,66 @@ namespace SolutionErp.Infrastructure.Persistence.Migrations
b.ToTable("Holidays", (string)null);
});
modelBuilder.Entity("SolutionErp.Domain.Hrm.LeaveBalance", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<decimal>("AdjustmentDays")
.HasPrecision(5, 2)
.HasColumnType("decimal(5,2)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<Guid?>("CreatedBy")
.HasColumnType("uniqueidentifier");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("datetime2");
b.Property<Guid?>("DeletedBy")
.HasColumnType("uniqueidentifier");
b.Property<decimal>("EntitledDays")
.HasPrecision(5, 2)
.HasColumnType("decimal(5,2)");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<Guid>("LeaveTypeId")
.HasColumnType("uniqueidentifier");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("datetime2");
b.Property<Guid?>("UpdatedBy")
.HasColumnType("uniqueidentifier");
b.Property<decimal>("UsedDays")
.HasPrecision(5, 2)
.HasColumnType("decimal(5,2)");
b.Property<Guid>("UserId")
.HasColumnType("uniqueidentifier");
b.Property<int>("Year")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("LeaveTypeId");
b.HasIndex("UserId");
b.HasIndex("UserId", "LeaveTypeId", "Year")
.IsUnique();
b.ToTable("LeaveBalances", (string)null);
});
modelBuilder.Entity("SolutionErp.Domain.Hrm.LeaveType", b =>
{
b.Property<Guid>("Id")
@ -5827,6 +5887,17 @@ namespace SolutionErp.Infrastructure.Persistence.Migrations
b.Navigation("EmployeeProfile");
});
modelBuilder.Entity("SolutionErp.Domain.Hrm.LeaveBalance", b =>
{
b.HasOne("SolutionErp.Domain.Hrm.LeaveType", "LeaveType")
.WithMany()
.HasForeignKey("LeaveTypeId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("LeaveType");
});
modelBuilder.Entity("SolutionErp.Domain.Identity.MenuItem", b =>
{
b.HasOne("SolutionErp.Domain.Identity.MenuItem", "Parent")