using System.Net; using System.Net.Http.Json; using Helpdesk.Application.Auth; using Helpdesk.Application.Catalogs; using Helpdesk.Application.Common; using Helpdesk.Application.Issues; using Helpdesk.Application.Notifications; using Helpdesk.Application.Statistics; using Helpdesk.Application.Users; using static Helpdesk.IntegrationTests.BackendFixture; namespace Helpdesk.IntegrationTests; public sealed class IssueWorkflowTests(BackendFixture fixture) : IClassFixture { [Fact] public async Task ReporterAndAssigneesCanResolveAnIssueAndManagementCanReportOnIt() { using var anonymous = fixture.CreateClient(); using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken); var author = await fixture.RegisterAsync(anonymous); using var reporter = fixture.CreateClient((await LoginAsync(anonymous, author.Email)).AccessToken); var management = await CreateStaffAsync(anonymous, admin, "MANAGEMENT"); var firstAssignee = await CreateStaffAsync(anonymous, admin, "FACILITY_MANAGER"); var secondAssignee = await CreateStaffAsync(anonymous, admin, "FACILITY_MANAGER"); using var manager = fixture.CreateClient(management.AccessToken); using var firstSolver = fixture.CreateClient(firstAssignee.AccessToken); using var secondSolver = fixture.CreateClient(secondAssignee.AccessToken); var categories = await ReadAsync(reporter.GetAsync("/api/v1/issue-categories", Cancellation)); var buildings = await ReadAsync(reporter.GetAsync("/api/v1/buildings", Cancellation)); var request = new CreateIssueRequest("Nefunkční projektor", "Projektor se nezapne.", categories[0].Id, buildings[0].Id, "A101"); var issue = await PostIssueAsync(reporter, "/api/v1/issues", request, HttpStatusCode.Created); var path = $"/api/v1/issues/{issue.Id}"; Assert.Equal("RECEIVED", issue.Status); await ExpectAsync(anonymous.GetAsync(path, Cancellation), HttpStatusCode.Unauthorized); var assign = new AssignIssueRequest(firstAssignee.User.Id, null, issue.RowVersion); await ExpectAsync(reporter.PostAsJsonAsync(path + "/assign", assign, Cancellation), HttpStatusCode.Forbidden); issue = await PostIssueAsync(manager, path + "/assign", assign); await ExpectAsync(firstSolver.PostAsJsonAsync(path + "/start", new IssueVersionRequest(assign.RowVersion), Cancellation), HttpStatusCode.Conflict); var reporterNotifications = await InboxAsync(reporter, issue.Id); Assert.Equal("ISSUE_STATUS_CHANGED", Assert.Single(reporterNotifications).Type); issue = await PostIssueAsync(manager, path + "/assign", new AssignIssueRequest(secondAssignee.User.Id, null, issue.RowVersion)); Assert.Equal("ISSUE_ASSIGNED", Assert.Single(await InboxAsync(secondSolver, issue.Id)).Type); Assert.Equal(reporterNotifications, await InboxAsync(reporter, issue.Id)); issue = await PostIssueAsync(secondSolver, path + "/start", new IssueVersionRequest(issue.RowVersion)); Assert.Equal("IN_PROGRESS", issue.Status); reporterNotifications = await InboxAsync(reporter, issue.Id); issue = await PostIssueAsync(manager, path + "/assign", new AssignIssueRequest(firstAssignee.User.Id, null, issue.RowVersion)); Assert.Equal("ASSIGNED", issue.Status); var reassignment = Assert.Single((await InboxAsync(reporter, issue.Id)).Except(reporterNotifications)); Assert.Equal("ISSUE_STATUS_CHANGED", reassignment.Type); Assert.Equal(2, (await InboxAsync(firstSolver, issue.Id)).Count(notification => notification.Type == "ISSUE_ASSIGNED")); issue = await PostIssueAsync(firstSolver, path + "/start", new IssueVersionRequest(issue.RowVersion)); const string commentText = "Porucha stále trvá."; var comment = await ReadAsync(reporter.PostAsJsonAsync(path + "/comments", new CommentRequest(commentText), Cancellation)); issue = await ReadAsync(firstSolver.GetAsync(path, Cancellation)); issue = await PostIssueAsync(firstSolver, path + "/submit-for-verification", new VerifyIssueRequest(issue.RowVersion, null)); Assert.Equal("PENDING_VERIFICATION", issue.Status); issue = await PostIssueAsync(reporter, path + "/confirm", new IssueVersionRequest(issue.RowVersion)); Assert.Equal("RESOLVED", issue.Status); var events = await ReadAsync(reporter.GetAsync(path + "/events", Cancellation)); Assert.Equal(commentText, Assert.Single(events, entry => entry.Id == comment.Id).Note); Assert.Equal("RESOLVED", events[^1].ToStatus); var openIssue = await PostIssueAsync(reporter, "/api/v1/issues", request with { Title = "Porucha osvětlení" }, HttpStatusCode.Created); await VerifyReadsAsync(reporter, manager, issue, openIssue); } private async Task CreateStaffAsync(HttpClient anonymous, HttpClient admin, string role) { var user = await fixture.RegisterAsync(anonymous); await ExpectAsync(admin.PutAsJsonAsync($"/api/v1/admin/users/{user.Id}/role", new ChangeRoleRequest(role), Cancellation), HttpStatusCode.NoContent); return await LoginAsync(anonymous, user.Email); } private static async Task VerifyReadsAsync(HttpClient reporter, HttpClient manager, IssueDetail resolved, IssueDetail open) { var query = $"/api/v1/issues?categoryId={resolved.CategoryId}&buildingId={resolved.BuildingId}&pageSize=1"; var first = await ReadAsync>(manager.GetAsync(query + "&page=1", Cancellation)); var second = await ReadAsync>(manager.GetAsync(query + "&page=2", Cancellation)); var filtered = await ReadAsync>(manager.GetAsync(query + "&status=RESOLVED", Cancellation)); Assert.Equal(2, first.TotalCount); Assert.Equal(open.Id, Assert.Single(first.Items).Id); Assert.Equal(resolved.Id, Assert.Single(second.Items).Id); Assert.Equal(1, filtered.TotalCount); Assert.Equal(resolved.Id, Assert.Single(filtered.Items).Id); const string overview = "/api/v1/statistics/overview"; await ExpectAsync(reporter.GetAsync(overview, Cancellation), HttpStatusCode.Forbidden); var from = Uri.EscapeDataString(resolved.CreatedAt.AddMinutes(-1).ToString("O")); var to = Uri.EscapeDataString(open.CreatedAt.AddMinutes(1).ToString("O")); var period = $"?from={from}&to={to}"; var statistics = await ReadAsync(manager.GetAsync(overview + period, Cancellation)); Assert.Equal(2, statistics.TotalIssues); Assert.Equal(1, statistics.IssuesByStatus["RESOLVED"]); Assert.Equal(1, statistics.IssuesByStatus["RECEIVED"]); Assert.NotNull(statistics.AverageHoursToAssignment); Assert.NotNull(statistics.AverageHoursToClosure); var trend = await ReadAsync(manager.GetAsync("/api/v1/statistics/issues/trend" + period, Cancellation)); Assert.Equal(2, trend.Sum(point => point.CreatedIssues)); Assert.Equal(1, trend.Sum(point => point.ClosedIssues)); var after = await ReadAsync(manager.GetAsync(overview + $"?from={to}", Cancellation)); Assert.Equal(0, after.TotalIssues); } private static Task PostIssueAsync( HttpClient client, string path, T request, HttpStatusCode expected = HttpStatusCode.OK) => ReadAsync(client.PostAsJsonAsync(path, request, Cancellation), expected); private static async Task InboxAsync(HttpClient client, int issueId) { var notifications = await ReadAsync>(client.GetAsync("/api/v1/notifications", Cancellation)); return notifications.Items.Where(notification => notification.IssueId == issueId).ToArray(); } }