public async Task <StatusCodes> CreateItemAsync(Industry entity, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - IndustryRepo_CreateItemAsync called.");

            try
            {
                var siteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.IndustryListId
                };

                // Create Json object for SharePoint create list item
                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.Title = entity.Id;
                itemFieldsJson.Name  = entity.Name;

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                var result = await _graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - IndustryRepo_CreateItemAsync finished creating SharePoint list item.");

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - IndustryRepo_CreateItemAsync error: {ex}");
                throw;
            }
        }
Beispiel #2
0
        public async override Task <object> InsertEventAsync(AuditEvent auditEvent)
        {
            try
            {
                var webApiAuditEvent = auditEvent as AuditEventWebApi;

                if (webApiAuditEvent == null)
                {
                    return(auditEvent);
                }

                var userName = httpContextAccessor.HttpContext.User.FindFirst("preferred_username")?.Value;
                if (string.IsNullOrWhiteSpace(userName))
                {
                    return(auditEvent);
                }

                webApiAuditEvent.Action.UserName = userName;

                dynamic itemFieldsJson = new JObject();
                itemFieldsJson.Log        = webApiAuditEvent.ToJson();
                itemFieldsJson.User       = userName;
                itemFieldsJson.Action     = webApiAuditEvent.Action.ActionName;
                itemFieldsJson.Controller = webApiAuditEvent.Action.ControllerName;
                itemFieldsJson.Method     = webApiAuditEvent.Action.HttpMethod;

                dynamic itemJson = new JObject();
                itemJson.fields = itemFieldsJson;

                await graphSharePointAppService.CreateListItemAsync(siteList, itemJson.ToString());

                System.Diagnostics.Trace.WriteLine(auditEvent);
                return(auditEvent);
            }
            catch (Exception ex)
            {
                logger.LogError($"Error writing audit log: {ex}");
                return(auditEvent);
            }
        }
Beispiel #3
0
        public async Task <StatusCodes> CreateItemAsync(Notification notification, string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - NotificationRepository_CreateItemAsync called.");

            try
            {
                Guard.Against.Null(notification, "CreateItemAsync_notification null", requestId);
                Guard.Against.NullOrEmpty(notification.Title, "CreateItemAsync_title null-empty", requestId);
                Guard.Against.Null(notification.Fields, "CreateItemAsync_fields null-empty", requestId);
                Guard.Against.NullOrEmpty(notification.Fields.SentTo, "CreateItemAsync_SentTo null-empty", requestId);
                Guard.Against.NullOrEmpty(notification.Fields.Message, "CreateItemAsync_Message null-empty", requestId);
                Guard.Against.NullOrEmpty(notification.Fields.SentFrom, "CreateItemAsync_SentFrom null-empty", requestId);

                // Check result belongs to caller
                var currentUser = (_userContext.User.Claims).ToList().Find(x => x.Type == "preferred_username")?.Value;
                Guard.Against.NullOrEmpty(currentUser, "NotificationRepository_CreateItemAsync CurrentUser null-empty", requestId);

                if (notification.Fields.SentFrom != currentUser)
                {
                    _logger.LogError($"RequestId: {requestId} - NotificationRepository_CreateItemAsync sentFrom: {notification.Fields.SentFrom} current user: {currentUser} AccessDeniedException");
                    throw new AccessDeniedException($"RequestId: {requestId} - NotificationRepository_CreateItemAsync sentFrom: {notification.Fields.SentFrom} current user: {currentUser} AccessDeniedException");
                }

                // Ensure id is blank since it will be set by SharePoint
                notification.Id = String.Empty;

                _logger.LogInformation($"RequestId: {requestId} - NotificationRepository_CreateItemAsync creating SharePoint List for opportunity.");

                // Create Json object for SharePoint update list item
                dynamic notificationFieldsJson = new JObject();
                notificationFieldsJson.Title    = notification.Title;
                notificationFieldsJson.SentTo   = notification.Fields.SentTo;
                notificationFieldsJson.SentFrom = notification.Fields.SentFrom;
                notificationFieldsJson.SentDate = DateTimeOffset.Now;
                notificationFieldsJson.IsRead   = notification.Fields.IsRead;
                notificationFieldsJson.Message  = notification.Fields.Message;

                dynamic notificationJson = new JObject();
                notificationJson.fields = notificationFieldsJson;

                var notificationSiteList = new SiteList
                {
                    SiteId = _appOptions.ProposalManagementRootSiteId,
                    ListId = _appOptions.NotificationsListId
                };

                var result = await _graphSharePointAppService.CreateListItemAsync(notificationSiteList, notificationJson.ToString(), requestId);

                _logger.LogInformation($"RequestId: {requestId} - NotificationRepository_CreateItemAsync finished creating SharePoint List for notification, sending email now.");

                try
                {
                    var sendEmailResponse = await SendEmailAsync(notification, requestId);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"RequestId: {requestId} - NotificationRepository_CreateItemAsync_SendEmail Service Exception: {ex}");
                }

                return(StatusCodes.Status201Created);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - CreateItemAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - CreateItemAsync Service Exception: {ex}");
            }
        }