Beispiel #1
0
        public async Task CompleteJobAsyncWorksCorrectly(ServiceFrequency serviceFrequency, int expectedJobsCount)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var id  = Guid.NewGuid().ToString();
            var job = new Job()
            {
                Id = id,
            };

            dbContext.Add(job);
            await dbContext.SaveChangesAsync();

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(d => d.GetUtcNow()).Returns(new DateTime(2020, 5, 4, 12, 30, 0));
            var repository = new EfDeletableEntityRepository <Job>(dbContext);
            var service    = new JobsService.JobsService(repository, dateTimeProviderMock.Object);

            await service.CompleteJobAsync(id, Guid.NewGuid().ToString(), serviceFrequency, new DateTime(2020, 5, 4, 10, 0, 0), new DateTime(2020, 5, 4, 12, 0, 0));

            var result = dbContext.Jobs.Count();

            Assert.Equal(expectedJobsCount, result);
        }
Beispiel #2
0
        public async Task CompleteJobAsync(string id, string orderId, ServiceFrequency serviceFrequency, DateTime appointmentStartDate, DateTime appointmentEndDate)
        {
            var job = this.jobsRepository.All()
                      .FirstOrDefault(j => j.Id == id);

            job.JobStatus = JobStatus.Done;
            this.jobsRepository.Update(job);
            await this.jobsRepository.SaveChangesAsync();

            if (serviceFrequency != ServiceFrequency.Once)
            {
                await this.CreateAsync(orderId, serviceFrequency, appointmentStartDate, appointmentEndDate);
            }
        }
