private async Task AddIssueAsync(Issue issue)
        {
            _supportRequestDbContext.Issues.Add(issue);

            await _supportRequestDbContext.CommitChangesAsync();

            var historyEntry = new History
            {
                EntryDate = DateTime.UtcNow,
                IssueId = issue.Key,
                AssignedToId = issue.AssignedToId.HasValue ? issue.AssignedToId : null,
                IssueStatusId = issue.IssueStatusId,
                Comments = "New Issue Created",
                EditedBy = issue.CreatedBy
            };

            await AddHistoryRecordAsync(historyEntry);
        }
        public async Task AddHistoryRecordAsync(History entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            _supportRequestDbContext.Histories.Add(entry);

            await _supportRequestDbContext.CommitChangesAsync();
        }
        public async Task UpdateIssueAsync(int issueId, int? assignedToId, int issueStatusId, string comment, string editedBy)
        {
            if (string.IsNullOrEmpty(editedBy))
            {
                throw new ArgumentNullException(nameof(editedBy));
            }

            var currentIssue = GetIssueById(issueId);

            if (currentIssue == null)
            {
                throw new ArgumentOutOfRangeException(nameof(editedBy));
            }
            else
            {
                var changesDetected = false;

                var comments = comment?.Trim();
                if (!string.IsNullOrEmpty(comments))
                {
                    comments += "\r\n";
                }

                if (currentIssue.AssignedToId != assignedToId)
                {
                    var previousAssignedUsername = currentIssue.AssignedTo?.GalleryUsername ?? "unassigned";
                    string newAssignedUsername;
                    if (assignedToId.HasValue)
                    {
                        var admin = GetAdminByKey(assignedToId.Value);
                        if (admin == null)
                        {
                            newAssignedUsername = "******";
                        }
                        else
                        {
                            newAssignedUsername = admin.GalleryUsername;
                            currentIssue.AssignedToId = assignedToId;
                        }
                    }
                    else
                    {
                        newAssignedUsername = "******";
                    }

                    comments += $"Reassigned issue from '{previousAssignedUsername}' to '{newAssignedUsername}'.\r\n";

                    currentIssue.AssignedTo = null;

                    changesDetected = true;
                }

                if (currentIssue.IssueStatusId != issueStatusId)
                {
                    var previousIssueStatusName = currentIssue.IssueStatus.Name;
                    var newIssueStatusName = GetIssueStatusNameById(issueStatusId);

                    currentIssue.IssueStatusId = issueStatusId;
                    currentIssue.IssueStatus = null;

                    comments += $"Updated issue status from '{previousIssueStatusName}' to '{newIssueStatusName}'.\r\n";

                    changesDetected = true;
                }

                if (!changesDetected && !string.IsNullOrEmpty(comments))
                {
                    changesDetected = true;
                }

                if (changesDetected)
                {
                    await _supportRequestDbContext.CommitChangesAsync();

                    var history = new History
                    {
                        IssueId = issueId,
                        EditedBy = editedBy,
                        AssignedToId = assignedToId == -1 ? null : assignedToId,
                        EntryDate = DateTime.UtcNow,
                        IssueStatusId = issueStatusId,
                        Comments = comments
                    };

                    await AddHistoryRecordAsync(history);
                }
            }
        }