public async Task <object> Handle(UploadCommand request, CancellationToken cancellationToken) { string filePath = "", guid = ""; Image image = null; if (!Helper.FileIsImage(request.FileName)) { throw new UnsupportedException("File", request.FileName); } guid = Helper.GenerateGUID(); filePath = GlobalVar.FileBERelativePath + "\\App_Data\\" + guid + Path.GetExtension(request.FileName); using (var ms = request.Stream) { image = Image.FromStream(ms); image.Save(filePath); image.Dispose(); } string result = _cloudinary.UploadToCloudinary(filePath, guid, "TicketContent"); if (result != "Error") { Helper.DeleteLocalFile(filePath); return(new { url = result }); } return(new { }); }
public async Task <int> Handle(MyAccountCommand request, CancellationToken cancellationToken) { Image image; string userId; string filePath; AppUser appUser; userId = _currentUserService.UserId; appUser = _context.User.FirstOrDefault(x => x.Id == userId); if (request.imageData.Length > 0) { filePath = GlobalVar.FileBERelativePath + "\\App_Data\\" + userId + ".png"; image = Helper.Base64ToImage(request.imageData); using (image = new Bitmap(image, 150, 150)) { image.Save(filePath); image.Dispose(); } string result = _cloudinary.UploadToCloudinary(filePath, userId, "Profil Picture"); if (result != "Error") { appUser.ProfilPicture = result; Helper.DeleteLocalFile(filePath); } } if (request.Language.Length > 0) { appUser.Language = request.Language; } appUser.Signature = request.Signature; return(await _context.SaveChangesAsync(cancellationToken)); }
public async Task <object> Handle(CreateEmployeeCommand request, CancellationToken cancellationToken) { IdentityResult result; ApplicationUser employee; AppUser appEmployeeUser, currentUser; string password, defaultPP, PPLink; currentUser = _context.User.FirstOrDefault(x => x.Id == _currentUserService.UserId); employee = new ApplicationUser() { UserName = request.Email, EmailConfirmed = true, NormalizedEmail = request.Email.ToUpper(), Email = request.Email, }; password = Helper.GeneratePassword(8); try { result = await _userManager.CreateAsync(employee, password); if (!result.Succeeded) { throw new ApplicationException(_userManager.IdentityResultError(result)); } else { appEmployeeUser = new AppUser { Id = employee.Id, UserName = request.Email, LastName = request.LastName, FirstName = request.FirstName, PhoneNumber = request.PhoneNumber, Email = request.Email, IsEmployee = true, CompanyName = currentUser.CompanyName }; employee.User = appEmployeeUser; result = await _userManager.AddToRoleAsync(employee, "Customer"); if (!result.Succeeded) { await _userManager.DeleteAsync(employee); throw new ApplicationException(_userManager.IdentityResultError(result)); } defaultPP = Helper.CreateDefaultPP(appEmployeeUser.Id, appEmployeeUser.FirstName, appEmployeeUser.LastName); PPLink = ""; if (defaultPP != "Error") { PPLink = _cloudinaryService.UploadToCloudinary(defaultPP, appEmployeeUser.Id, "Profil Picture"); if (PPLink != "" || PPLink != "Error") { appEmployeeUser.ProfilPicture = PPLink; } Helper.DeleteLocalFile(defaultPP); } else { appEmployeeUser.ProfilPicture = @"https://res.cloudinary.com/doifcljfo/image/upload/v1581022559/NoIdent_afyvgb.png"; } currentUser.UserList.Add(appEmployeeUser); if (await _context.SaveChangesAsync(cancellationToken) > 0) { _email.SendEmail(request.Email, "Account created", "Hello " + request.FirstName + "" + ".<br> You now have an account on My ticket. here's your connexion data : " + "<br> Login : "******"<br> Password :"******"<br>" + "Kind regards. <br>My ticket team."); } } } catch (Exception e) { throw e; } return(await Task.FromResult(new { Id = appEmployeeUser.Id })); }
public async Task <int> Handle(CreateReplyTicketByMailCommand request, CancellationToken cancellationToken) { try { List <Email> lt = _email.ReadInbox() as List <Email>; string senderEmail = ""; bool createTicket = true; string internRef = ""; int ticketId = 0; int index = -1; string subject = ""; bool isMemberOrAdmin = false; List <byte[]> attachementList; TicketLine ticketLine; TicketHeader ticket; AppUser user; int result = 0; foreach (var t in lt) { user = _context.User.FirstOrDefault(x => x.Email.ToUpper() == t.from.ToUpper()); if (user == null) { senderEmail = t.from; } else { if (await _userManager.IsInRoleAsync(user.Id, "Member")) { isMemberOrAdmin = true; } else if (await _userManager.IsInRoleAsync(user.Id, "Admin")) { isMemberOrAdmin = true; } } if (t.Subject.Length > 0) { subject = Regex.Replace(t.Subject, @"\s+", ""); index = subject.IndexOf("-#"); if (index > -1) { internRef = subject.Substring(index + 2); Int32.TryParse(internRef, out ticketId); ticket = _context.TickerHeader.FirstOrDefault(x => x.Id == ticketId); if (ticket != null) { if (user == null && ticket.Email.ToUpper() == senderEmail.ToUpper()) { ticket.Status = _context.Status.FirstOrDefault(x => x.Name == "Open"); ticketLine = new TicketLine { AskForClose = false, Content = t.Body, Email = senderEmail, }; } else if ((user != null && ticket.Email.ToUpper() == user.Email.ToUpper()) || isMemberOrAdmin) { if (isMemberOrAdmin) { ticket.Status = _context.Status.FirstOrDefault(x => x.Name == "Waiting on customer"); ticket.Readed = TicketHeader.ReadedByMemberOrAdmin; } else { ticket.Status = _context.Status.FirstOrDefault(x => x.Name == "Open"); ticket.Readed = TicketHeader.ReadedByCustomer; } ticketLine = new TicketLine { AskForClose = false, Content = t.Body, ResponseBy = user, }; } else { throw new NotFoundException(nameof(TicketHeader), ticket.Id); } foreach (var str in _cloudinary.UploadToCloudinary(t.attachement.ToList())) { ticketLine.Content += str; } ticket.TicketLine.Add(ticketLine); result += await _context.SaveChangesAsync(cancellationToken); } else { throw new NotFoundException(nameof(TicketHeader), ticketId); } } else { //We need to create the ticket ticket = new TicketHeader { Title = t.Subject, Description = t.Body, Priority = _context.Priority.FirstOrDefault(x => x.Name == "Unknow"), Project = _context.Project.FirstOrDefault(x => x.Name == "Unknow"), Type = _context.Type.FirstOrDefault(x => x.Name == "Unknow"), AssignTO = null, ClosedDate = null, Requester = user, Status = _context.Status.FirstOrDefault(x => x.Name == "Open"), CreatedBy = user?.Id, Email = user?.Email, Readed = TicketHeader.ReadedByCustomer }; if (user == null) { ticket.Email = senderEmail; } foreach (var str in _cloudinary.UploadToCloudinary(t.attachement.ToList())) { ticket.Description += str; } _context.TickerHeader.Add(ticket); await _context.SaveChangesAsync(cancellationToken); ticket.InternTitle = ticket.Title + " - #" + ticket.Id; result += await _context.SaveChangesAsync(cancellationToken); if (result > 0 && !isMemberOrAdmin) { if (user != null) { senderEmail = user.Email; } _email.SendEmail(senderEmail, ticket.InternTitle, "Your ticket is successfully created. <br>" + "We will reply soon as possible.<br>" + "<hr>Ticket content :<br>" + ticket.Description); } } } else { throw new ApplicationException(); } } return(result); } catch (Exception e) { throw e; } }
public async Task <bool> Handle(UserSignUpCommand request, CancellationToken cancellationToken) { IdentityResult result; AppUser appUser; ApplicationUser user; string defaultPP; string PPLink; List <TicketHeader> tl; user = new ApplicationUser { UserName = request.Email, Email = request.Email, }; try { result = await _userManager.CreateAsync(user, request.Password); if (!result.Succeeded) { throw new ApplicationException(_userManager.IdentityResultError(result)); } else { appUser = new AppUser { Id = user.Id, UserName = request.Email, LastName = request.LastName, FirstName = request.FirstName, Email = request.Email, }; _context.User.Add(appUser); await _context.SaveChangesAsync(cancellationToken); user.User = appUser; result = await _userManager.AddToRoleAsync(user, "Customer"); if (!result.Succeeded) { await _userManager.DeleteAsync(user); throw new ApplicationException(_userManager.IdentityResultError(result)); } } defaultPP = Helper.CreateDefaultPP(appUser.Id, appUser.FirstName, appUser.LastName); PPLink = ""; if (defaultPP != "Error") { PPLink = _cloudinaryService.UploadToCloudinary(defaultPP, appUser.Id, "Profil Picture"); if (PPLink != "" || PPLink != "Error") { appUser.ProfilPicture = PPLink; } Helper.DeleteLocalFile(defaultPP); } else { appUser.ProfilPicture = @"https://res.cloudinary.com/doifcljfo/image/upload/v1581022559/NoIdent_afyvgb.png"; } //Associate ticket is previous ticket tl = _context.TickerHeader.Where(x => x.Email == appUser.Email).ToList(); foreach (var t in tl) { t.Requester = appUser; } await _context.SaveChangesAsync(cancellationToken); } catch (Exception e) { throw e; } return(true); }