Exemplo n.º 1
0
        public async Task SendBlaBlaCarNotification(BlaBlaCarMonitoring blaBlaCarMonitoring, string email)
        {
            var to   = new MailAddress(email);
            var body = await renderer.RenderViewToStringAsync("Templates/BlaBlaCarNotificationTemplate.cshtml", blaBlaCarMonitoring);

            var message = new MailMessage(from, to)
            {
                Subject    = $"{blaBlaCarMonitoring.From} - {blaBlaCarMonitoring.To}. Поїздки знайдено!",
                Body       = body,
                IsBodyHtml = true
            };

            SendEmail(message);
        }
Exemplo n.º 2
0
        public async Task FindTrips(BlaBlaCarMonitoring monitoring)
        {
            var monitoringResult =
                dataContext.BlaBlaCarMonitoring.Include(m => m.Trips).FirstOrDefault(m => m.Id == monitoring.Id);

            if (monitoringResult == null)
            {
                RecurringJob.RemoveIfExists(monitoring.Guid);
                return;
            }

            if (monitoringResult.DepartureDate <= DateTime.Now)
            {
                RecurringJob.RemoveIfExists(monitoring.Guid);
                monitoringResult.IsSuccessful             = false;
                monitoringResult.IsInProcess              = false;
                dataContext.Entry(monitoringResult).State = EntityState.Modified;
                await dataContext.SaveChangesAsync();

                return;
            }

            var trips = (await tripFinder.FindTripsAsync(monitoringResult.From, monitoringResult.To,
                                                         monitoringResult.DepartureDate)).ToList().ConvertAll(t => (Trip)t)
                        .FindAll(t => t.SeatsLeft >= monitoring.MinPlaces);

            if (trips.Any())
            {
                monitoringResult.IsSuccessful             = true;
                monitoringResult.IsInProcess              = false;
                monitoringResult.Trips                    = trips;
                dataContext.Entry(monitoringResult).State = EntityState.Modified;
                await dataContext.SaveChangesAsync();

                var user = await dataContext.Users.FindAsync(monitoringResult.UserId);

                if (user.EmailNotificationEnabled)
                {
                    smtpService.SendBlaBlaCarNotification(monitoringResult, user.Email);
                }

                if (user.SmsNotificationEnabled)
                {
                    smsService.SendBlaBlaCarNotification(monitoringResult, user.PhoneNumber);
                }

                RecurringJob.RemoveIfExists(monitoring.Guid);
            }
        }
Exemplo n.º 3
0
        public void SendBlaBlaCarNotification(BlaBlaCarMonitoring blaBlaCarMonitoring, string phoneNumber)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine($"{blaBlaCarMonitoring.From} - {blaBlaCarMonitoring.To}. Поїздки знайдено!");
            stringBuilder.AppendLine("BlaBlaCar");
            foreach (var blaBlaCarMonitoringTrip in blaBlaCarMonitoring.Trips)
            {
                stringBuilder.AppendLine(
                    $"{blaBlaCarMonitoringTrip.DeparturePlace.CityName} - {blaBlaCarMonitoringTrip.ArrivalPlace.CityName}: {blaBlaCarMonitoringTrip.DepartureDate}");
            }
            MessageResource.Create(
                from: new PhoneNumber(smsConfig.Number),
                to: new PhoneNumber(phoneNumber),
                body: stringBuilder.ToString(),
                client: twilioRestClient);
        }
Exemplo n.º 4
0
        public async Task StartMonitoring(string from, string to, DateTime departureDate, int minPlaces, string userId)
        {
            var user = await dataContext.Users.Include(u => u.BlaBlaCarMonitoring)
                       .FirstOrDefaultAsync(u => u.Id == userId);

            var monitoring = new BlaBlaCarMonitoring
            {
                Guid          = Guid.NewGuid().ToString(),
                DepartureDate = departureDate,
                From          = from,
                To            = to,
                MinPlaces     = minPlaces,
                IsInProcess   = true
            };

            user.BlaBlaCarMonitoring.Add(monitoring);
            dataContext.Entry(user).State = EntityState.Modified;
            await dataContext.SaveChangesAsync();

            RecurringJob.AddOrUpdate <BlaBlaCarJob>(monitoring.Guid, j => j.FindTrips(monitoring),
                                                    hangFireConfig.MonitoringCron);
        }