public async Task <MonitoredProduct> CreateProduct(Uri url) { var handler = this._handlerFactory.CreateHandler(url); var cleanUrl = handler.HandleCleanUrl(url); var monitoredProduct = await this._productRepository.GetByUrlAsync(cleanUrl.AbsoluteUri); if (monitoredProduct == null) { var siteInfo = await handler.HandleGetInfo(cleanUrl); if (siteInfo == null) { throw new ParseException("PriceWise was unable to get the product information.", cleanUrl); } monitoredProduct = new MonitoredProduct { ProductIdentifier = siteInfo.ProductIdentifier, Uri = siteInfo.Uri, Title = siteInfo.Title, ImageUrl = siteInfo.ImageUrl }; monitoredProduct.PriceHistory.Add(new PriceChange { Price = siteInfo.Price, ModifiedAt = DateTime.UtcNow }); monitoredProduct = await this._productRepository.InsertAsync(monitoredProduct); } return(monitoredProduct); }
public IActionResult Create(string productId) { if (string.IsNullOrWhiteSpace(productId)) { return(BadRequest()); } var user = getCurrentUser(); if (user.MonitoredProducts != null && user.MonitoredProducts.Any(x => x.ProductId.Equals(productId, StringComparison.InvariantCultureIgnoreCase))) { return(Content("Already subscribed")); } MonitoredProduct p = new MonitoredProduct { ProductId = productId, NotificationSettings = new NotificationSettings { Availability = true, PriceChanging = true } }; user.MonitoredProducts.Add(p); UserRepository.Update(user); return(Content("Done")); }
public async Task <MonitoredProduct> CreateUpdatedProduct(Uri url) { var handler = this._handlerFactory.CreateHandler(url); var cleanUrl = handler.HandleCleanUrl(url); var siteInfo = await handler.HandleGetInfo(cleanUrl); if (siteInfo == null) { throw new ParseException("PriceWise was unable to get the product information.", cleanUrl); } var monitoredProduct = await this._productRepository.GetByUrlAsync(cleanUrl.AbsoluteUri); if (monitoredProduct == null) { monitoredProduct = new MonitoredProduct { ProductIdentifier = siteInfo.ProductIdentifier, Uri = siteInfo.Uri, Title = siteInfo.Title, ImageUrl = siteInfo.ImageUrl }; monitoredProduct.PriceHistory.Add(new PriceChange { Price = siteInfo.Price, ModifiedAt = DateTime.UtcNow }); monitoredProduct = await this._productRepository.InsertAsync(monitoredProduct); } else { var lastEntry = monitoredProduct.PriceHistory.LastOf(x => x.ModifiedAt); var newPrice = siteInfo.Price; var lastPrice = lastEntry.Price; var currentDate = DateTime.UtcNow.Date; var lastDate = lastEntry.ModifiedAt.Date; if (newPrice != lastPrice || currentDate != lastDate) { monitoredProduct.PriceHistory.Add(new PriceChange { Price = siteInfo.Price, ModifiedAt = DateTime.UtcNow }); monitoredProduct = await this._productRepository.UpdateAsync(monitoredProduct.Id, monitoredProduct); } } return(monitoredProduct); }
// [HttpPost] // [LoggingDescription("*** REQUEST to find products ***")] // public virtual async Task<IActionResult> FindProductsByIdentifier([FromBody] string[] productIdentifiers) // { // // Do not search for an empty identifier // if (!productIdentifiers.Any()) // { // return this.NoContent(); // } // var lockObject = new object(); // try // { // var knownProducts = new List<MonitoredProduct>(); // var newProducts = new List<MonitoredProduct>(); // foreach (var productIdentifier in productIdentifiers) // { // // Get all products with the product identifier from the database // var productIdentifierProducts = (await this._productRepository.GetAllByProductIdentifierAsync(productIdentifier)).ToList(); // var knownProductsSources = productIdentifierProducts.Select(x => new Uri(x.Uri).Authority).ToList(); // knownProducts.AddRange(productIdentifierProducts); // await Task.WhenAll(this._handlers.Select(async handler => // { // if (!knownProductsSources.Contains(handler.Domain.Authority)) // { // var newProductsUrls = await handler.HandleSearch(productIdentifier); // foreach (var url in newProductsUrls) // { // try // { // var newProduct = await this._productFactory.CreateUpdatedProduct(url); // lock (lockObject) // { // newProducts.Add(newProduct); // } // } // catch (Exception) // { // // ignored // } // } // } // })); // } // var allProducts = knownProducts.Concat(newProducts).DistinctBy(x => x.Uri).Select(this.CreateProductInfo); // return this.Ok(allProducts); // } // catch (ParseException e) // { // return this.BadRequest(e.Message); // } // } private ProductInfo CreateProductInfo(MonitoredProduct product) { var originalUrl = new Uri(product.Uri); var handler = this._handlerFactory.CreateHandler(originalUrl); return(new ProductInfo { OriginalUrl = originalUrl.AbsoluteUri, ProductUrl = handler.HandleManipulateUrl(originalUrl).AbsoluteUri, Title = product.Title, Price = product.PriceHistory.LastOf(y => y.ModifiedAt).Price, LastUpdate = product.PriceHistory.LastOf(y => y.ModifiedAt).ModifiedAt, ImageUrl = product.ImageUrl, ProductIdentifier = product.ProductIdentifier }); }
public async Task SendWatchedListProductEmail(string correspondenceLanguage, decimal oldPrice, decimal newPrice) { var user = new User { Email = "*****@*****.**", FirstName = "Maxime", Settings = new Settings { CorrespondenceLanguage = correspondenceLanguage } }; var listUser = new User { FirstName = "Toto" }; var list = new List { Name = "Legos", Id = "9876" }; var alert = new UserAlert { Id = "123456", Title = "Test Alert", ImageUrl = "https://pricewi.se/images/pricewise-logo.png", BestCurrentDeal = new Deal { ProductId = "Product1", Price = oldPrice, ModifiedAt = DateTime.Today.AddDays(-1) } }; var newBestDealProduct = new MonitoredProduct { Uri = "https://www.amazon.ca/dp/B073W77D5B/" }; var job = this._container.Resolve <AlertUsersJob>(); var emailInformation = job.BuildWatchedListProductEmail(user, list, listUser, alert, newBestDealProduct, newPrice); Console.WriteLine($"Sending email {emailInformation.TemplateName} to user {user.FirstName} for alert {alert.Title}"); var sendEmailTask = await this._emailSender.SendEmail(emailInformation); Assert.NotEmpty(sendEmailTask.MessageID); Assert.NotEmpty(sendEmailTask.TransactionID); }
internal EmailInformation BuildPriceChangeEmail(User user, UserAlert alert, MonitoredProduct newBestDealProduct, decimal newBestDealPrice) { var emailInformation = new EmailInformation(); emailInformation.RecipientAddress = user.Email; var productUrl = new Uri(newBestDealProduct.Uri); var manipulatedUrl = this.GetManipulatedUrl(productUrl); var productTitle = GetProductTitle(user, alert); emailInformation.Parameters = new Dictionary <string, string> { { "subject", user.Settings.CorrespondenceLanguage == "en" ? $"Price alert from PriceWise: The price of {productTitle} changed!" : $"Alerte de prix de PriceWise: Le prix de {productTitle} a changé!" }, { "merge_email", user.Email }, { "merge_firstname", user.FirstName ?? string.Empty }, { "merge_productName", alert.Title ?? string.Empty }, { "merge_previousPrice", alert.BestCurrentDeal.Price.ToString(CultureInfo.InvariantCulture) }, { "merge_newPrice", newBestDealPrice.ToString(CultureInfo.InvariantCulture) }, { "merge_alertId", alert.Id }, { "merge_productUrl", manipulatedUrl.AbsoluteUri }, { "merge_productDomain", productUrl.Authority }, { "merge_imageUrl", alert.ImageUrl.IsBase64Url() ? string.Empty : alert.ImageUrl } }; var changeQualifier = "Drop"; if (alert.BestCurrentDeal.Price < newBestDealPrice) { changeQualifier = "Raise"; } emailInformation.TemplateName = $"Price{changeQualifier}_Unsubscribe_{user.Settings.CorrespondenceLanguage}"; return(emailInformation); }