public Task SendInviteAsync(IUser assigner, IUser user, string appName)
        {
            Guard.NotNull(assigner);
            Guard.NotNull(user);
            Guard.NotNull(appName);

            var vars = new TemplatesVars {
                Assigner = assigner, AppName = appName
            };

            if (user.HasConsent())
            {
                return(SendEmailAsync("ExistingUser",
                                      texts.ExistingUserSubject,
                                      texts.ExistingUserBody,
                                      user, vars));
            }
            else
            {
                return(SendEmailAsync("NewUser",
                                      texts.NewUserSubject,
                                      texts.NewUserBody,
                                      user, vars));
            }
        }
        private static string Format(string text, TemplatesVars vars)
        {
            text = text.Replace("$APP_NAME", vars.AppName);

            if (vars.Assigner != null)
            {
                text = text.Replace("$ASSIGNER_EMAIL", vars.Assigner.Email);
                text = text.Replace("$ASSIGNER_NAME", vars.Assigner.DisplayName());
            }

            if (vars.User != null)
            {
                text = text.Replace("$USER_EMAIL", vars.User.Email);
                text = text.Replace("$USER_NAME", vars.User.DisplayName());
            }

            if (vars.ApiCallsLimit != null)
            {
                text = text.Replace("$API_CALLS_LIMIT", vars.ApiCallsLimit.ToString());
            }

            if (vars.ApiCalls != null)
            {
                text = text.Replace("$API_CALLS", vars.ApiCalls.ToString());
            }

            text = text.Replace("$UI_URL", vars.URL);

            return(text);
        }
        public Task SendUsageAsync(IUser user, string appName, long usage, long usageLimit)
        {
            Guard.NotNull(user);
            Guard.NotNull(appName);

            var vars = new TemplatesVars
            {
                ApiCalls      = usage,
                ApiCallsLimit = usageLimit,
                AppName       = appName
            };

            return(SendEmailAsync("Usage",
                                  texts.UsageSubject,
                                  texts.UsageBody,
                                  user, vars));
        }
        private async Task SendEmailAsync(string template, string emailSubj, string emailBody, IUser user, TemplatesVars vars)
        {
            if (string.IsNullOrWhiteSpace(emailBody))
            {
                LogWarning($"No email subject configured for {template}");
                return;
            }

            if (string.IsNullOrWhiteSpace(emailSubj))
            {
                LogWarning($"No email body configured for {template}");
                return;
            }

            vars.URL = urlGenerator.UI();

            vars.User = user;

            emailSubj = Format(emailSubj, vars);
            emailBody = Format(emailBody, vars);

            try
            {
                await emailSender.SendAsync(user.Email, emailSubj, emailBody);
            }
            catch (Exception ex)
            {
                log.LogError(ex, w => w
                             .WriteProperty("action", "SendNotification")
                             .WriteProperty("status", "Failed"));

                throw;
            }
        }