public void AnnounceNextMatch_Test(string cultureName, bool venueIsSet) { var m = new AnnounceNextMatchModel { Fixture = new PlannedMatchRow { Id = 1000, PlannedStart = new DateTime(2021, 05, 05, 19, 00, 00), HomeTeamNameForRound = "HomeTeam", GuestTeamNameForRound = "GuestTeam" }, Venue = new TournamentManager.DAL.EntityClasses.VenueEntity { Name = "Venue Name", Extension = "Venue Ext.", Street = "Venue Street", PostalCode = "PostalCode", City = "Venue City" } }; m.IcsCalendarUrl = $"https://ics-calendar-url/{m.Fixture.Id}"; if (!venueIsSet) { m.Venue = null; } string text = string.Empty, html = string.Empty; Assert.Multiple(() => { Assert.DoesNotThrowAsync(async() => { text = await _renderer.RenderAsync(TemplateName.AnnounceNextMatchTxt, m, cultureName); Console.WriteLine($"*** {TemplateName.AnnounceNextMatchTxt} ***"); Console.WriteLine(text); }); Assert.That(text.Contains(cultureName == "en" ? "Hello" : "Hallo")); Assert.That(text.Contains(m.IcsCalendarUrl)); }); }
public async IAsyncEnumerable <MailMergeMessage> GetMailMergeMessages(ITenantContext tenantContext, ITemplateRenderer renderer, IMailMergeService mailMergeService, IStringLocalizer <EmailResource> localizer, [EnumeratorCancellation] CancellationToken cancellationToken) { var fixtures = await tenantContext.DbContext.AppDb.MatchRepository.GetPlannedMatchesAsync( new PredicateExpression(PlannedMatchFields.PlannedStart .Between(Parameters.ReferenceDateUtc, Parameters.ReferenceDateUtc.AddDays(1).AddSeconds(-1)) .And(PlannedMatchFields.PlannedStart.IsNotNull() .And(PlannedMatchFields.PlannedEnd.IsNotNull().And(PlannedMatchFields.TournamentId == tenantContext.TournamentContext .MatchPlanTournamentId)))), cancellationToken); if (!fixtures.Any()) { yield break; } var teamIds = new HashSet <long>(); fixtures.ForEach(f => { teamIds.Add(f.HomeTeamId); teamIds.Add(f.GuestTeamId); }); var teamUserRoundInfos = await tenantContext.DbContext.AppDb.TeamRepository.GetTeamUserRoundInfosAsync( new PredicateExpression(TeamUserRoundFields.TeamId.In(teamIds).And(TeamUserRoundFields.TournamentId == tenantContext.TournamentContext.MatchPlanTournamentId)), cancellationToken); foreach (var fixture in fixtures) { var model = new AnnounceNextMatchModel { IcsCalendarUrl = Parameters.IcsCalendarBaseUrl + "?id=" + fixture.Id, Fixture = fixture, Venue = (await tenantContext.DbContext.AppDb.VenueRepository.GetVenuesAsync(new PredicateExpression(VenueFields.Id == fixture.VenueId), cancellationToken)).FirstOrDefault() }; var recipientGroups = new[] { teamUserRoundInfos.Where(tur => tur.TeamId == fixture.HomeTeamId), // home team users teamUserRoundInfos.Where(tur => tur.TeamId == fixture.GuestTeamId) // guest team users }; var plainTextContent = await renderer.RenderAsync( Templates.Email.TemplateName.AnnounceNextMatchTxt, model, Parameters.CultureInfo.TwoLetterISOLanguageName); // each recipient group will get the same email text foreach (var recipients in recipientGroups) { var mailMergeMessage = mailMergeService.CreateStandardMessage(); mailMergeMessage.EnableFormatter = false; mailMergeMessage.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.From, tenantContext.SiteContext.Email.GeneralFrom.DisplayName, tenantContext.SiteContext.Email.GeneralFrom.Address)); foreach (var tur in recipients) { if (string.IsNullOrWhiteSpace(mailMergeMessage.Subject)) { using (new CultureSwitcher(Parameters.CultureInfo, Parameters.CultureInfo)) { mailMergeMessage.Subject = localizer["Next match day for team '{0}'", tur.TeamNameForRound].Value; } } mailMergeMessage.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, $"{tur.CompleteName}", tur.Email)); if (!string.IsNullOrEmpty(tur.Email2)) { mailMergeMessage.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.CC, $"{tur.CompleteName}", tur.Email2)); } } // Send registration info also to league administration mailMergeMessage.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.Bcc, tenantContext.SiteContext.Email.GeneralBcc.DisplayName, tenantContext.SiteContext.Email.GeneralBcc.Address)); mailMergeMessage.PlainText = plainTextContent; yield return(mailMergeMessage); } } }