From 572f66f905a139eb25f4e5b63058763868eda22c Mon Sep 17 00:00:00 2001 From: mahan Date: Wed, 7 Jan 2026 16:52:50 +0330 Subject: [PATCH] feat: implement auto-pending for task sections reaching estimated time --- .../AutoPendingFullTimeTaskSectionsCommand.cs | 62 +++++++++++++++++++ .../ProjectAgg/Entities/TaskSection.cs | 2 +- .../Repositories/ITaskSectionRepository.cs | 3 + .../Repositories/TaskSectionRepository.cs | 9 +++ .../ProgramManager/ProjectController.cs | 3 + 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoPendingFullTimeTaskSections/AutoPendingFullTimeTaskSectionsCommand.cs diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoPendingFullTimeTaskSections/AutoPendingFullTimeTaskSectionsCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoPendingFullTimeTaskSections/AutoPendingFullTimeTaskSectionsCommand.cs new file mode 100644 index 00000000..6e1e9802 --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/AutoPendingFullTimeTaskSections/AutoPendingFullTimeTaskSectionsCommand.cs @@ -0,0 +1,62 @@ +using GozareshgirProgramManager.Application._Common.Interfaces; +using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain._Common; +using GozareshgirProgramManager.Domain.ProjectAgg.Repositories; +using GozareshgirProgramManager.Domain.ProjectAgg.Enums; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoPendingFullTimeTaskSections; + +public record AutoPendingFullTimeTaskSectionsCommand : IBaseCommand; + +public class AutoPendingFullTimeTaskSectionsCommandHandler : IBaseCommandHandler +{ + private readonly ITaskSectionRepository _taskSectionRepository; + private readonly IUnitOfWork _unitOfWork; + + public AutoPendingFullTimeTaskSectionsCommandHandler( + ITaskSectionRepository taskSectionRepository, + IUnitOfWork unitOfWork) + { + _taskSectionRepository = taskSectionRepository; + _unitOfWork = unitOfWork; + } + + public async Task Handle(AutoPendingFullTimeTaskSectionsCommand request, CancellationToken cancellationToken) + { + try + { + // تمام سکشن‌هایی که هنوز Pending یا Completed نشده‌اند را دریافت کن + var taskSections = await _taskSectionRepository.GetAllNotCompletedOrPendingIncludeAllAsync(cancellationToken); + + foreach (var section in taskSections) + { + var totalSpent = section.GetTotalTimeSpent(); + var estimate = section.FinalEstimatedHours; + + if (estimate.TotalMinutes <= 0) + continue; // تسک بدون تخمین را نادیده بگیر + + if (totalSpent >= estimate) + { + // مهم: وضعیت را مستقل از فعال/غیرفعال بودن فعالیت‌ها PendingForCompletion کنیم + if (section.IsInProgress()) + { + // اگر فعالیت فعال دارد، با وضعیت جدید متوقف شود + section.StopWork(TaskSectionStatus.PendingForCompletion, "اتمام خودکار - رسیدن به ۱۰۰٪ زمان تخمینی"); + } + else + { + section.UpdateStatus(TaskSectionStatus.PendingForCompletion); + } + } + } + + await _unitOfWork.SaveChangesAsync(cancellationToken); + return OperationResult.Success(); + } + catch (Exception ex) + { + return OperationResult.Failure($"خطا در در انتظار تکمیل قرار دادن خودکار تسک‌ها: {ex.Message}"); + } + } +} diff --git a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/TaskSection.cs b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/TaskSection.cs index 072422fe..83b86c23 100644 --- a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/TaskSection.cs +++ b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/TaskSection.cs @@ -270,7 +270,7 @@ public class TaskSection : EntityBase // متوقف کردن فعالیت با EndDate دقیق شده activeActivity.StopWorkWithSpecificTime(adjustedEndDate, "متوقف خودکار - بیش از تایم تعیین شده"); - UpdateStatus(TaskSectionStatus.Incomplete); + UpdateStatus(TaskSectionStatus.PendingForCompletion); } } } \ No newline at end of file diff --git a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs index 487991a8..50e1a2b0 100644 --- a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs +++ b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs @@ -14,4 +14,7 @@ public interface ITaskSectionRepository: IRepository Task> GetAssignedToUserAsync(long userId); Task> GetActiveSectionsIncludeAllAsync(CancellationToken cancellationToken); Task HasUserAnyInProgressSectionAsync(long userId, CancellationToken cancellationToken = default); + + // جدید: دریافت سکشن‌هایی که هنوز Completed یا PendingForCompletion نشده‌اند با اطلاعات کامل + Task> GetAllNotCompletedOrPendingIncludeAllAsync(CancellationToken cancellationToken); } \ No newline at end of file diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs index d6d60b69..ce590dc7 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs @@ -53,4 +53,13 @@ public class TaskSectionRepository:RepositoryBase,ITaskSection .AnyAsync(x => x.CurrentAssignedUserId == userId && x.Status == TaskSectionStatus.InProgress, cancellationToken); } + + public Task> GetAllNotCompletedOrPendingIncludeAllAsync(CancellationToken cancellationToken) + { + return _context.TaskSections + .Where(x => x.Status != TaskSectionStatus.Completed && x.Status != TaskSectionStatus.PendingForCompletion) + .Include(x => x.Activities) + .Include(x => x.AdditionalTimes) + .ToListAsync(cancellationToken); + } } \ No newline at end of file diff --git a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs index 95d6da94..b2841335 100644 --- a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs +++ b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs @@ -22,6 +22,7 @@ using GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectH using MediatR; using Microsoft.AspNetCore.Mvc; using ServiceHost.BaseControllers; +using GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoPendingFullTimeTaskSections; namespace ServiceHost.Areas.Admin.Controllers.ProgramManager; @@ -122,6 +123,8 @@ public class ProjectController : ProgramManagerBaseController { // اجرای Command برای متوقف کردن تسک‌های overtime قبل از نمایش await _mediator.Send(new AutoStopOverTimeTaskSectionsCommand()); + // سپس تسک‌هایی که به 100% زمان تخمینی رسیده‌اند را به حالت PendingForCompletion ببریم + await _mediator.Send(new AutoPendingFullTimeTaskSectionsCommand()); var res = await _mediator.Send(query); return res;