Ejemplo n.º 1
0
        public void NotifyUserForStock()
        {
            var items = _stockNotificationRepository.TableNoTracking
                        .Join(_productPriceRepository.Table, s => s.ProductId, pp => pp.ProductId, (s, pp) => new { s, pp })
                        .Join(_productRepository.Table, x => x.s.ProductId, p => p.Id, (x, p) => new { x.s, p, x.pp })
                        .Where(x => x.s.Notified == false)
                        .Where(x => x.p.Enabled == true)
                        .Where(x => x.pp.Stock > 0)
                        .Where(x => x.pp.Enabled == true)
                        .Select(x => new { x.s, x.p })
                        .ToList()
                        .Select(x => new { x.s, p = _productBuilder.Build(x.p) })
                        .ToList();

            foreach (var item in items)
            {
                var option  = string.Empty;
                var proceed = false;

                if (item.s.ProductPriceId > 0 && item.p.ProductPrices.Where(x => x.Id == item.s.ProductPriceId).Any())
                {
                    option  = item.p.ProductPrices.Where(x => x.Id == item.s.ProductPriceId).Select(x => x.Option).FirstOrDefault();
                    proceed = true;
                }
                else if (item.s.ProductPriceId == 0)
                {
                    proceed = true;
                }

                if (proceed)
                {
                    item.s.Notified      = true;
                    item.s.UpdatedOnDate = DateTime.Now;
                    _emailManager.SendBackInStockEmail(item.s.Email, item.p.Name, item.p.UrlRewrite, option, item.p.ProductMedias[0].MediaFilename);

                    _stockNotificationRepository.Update(item.s);
                }
            }
        }