diff --git a/src/Ombi.Core/Engine/IRecentlyAddedEngine.cs b/src/Ombi.Core/Engine/IRecentlyAddedEngine.cs index c119abbb4..3087ec829 100644 --- a/src/Ombi.Core/Engine/IRecentlyAddedEngine.cs +++ b/src/Ombi.Core/Engine/IRecentlyAddedEngine.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Ombi.Core.Models; namespace Ombi.Core.Engine @@ -10,5 +11,6 @@ namespace Ombi.Core.Engine IEnumerable GetRecentlyAddedMovies(DateTime from, DateTime to); IEnumerable GetRecentlyAddedTv(DateTime from, DateTime to, bool groupBySeason); IEnumerable GetRecentlyAddedTv(bool groupBySeason); + Task UpdateRecentlyAddedDatabase(); } } \ No newline at end of file diff --git a/src/Ombi.Core/Engine/RecentlyAddedEngine.cs b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs index b8cc0ee6b..61866af9d 100644 --- a/src/Ombi.Core/Engine/RecentlyAddedEngine.cs +++ b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs @@ -1,27 +1,28 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Internal; using Ombi.Core.Models; using Ombi.Helpers; using Ombi.Store.Entities; using Ombi.Store.Repository; +using RecentlyAddedType = Ombi.Store.Entities.RecentlyAddedType; namespace Ombi.Core.Engine { public class RecentlyAddedEngine : IRecentlyAddedEngine { - public RecentlyAddedEngine(IPlexContentRepository plex, IEmbyContentRepository emby) + public RecentlyAddedEngine(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository recentlyAdded) { _plex = plex; _emby = emby; + _recentlyAddedLog = recentlyAdded; } private readonly IPlexContentRepository _plex; private readonly IEmbyContentRepository _emby; + private readonly IRepository _recentlyAddedLog; public IEnumerable GetRecentlyAddedMovies(DateTime from, DateTime to) { @@ -55,6 +56,67 @@ namespace Ombi.Core.Engine return GetRecentlyAddedTv(plexTv, embyTv, groupBySeason); } + public async Task UpdateRecentlyAddedDatabase() + { + var plexContent = _plex.GetAll().Include(x => x.Episodes); + var embyContent = _emby.GetAll().Include(x => x.Episodes); + var recentlyAddedLog = new HashSet(); + foreach (var p in plexContent) + { + if (p.Type == PlexMediaTypeEntity.Movie) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = p.Id + }); + } + else + { + // Add the episodes + foreach (var ep in p.Episodes) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = ep.Id + }); + } + } + } + + foreach (var e in embyContent) + { + if (e.Type == EmbyMediaType.Movie) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Emby, + ContentId = e.Id + }); + } + else + { + // Add the episodes + foreach (var ep in e.Episodes) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = ep.Id + }); + } + } + } + await _recentlyAddedLog.AddRange(recentlyAddedLog); + + return true; + } + private IEnumerable GetRecentlyAddedTv(IQueryable plexTv, IQueryable embyTv, bool groupBySeason) { diff --git a/src/Ombi.Core/Models/UI/NewsletterNotificationViewModel.cs b/src/Ombi.Core/Models/UI/NewsletterNotificationViewModel.cs new file mode 100644 index 000000000..a40044670 --- /dev/null +++ b/src/Ombi.Core/Models/UI/NewsletterNotificationViewModel.cs @@ -0,0 +1,23 @@ + +using System.Collections.Generic; +using Ombi.Settings.Settings.Models.Notifications; +using Ombi.Store.Entities; + +namespace Ombi.Core.Models.UI +{ + /// + /// The view model for the notification settings page + /// + /// + public class NewsletterNotificationViewModel : NewsletterSettings + { + /// + /// Gets or sets the notification templates. + /// + /// + /// The notification templates. + /// + public NotificationTemplates NotificationTemplate { get; set; } + + } +} diff --git a/src/Ombi.Helpers/OmbiRoles.cs b/src/Ombi.Helpers/OmbiRoles.cs index 1b88b5d67..e7527279d 100644 --- a/src/Ombi.Helpers/OmbiRoles.cs +++ b/src/Ombi.Helpers/OmbiRoles.cs @@ -9,5 +9,6 @@ public const string RequestTv = nameof(RequestTv); public const string RequestMovie = nameof(RequestMovie); public const string Disabled = nameof(Disabled); + public const string RecievesNewsletter = nameof(RecievesNewsletter); } } \ No newline at end of file diff --git a/src/Ombi.Notifications/NotificationMessageCurlys.cs b/src/Ombi.Notifications/NotificationMessageCurlys.cs index d958cfc74..9dd3cc486 100644 --- a/src/Ombi.Notifications/NotificationMessageCurlys.cs +++ b/src/Ombi.Notifications/NotificationMessageCurlys.cs @@ -38,6 +38,13 @@ namespace Ombi.Notifications AdditionalInformation = opts?.AdditionalInformation ?? string.Empty; } + public void SetupNewsletter(CustomizationSettings s, string username) + { + ApplicationUrl = (s?.ApplicationUrl.HasValue() ?? false) ? s.ApplicationUrl : string.Empty; + ApplicationName = string.IsNullOrEmpty(s?.ApplicationName) ? "Ombi" : s?.ApplicationName; + RequestedUser = username; + } + public void Setup(NotificationOptions opts, ChildRequests req, CustomizationSettings s) { LoadIssues(opts); diff --git a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs index 544bbc6dd..693c8ae7a 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Ombi.Api.TheMovieDb; using Ombi.Api.TheMovieDb.Models; @@ -22,7 +24,8 @@ namespace Ombi.Schedule.Jobs.Ombi { public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository addedLog, IMovieDbApi movieApi, ITvMazeApi tvApi, IEmailProvider email, ISettingsService custom, - ISettingsService emailSettings, INotificationTemplatesRepository templateRepo) + ISettingsService emailSettings, INotificationTemplatesRepository templateRepo, + UserManager um, ISettingsService newsletter) { _plex = plex; _emby = emby; @@ -33,6 +36,8 @@ namespace Ombi.Schedule.Jobs.Ombi _customizationSettings = custom; _templateRepo = templateRepo; _emailSettings = emailSettings; + _newsletterSettings = newsletter; + _userManager = um; } private readonly IPlexContentRepository _plex; @@ -44,9 +49,16 @@ namespace Ombi.Schedule.Jobs.Ombi private readonly ISettingsService _customizationSettings; private readonly INotificationTemplatesRepository _templateRepo; private readonly ISettingsService _emailSettings; + private readonly ISettingsService _newsletterSettings; + private readonly UserManager _userManager; public async Task Start() { + var newsletterSettings = await _newsletterSettings.GetSettingsAsync(); + if (!newsletterSettings.Enabled) + { + return; + } var template = await _templateRepo.GetTemplate(NotificationAgent.Email, NotificationType.Newsletter); if (!template.Enabled) { @@ -59,40 +71,146 @@ namespace Ombi.Schedule.Jobs.Ombi return; } + var customization = await _customizationSettings.GetSettingsAsync(); + // Get the Content var plexContent = _plex.GetAll().Include(x => x.Episodes); var embyContent = _emby.GetAll().Include(x => x.Episodes); var addedLog = _recentlyAddedLog.GetAll(); - var addedPlexLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex).Select(x => x.ContentId); - var addedEmbyLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby).Select(x => x.ContentId); + var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId); + var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId); + + var addedPlexEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode).Select(x => x.ContentId); + var addedEmbyEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode).Select(x => x.ContentId); // Filter out the ones that we haven't sent yet - var plexContentToSend = plexContent.Where(x => !addedPlexLogIds.Contains(x.Id)); - var embyContentToSend = embyContent.Where(x => !addedEmbyLogIds.Contains(x.Id)); + var plexContentMoviesToSend = plexContent.Where(x => !addedPlexMovieLogIds.Contains(x.Id)); + var embyContentMoviesToSend = embyContent.Where(x => !addedEmbyMoviesLogIds.Contains(x.Id)); + + var plexContentTvToSend = plexContent.Where(x => x.Episodes.Any(e => !addedPlexEpisodesLogIds.Contains(e.Id))); + var embyContentTvToSend = embyContent.Where(x => x.Episodes.Any(e => !addedEmbyEpisodesLogIds.Contains(e.Id))); + + var plexContentToSend = plexContentMoviesToSend.Union(plexContentTvToSend); + var embyContentToSend = embyContentMoviesToSend.Union(embyContentTvToSend); + var body = await BuildHtml(plexContentToSend, embyContentToSend); + // Get the users to send it to + var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter); + if (!users.Any()) + { + return; + } + var emailTasks = new List(); + foreach (var user in users) + { + if (user.Email.IsNullOrEmpty()) + { + continue; + } + + var html = LoadTemplate(body, template, customization, user.Alias); + + emailTasks.Add(_email.Send(new NotificationMessage { Message = html, Subject = template.Subject, To = user.Email }, emailSettings)); + } + + // Now add all of this to the Recently Added log + var recentlyAddedLog = new HashSet(); + foreach (var p in plexContentMoviesToSend) + { + if (p.Type == PlexMediaTypeEntity.Movie) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = p.Id + }); + } + else + { + // Add the episodes + foreach (var ep in p.Episodes) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = ep.Id + }); + } + } + } + + foreach (var e in embyContentMoviesToSend) + { + if (e.Type == EmbyMediaType.Movie) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Emby, + ContentId = e.Id + }); + } + else + { + // Add the episodes + foreach (var ep in e.Episodes) + { + recentlyAddedLog.Add(new RecentlyAddedLog + { + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentId = ep.Id + }); + } + } + } + await _recentlyAddedLog.AddRange(recentlyAddedLog); + + await Task.WhenAll(emailTasks.ToArray()); + } + + private string LoadTemplate(string body, NotificationTemplates template, CustomizationSettings settings, string username) + { var email = new NewsletterTemplate(); - var customization = await _customizationSettings.GetSettingsAsync(); + var resolver = new NotificationMessageResolver(); + var curlys = new NotificationMessageCurlys(); - var html = email.LoadTemplate(template.Subject, template.Message, body, customization.Logo); + curlys.SetupNewsletter(settings, username); - await _email.Send(new NotificationMessage {Message = html, Subject = template.Subject, To = "tidusjar@gmail.com"}, emailSettings); + var parsed = resolver.ParseMessage(template, curlys); + + var html = email.LoadTemplate(parsed.Subject, parsed.Message, body, settings.Logo); + + return html; } private async Task BuildHtml(IQueryable plexContentToSend, IQueryable embyContentToSend) { var sb = new StringBuilder(); - sb.Append("

New Movies:



"); - await ProcessPlexMovies(plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Movie), sb); - await ProcessEmbyMovies(embyContentToSend.Where(x => x.Type == EmbyMediaType.Movie), sb); + var plexMovies = plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Movie); + var embyMovies = embyContentToSend.Where(x => x.Type == EmbyMediaType.Movie); + if (plexMovies.Any() || embyMovies.Any()) + { + sb.Append("

New Movies:



"); + await ProcessPlexMovies(plexMovies, sb); + await ProcessEmbyMovies(embyMovies, sb); + } - sb.Append("

New Episodes:



"); - await ProcessPlexTv(plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Show), sb); - await ProcessEmbyMovies(embyContentToSend.Where(x => x.Type == EmbyMediaType.Series), sb); + var plexTv = plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Show); + var embyTv = embyContentToSend.Where(x => x.Type == EmbyMediaType.Series); + if (plexTv.Any() || embyTv.Any()) + { + sb.Append("

New Episodes:



"); + await ProcessPlexTv(plexTv, sb); + await ProcessEmbyMovies(embyTv, sb); + } return sb.ToString(); } @@ -330,7 +448,7 @@ namespace Ombi.Schedule.Jobs.Ombi sb.Append(""); sb.Append( ""); - + Href(sb, $"https://www.imdb.com/title/{info.externals.imdb}/"); Header(sb, 3, t.Title); EndTag(sb, "a"); @@ -426,6 +544,12 @@ namespace Ombi.Schedule.Jobs.Ombi { _plex?.Dispose(); _emby?.Dispose(); + _newsletterSettings?.Dispose(); + _customizationSettings?.Dispose(); + _emailSettings.Dispose(); + _recentlyAddedLog.Dispose(); + _templateRepo?.Dispose(); + _userManager?.Dispose(); } _disposed = true; } diff --git a/src/Ombi.Settings/Settings/Models/Notifications/NewsletterSettings.cs b/src/Ombi.Settings/Settings/Models/Notifications/NewsletterSettings.cs new file mode 100644 index 000000000..380e2d743 --- /dev/null +++ b/src/Ombi.Settings/Settings/Models/Notifications/NewsletterSettings.cs @@ -0,0 +1,7 @@ +namespace Ombi.Settings.Settings.Models.Notifications +{ + public class NewsletterSettings : Settings + { + public bool Enabled { get; set; } + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Entities/RecentlyAddedLog.cs b/src/Ombi.Store/Entities/RecentlyAddedLog.cs index 52b047993..ba26eb566 100644 --- a/src/Ombi.Store/Entities/RecentlyAddedLog.cs +++ b/src/Ombi.Store/Entities/RecentlyAddedLog.cs @@ -7,13 +7,20 @@ namespace Ombi.Store.Entities public class RecentlyAddedLog : Entity { public RecentlyAddedType Type { get; set; } + public ContentType ContentType { get; set; } public int ContentId { get; set; } // This is dependant on the type public DateTime AddedAt { get; set; } } public enum RecentlyAddedType { - Plex, - Emby + Plex = 0, + Emby = 1 + } + + public enum ContentType + { + Parent = 0, + Episode = 1 } } \ No newline at end of file diff --git a/src/Ombi.Store/Repository/INotificationTemplatesRepository.cs b/src/Ombi.Store/Repository/INotificationTemplatesRepository.cs index 861c6b9bd..2ef0e09cf 100644 --- a/src/Ombi.Store/Repository/INotificationTemplatesRepository.cs +++ b/src/Ombi.Store/Repository/INotificationTemplatesRepository.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Ombi.Helpers; @@ -6,7 +7,7 @@ using Ombi.Store.Entities; namespace Ombi.Store.Repository { - public interface INotificationTemplatesRepository + public interface INotificationTemplatesRepository : IDisposable { IQueryable All(); Task> GetAllTemplates(); diff --git a/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs b/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs index e4b484967..de892e934 100644 --- a/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs +++ b/src/Ombi.Store/Repository/NotificationTemplatesRepository.cs @@ -60,5 +60,26 @@ namespace Ombi.Store.Repository await Db.SaveChangesAsync().ConfigureAwait(false); return settings.Entity; } + + private bool _disposed; + // Protected implementation of Dispose pattern. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + Db?.Dispose(); + } + + _disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } } \ No newline at end of file diff --git a/src/Ombi/ClientApp/app/interfaces/INotificationSettings.ts b/src/Ombi/ClientApp/app/interfaces/INotificationSettings.ts index 4446f6340..67a2594d2 100644 --- a/src/Ombi/ClientApp/app/interfaces/INotificationSettings.ts +++ b/src/Ombi/ClientApp/app/interfaces/INotificationSettings.ts @@ -54,6 +54,10 @@ export interface IDiscordNotifcationSettings extends INotificationSettings { notificationTemplates: INotificationTemplates[]; } +export interface INewsletterNotificationSettings extends INotificationSettings { + notificationTemplate: INotificationTemplates; +} + export interface ITelegramNotifcationSettings extends INotificationSettings { botApi: string; chatId: string; diff --git a/src/Ombi/ClientApp/app/services/settings.service.ts b/src/Ombi/ClientApp/app/services/settings.service.ts index 54f3458d5..f6f770a19 100644 --- a/src/Ombi/ClientApp/app/services/settings.service.ts +++ b/src/Ombi/ClientApp/app/services/settings.service.ts @@ -20,6 +20,7 @@ import { ILandingPageSettings, IMattermostNotifcationSettings, IMobileNotifcationSettings, + INewsletterNotificationSettings, IOmbiSettings, IPlexSettings, IPushbulletNotificationSettings, @@ -265,4 +266,17 @@ export class SettingsService extends ServiceHelpers { return this.http .post(`${this.url}/issues`, JSON.stringify(settings), {headers: this.headers}); } + + public getNewsletterSettings(): Observable { + return this.http.get(`${this.url}/notifications/newsletter`, {headers: this.headers}); + } + + public updateNewsletterDatabase(): Observable { + return this.http.post(`${this.url}/notifications/newsletterdatabase`, {headers: this.headers}); + } + + public saveNewsletterSettings(settings: INewsletterNotificationSettings): Observable { + return this.http + .post(`${this.url}/notifications/newsletter`, JSON.stringify(settings), {headers: this.headers}); + } } diff --git a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html new file mode 100644 index 000000000..5f663dd20 --- /dev/null +++ b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.html @@ -0,0 +1,52 @@ + + +
+
+ Newsletter +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ + +
+
+
+
+ + +
+ +
+
+
\ No newline at end of file diff --git a/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts new file mode 100644 index 000000000..7742e6f4e --- /dev/null +++ b/src/Ombi/ClientApp/app/settings/notifications/newsletter.component.ts @@ -0,0 +1,71 @@ +import { Component, OnInit } from "@angular/core"; +import { FormBuilder, FormGroup } from "@angular/forms"; + +import { INewsletterNotificationSettings, INotificationTemplates, NotificationType } from "../../interfaces"; +import { TesterService } from "../../services"; +import { NotificationService } from "../../services"; +import { SettingsService } from "../../services"; + +@Component({ + templateUrl: "./newsletter.component.html", +}) +export class NewsletterComponent implements OnInit { + + public NotificationType = NotificationType; + public template: INotificationTemplates; + public form: FormGroup; + + constructor(private settingsService: SettingsService, + private notificationService: NotificationService, + private fb: FormBuilder, + private testerService: TesterService) { } + + public ngOnInit() { + this.settingsService.getNewsletterSettings().subscribe(x => { + this.template = x.notificationTemplate; + + this.form = this.fb.group({ + enabled: [x.enabled], + }); + }); + } + + public updateDatabase() { + this.settingsService.updateNewsletterDatabase().subscribe(); + } + + public onSubmit(form: FormGroup) { + if (form.invalid) { + this.notificationService.error("Please check your entered values"); + return; + } + + const settings = form.value; + settings.notificationTemplate = this.template; + + this.settingsService.saveNewsletterSettings(settings).subscribe(x => { + if (x) { + this.notificationService.success("Successfully saved the Newsletter settings"); + } else { + this.notificationService.error("There was an error when saving the Newsletter settings"); + } + }); + + } + + public test(form: FormGroup) { + if (form.invalid) { + this.notificationService.error("Please check your entered values"); + return; + } + + this.testerService.discordTest(form.value).subscribe(x => { + if (x) { + this.notificationService.success("Successfully sent a Discord message, please check the discord channel"); + } else { + this.notificationService.error("There was an error when sending the Discord message. Please check your settings"); + } + }); + + } +} diff --git a/src/Ombi/Controllers/SettingsController.cs b/src/Ombi/Controllers/SettingsController.cs index c0148c1bb..7970252a3 100644 --- a/src/Ombi/Controllers/SettingsController.cs +++ b/src/Ombi/Controllers/SettingsController.cs @@ -32,6 +32,7 @@ using Ombi.Settings.Settings.Models.Notifications; using Ombi.Store.Entities; using Ombi.Store.Repository; using Ombi.Api.Github; +using Ombi.Core.Engine; namespace Ombi.Controllers { @@ -60,7 +61,8 @@ namespace Ombi.Controllers IEmbyApi embyApi, IRadarrSync radarrSync, ICacheService memCache, - IGithubApi githubApi) + IGithubApi githubApi, + IRecentlyAddedEngine engine) { SettingsResolver = resolver; Mapper = mapper; @@ -78,6 +80,7 @@ namespace Ombi.Controllers private readonly IRadarrSync _radarrSync; private readonly ICacheService _cache; private readonly IGithubApi _githubApi; + private readonly IRecentlyAddedEngine _recentlyAdded; /// /// Gets the Ombi settings. @@ -865,13 +868,53 @@ namespace Ombi.Controllers return model; } + /// + /// Saves the Newsletter notification settings. + /// + /// The model. + /// + [HttpPost("notifications/newsletter")] + public async Task NewsletterSettings([FromBody] NewsletterNotificationViewModel model) + { + // Save the email settings + var settings = Mapper.Map(model); + var result = await Save(settings); + + // Save the templates + await TemplateRepository.Update(model.NotificationTemplate); + + return result; + } + + [ApiExplorerSettings(IgnoreApi = true)] + [HttpPost("notifications/newsletterdatabase")] + public async Task UpdateNewsletterDatabase() + { + return await _recentlyAdded.UpdateRecentlyAddedDatabase(); + } + + /// + /// Gets the Newsletter Notification Settings. + /// + /// + [HttpGet("notifications/newsletter")] + public async Task NewsletterSettings() + { + var settings = await Get(); + var model = Mapper.Map(settings); + + // Lookup to see if we have any templates saved + var templates = await BuildTemplates(NotificationAgent.Email); + model.NotificationTemplate = templates.FirstOrDefault(x => x.NotificationType == NotificationType.Newsletter); + return model; + } + private async Task> BuildTemplates(NotificationAgent agent) { var templates = await TemplateRepository.GetAllTemplates(agent); return templates.OrderBy(x => x.NotificationType.ToString()).ToList(); } - private async Task Get() { var settings = SettingsResolver.Resolve();