diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoUpdateDeployStatus/AutoUpdateDeployStatusCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoUpdateDeployStatus/AutoUpdateDeployStatusCommand.cs new file mode 100644 index 00000000..5164a0c0 --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoUpdateDeployStatus/AutoUpdateDeployStatusCommand.cs @@ -0,0 +1,52 @@ +using System.Linq; +using GozareshgirProgramManager.Application._Common.Interfaces; +using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain.ProjectAgg.Entities; +using GozareshgirProgramManager.Domain.ProjectAgg.Enums; +using Microsoft.EntityFrameworkCore; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoUpdateDeployStatus; + +public record AutoUpdateDeployStatusCommand : IBaseCommand; + +public class AutoUpdateDeployStatusCommandHandler : IBaseCommandHandler +{ + private readonly IProgramManagerDbContext _dbContext; + + public AutoUpdateDeployStatusCommandHandler(IProgramManagerDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task Handle(AutoUpdateDeployStatusCommand request, + CancellationToken cancellationToken) + { + // Fetch all sections whose phase is still marked as not completed + var sections = await _dbContext.TaskSections + .Include(ts => ts.Task) + .ThenInclude(t => t.Phase) + .Where(ts => ts.Task.Phase.DeployStatus == ProjectDeployStatus.NoTCompleted) + .ToListAsync(cancellationToken); + + if (sections.Count == 0) + return OperationResult.Success(); + + var phasesToUpdate = sections + .GroupBy(ts => ts.Task.PhaseId) + .Where(g => g.All(s => s.Status == TaskSectionStatus.Completed)) + .Select(g => g.First().Task.Phase) + .Distinct() + .ToList(); + + if (phasesToUpdate.Count == 0) + return OperationResult.Success(); + + foreach (var phase in phasesToUpdate) + { + phase.UpdateDeployStatus(ProjectDeployStatus.PendingDevDeploy); + } + + await _dbContext.SaveChangesAsync(cancellationToken); + return OperationResult.Success(); + } +} diff --git a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs index 39b40a19..a0fe2e7c 100644 --- a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs +++ b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs @@ -2,6 +2,7 @@ using GozareshgirProgramManager.Application._Common.Models; using GozareshgirProgramManager.Application.Modules.Projects.Commands.AssignProject; using GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoStopOverTimeTaskSections; +using GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoUpdateDeployStatus; using GozareshgirProgramManager.Application.Modules.Projects.Commands.ChangeStatusSection; using GozareshgirProgramManager.Application.Modules.Projects.Commands.CreateProject; using GozareshgirProgramManager.Application.Modules.Projects.Commands.DeleteProject; @@ -124,7 +125,8 @@ public class ProjectController : ProgramManagerBaseController [HttpGet("deploy-board")] public async Task>> GetProjectDeployBoard() { - + // قبل از دریافت دیتا، وضعیت دیپلوی را بر اساس تکمیل شدن تمام سکشن‌ها به‌روزرسانی می‌کنیم + await _mediator.Send(new AutoUpdateDeployStatusCommand()); var request = new GetProjectDeployBoardListQuery(); return await _mediator.Send(request); }