new mailling for not confirmed emails

vnext
Paul Schneider 3 years ago
parent 79d4d64071
commit 0201ea64e6
25 changed files with 46 additions and 31 deletions

@ -35,7 +35,7 @@ restore: /home/paul/workspace/tmp/yavsc/src/Yavsc/bin/output/wwwroot
make -C src/test restore make -C src/test restore
test: test:
make -C src/test make -C test/yavscTests
web: web:
make -C src/Yavsc web make -C src/Yavsc web

@ -5,42 +5,62 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using cli.Services; using cli.Services;
using cli.Model; using cli.Model;
using System.Linq;
using Yavsc.Models;
using System.Collections.Generic;
using System;
namespace cli { namespace cli
{
public class SendMailCommandProvider : ICommander { public class SendMailCommandProvider : ICommander
{
readonly Dictionary<string, Func<ApplicationUser, bool>> Criterias =
new Dictionary<string, Func<ApplicationUser, bool>>
{
{"allow-monthly", u => u.AllowMonthlyEmail },
{ "email-not-confirmed", u => !u.EmailConfirmed }
};
public CommandLineApplication Integrate(CommandLineApplication rootApp) public CommandLineApplication Integrate(CommandLineApplication rootApp)
{ {
CommandArgument sendMailCommandArg = null;
CommandOption sendHelpOption = null; CommandArgument codeCommandArg = null;
CommandLineApplication sendMailCommandApp CommandArgument critCommandArg = null;
= rootApp.Command("send", CommandOption sendHelpOption = null;
(target) => CommandLineApplication sendMailCommandApp
{ = rootApp.Command("send-monthly",
target.FullName = "Send email"; (target) =>
target.Description = "Sends emails using given template"; {
sendHelpOption = target.HelpOption("-? | -h | --help"); target.FullName = "Send email";
sendMailCommandArg = target.Argument( target.Description = "Sends monthly emails using given template from code";
"class", sendHelpOption = target.HelpOption("-? | -h | --help");
"class name of mailling to execute (actually, only 'monthly') .", codeCommandArg = target.Argument(
multipleValues: true); "code",
}, false); "template code of mailling to execute.");
critCommandArg = target.Argument(
"criteria",
"user selection criteria : 'allow-monthly' or 'email-not-confirmed'");
}, false);
sendMailCommandApp.OnExecute(() => sendMailCommandApp.OnExecute(() =>
{ {
if (sendMailCommandArg.Value == "monthly") int code;
bool showhelp = !int.TryParse(codeCommandArg.Value, out code)
|| Criterias.ContainsKey(critCommandArg.Value);
if (!showhelp)
{ {
var host = new WebHostBuilder(); var host = new WebHostBuilder();
var hostengnine = host.UseEnvironment("Development")
var hostengnine = host.UseEnvironment(Program.HostingEnvironment.EnvironmentName)
.UseServer("cli") .UseServer("cli")
.UseStartup<Startup>() .UseStartup<Startup>()
.Build(); .Build();
var app = hostengnine.Start(); var app = hostengnine.Start();
var mailer = app.Services.GetService<EMailer>(); var mailer = app.Services.GetService<EMailer>();
var loggerFactory = app.Services.GetService<ILoggerFactory>(); var loggerFactory = app.Services.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<cli.Program>(); var logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("Starting emailling"); logger.LogInformation("Starting emailling");
mailer.SendMonthlyEmail(1, "UserOrientedTemplate"); mailer.SendEmailFromCriteria(code, Criterias[critCommandArg.Value]);
logger.LogInformation("Finished emailling"); logger.LogInformation("Finished emailling");
} }
else else

@ -61,9 +61,9 @@ namespace cli.Services
} }
public void SendMonthlyEmail(long templateCode, string baseclassName = DefaultBaseClassName) public void SendEmailFromCriteria(long templateCode, Func<ApplicationUser,bool> criteria)
{ {
string className = "Generated" + baseclassName; string className = "GeneratedTemplate";
string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value; string subtemp = stringLocalizer["MonthlySubjectTemplate"].Value;
@ -156,7 +156,7 @@ namespace cli.Services
throw new InvalidOperationException("No generated template"); throw new InvalidOperationException("No generated template");
} }
foreach (var user in dbContext.ApplicationUser.Where( foreach (var user in dbContext.ApplicationUser.Where(
u => u.AllowMonthlyEmail u => criteria(u)
)) ))
{ {
logger.LogInformation("Generation for " + user.UserName); logger.LogInformation("Generation for " + user.UserName);
@ -172,15 +172,10 @@ namespace cli.Services
logger.LogError($"{mailSentInfo.ErrorMessage}"); logger.LogError($"{mailSentInfo.ErrorMessage}");
else else
logger.LogInformation($"mailId:{mailSentInfo?.MessageId} \nto:{user.UserName}"); logger.LogInformation($"mailId:{mailSentInfo?.MessageId} \nto:{user.UserName}");
} }
} }
} }
} }
} }
} }
} }

Loading…