public async Task <OperationResult <Unit> > Handle(SendEmailCommandRequest request, CancellationToken cancellationToken) { var response = new SendEmailCommandResponse(request.MessageId); OperationResult <Unit> result; var newEmail = await _dbContext.Email.SingleOrDefaultAsync(f => f.EmailId == request.TransferObject.EmailId, cancellationToken); if (newEmail.IsNull()) { newEmail = new Email { EmailId = request.TransferObject.EmailId, CreateDate = _apiInfrastructure.Clock.NowAsSouthAfrican }; switch (request.TransferObject.EmailType) { case EmailType.NewUserAdd: CommandService.NewUserAddEmailGen(request, newEmail, _dbContext, _apiInfrastructure); break; case EmailType.ForgotPassword: CommandService.ForgotPasswordNotificationEmailGen(request, newEmail, _dbContext, _apiInfrastructure); break; default: throw new Exception("Invalid Email Type"); } _dbContext.Email.Add(newEmail); await _dbContext.SaveChangesAsync(cancellationToken); } var apiKey = _apiInfrastructure.ApiConfiguration.GetAppSetting("SendGrid:ApiKey"); var fromEmail = _apiInfrastructure.ApiConfiguration.GetAppSetting("SendGrid:FromEmail"); var fromName = _apiInfrastructure.ApiConfiguration.GetAppSetting("SendGrid:FromName"); var responses = await CommandService.SendEmailViaSendGrid(cancellationToken, newEmail, apiKey, fromEmail, fromName); if (responses.StatusCode == HttpStatusCode.Accepted || responses.StatusCode == HttpStatusCode.OK) { newEmail.SentDate = _apiInfrastructure.Clock.NowAsSouthAfrican; newEmail.Result = Newtonsoft.Json.JsonConvert.SerializeObject(responses.Body); await _dbContext.SaveChangesAsync(cancellationToken); response.Success = true; result = new OperationResult <Unit>(EnumOperationResult.Ok); } else { result = new OperationResult <Unit>(EnumOperationResult.Error); } return(result); }
public static void UserAddedToStokvelEmailGen(SendEmailCommandRequest request, Object email, DvtDatabaseContext dbContext, IApiInfrastructure apiInfrastructure) { //var user = dbContext.Users.Include(g => g.UserTokens).Single(f => f.Id == request.TransferObject.UserId); //var model = new StokvelWelcomeModel //{ // Name = user.FirstName //}; //var emailBody = CompileEmailTemplate.Compile(model, "StokvelWelcome.cshtml"); //email.Body = emailBody; //email.Subject = "eStokvel Invitation"; //email.To = user.Email; }
public static void ForgotPasswordNotificationEmailGen(SendEmailCommandRequest request, Email newEmail, DvtDatabaseContext dbContext, IApiInfrastructure apiInfrastructure) { var token = dbContext.Token.Include(o => o.Id).Single(o => o.Id == request.TransferObject.TokenId); var model = new ForgotPasswordModel { Name = token.User.FirstName, HostUrl = apiInfrastructure.ApiConfiguration.GetAppSetting("HostUrl"), Token = token.Value.ToString() }; var emailBody = CompileEmailTemplate.Compile(model, "ForgotPassword.cshtml"); newEmail.Body = emailBody; newEmail.Subject = "Password Reset"; newEmail.To = token.User.Email; }
public static void NewUserAddEmailGen(SendEmailCommandRequest request, Email email, DvtDatabaseContext dbContext, IApiInfrastructure apiInfrastructure) { var user = dbContext.UserAccount.Include(g => g.Token).Single(f => f.UserAccountId == request.TransferObject.UserAccountId); var model = new NewUserAddModel { Name = user.FirstName, Email = user.Email, HostUrl = apiInfrastructure.ApiConfiguration.GetAppSetting("HostUrl"), Token = user.Token.Single(g => g.ExpiryDate > apiInfrastructure.Clock.NowAsSouthAfrican && g.TokenTypeId == (int)TokenType.SetPassword).Value.ToString() }; var emailBody = CompileEmailTemplate.Compile(model, "NewUserAdd.cshtml"); email.Body = emailBody; email.Subject = "User Invitation"; email.To = user.Email; }