using Data; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.CodeAnalysis; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.VisualStudio.TextTemplating; using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages.Manage; using Model; using Newtonsoft.Json.Linq; using OpenAI_API; using Services.EmailSend; using Services.Implemnetation; using Services.Interaces; using Web.AIConfiguration; using Web.ViewModel.NewsLetterVM; namespace Web.Areas.Admin.Controllers { public class NewslettersController : Controller { private readonly INewsLetterRepository _repository; private readonly SurveyContext _context; private readonly IEmailServices _emailServices; private readonly IConfiguration _configuration; private readonly IMemoryCache _memoryCache; public NewslettersController(INewsLetterRepository repository,SurveyContext context,IEmailServices emailServices,IConfiguration configuration) { _repository = repository; _context = context; _emailServices = emailServices; _configuration = configuration; } public IActionResult Index() { var totalSubscribedUsers = _context.Subscriptions.Count(s => s.IsSubscribed); // Pass the total count to the view ViewBag.TotalSubscribedUsers = totalSubscribedUsers; var newsLetterFromdb = _repository.GetAll(); var viewmodel = new List(); foreach (var item in newsLetterFromdb) { viewmodel.Add(new NewsLetterViewModel { Id=item.Id, Name=item.Name, Email=item.Email, IsSubscribed=item.IsSubscribed }); } return View(viewmodel); } public IActionResult Create() { var totalSubscribedUsers = _context.Subscriptions.Count(s => s.IsSubscribed); // Pass the total count to the view ViewBag.TotalSubscribedUsers = totalSubscribedUsers; return View(); } [HttpPost] public async Task Create(SendNewsLetterViewModel viewModel) { if (ModelState.IsValid) { try { // Retrieve all subscribed users var subscribedUsers = await _context.Subscriptions.Where(s => s.IsSubscribed).ToListAsync(); string confirmationPath = _configuration["Email:unsubscribePath"]; // Send the newsletter email to each subscribed user foreach (var user in subscribedUsers) { string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={user.Email}"; string emailBody = $@" Email Confirmation

Hey {user.Name},

{viewModel.Body}


Søren Eggert Lundsteen Olsen
SeoSoft ApS

Hovedgaden 3
Jordrup
Kolding 6064
Denmark
Unsubscribe
"; var email = new EmailToSend(user.Email, viewModel.Subject, emailBody); var isSent = await _emailServices.SendConfirmationEmailAsync(email); // Create a record for the sent email var sentEmail = new SentNewsletterEamil { RecipientEmail = user.Email, Subject = viewModel.Subject, Body = emailBody, SentDate = DateTime.UtcNow, IsSent = isSent // Assuming isSent returns a boolean indicating success }; _context.SentNewsletterEamils.Add(sentEmail); // Handle failure to send email if needed } await _context.SaveChangesAsync(); // Save changes for all sent emails TempData["success"] = "Newsletter sent successfully."; return RedirectToAction(nameof(Index)); } catch (Exception ex) { // Log or handle the exception as needed TempData["error"] = "Something went wrong: " + ex.Message; return RedirectToAction(nameof(Index)); } } return View(viewModel); } [HttpPost] public async Task MailjetWebhook() { using (var reader = new StreamReader(Request.Body)) { var requestBody = await reader.ReadToEndAsync(); Console.WriteLine("Received payload: " + requestBody); Request.Body.Position = 0; try { var events = JArray.Parse(requestBody); if (events == null) { return BadRequest("Parsed data is null"); } foreach (JObject e in events) { string email = e.Value("email"); string eventType = e.Value("event"); Console.WriteLine($"Processing {eventType} for {email}"); var newsletterEmail = await _context.SentNewsletterEamils .FirstOrDefaultAsync(n => n.RecipientEmail == email); if (newsletterEmail == null) { Console.WriteLine("No newsletter email record found for email: " + email); continue; } switch (eventType) { case "sent": newsletterEmail.IsDelivered = true; break; case "open": newsletterEmail.IsOpened = true; break; case "click": newsletterEmail.IsClicked = true; break; case "bounce": newsletterEmail.IsBounced = true; break; case "spam": newsletterEmail.IsSpam = true; break; case "unsub": newsletterEmail.IsUnsubscribed = true; break; case "blocked": newsletterEmail.IsBlocked = true; break; default: Console.WriteLine($"Unhandled event type: {eventType}"); break; } _context.Entry(newsletterEmail).State = EntityState.Modified; } Console.WriteLine("Email got updated "); await _context.SaveChangesAsync(); } catch (Exception ex) { Console.WriteLine("Exception parsing JSON: " + ex.Message); return BadRequest("Error parsing JSON: " + ex.Message); } } return Ok(); } public async Task EmailStats() { var emails = await _context.SentNewsletterEamils.ToListAsync(); return View(emails); } public async Task GetEmailStatsData() { var model = await _context.SentNewsletterEamils.ToListAsync(); return Json(model); // Returns the list of emails as JSON. } public async Task GetChartData() { var emails = await _context.SentNewsletterEamils.ToListAsync(); var data = new { Sent = emails.Count(e => e.IsSent), Delivered = emails.Count(e => e.IsDelivered), Opened = emails.Count(e => e.IsOpened), Clicked = emails.Count(e => e.IsClicked), Bounced = emails.Count(e => e.IsBounced), Spam = emails.Count(e => e.IsSpam), Blocked = emails.Count(e => e.IsBlocked), Unsubscribed = emails.Count(e => e.IsUnsubscribed) }; return Json(data); } //[HttpPost] //public async Task Create(SendNewsLetterViewModel viewModel) //{ // if(ModelState.IsValid) // { // try // { // // Retrieve all subscribed users // var subscribedUsers = await _context.Subscriptions.Where(s => s.IsSubscribed).ToListAsync(); // string confirmationPath = _configuration["Email:unsubscribePath"]; // // Send the newsletter email to each subscribed user // foreach (var user in subscribedUsers) // { // string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={user.Email}"; // string emailBody = $@" // // // Email Confirmation // // // //
//

Hey {user.Name},

//

{viewModel.Body}


//
Søren Eggert Lundsteen Olsen
//
SeoSoft ApS
//
//
Hovedgaden 3
Jordrup
Kolding 6064
Denmark
//
// Unsubscribe //
//
// "; // var email = new EmailToSend(user.Email, viewModel.Subject, emailBody); // var isSent = await _emailServices.SendConfirmationEmailAsync(email); // // Handle failure to send email if needed // } // TempData["success"] = "Nesletter sent successfully."; // return RedirectToAction(nameof(Index)); // } // catch (Exception) // { // // Log or handle the exception as needed // TempData["success"] = "something went wrong."; // return RedirectToAction(nameof(Index)); // } // } // return View(viewModel); //} [HttpGet] public IActionResult Delete(int id) { var newsLetterFromDb = _repository.GetById(id); var viewmodel = new NewsLetterViewModel { Id=newsLetterFromDb.Id, Email=newsLetterFromDb.Email, Name=newsLetterFromDb.Name, IsSubscribed=newsLetterFromDb.IsSubscribed }; return View(viewmodel); } [HttpPost] [ActionName("Delete")] public IActionResult DeleteConfirm(int id) { _repository.Delete(id); _context.SaveChanges(); TempData["Success"] = "Subscriber deleted successfully"; return RedirectToAction(nameof(Index)); } } }