public TicketNotificationController()
 {
     DbContext                    = new ApplicationDbContext();
     UserRepository               = new UserRepository(DbContext);
     UserRoleRepository           = new UserRoleRepository(DbContext);
     TicketRepository             = new TicketRepository(DbContext);
     TicketNotificationRepository = new TicketNotificationRepository(DbContext);
 }
Ejemplo n.º 2
0
        public ActionResult Create(TicketAttachmentCreateViewModel formData)
        {
            if (formData == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            try
            {
                string userId = User.Identity.GetUserId();
                if (!UserRepository.DoesUserExist(userId))
                {
                    throw new Exception("User Not Found");
                }
                if (!TicketRepository.CanUserEditTicket(userId, formData.TicketId))
                {
                    return(RedirectToAction(nameof(HomeController.UnauthorizedRequest), "Home", new { error = $"You don't have the appropriate permissions to add a attachment to this ticket ({formData.TicketTitle})" }));
                }
                if (formData.Media == null)
                {
                    ModelState.AddModelError(nameof(TicketAttachmentCreateViewModel.Media), "You must attach/upload a file");
                    return(View(formData));
                }

                FileSystemRepository fileSystemRepository = new FileSystemRepository(Server, new HashSet <string>());

                TicketAttachment newTicketAttachment = new TicketAttachment()
                {
                    Description = formData.Description,
                    UserId      = userId,
                    TicketId    = formData.TicketId,
                };

                (bool hasSuccessfullySaved, string filePath, string fileUrl, string resultMessage) = fileSystemRepository.SaveFile(formData.Media);

                if (!hasSuccessfullySaved)
                {
                    throw new Exception($"File wasn't saved\n\tFileSystemRepository - Message: {resultMessage}");
                }

                newTicketAttachment.FilePath = filePath;
                newTicketAttachment.FileUrl  = Url.Content(fileUrl);

                DbContext.TicketAttachments.Add(newTicketAttachment);
                int savedEntities = DbContext.SaveChanges();

                if (savedEntities <= 0)
                {
                    throw new Exception($"Database wasn't saved\n\tFileSystemRepository - Message: {resultMessage}");
                }

                #region Send Emails
                List <TicketNotification> ticketNotifications = new TicketNotificationRepository(DbContext)
                                                                .GetTicketsTicketNotifications(formData.TicketId)
                                                                .ToList();

                if (ticketNotifications.Count > 0)
                {
                    ApplicationUser foundAttachmentAuthor = UserRepository.GetUserById(userId) ?? throw new Exception("Attachment author not found");

                    string callBackUrl = Url.Action(nameof(TicketController.Details), "Ticket", new { id = formData.TicketId }, Request.Url.Scheme);

                    EmailSystemRepository emailRepository = new EmailSystemRepository();
                    string body = emailRepository.GetSampleBodyString(
                        $"The new attachment was added to the <i>\"{formData.TicketTitle}\"</i> ticket",
                        $"made by {foundAttachmentAuthor.Email}",
                        $"Click here for the ticket details",
                        $"{callBackUrl}");

                    emailRepository.SendAll(("A new attachment was added", body), ticketNotifications);
                }
                #endregion

                return(RedirectToAction(nameof(Index), new { ticketId = formData.TicketId }));
            }
            catch
            {
                if (formData?.TicketId != null && !TicketRepository.DoesTicketExist(formData.TicketId))
                {
                    return(RedirectToAction(nameof(Index)));
                }
                return(RedirectToAction(nameof(Create), new { ticketId = formData.TicketId }));
            }
        }