[CLAUDE] Drastic refactor: flat workflow Phòng × Cấp + Migration 21 (Chunk A)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m18s
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m18s
User chốt drastic refactor — bỏ phase enum hoàn toàn, dùng ChoDuyet=10 đơn nhất + currentStepIndex tracking. Workflow flat list (Phòng × Cấp × Approvers). Mỗi PE/HĐ pin WorkflowDefinitionId chạy hết quy trình đó. Schema (Migration 21 `RefactorWorkflowToFlatModel`): - Phase enum +ChoDuyet=10 (PE + Contract). Legacy 2-9 + 98 deprecated. - WorkflowStep + DepartmentId Guid? (FK Restrict) + PositionLevel int? (PE + Contract — mirror). - PE/Contract + CurrentWorkflowStepIndex int? + RejectedAtStepIndex int? - DROP table PurchaseEvaluationWorkflowStepInnerSteps (Mig 18) - DROP table WorkflowStepInnerSteps (Mig 20) - DROP column ContractDeptApproval.InnerStepId (Mig 20) - DROP column PEDeptApproval.InnerStepId (Mig 18) - DROP filtered indexes (Mig 19/20) + restore simple unique (TargetId, Phase, Dept, Stage) non-filtered Service rewrite (PE + Contract WorkflowService.TransitionAsync): - Phase transitions: DangSoanThao → ChoDuyet (Drafter trình, init idx=0) - ChoDuyet → ChoDuyet (advance idx per approve) - ChoDuyet → DaDuyet/DaPhatHanh (idx >= steps.Count → terminal) - ChoDuyet → DangSoanThao (Trả lại — save RejectedAtStepIndex) - ChoDuyet → TuChoi (Từ chối — khoá vĩnh viễn) - DangSoanThao + RejectedAtStepIndex → ChoDuyet jump-back to saved idx - Approver match: actor.Dept == step.Dept AND actor.PositionLevel >= step.PositionLevel (OR-of-many cùng cấp/dept = pass) OR Approvers.Any(Kind=User AND id match) OR Approvers.Any(Kind=Role AND actorRoles contains) - Admin role bypass policy. Last step done → gen mã HĐ (Contract only) App CQRS: - WorkflowStepDto + WorkflowStepInput drop InnerStep, add DepartmentId + PositionLevel fields. PE + Contract mirror. Tests rewrite: - DROP PeNStageApprovalTests.cs (6 test) + ContractNStageApprovalTests.cs (6 test) + PeTwoStageApprovalTests.cs (7 test) — legacy N-stage/2-stage no longer applicable - UPDATE PeWorkflowAdminTests signature to new flat input - 96 → 77 test pass (drop 19 legacy) Reference Domain entities removed: - WorkflowStepInnerStep (Contract) - PurchaseEvaluationWorkflowStepInnerStep (PE) - DTOs WorkflowStepInnerStepDto / CreateWorkflowStepInnerStepInput per module Memory `feedback_drastic_refactor_scope.md` validated: drastic refactor done in dedicated session với context fresh, scope ~5h actual (planned ~8-10h with 2x buffer). Verify: - dotnet build SolutionErp.slnx 0 error - dotnet ef database update Mig 21 LocalDB applied OK - dotnet test 77 pass (54 Domain + 23 Infra) - 3-file rule: Migration .cs + Designer.cs + Snapshot updated Pending Chunk B: FE Designer flat UI (PeWorkflowsPage + WorkflowsPage). Pending Chunk C: FE PeWorkflowPanel + workflow timeline display. Pending Chunk D: Docs + Skill + Memory + session log. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -23,12 +23,14 @@ public class CreatePeWorkflowDefinitionCommandHandlerTests
|
||||
Description: null,
|
||||
Steps: new List<CreatePeWorkflowStepInput>
|
||||
{
|
||||
new(Order: 1, Phase: (int)PurchaseEvaluationPhase.DangSoanThao, Name: "Soạn", SlaDays: 3,
|
||||
new(Order: 1, Phase: (int)PurchaseEvaluationPhase.ChoDuyet, Name: "Soạn",
|
||||
SlaDays: 3, DepartmentId: null, PositionLevel: null,
|
||||
Approvers: new List<CreatePeWorkflowStepApproverInput>
|
||||
{
|
||||
new(Kind: (int)WorkflowApproverKind.Role, AssignmentValue: AppRoles.Drafter),
|
||||
}),
|
||||
new(Order: 2, Phase: (int)PurchaseEvaluationPhase.ChoCCM, Name: "CCM duyệt", SlaDays: 2,
|
||||
new(Order: 2, Phase: (int)PurchaseEvaluationPhase.ChoDuyet, Name: "CCM duyệt",
|
||||
SlaDays: 2, DepartmentId: null, PositionLevel: null,
|
||||
Approvers: new List<CreatePeWorkflowStepApproverInput>
|
||||
{
|
||||
new(Kind: (int)WorkflowApproverKind.Role, AssignmentValue: AppRoles.CostControl),
|
||||
@ -110,10 +112,10 @@ public class CreatePeWorkflowDefinitionCommandHandlerTests
|
||||
|
||||
steps.Should().HaveCount(2);
|
||||
steps[0].Order.Should().Be(1);
|
||||
steps[0].Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao);
|
||||
steps[0].Phase.Should().Be(PurchaseEvaluationPhase.ChoDuyet);
|
||||
steps[0].Name.Should().Be("Soạn");
|
||||
steps[1].Order.Should().Be(2);
|
||||
steps[1].Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
steps[1].Phase.Should().Be(PurchaseEvaluationPhase.ChoDuyet);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@ -1,350 +0,0 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SolutionErp.Application.Common.Exceptions;
|
||||
using SolutionErp.Application.Common.Interfaces;
|
||||
using SolutionErp.Application.Contracts.Services;
|
||||
using SolutionErp.Application.Notifications;
|
||||
using SolutionErp.Domain.Common;
|
||||
using SolutionErp.Domain.Contracts;
|
||||
using SolutionErp.Domain.Identity;
|
||||
using SolutionErp.Domain.Notifications;
|
||||
using SolutionErp.Infrastructure.Services;
|
||||
using SolutionErp.Infrastructure.Tests.Common;
|
||||
|
||||
namespace SolutionErp.Infrastructure.Tests.Services;
|
||||
|
||||
// Tests cho N-stage department approval logic ở ContractWorkflowService (Mig 20).
|
||||
// Mirror PeNStageApprovalTests pattern. Cover Phòng × PositionLevel sequential
|
||||
// trong cùng phase + bypass cùng dept + reject reset + legacy fallback.
|
||||
public class ContractNStageApprovalTests : IClassFixture<IdentityFixture>
|
||||
{
|
||||
private readonly IdentityFixture _fx;
|
||||
private readonly TestApplicationDbContext _db;
|
||||
private readonly UserManager<User> _userManager;
|
||||
private readonly ContractWorkflowService _service;
|
||||
private readonly Guid _deptPro;
|
||||
private readonly Guid _deptCcm;
|
||||
|
||||
public ContractNStageApprovalTests(IdentityFixture fx)
|
||||
{
|
||||
_fx = fx;
|
||||
_db = fx.Services.GetRequiredService<TestApplicationDbContext>();
|
||||
_userManager = fx.Services.GetRequiredService<UserManager<User>>();
|
||||
|
||||
_deptPro = SeedDept("PRO-CTR-NS", "Phòng Cung ứng (CTR-NS)");
|
||||
_deptCcm = SeedDept("CCM-CTR-NS", "Phòng Kiểm soát (CTR-NS)");
|
||||
|
||||
var clock = new FixedDateTime(new DateTime(2026, 5, 7, 11, 0, 0, DateTimeKind.Utc));
|
||||
var fakeNotifications = new FakeNotificationService();
|
||||
var fakeChangelog = new FakeChangelogService();
|
||||
var fakeCodeGen = new FakeContractCodeGenerator();
|
||||
|
||||
_service = new ContractWorkflowService(
|
||||
_db, fakeCodeGen, clock, fakeNotifications, fakeChangelog, _userManager);
|
||||
}
|
||||
|
||||
private Guid SeedDept(string code, string name)
|
||||
{
|
||||
var existing = _db.Departments.FirstOrDefault(d => d.Code == code);
|
||||
if (existing is not null) return existing.Id;
|
||||
var d = new SolutionErp.Domain.Master.Department { Id = Guid.NewGuid(), Code = code, Name = name };
|
||||
_db.Departments.Add(d);
|
||||
_db.SaveChanges();
|
||||
return d.Id;
|
||||
}
|
||||
|
||||
private async Task<Guid> SeedWorkflowDefinitionAsync(
|
||||
params (Guid deptId, PositionLevel level)[] innerSteps)
|
||||
{
|
||||
// 2 step adjacent: DangGopY (current, có inner steps) → DangDamPhan (next).
|
||||
// FromDefinition build transition (DangGopY → DangDamPhan) từ step[1].Approvers role.
|
||||
var def = new WorkflowDefinition
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"NS-CTR-{Guid.NewGuid():N}".Substring(0, 18),
|
||||
Version = 1,
|
||||
ContractType = ContractType.HopDongThauPhu,
|
||||
Name = "N-stage Contract test workflow",
|
||||
IsActive = true,
|
||||
ActivatedAt = DateTime.UtcNow,
|
||||
};
|
||||
var step1 = new WorkflowStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = 1,
|
||||
Phase = ContractPhase.DangGopY,
|
||||
Name = "Góp ý",
|
||||
Approvers =
|
||||
{
|
||||
new WorkflowStepApprover
|
||||
{
|
||||
Kind = WorkflowApproverKind.Role,
|
||||
AssignmentValue = "Procurement",
|
||||
},
|
||||
},
|
||||
};
|
||||
for (int i = 0; i < innerSteps.Length; i++)
|
||||
{
|
||||
step1.InnerSteps.Add(new WorkflowStepInnerStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = i + 1,
|
||||
DepartmentId = innerSteps[i].deptId,
|
||||
PositionLevel = innerSteps[i].level,
|
||||
IsRequired = true,
|
||||
});
|
||||
}
|
||||
def.Steps.Add(step1);
|
||||
// Step 2 — chỉ để FromDefinition build transition (DangGopY → DangDamPhan).
|
||||
def.Steps.Add(new WorkflowStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = 2,
|
||||
Phase = ContractPhase.DangDamPhan,
|
||||
Name = "Đàm phán",
|
||||
Approvers =
|
||||
{
|
||||
new WorkflowStepApprover
|
||||
{
|
||||
Kind = WorkflowApproverKind.Role,
|
||||
AssignmentValue = "Procurement",
|
||||
},
|
||||
},
|
||||
});
|
||||
_db.WorkflowDefinitions.Add(def);
|
||||
await _db.SaveChangesAsync();
|
||||
return def.Id;
|
||||
}
|
||||
|
||||
private async Task<Contract> SeedContractAsync(
|
||||
ContractPhase phase,
|
||||
Guid? workflowDefinitionId = null)
|
||||
{
|
||||
var pid = Guid.NewGuid();
|
||||
if (!_db.Projects.Any(p => p.Id == pid))
|
||||
{
|
||||
_db.Projects.Add(new SolutionErp.Domain.Master.Project
|
||||
{
|
||||
Id = pid,
|
||||
Code = $"PRJ-CTR-{Random.Shared.Next(10000):D4}",
|
||||
Name = "Test project CTR-NS",
|
||||
});
|
||||
}
|
||||
var sid = Guid.NewGuid();
|
||||
if (!_db.Suppliers.Any(s => s.Id == sid))
|
||||
{
|
||||
_db.Suppliers.Add(new SolutionErp.Domain.Master.Supplier
|
||||
{
|
||||
Id = sid,
|
||||
Code = $"NCC-{Random.Shared.Next(10000):D4}",
|
||||
Name = "Test supplier",
|
||||
Type = SolutionErp.Domain.Master.SupplierType.NhaCungCap,
|
||||
});
|
||||
}
|
||||
|
||||
var contract = new Contract
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Type = ContractType.HopDongThauPhu,
|
||||
Phase = phase,
|
||||
TenHopDong = "Test HĐ N-stage",
|
||||
ProjectId = pid,
|
||||
SupplierId = sid,
|
||||
WorkflowDefinitionId = workflowDefinitionId,
|
||||
};
|
||||
_db.Contracts.Add(contract);
|
||||
await _db.SaveChangesAsync();
|
||||
return contract;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_FirstInner_NV_Approve_Blocks_Phase_Transition()
|
||||
{
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var contract = await SeedContractAsync(ContractPhase.DangGopY, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-pro-ctr-{Guid.NewGuid():N}@test", "NV PRO Contract",
|
||||
_deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
contract, ContractPhase.DangDamPhan, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "duyệt NV");
|
||||
|
||||
var fresh = await _db.Contracts.AsNoTracking().FirstAsync(x => x.Id == contract.Id);
|
||||
fresh.Phase.Should().Be(ContractPhase.DangGopY);
|
||||
|
||||
var rows = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id).ToListAsync();
|
||||
rows.Should().HaveCount(1);
|
||||
rows[0].InnerStepId.Should().NotBeNull();
|
||||
rows[0].IsBypassed.Should().BeFalse();
|
||||
rows[0].ApproverRoleSnapshot.Should().Contain("NhanVien");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_All_3_Levels_Sequential_Pass_Allow_Phase_Transition()
|
||||
{
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var contract = await SeedContractAsync(ContractPhase.DangGopY, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync($"nv-{Guid.NewGuid():N}@test", "NV", _deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
var pp = await _fx.CreateUserAsync($"pp-{Guid.NewGuid():N}@test", "PP", _deptPro, ["Procurement"], positionLevel: PositionLevel.PhoPhong);
|
||||
var tp = await _fx.CreateUserAsync($"tp-{Guid.NewGuid():N}@test", "TP", _deptPro, ["Procurement"], positionLevel: PositionLevel.TruongPhong);
|
||||
|
||||
await _service.TransitionAsync(contract, ContractPhase.DangDamPhan, nv.Id, ["Procurement"], ApprovalDecision.Approve, "NV");
|
||||
contract = await _db.Contracts.FirstAsync(x => x.Id == contract.Id);
|
||||
await _service.TransitionAsync(contract, ContractPhase.DangDamPhan, pp.Id, ["Procurement"], ApprovalDecision.Approve, "PP");
|
||||
contract = await _db.Contracts.FirstAsync(x => x.Id == contract.Id);
|
||||
await _service.TransitionAsync(contract, ContractPhase.DangDamPhan, tp.Id, ["Procurement"], ApprovalDecision.Approve, "TP");
|
||||
|
||||
var fresh = await _db.Contracts.AsNoTracking().FirstAsync(x => x.Id == contract.Id);
|
||||
fresh.Phase.Should().Be(ContractPhase.DangDamPhan);
|
||||
|
||||
var rows = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id && a.InnerStepId != null).ToListAsync();
|
||||
rows.Should().HaveCount(3);
|
||||
rows.All(r => !r.IsBypassed).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_TP_Bypass_Skips_Lower_Levels_Same_Dept()
|
||||
{
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var contract = await SeedContractAsync(ContractPhase.DangGopY, defId);
|
||||
|
||||
var tp = await _fx.CreateUserAsync(
|
||||
$"tp-bypass-{Guid.NewGuid():N}@test", "TP bypass",
|
||||
_deptPro, ["Procurement"],
|
||||
canBypassReview: true, positionLevel: PositionLevel.TruongPhong);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
contract, ContractPhase.DangDamPhan, tp.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "TP bypass");
|
||||
|
||||
var fresh = await _db.Contracts.AsNoTracking().FirstAsync(x => x.Id == contract.Id);
|
||||
fresh.Phase.Should().Be(ContractPhase.DangDamPhan);
|
||||
|
||||
var rows = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id && a.InnerStepId != null)
|
||||
.ToListAsync();
|
||||
rows.Should().HaveCount(3);
|
||||
rows.Count(r => r.IsBypassed).Should().Be(2);
|
||||
rows.Count(r => !r.IsBypassed).Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_Wrong_Department_Throws_Forbidden()
|
||||
{
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien));
|
||||
var contract = await SeedContractAsync(ContractPhase.DangGopY, defId);
|
||||
|
||||
var ccmActor = await _fx.CreateUserAsync(
|
||||
$"ccm-wrong-{Guid.NewGuid():N}@test", "CCM wrong",
|
||||
_deptCcm, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
|
||||
var act = async () => await _service.TransitionAsync(
|
||||
contract, ContractPhase.DangDamPhan, ccmActor.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "wrong dept");
|
||||
await act.Should().ThrowAsync<ForbiddenException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_Reject_Clears_InnerStep_Rows_At_Phase()
|
||||
{
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong));
|
||||
var contract = await SeedContractAsync(ContractPhase.DangGopY, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync($"nv-rej-{Guid.NewGuid():N}@test", "NV", _deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
await _service.TransitionAsync(contract, ContractPhase.DangDamPhan, nv.Id, ["Procurement"], ApprovalDecision.Approve, "NV");
|
||||
|
||||
var rowsBefore = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id && a.InnerStepId != null).CountAsync();
|
||||
rowsBefore.Should().Be(1);
|
||||
|
||||
contract = await _db.Contracts.FirstAsync(x => x.Id == contract.Id);
|
||||
|
||||
// Admin reject (skip dept block guard).
|
||||
var admin = await _fx.CreateUserAsync($"adm-rej-{Guid.NewGuid():N}@test", "Admin", null, ["Admin"]);
|
||||
await _service.TransitionAsync(
|
||||
contract, ContractPhase.TuChoi, admin.Id, ["Admin"],
|
||||
ApprovalDecision.Reject, "reject test");
|
||||
|
||||
var fresh = await _db.Contracts.AsNoTracking().FirstAsync(x => x.Id == contract.Id);
|
||||
fresh.Phase.Should().Be(ContractPhase.DangSoanThao);
|
||||
fresh.RejectedFromPhase.Should().Be(ContractPhase.DangGopY);
|
||||
|
||||
var rowsAfter = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id && a.InnerStepId != null).CountAsync();
|
||||
rowsAfter.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LegacyFallback_NoInnerSteps_Uses_2Stage_Logic()
|
||||
{
|
||||
// Không pin WorkflowDefinitionId → service fallback hardcoded Standard
|
||||
// policy → no inner steps → legacy 2-stage logic kick in.
|
||||
// Phase pair DangKiemTraCCM → DangTrinhKy yêu cầu role CostControl
|
||||
// (Standard.Transitions). NV.CCM (role CostControl, KHÔNG DeptManager)
|
||||
// → Stage=Review block.
|
||||
var contract = await SeedContractAsync(ContractPhase.DangKiemTraCCM, workflowDefinitionId: null);
|
||||
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-legacy-ctr-{Guid.NewGuid():N}@test", "NV legacy CTR",
|
||||
_deptCcm, ["CostControl"]);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
contract, ContractPhase.DangTrinhKy, nv.Id, ["CostControl"],
|
||||
ApprovalDecision.Approve, "legacy review");
|
||||
|
||||
var fresh = await _db.Contracts.AsNoTracking().FirstAsync(x => x.Id == contract.Id);
|
||||
fresh.Phase.Should().Be(ContractPhase.DangKiemTraCCM);
|
||||
|
||||
var rows = await _db.ContractDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.ContractId == contract.Id).ToListAsync();
|
||||
rows.Should().HaveCount(1);
|
||||
rows[0].InnerStepId.Should().BeNull();
|
||||
rows[0].Stage.Should().Be(ApprovalStage.Review);
|
||||
}
|
||||
}
|
||||
|
||||
// Stub services — Contract workflow tests không cần verify changelog/codegen
|
||||
// (best effort try/catch ở service đã cover fail case).
|
||||
internal class FakeChangelogService : IChangelogService
|
||||
{
|
||||
public Task LogContractChangeAsync(Guid contractId, ChangelogAction action,
|
||||
string? summary = null, string? fieldChangesJson = null, string? contextNote = null,
|
||||
ContractPhase? phaseAtChange = null, CancellationToken ct = default) => Task.CompletedTask;
|
||||
|
||||
public Task LogDetailChangeAsync(Guid contractId, Guid detailId, ChangelogAction action,
|
||||
string? summary = null, string? fieldChangesJson = null,
|
||||
ContractPhase? phaseAtChange = null, CancellationToken ct = default) => Task.CompletedTask;
|
||||
|
||||
public Task LogWorkflowTransitionAsync(Guid contractId, ContractPhase fromPhase,
|
||||
ContractPhase toPhase, string? comment, CancellationToken ct = default) => Task.CompletedTask;
|
||||
|
||||
public Task LogCommentAddedAsync(Guid contractId, string content, ContractPhase phase,
|
||||
CancellationToken ct = default) => Task.CompletedTask;
|
||||
|
||||
public Task LogAttachmentAsync(Guid contractId, Guid attachmentId, ChangelogAction action,
|
||||
string fileName, ContractPhase phase, CancellationToken ct = default) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
internal class FakeContractCodeGenerator : IContractCodeGenerator
|
||||
{
|
||||
public Task<string> GenerateAsync(Contract contract, string projectCode, string supplierCode,
|
||||
CancellationToken ct = default) => Task.FromResult($"FAKE-{projectCode}-{supplierCode}-001");
|
||||
}
|
||||
@ -1,324 +0,0 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SolutionErp.Application.Common.Exceptions;
|
||||
using SolutionErp.Domain.Common;
|
||||
using SolutionErp.Domain.Contracts;
|
||||
using SolutionErp.Domain.Identity;
|
||||
using SolutionErp.Domain.PurchaseEvaluations;
|
||||
using SolutionErp.Infrastructure.Services;
|
||||
using SolutionErp.Infrastructure.Tests.Common;
|
||||
|
||||
namespace SolutionErp.Infrastructure.Tests.Services;
|
||||
|
||||
// Tests cho N-stage department approval logic (Mig 18) ở
|
||||
// PurchaseEvaluationWorkflowService. Cover chuỗi inner step Order asc theo
|
||||
// Department × PositionLevel. Bypass cùng dept (TP có CanBypassReview).
|
||||
//
|
||||
// Pattern: dùng IdentityFixture + seed WorkflowDefinition pinned to PE.
|
||||
// Reuse FakeNotificationService + FixedDateTime từ PeTwoStageApprovalTests.cs.
|
||||
public class PeNStageApprovalTests : IClassFixture<IdentityFixture>
|
||||
{
|
||||
private readonly IdentityFixture _fx;
|
||||
private readonly TestApplicationDbContext _db;
|
||||
private readonly UserManager<User> _userManager;
|
||||
private readonly PurchaseEvaluationWorkflowService _service;
|
||||
private readonly Guid _deptPro;
|
||||
private readonly Guid _deptCcm;
|
||||
|
||||
public PeNStageApprovalTests(IdentityFixture fx)
|
||||
{
|
||||
_fx = fx;
|
||||
_db = fx.Services.GetRequiredService<TestApplicationDbContext>();
|
||||
_userManager = fx.Services.GetRequiredService<UserManager<User>>();
|
||||
|
||||
_deptPro = SeedDept("PRO-NS", "Phòng Cung ứng (NS)");
|
||||
_deptCcm = SeedDept("CCM-NS", "Phòng Kiểm soát (NS)");
|
||||
|
||||
var clock = new FixedDateTime(new DateTime(2026, 5, 7, 10, 0, 0, DateTimeKind.Utc));
|
||||
var fakeNotifications = new FakeNotificationService();
|
||||
|
||||
_service = new PurchaseEvaluationWorkflowService(
|
||||
_db, clock, fakeNotifications, _userManager);
|
||||
}
|
||||
|
||||
private Guid SeedDept(string code, string name)
|
||||
{
|
||||
var existing = _db.Departments.FirstOrDefault(d => d.Code == code);
|
||||
if (existing is not null) return existing.Id;
|
||||
var d = new SolutionErp.Domain.Master.Department { Id = Guid.NewGuid(), Code = code, Name = name };
|
||||
_db.Departments.Add(d);
|
||||
_db.SaveChanges();
|
||||
return d.Id;
|
||||
}
|
||||
|
||||
private async Task<Guid> SeedWorkflowDefinitionAsync(
|
||||
params (Guid deptId, PositionLevel level)[] innerSteps)
|
||||
{
|
||||
// 2 step adjacent: ChoPurchasing (current, có inner steps) → ChoCCM (next).
|
||||
// FromDefinition build transition (ChoPurchasing → ChoCCM) từ step[1].Approvers role.
|
||||
var def = new PurchaseEvaluationWorkflowDefinition
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Code = $"NS-TEST-{Guid.NewGuid():N}".Substring(0, 20),
|
||||
Version = 1,
|
||||
EvaluationType = PurchaseEvaluationType.DuyetNcc,
|
||||
Name = "N-stage test workflow",
|
||||
IsActive = true,
|
||||
ActivatedAt = DateTime.UtcNow,
|
||||
};
|
||||
var step1 = new PurchaseEvaluationWorkflowStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = 1,
|
||||
Phase = PurchaseEvaluationPhase.ChoPurchasing,
|
||||
Name = "Duyệt Purchasing",
|
||||
Approvers =
|
||||
{
|
||||
new PurchaseEvaluationWorkflowStepApprover
|
||||
{
|
||||
Kind = WorkflowApproverKind.Role,
|
||||
AssignmentValue = "Procurement",
|
||||
},
|
||||
},
|
||||
};
|
||||
for (int i = 0; i < innerSteps.Length; i++)
|
||||
{
|
||||
step1.InnerSteps.Add(new PurchaseEvaluationWorkflowStepInnerStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = i + 1,
|
||||
DepartmentId = innerSteps[i].deptId,
|
||||
PositionLevel = innerSteps[i].level,
|
||||
IsRequired = true,
|
||||
});
|
||||
}
|
||||
def.Steps.Add(step1);
|
||||
// Step 2 — chỉ để FromDefinition build transition (ChoPurchasing → ChoCCM).
|
||||
// KHÔNG có inner steps → nếu PE chuyển tiếp tới phase này, sẽ fallback legacy
|
||||
// hoặc admin bypass (test scope chỉ chuyển tới đây 1 lần).
|
||||
def.Steps.Add(new PurchaseEvaluationWorkflowStep
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = 2,
|
||||
Phase = PurchaseEvaluationPhase.ChoCCM,
|
||||
Name = "Duyệt CCM",
|
||||
Approvers =
|
||||
{
|
||||
new PurchaseEvaluationWorkflowStepApprover
|
||||
{
|
||||
Kind = WorkflowApproverKind.Role,
|
||||
AssignmentValue = "Procurement", // mirror step 1 để policy guard accept actor cùng role
|
||||
},
|
||||
},
|
||||
});
|
||||
_db.PurchaseEvaluationWorkflowDefinitions.Add(def);
|
||||
await _db.SaveChangesAsync();
|
||||
return def.Id;
|
||||
}
|
||||
|
||||
private async Task<PurchaseEvaluation> SeedPeAsync(
|
||||
PurchaseEvaluationPhase phase,
|
||||
Guid? workflowDefinitionId = null,
|
||||
Guid? projectId = null)
|
||||
{
|
||||
var pid = projectId ?? Guid.NewGuid();
|
||||
if (!_db.Projects.Any(p => p.Id == pid))
|
||||
{
|
||||
_db.Projects.Add(new SolutionErp.Domain.Master.Project
|
||||
{
|
||||
Id = pid,
|
||||
Code = $"PRJ-NS-{Random.Shared.Next(10000):D4}",
|
||||
Name = "Test project NS",
|
||||
});
|
||||
}
|
||||
|
||||
var pe = new PurchaseEvaluation
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Type = PurchaseEvaluationType.DuyetNcc,
|
||||
Phase = phase,
|
||||
TenGoiThau = "Test gói thầu NS",
|
||||
ProjectId = pid,
|
||||
WorkflowDefinitionId = workflowDefinitionId,
|
||||
};
|
||||
_db.PurchaseEvaluations.Add(pe);
|
||||
await _db.SaveChangesAsync();
|
||||
return pe;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_FirstInner_NV_Approve_Blocks_Phase_Transition()
|
||||
{
|
||||
// Arrange: 1 dept (PRO) × 3 cấp.
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-pro-ns-{Guid.NewGuid():N}@test", "NV PRO NS",
|
||||
_deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
|
||||
// Act: NV.PRO duyệt cấp 1 (NV).
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "duyệt NV");
|
||||
|
||||
// Assert: phase chưa đổi (còn 2 cấp PP+TP), 1 row InnerStepId set.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
var rows = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
rows.Should().HaveCount(1);
|
||||
rows[0].InnerStepId.Should().NotBeNull();
|
||||
rows[0].IsBypassed.Should().BeFalse();
|
||||
rows[0].ApproverRoleSnapshot.Should().Contain("NhanVien");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_All_3_Levels_Sequential_Pass_Allow_Phase_Transition()
|
||||
{
|
||||
// Arrange: 1 dept × 3 cấp.
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync($"nv-{Guid.NewGuid():N}@test", "NV", _deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
var pp = await _fx.CreateUserAsync($"pp-{Guid.NewGuid():N}@test", "PP", _deptPro, ["Procurement"], positionLevel: PositionLevel.PhoPhong);
|
||||
var tp = await _fx.CreateUserAsync($"tp-{Guid.NewGuid():N}@test", "TP", _deptPro, ["Procurement"], positionLevel: PositionLevel.TruongPhong);
|
||||
|
||||
// Act: lần lượt NV → PP → TP.
|
||||
await _service.TransitionAsync(pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"], ApprovalDecision.Approve, "NV");
|
||||
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id);
|
||||
await _service.TransitionAsync(pe, PurchaseEvaluationPhase.ChoCCM, pp.Id, ["Procurement"], ApprovalDecision.Approve, "PP");
|
||||
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id);
|
||||
await _service.TransitionAsync(pe, PurchaseEvaluationPhase.ChoCCM, tp.Id, ["Procurement"], ApprovalDecision.Approve, "TP");
|
||||
|
||||
// Assert: phase chuyển + 3 rows + KHÔNG bypass.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
var rows = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id && a.InnerStepId != null).ToListAsync();
|
||||
rows.Should().HaveCount(3);
|
||||
rows.All(r => !r.IsBypassed).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_TP_Bypass_Skips_Lower_Levels_Same_Dept()
|
||||
{
|
||||
// Arrange: 1 dept × 3 cấp. TP có CanBypassReview=true.
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong),
|
||||
(_deptPro, PositionLevel.TruongPhong));
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, defId);
|
||||
|
||||
var tp = await _fx.CreateUserAsync(
|
||||
$"tp-bypass-{Guid.NewGuid():N}@test", "TP bypass",
|
||||
_deptPro, ["Procurement"],
|
||||
canBypassReview: true, positionLevel: PositionLevel.TruongPhong);
|
||||
|
||||
// Act: TP bypass approve trực tiếp (skip NV+PP cùng dept).
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, tp.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "TP bypass");
|
||||
|
||||
// Assert: phase chuyển, 3 rows (NV+PP=bypass true, TP=bypass false).
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
var rows = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id && a.InnerStepId != null)
|
||||
.ToListAsync();
|
||||
rows.Should().HaveCount(3);
|
||||
rows.Count(r => r.IsBypassed).Should().Be(2); // NV + PP bypassed
|
||||
rows.Count(r => !r.IsBypassed).Should().Be(1); // TP exact match
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_Wrong_Department_Throws_Forbidden()
|
||||
{
|
||||
// Arrange: inner step yêu cầu dept PRO. Actor thuộc CCM.
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien));
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, defId);
|
||||
|
||||
var ccmActor = await _fx.CreateUserAsync(
|
||||
$"ccm-wrong-{Guid.NewGuid():N}@test", "CCM wrong",
|
||||
_deptCcm, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
|
||||
// Act + Assert.
|
||||
var act = async () => await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, ccmActor.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "wrong dept");
|
||||
await act.Should().ThrowAsync<ForbiddenException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NStage_Reject_Clears_InnerStep_Rows_At_Phase()
|
||||
{
|
||||
// Arrange: NV approve trước → 1 row N-stage. Sau đó reject.
|
||||
var defId = await SeedWorkflowDefinitionAsync(
|
||||
(_deptPro, PositionLevel.NhanVien),
|
||||
(_deptPro, PositionLevel.PhoPhong));
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, defId);
|
||||
|
||||
var nv = await _fx.CreateUserAsync($"nv-rej-{Guid.NewGuid():N}@test", "NV", _deptPro, ["Procurement"], positionLevel: PositionLevel.NhanVien);
|
||||
await _service.TransitionAsync(pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"], ApprovalDecision.Approve, "NV");
|
||||
|
||||
var rowsBeforeReject = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id && a.InnerStepId != null).CountAsync();
|
||||
rowsBeforeReject.Should().Be(1);
|
||||
|
||||
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id);
|
||||
|
||||
// Act: admin "Trả lại" (target=DangSoanThao + decision=Reject Session 14).
|
||||
var admin = await _fx.CreateUserAsync($"adm-rej-{Guid.NewGuid():N}@test", "Admin", null, ["Admin"]);
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.DangSoanThao, admin.Id, ["Admin"],
|
||||
ApprovalDecision.Reject, "trả lại test");
|
||||
|
||||
// Assert: phase = DangSoanThao, RejectedFromPhase = ChoPurchasing,
|
||||
// N-stage rows tại ChoPurchasing đã clear.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao);
|
||||
fresh.RejectedFromPhase.Should().Be(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
var rowsAfterReject = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id && a.InnerStepId != null).CountAsync();
|
||||
rowsAfterReject.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LegacyFallback_NoInnerSteps_Uses_2Stage_Logic()
|
||||
{
|
||||
// Arrange: KHÔNG pin WorkflowDefinitionId → service fallback hardcoded
|
||||
// policy → no inner steps → legacy 2-stage logic kick in.
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing, workflowDefinitionId: null);
|
||||
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-legacy-{Guid.NewGuid():N}@test", "NV legacy",
|
||||
_deptPro, ["Procurement"]); // KHÔNG có positionLevel — legacy không cần
|
||||
|
||||
// Act: NV approve → legacy 2-stage Stage=Review row.
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "legacy review");
|
||||
|
||||
// Assert: phase chưa đổi (NV chỉ Review), 1 row InnerStepId=NULL (legacy).
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
var rows = await _db.PurchaseEvaluationDepartmentApprovals.AsNoTracking()
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
rows.Should().HaveCount(1);
|
||||
rows[0].InnerStepId.Should().BeNull();
|
||||
rows[0].Stage.Should().Be(ApprovalStage.Review);
|
||||
}
|
||||
}
|
||||
@ -1,274 +0,0 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SolutionErp.Application.Notifications;
|
||||
using SolutionErp.Domain.Common;
|
||||
using SolutionErp.Domain.Contracts;
|
||||
using SolutionErp.Domain.Identity;
|
||||
using SolutionErp.Domain.Notifications;
|
||||
using SolutionErp.Domain.PurchaseEvaluations;
|
||||
using SolutionErp.Infrastructure.Services;
|
||||
using SolutionErp.Infrastructure.Tests.Common;
|
||||
|
||||
namespace SolutionErp.Infrastructure.Tests.Services;
|
||||
|
||||
// Tests cho 2-stage department approval logic ở PurchaseEvaluationWorkflowService.
|
||||
// Cover bug fix anh Kiệt: NV.PRO duyệt phase ChoPurchasing → BLOCK transition.
|
||||
// TPB.PRO confirm → ALLOW transition.
|
||||
//
|
||||
// Pattern: dùng IdentityFixture (Identity stack + DbContext SQLite) để
|
||||
// test thật end-to-end service thay vì mock.
|
||||
public class PeTwoStageApprovalTests : IClassFixture<IdentityFixture>
|
||||
{
|
||||
private readonly IdentityFixture _fx;
|
||||
private readonly TestApplicationDbContext _db;
|
||||
private readonly UserManager<User> _userManager;
|
||||
private readonly PurchaseEvaluationWorkflowService _service;
|
||||
private readonly Guid _deptPro;
|
||||
private readonly Guid _deptCcm;
|
||||
|
||||
public PeTwoStageApprovalTests(IdentityFixture fx)
|
||||
{
|
||||
_fx = fx;
|
||||
_db = fx.Services.GetRequiredService<TestApplicationDbContext>();
|
||||
_userManager = fx.Services.GetRequiredService<UserManager<User>>();
|
||||
|
||||
// Seed 2 departments (idempotent — check trước khi insert vì fixture
|
||||
// shared across tests trong class).
|
||||
_deptPro = SeedDept("PRO", "Phòng Cung ứng");
|
||||
_deptCcm = SeedDept("CCM", "Phòng Kiểm soát chi phí");
|
||||
|
||||
var clock = new FixedDateTime(new DateTime(2026, 5, 4, 10, 0, 0, DateTimeKind.Utc));
|
||||
var fakeNotifications = new FakeNotificationService();
|
||||
|
||||
_service = new PurchaseEvaluationWorkflowService(
|
||||
_db,
|
||||
clock,
|
||||
fakeNotifications,
|
||||
_userManager);
|
||||
}
|
||||
|
||||
private Guid SeedDept(string code, string name)
|
||||
{
|
||||
var existing = _db.Departments.FirstOrDefault(d => d.Code == code);
|
||||
if (existing is not null) return existing.Id;
|
||||
var d = new SolutionErp.Domain.Master.Department { Id = Guid.NewGuid(), Code = code, Name = name };
|
||||
_db.Departments.Add(d);
|
||||
_db.SaveChanges();
|
||||
return d.Id;
|
||||
}
|
||||
|
||||
private async Task<PurchaseEvaluation> SeedPeAsync(PurchaseEvaluationPhase phase, Guid? projectId = null)
|
||||
{
|
||||
// Project required by FK constraint.
|
||||
var pid = projectId ?? Guid.NewGuid();
|
||||
if (!_db.Projects.Any(p => p.Id == pid))
|
||||
{
|
||||
_db.Projects.Add(new SolutionErp.Domain.Master.Project
|
||||
{
|
||||
Id = pid,
|
||||
Code = $"PRJ-{Random.Shared.Next(10000):D4}",
|
||||
Name = "Test project",
|
||||
});
|
||||
}
|
||||
|
||||
var pe = new PurchaseEvaluation
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Type = PurchaseEvaluationType.DuyetNcc,
|
||||
Phase = phase,
|
||||
TenGoiThau = "Test gói thầu",
|
||||
ProjectId = pid,
|
||||
};
|
||||
_db.PurchaseEvaluations.Add(pe);
|
||||
await _db.SaveChangesAsync();
|
||||
return pe;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NV_Review_Blocks_Phase_Transition()
|
||||
{
|
||||
// Arrange: NV.PRO (role Procurement, dept PRO, NOT DeptManager).
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-{Guid.NewGuid():N}@test", "NV PRO", _deptPro, ["Procurement"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
// Act: NV approve to ChoCCM.
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "review");
|
||||
|
||||
// Assert: phase KHÔNG đổi, có 1 row Stage=Review.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
var deptApprovals = await _db.PurchaseEvaluationDepartmentApprovals
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
deptApprovals.Should().HaveCount(1);
|
||||
deptApprovals[0].Stage.Should().Be(ApprovalStage.Review);
|
||||
deptApprovals[0].DepartmentId.Should().Be(_deptPro);
|
||||
deptApprovals[0].IsBypassed.Should().BeFalse();
|
||||
|
||||
var approvals = await _db.PurchaseEvaluationApprovals
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
approvals.Should().HaveCount(1);
|
||||
approvals[0].Comment.Should().StartWith("[Review NV]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TPB_Confirm_After_NV_Review_Allows_Transition()
|
||||
{
|
||||
// Arrange: NV review trước, sau đó TPB confirm.
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-{Guid.NewGuid():N}@test", "NV PRO", _deptPro, ["Procurement"]);
|
||||
var tpb = await _fx.CreateUserAsync(
|
||||
$"tpb-{Guid.NewGuid():N}@test", "TPB PRO", _deptPro, ["DeptManager", "Procurement"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "review NV");
|
||||
|
||||
// Re-fetch tracked entity (service modifies state ở Phase prior).
|
||||
pe = await _db.PurchaseEvaluations.FirstAsync(x => x.Id == pe.Id);
|
||||
|
||||
// Act: TPB confirm.
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, tpb.Id, ["DeptManager", "Procurement"],
|
||||
ApprovalDecision.Approve, "confirm TPB");
|
||||
|
||||
// Assert: phase đổi, có 2 row (Review + Confirm).
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
var deptApprovals = await _db.PurchaseEvaluationDepartmentApprovals
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
deptApprovals.Should().HaveCount(2);
|
||||
deptApprovals.Should().Contain(a => a.Stage == ApprovalStage.Review && a.ApproverUserId == nv.Id);
|
||||
deptApprovals.Should().Contain(a => a.Stage == ApprovalStage.Confirm && a.ApproverUserId == tpb.Id && !a.IsBypassed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NV_With_BypassReview_Allows_Transition_With_IsBypassed_True()
|
||||
{
|
||||
// Arrange: NV CanBypassReview=true.
|
||||
var nv = await _fx.CreateUserAsync(
|
||||
$"nv-bypass-{Guid.NewGuid():N}@test", "NV PRO bypass",
|
||||
_deptPro, ["Procurement"], canBypassReview: true);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
// Act: bypass user approve → đẩy thẳng Stage=Confirm.
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, nv.Id, ["Procurement"],
|
||||
ApprovalDecision.Approve, "bypass approve");
|
||||
|
||||
// Assert: phase đổi, có 1 row Stage=Confirm + IsBypassed=true.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
var deptApprovals = await _db.PurchaseEvaluationDepartmentApprovals
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
deptApprovals.Should().HaveCount(1);
|
||||
deptApprovals[0].Stage.Should().Be(ApprovalStage.Confirm);
|
||||
deptApprovals[0].IsBypassed.Should().BeTrue();
|
||||
deptApprovals[0].ApproverRoleSnapshot.Should().Be("NV(bypass)");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Admin_Skips_TwoStage_Logic_Entirely()
|
||||
{
|
||||
// Arrange: Admin role.
|
||||
var admin = await _fx.CreateUserAsync(
|
||||
$"admin-{Guid.NewGuid():N}@test", "Admin user", null, ["Admin"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoPurchasing);
|
||||
|
||||
// Act: Admin approve.
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoCCM, admin.Id, ["Admin"],
|
||||
ApprovalDecision.Approve, "admin force");
|
||||
|
||||
// Assert: phase đổi, KHÔNG có DepartmentApproval row.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
var deptApprovals = await _db.PurchaseEvaluationDepartmentApprovals
|
||||
.Where(a => a.PurchaseEvaluationId == pe.Id).ToListAsync();
|
||||
deptApprovals.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reject_To_DangSoanThao_Sets_RejectedFromPhase_TraLai()
|
||||
{
|
||||
// Session 14: "Trả lại" semantic — target=DangSoanThao + decision=Reject.
|
||||
// Service set RejectedFromPhase + force về DangSoanThao + Drafter resume jump-back.
|
||||
var actor = await _fx.CreateUserAsync(
|
||||
$"ccm-{Guid.NewGuid():N}@test", "CCM TPB", _deptCcm, ["DeptManager", "CostControl"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.DangSoanThao, actor.Id, ["DeptManager", "CostControl"],
|
||||
ApprovalDecision.Reject, "trả lại Drafter sửa");
|
||||
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.DangSoanThao);
|
||||
fresh.RejectedFromPhase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reject_To_TuChoi_Locks_Permanently_No_RejectedFromPhase()
|
||||
{
|
||||
// Session 14: "Từ chối" semantic — target=TuChoi + decision=Reject.
|
||||
// Service KHÔNG override target + KHÔNG set RejectedFromPhase (phiếu khoá vĩnh viễn).
|
||||
var actor = await _fx.CreateUserAsync(
|
||||
$"ccm-{Guid.NewGuid():N}@test", "CCM TPB cancel", _deptCcm, ["DeptManager", "CostControl"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.ChoCCM);
|
||||
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.TuChoi, actor.Id, ["DeptManager", "CostControl"],
|
||||
ApprovalDecision.Reject, "từ chối hoàn toàn");
|
||||
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.TuChoi);
|
||||
fresh.RejectedFromPhase.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Resume_After_Reject_Jumps_Back_To_RejectedPhase()
|
||||
{
|
||||
// Arrange: PE rejected từ ChoCCM, đang ở DangSoanThao + RejectedFromPhase=ChoCCM.
|
||||
var drafter = await _fx.CreateUserAsync(
|
||||
$"drafter-{Guid.NewGuid():N}@test", "Drafter", _deptPro, ["Drafter"]);
|
||||
var pe = await SeedPeAsync(PurchaseEvaluationPhase.DangSoanThao);
|
||||
pe.RejectedFromPhase = PurchaseEvaluationPhase.ChoCCM;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
// Act: drafter trình lại từ DangSoanThao → ChoPurchasing (target không
|
||||
// quan trọng vì resume sẽ override = RejectedFromPhase). Note: service
|
||||
// jump tới ChoCCM, nhưng nếu actor có dept thì sẽ hit 2-stage logic.
|
||||
// Simpler: dùng admin để bypass 2-stage gate khi resume cũng OK.
|
||||
var admin = await _fx.CreateUserAsync(
|
||||
$"admin-resume-{Guid.NewGuid():N}@test", "Admin resume", null, ["Admin"]);
|
||||
await _service.TransitionAsync(
|
||||
pe, PurchaseEvaluationPhase.ChoPurchasing, admin.Id, ["Admin"],
|
||||
ApprovalDecision.Approve, "drafter resume");
|
||||
|
||||
// Assert: phase jump tới ChoCCM (không phải ChoPurchasing target),
|
||||
// RejectedFromPhase=null.
|
||||
var fresh = await _db.PurchaseEvaluations.AsNoTracking().FirstAsync(x => x.Id == pe.Id);
|
||||
fresh.Phase.Should().Be(PurchaseEvaluationPhase.ChoCCM);
|
||||
fresh.RejectedFromPhase.Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
// Stub notification service — tests không cần verify notification path
|
||||
// (best effort try/catch ở service đã cover fail case).
|
||||
internal class FakeNotificationService : INotificationService
|
||||
{
|
||||
public Task NotifyAsync(Guid userId, NotificationType type, string title,
|
||||
string? description = null, string? href = null, Guid? refId = null,
|
||||
CancellationToken ct = default) => Task.CompletedTask;
|
||||
|
||||
public Task NotifyManyAsync(IEnumerable<Guid> userIds, NotificationType type,
|
||||
string title, string? description = null, string? href = null,
|
||||
Guid? refId = null, CancellationToken ct = default) => Task.CompletedTask;
|
||||
}
|
||||
Reference in New Issue
Block a user