Beispiel #3
0
        private DateTime GetNextJobStartDate(ServiceFrequency serviceFrequency, DateTime appointmentStartDate)
        {
            var todaysDate = this.dateTimeProvider.GetUtcNow().Date;

            var startDate = serviceFrequency switch
            {
                ServiceFrequency.Once => appointmentStartDate,
                ServiceFrequency.Daily => todaysDate < appointmentStartDate.Date
                    ? appointmentStartDate
                    : todaysDate.AddDays(todaysDate.DayOfWeek == DayOfWeek.Friday ? 3 : 1).AddHours(appointmentStartDate.TimeOfDay.Hours),
                ServiceFrequency.Weekly => todaysDate.Date < appointmentStartDate.Date
                    ? appointmentStartDate
                    : todaysDate.AddDays(7).AddHours(appointmentStartDate.TimeOfDay.Hours),
                ServiceFrequency.Monthly => this.GetCurrentMothJobsDate(appointmentStartDate, todaysDate),
                _ => default,
Beispiel #4
0
        public async Task CreateAsync(string orderId, ServiceFrequency serviceFrequency, DateTime appointmentStartDate, DateTime appointmetnEndDate)
        {
            var startDate = this.GetNextJobStartDate(serviceFrequency, appointmentStartDate);

            var job = new Job()
            {
                OrderId    = orderId,
                StartDate  = startDate,
                FinishDate = startDate.Date.AddHours(appointmetnEndDate.TimeOfDay.Hours),
                JobStatus  = JobStatus.InProgress,
            };

            await this.jobsRepository.AddAsync(job);

            await this.jobsRepository.SaveChangesAsync();
        }
Beispiel #5
0
        public async Task CreateAsyncWorksCorrectly(DateTime currentDate, ServiceFrequency serviceFrequency, DateTime appointmentStartDate, DateTime appointmentEndDate, DateTime expectedStartDate, DateTime expectedFinishDate)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            dateTimeProviderMock.Setup(d => d.GetUtcNow()).Returns(currentDate);

            var repository = new EfDeletableEntityRepository <Job>(dbContext);

            var service = new JobsService.JobsService(repository, dateTimeProviderMock.Object);
            await service.CreateAsync(Guid.NewGuid().ToString(), serviceFrequency, appointmentStartDate, appointmentEndDate);

            var result = dbContext.Jobs.FirstOrDefault();

            Assert.Equal(expectedStartDate, result.StartDate);
            Assert.Equal(expectedFinishDate, result.FinishDate);
        }
Beispiel #6
0
        public async Task CreateAsync(string orderId, string clientId, int serviceId, ServiceFrequency serviceFrequency, int addressId, int jobsCount)
        {
            var servicePrice = this.servicesService.GetServicePrice(serviceId);

            var discount  = serviceFrequency != ServiceFrequency.Once ? GlobalConstants.DiscountForRecurrentService : 0;
            var netAmount = (jobsCount * servicePrice) * (1 - discount);

            var invoice = new Invoice()
            {
                AddressId             = addressId,
                ClientId              = clientId,
                Discount              = discount,
                NetAmount             = netAmount,
                TotalAmount           = netAmount * (1 + GlobalConstants.VAT),
                OrderId               = orderId,
                Status                = InvoiceStatus.Pending,
                UseDiscountPercentage = true,
            };

            await this.invoicesRepository.AddAsync(invoice);

            await this.invoicesRepository.SaveChangesAsync();
        }
Beispiel #7
0
        public IEnumerable <T> GetFreeTeams <T>(DateTime startDate, DateTime endDate, int cityId, int serviceId, ServiceFrequency serviceFrequency)
        {
            var weekOfMonth = startDate.GetWeekOfMonth();
            var dayOfWeek   = (int)startDate.DayOfWeek;

            Expression <Func <Team, bool> > checkSingleOrders = t =>
                                                                !t.Orders
                                                                .Where(o => o.ServiceFrequency == ServiceFrequency.Once &&
                                                                       (o.Status == OrderStatus.InProgress || o.Status == OrderStatus.Pending))
                                                                .Select(o => o.Appointment)
                                                                .Any(a => (startDate >= a.StartDate && startDate <= a.EndDate) ||
                                                                     (startDate < a.StartDate && endDate >= a.StartDate));

            Expression <Func <Team, bool> > checkDailyOrders = t => !t.Orders
                                                               .Where(o => o.ServiceFrequency == ServiceFrequency.Daily &&
                                                                      (o.Status == OrderStatus.InProgress || o.Status == OrderStatus.Pending))
                                                               .Select(o => o.Appointment)
                                                               .Any(a => (serviceFrequency == ServiceFrequency.Once ? a.StartDate < startDate : true) &&
                                                                    ((startDate.Hour >= a.StartDate.Hour && startDate.Hour <= a.EndDate.Hour) ||
                                                                     (startDate.Hour < a.StartDate.Hour && endDate.Hour >= a.StartDate.Hour)));

            Expression <Func <Team, bool> > checkWeeklyOrders = t => !t.Orders
                                                                .Where(o => o.ServiceFrequency == ServiceFrequency.Weekly &&
                                                                       (o.Status == OrderStatus.InProgress || o.Status == OrderStatus.Pending))
                                                                .Select(o => o.Appointment)
                                                                .Any(a => (serviceFrequency == ServiceFrequency.Once ? a.StartDate < startDate : true) &&
                                                                     (a.DayOfWeek == dayOfWeek &&
                                                                      ((startDate.Hour >= a.StartDate.Hour && startDate.Hour <= a.EndDate.Hour) ||
                                                                       (startDate.Hour < a.StartDate.Hour && endDate.Hour >= a.StartDate.Hour))));

            var teams = this.teamsRepository.All()
                        .Where(t =>
                               t.CityId == cityId &&
                               t.Services.Select(s => s.ServiceId).Contains(serviceId) &&
                               t.TeamMembers.Any());

            teams = teams.Where(checkSingleOrders);
            teams = teams.Where(checkDailyOrders);
            teams = teams.Where(checkWeeklyOrders);

            var freeTeams = this.CheckMonthlyFreeTeams <T>(startDate, endDate, teams, serviceFrequency);

            return(freeTeams
                   .ToList());
        }
Beispiel #8
0
        public bool HasFreeTeams(DateTime startDate, DateTime endDate, int cityId, int serviceId, ServiceFrequency serviceFrequency)
        {
            var freeTeams = this.GetFreeTeams <TeamServiceModel>(startDate, endDate, cityId, serviceId, serviceFrequency);

            return(freeTeams.Count() > 0);
        }
Beispiel #9
0
        private IEnumerable <T> CheckMonthlyFreeTeams <T>(DateTime currentDate, DateTime endDate, IQueryable <Team> teams, ServiceFrequency serviceFrequency)
        {
            var allTeams = teams.ToList();

            var result = allTeams
                         .Where(t => !t.Orders
                                .Where(o => o.ServiceFrequency == ServiceFrequency.Monthly &&
                                       (o.Status == OrderStatus.InProgress || o.Status == OrderStatus.Pending))
                                .Select(o => o.Appointment)
                                .Any(a => (serviceFrequency == ServiceFrequency.Once ? a.StartDate < currentDate : true) &&
                                     this.CheckMonthlyAppointments(a, currentDate, endDate, serviceFrequency)))
                         .Select(t => t.To <T>())
                         .ToList();

            return(result);
        }
Beispiel #10
0
        private bool CheckMonthlyAppointments(Appointment appointment, DateTime startDate, DateTime endDate, ServiceFrequency serviceFrequency)
        {
            var currentMonthAppointmentStartDate = this.GetCurrentMothAppointmentDate(appointment, startDate);
            var currentMontAppointmentEndDate    = currentMonthAppointmentStartDate.Date.AddHours(appointment.EndDate.Hour);

            var result = serviceFrequency switch
            {
                ServiceFrequency.Daily => (startDate.TimeOfDay >= currentMonthAppointmentStartDate.TimeOfDay && startDate.TimeOfDay <= currentMontAppointmentEndDate.TimeOfDay) ||
                (startDate.TimeOfDay < currentMonthAppointmentStartDate.TimeOfDay && endDate.TimeOfDay >= currentMonthAppointmentStartDate.TimeOfDay),

                ServiceFrequency.Weekly => appointment.DayOfWeek == (int)currentMonthAppointmentStartDate.DayOfWeek &&
                ((startDate.TimeOfDay >= currentMonthAppointmentStartDate.TimeOfDay && startDate.TimeOfDay <= currentMontAppointmentEndDate.TimeOfDay) ||
                 (startDate.TimeOfDay < currentMonthAppointmentStartDate.TimeOfDay && endDate.TimeOfDay >= currentMonthAppointmentStartDate.TimeOfDay)),

                _ => (startDate >= currentMonthAppointmentStartDate && startDate <= currentMontAppointmentEndDate) ||
                (startDate < currentMonthAppointmentStartDate && endDate >= currentMonthAppointmentStartDate),
            };

            return(result);
        }
Beispiel #11
0
        public void GetFreeTeamsWorksCorrectly(ServiceFrequency appointmentServiceFrequency, DateTime appointmentStartDate, DateTime appointmentEndDate, ServiceFrequency serviceFrequency, DateTime startDate, DateTime endDate, int expectedCount)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var week = appointmentStartDate.GetWeekOfMonth();

            var repositoryMock = new Mock <IDeletableEntityRepository <Team> >();

            repositoryMock.Setup(x => x.All())
            .Returns(new List <Team>()
            {
                new Team()
                {
                    Id       = "1",
                    CityId   = 1,
                    Services = new List <TeamService>()
                    {
                        new TeamService()
                        {
                            TeamId    = "1",
                            ServiceId = 1,
                        },
                    },
                    Orders = new List <Order>()
                    {
                        new Order()
                        {
                            Id = "1",
                            ServiceFrequency = appointmentServiceFrequency,
                            Status           = OrderStatus.Pending,
                            AppointmetnId    = "1",
                            TeamId           = "1",
                            Appointment      = new Appointment()
                            {
                                Id          = "1",
                                OrderId     = "1",
                                DayOfWeek   = (int)appointmentStartDate.DayOfWeek,
                                StartDate   = appointmentStartDate,
                                EndDate     = appointmentEndDate,
                                WeekOfMonth = appointmentStartDate.GetWeekOfMonth(),
                            },
                        },
                    },
                    TeamMembers = new List <ApplicationUser>()
                    {
                        new ApplicationUser()
                        {
                            Id     = "1",
                            TeamId = "1",
                        },
                    },
                },
            }.AsQueryable());

            var service = new TeamsService(repositoryMock.Object);

            var result = service.GetFreeTeams <TeamServiceModel>(startDate, endDate, 1, 1, serviceFrequency);

            Assert.Equal(expectedCount, result.Count());
        }