Spaces:
Runtime error
Runtime error
File size: 13,309 Bytes
3dd5c9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TaskTrackingSystem.Shared;
using TaskTrackingSystem.Shared.Enums;
using TaskTrackingSystem.Shared.Models.Report;
using TaskTrackingSystem.Shared.Models.Issue;
using TaskTrackingSystem.WebApi.Infrastructure;
namespace TaskTrackingSystem.WebApi.Features.Report
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class ReportController : ControllerBase
{
private readonly ReportService _reportService;
public ReportController(ReportService reportService)
{
_reportService = reportService;
}
[HttpGet("issues")]
[ProducesResponseType(typeof(List<IssueDto>), StatusCodes.Status200OK)]
public async Task<ActionResult<Result<List<IssueDto>>>> GetIssuesReport()
{
var result = await _reportService.GetIssuesReportAsync(User.GetRoleId(), User.GetUserId());
return StatusCode(result.StatusCode, result);
}
[HttpGet("tasks")]
[ProducesResponseType(typeof(PagedResult<TaskReportDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<TaskReportDto>>> GetTasksReport(
[FromQuery] DateTime? startDate,
[FromQuery] DateTime? endDate,
[FromQuery] string? status,
[FromQuery] string? search,
[FromQuery] int? projectId,
[FromQuery] bool? assignedToMe,
[FromQuery] bool? assignedToMyTeam,
[FromQuery] PaginationQuery? paging = null)
{
(startDate, endDate) = NormalizeDateRange(startDate, endDate);
if (startDate.HasValue && endDate.HasValue && endDate.Value < startDate.Value)
{
return BadRequest(Result.Failure("End date cannot be earlier than start date.", 400));
}
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
var full = await _reportService.GetTasksReportAsync(
startDate, endDate, status, search, projectId, User.GetRoleId(), User.GetUserId(), assignedToMe, assignedToMyTeam);
return StatusCode(full.StatusCode, full);
}
var page = PaginationExtensions.NormalizePage(paging.Page);
var limit = PaginationExtensions.NormalizePageSize(paging.Limit ?? 0);
var result = await _reportService.GetPagedTasksReportAsync(
startDate, endDate, status, search, projectId, User.GetRoleId(), User.GetUserId(), page, limit, assignedToMe, assignedToMyTeam);
return Ok(result);
}
[HttpGet("user-productivity")]
[ProducesResponseType(typeof(PagedResult<UserProductivityDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<UserProductivityDto>>> GetUserProductivityReport(
[FromQuery] PaginationQuery? paging = null)
{
var users = await _reportService.GetUserProductivityReportAsync(User.GetRoleId(), User.GetUserId());
if (!users.IsSuccess || users.Value == null)
{
return StatusCode(users.StatusCode, new PagedResult<UserProductivityDto>());
}
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
return StatusCode(users.StatusCode, users);
}
var page = PaginationExtensions.NormalizePage(paging?.Page);
var limit = PaginationExtensions.NormalizePageSize(paging?.Limit ?? 0);
var query = users.Value.AsQueryable();
var paged = await query.ToPagedResultAsync(page, limit);
return Ok(paged);
}
[HttpGet("task-status-summary")]
public async Task<ActionResult<Result<IEnumerable<TaskStatusSummaryDto>>>> GetTaskStatusSummary(
[FromQuery] string? search,
[FromQuery] AppTaskStatus? statusId,
[FromQuery] long? projectId)
{
var result = await _reportService.GetTaskStatusSummaryAsync(search, statusId, projectId, User.GetRoleId(), User.GetUserId());
return StatusCode(result.StatusCode, result);
}
[HttpGet("task-status-summary/excel")]
public async Task<IActionResult> DownloadTaskStatusSummaryExcel(
[FromQuery] string? search,
[FromQuery] AppTaskStatus? statusId,
[FromQuery] long? projectId)
{
var result = await _reportService.GetTaskStatusSummaryAsync(search, statusId, projectId, User.GetRoleId(), User.GetUserId());
if (!result.IsSuccess || result.Value == null)
return BadRequest(new { message = result.ErrorMessage });
var bytes = _reportService.ExportTaskStatusSummaryToExcel(result.Value);
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"TaskStatusSummary_{DateTime.Today:yyyyMMdd}.xlsx");
}
[HttpGet("team-productivity")]
[ProducesResponseType(typeof(PagedResult<TeamProductivityReportDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<TeamProductivityReportDto>>> GetTeamProductivity(
[FromQuery] string? search,
[FromQuery] PaginationQuery? paging = null)
{
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
var full = await _reportService.GetTeamProductivityAsync(search, User.GetRoleId(), User.GetUserId());
return StatusCode(full.StatusCode, full);
}
var page = PaginationExtensions.NormalizePage(paging?.Page);
var limit = PaginationExtensions.NormalizePageSize(paging?.Limit ?? 0);
var result = await _reportService.GetPagedTeamProductivityAsync(search, User.GetRoleId(), User.GetUserId(), page, limit);
return Ok(result);
}
[HttpGet("team-productivity/excel")]
public async Task<IActionResult> DownloadTeamProductivityExcel([FromQuery] string? search)
{
var result = await _reportService.GetTeamProductivityAsync(search, User.GetRoleId(), User.GetUserId());
if (!result.IsSuccess || result.Value == null)
return BadRequest(new { message = result.ErrorMessage });
var bytes = _reportService.ExportTeamProductivityToExcel(result.Value);
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"TeamProductivity_{DateTime.Today:yyyyMMdd}.xlsx");
}
[HttpGet("overdue-critical")]
[ProducesResponseType(typeof(PagedResult<OverdueCriticalTaskDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<OverdueCriticalTaskDto>>> GetOverdueCritical(
[FromQuery] string? search,
[FromQuery] long? projectId,
[FromQuery] int? priorityId,
[FromQuery] string? delayType,
[FromQuery] bool? assignedToMe,
[FromQuery] bool? assignedToMyTeam,
[FromQuery] PaginationQuery? paging = null,
[FromQuery] int? page = null,
[FromQuery] int? limit = null)
{
var requestedPage = page ?? paging?.Page;
var requestedLimit = limit ?? paging?.Limit;
if (!requestedPage.HasValue && !requestedLimit.HasValue)
{
var full = await _reportService.GetOverdueCriticalTasksAsync(
search, projectId, User.GetRoleId(), User.GetUserId(), priorityId, delayType, assignedToMe, assignedToMyTeam);
return StatusCode(full.StatusCode, full);
}
var normalizedPage = PaginationExtensions.NormalizePage(requestedPage);
var normalizedLimit = PaginationExtensions.NormalizePageSize(requestedLimit ?? 0);
var result = await _reportService.GetPagedOverdueCriticalTasksAsync(
search, projectId, User.GetRoleId(), User.GetUserId(), normalizedPage, normalizedLimit, priorityId, delayType, assignedToMe, assignedToMyTeam);
return Ok(result);
}
[HttpGet("overdue-critical/excel")]
public async Task<IActionResult> DownloadOverdueCriticalExcel(
[FromQuery] string? search,
[FromQuery] long? projectId,
[FromQuery] bool? assignedToMe,
[FromQuery] bool? assignedToMyTeam)
{
var result = await _reportService.GetOverdueCriticalTasksAsync(
search, projectId, User.GetRoleId(), User.GetUserId(), assignedToMe: assignedToMe, assignedToMyTeam: assignedToMyTeam);
if (!result.IsSuccess || result.Value == null)
return BadRequest(new { message = result.ErrorMessage });
var bytes = _reportService.ExportOverdueCriticalToExcel(result.Value);
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"OverdueCriticalTasks_{DateTime.Today:yyyyMMdd}.xlsx");
}
[HttpGet("employee-productivity")]
[ProducesResponseType(typeof(PagedResult<EmployeeProductivityReportDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<EmployeeProductivityReportDto>>> GetEmployeeProductivity(
[FromQuery] string? search,
[FromQuery] DateTime? startDate,
[FromQuery] DateTime? endDate,
[FromQuery] string? status,
[FromQuery] bool? assignedToMe,
[FromQuery] bool? assignedToMyTeam,
[FromQuery] PaginationQuery? paging = null)
{
(startDate, endDate) = NormalizeDateRange(startDate, endDate);
if (startDate.HasValue && endDate.HasValue && endDate.Value < startDate.Value)
{
return BadRequest(Result.Failure("End date cannot be earlier than start date.", 400));
}
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
var reportData = await _reportService.GetEmployeeProductivityReportAsync(
search, startDate, endDate, status, User.GetRoleId(), User.GetUserId(), assignedToMe, assignedToMyTeam);
return StatusCode(reportData.StatusCode, reportData);
}
var reportDataPaged = await _reportService.GetPagedEmployeeProductivityReportAsync(
search, startDate, endDate, status, User.GetRoleId(), User.GetUserId(),
paging.Page ?? 1, paging.Limit ?? 10, assignedToMe, assignedToMyTeam);
return Ok(reportDataPaged);
}
[HttpGet("project-progress")]
[ProducesResponseType(typeof(PagedResult<ProjectProgressReportDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<PagedResult<ProjectProgressReportDto>>> GetProjectProgress(
[FromQuery] string? search,
[FromQuery] DateTime? startDate,
[FromQuery] DateTime? endDate,
[FromQuery] string? status,
[FromQuery] bool? assignedToMe,
[FromQuery] bool? assignedToMyTeam,
[FromQuery] PaginationQuery? paging = null)
{
(startDate, endDate) = NormalizeDateRange(startDate, endDate);
if (startDate.HasValue && endDate.HasValue && endDate.Value < startDate.Value)
{
return BadRequest(Result.Failure("End date cannot be earlier than start date.", 400));
}
if (paging == null || (!paging.Page.HasValue && !paging.Limit.HasValue))
{
var reportData = await _reportService.GetProjectProgressReportAsync(
search, startDate, endDate, status, User.GetRoleId(), User.GetUserId(), assignedToMe, assignedToMyTeam);
return StatusCode(reportData.StatusCode, reportData);
}
var reportDataPaged = await _reportService.GetPagedProjectProgressReportAsync(
search, startDate, endDate, status, User.GetRoleId(), User.GetUserId(),
paging.Page ?? 1, paging.Limit ?? 10, assignedToMe, assignedToMyTeam);
return Ok(reportDataPaged);
}
private static (DateTime? StartDate, DateTime? EndDate) NormalizeDateRange(DateTime? startDate, DateTime? endDate)
{
static DateTime? Normalize(DateTime? value) => value.HasValue
? DateTime.SpecifyKind(value.Value.Date, DateTimeKind.Utc)
: null;
return (Normalize(startDate), Normalize(endDate));
}
}
}
|