public SupportRequestViewModel(Issue issue)
 {
     Key = issue.Key;
     CreatedBy = issue.CreatedBy;
     CreatedDate = issue.CreatedDate;
     IssueTitle = issue.IssueTitle;
     Details = issue.Details;
     SiteRoot = VerifyAndFixTrailingSlash(issue.SiteRoot);
     PackageId = issue.PackageId;
     PackageVersion = issue.PackageVersion;
     OwnerEmail = issue.OwnerEmail;
     Reason = issue.Reason;
     AssignedTo = issue.AssignedToId;
     AssignedToGalleryUsername = issue.AssignedTo == null ? "unassigned" : issue.AssignedTo.GalleryUsername;
     IssueStatusId = issue.IssueStatusId;
     PackageRegistrationKey = issue.PackageRegistrationKey;
     UserKey = issue.UserKey;
 }
        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 AddNewSupportRequestAsync(string subject, string message, string requestorEmailAddress, string reason,
            User user, Package package = null)
        {
            var loggedInUser = user?.Username ?? "Anonymous";

            try
            {
                var newIssue = new Issue();

                // If primary on-call person is not yet configured in the Support Request DB, assign to 'unassigned'.
                var primaryOnCall = await _pagerDutyClient.GetPrimaryOnCallAsync();
                if (string.IsNullOrEmpty(primaryOnCall) || GetAdminKeyFromUsername(primaryOnCall) == -1)
                {
                    newIssue.AssignedTo = null;
                }
                else
                {
                    newIssue.AssignedToId = GetAdminKeyFromUsername(primaryOnCall);
                }

                newIssue.CreatedDate = DateTime.UtcNow;
                newIssue.Details = message;
                newIssue.IssueStatusId = IssueStatusKeys.New;
                newIssue.IssueTitle = subject;

                newIssue.CreatedBy = loggedInUser;
                newIssue.OwnerEmail = requestorEmailAddress;
                newIssue.PackageId = package?.PackageRegistration.Id;
                newIssue.PackageVersion = package?.Version;
                newIssue.Reason = reason;
                newIssue.SiteRoot = _siteRoot;
                newIssue.UserKey = user?.Key;
                newIssue.PackageRegistrationKey = package?.PackageRegistrationKey;

                await AddIssueAsync(newIssue);
            }
            catch (SqlException sqlException)
            {
                QuietLog.LogHandledException(sqlException);

                var packageInfo = "N/A";
                if (package != null)
                {
                    packageInfo = $"{package.PackageRegistration.Id} v{package.Version}";
                }

                var errorMessage = $"Error while submitting support request at {DateTime.UtcNow}. User requesting support = {loggedInUser}. Support reason = {reason ?? "N/A"}. Package info = {packageInfo}";

                await _pagerDutyClient.TriggerIncidentAsync(errorMessage);
            }
            catch (Exception e) //In case getting data from PagerDuty has failed
            {
                QuietLog.LogHandledException(e);
            }
        }