Beispiel #1
0
        /// <summary>
        /// Deletes a service
        /// </summary>
        /// <param name="service">The API model of a service</param>
        public void Delete(Service service)
        {
            var businessAccount = CoreEntitiesContainer.BusinessAccount(service.ServiceProviderId).FirstOrDefault();
            if (businessAccount == null)
                throw Request.NotAuthorized();

            var routeTask = CoreEntitiesContainer.RouteTasks.Include("RouteDestination").Include("RouteDestination.Route").FirstOrDefault(rt => rt.ServiceId == service.Id);
            if (routeTask != null)
            {
                CoreEntitiesContainer.RouteDestinations.DeleteObject(routeTask.RouteDestination);
                CoreEntitiesContainer.RouteTasks.DeleteObject(routeTask);
            }

            var existingService = CoreEntitiesContainer.Services.FirstOrDefault(s => s.Id == service.Id);
            var serviceTemplate = CoreEntitiesContainer.ServiceTemplates.FirstOrDefault(st => st.Id == service.Id);
            if (existingService != null)
            {
                CoreEntitiesContainer.Services.DeleteObject(existingService);
                CoreEntitiesContainer.ServiceTemplates.DeleteObject(serviceTemplate);
            }

            //add an excluded date to the recurring service
            if (service.RecurringServiceId.HasValue)
            {
                var recurringService = CoreEntitiesContainer.RecurringServices.FirstOrDefault(rs => rs.Id == service.RecurringServiceId.Value);
                var excludedDates = recurringService.ExcludedDates.ToList();
                excludedDates.Add(service.ServiceDate);
                recurringService.ExcludedDates = excludedDates;
            }

            SaveWithRetry();
        }
Beispiel #2
0
        /// <summary>
        /// Updates a Service and Fields. Inserts a new service if it doesn't exist yet
        /// Does not update Location or Repeat on the Service, must call those individually
        /// </summary>
        /// <param name="service">The API model of a Service</param>
        /// <param name="routeTaskId">Optional. If set update this route task to use this service. Only for generated services</param>
        public void Put(Service service, Guid? routeTaskId = null)
        {
            var businessAccount = CoreEntitiesContainer.BusinessAccount(service.ServiceProviderId, new[] { RoleType.Regular, RoleType.Administrator, RoleType.Mobile }).FirstOrDefault();
            if (businessAccount == null)
                throw Request.NotAuthorized();

            if (service.ClientId == Guid.Empty)
                throw Request.NotFound("Client");

            var userId = CoreEntitiesContainer.CurrentUserAccount().Id;

            var existingService = CoreEntitiesContainer.Services.FirstOrDefault(s => s.Id == service.Id);

            //the service exists. load all field information and update field values
            if (existingService != null)
            {
                //update the client id
                existingService.ClientId = service.ClientId;

                var serviceTemplate = HardCodedLoaders.LoadServiceTemplateWithDetails(CoreEntitiesContainer, existingService.Id, null, null, null).First();

                serviceTemplate.Name = service.Name;

                #region Update all Fields

                foreach (var field in service.Fields)
                {
                    var existingField = existingService.ServiceTemplate.Fields.FirstOrDefault(f => f.Id == field.Id);
                    if (existingField == null)
                        throw Request.NotFound("Service");

                    var numericField = existingField as NumericField;
                    var textBoxField = existingField as TextBoxField;
                    var signatureField = existingField as SignatureField;
                    var locationField = existingField as LocationField;
                    var optionsField = existingField as OptionsField;

                    if (numericField != null)
                    {
                        numericField.Value = ((Models.NumericField) field).Value;

                        numericField.LastModified = DateTime.UtcNow;
                        numericField.LastModifyingUserId = userId;
                    }

                    if (textBoxField != null)
                    {
                        textBoxField.Value = ((Models.TextBoxField) field).Value;

                        textBoxField.LastModified = DateTime.UtcNow;
                        textBoxField.LastModifyingUserId = userId;
                    }

                    if (signatureField != null)
                    {
                        signatureField.Value = ((Models.SignatureField) field).Value;
                        
                        signatureField.LastModified = DateTime.UtcNow;
                        signatureField.LastModifyingUserId = userId;
                    }
                    //If the field is a OptionsField, update the Options
                    if (optionsField != null)
                    {
                        //Cycle through each option and update the IsChecked value
                        foreach (var apiOption in ((Models.OptionsField)field).Options)
                        {
                            var modelOption = optionsField.Options.FirstOrDefault(op => op.Name == apiOption.Name);
                            if (modelOption != null)
                                modelOption.IsChecked = apiOption.IsChecked;
                        }

                        optionsField.LastModified = DateTime.UtcNow;
                        optionsField.LastModifyingUserId = userId;
                    }

                    if (locationField != null)
                    {
                        //Find the existing LocationField
                        var destinationField = serviceTemplate.GetDestinationField();

                        destinationField.Value = null;
                        destinationField.LocationId = ((Models.LocationField)field).LocationId;

                        var routeTasks = CoreEntitiesContainer.RouteTasks.Where(rt => rt.ServiceId == existingService.Id).Include(rt => rt.RouteDestination).ToArray();                        

                        //If any route tasks exist, update their location and clientId
                        //If any route tasks have been put into routes, update the route destination's location and ClientId
                        foreach (var routeTask in routeTasks)
                        {
                            //Update the RouteTask's LocationId and clientId
                            destinationField.LocationId = locationField.LocationId;

                            //If there is no RouteDestination for the RouteTask, move on to the next one
                            if (routeTask.RouteDestination == null) continue;

                            //Update the RouteDestination's LocationId and ClientId
                            routeTask.RouteDestination.LocationId = locationField.LocationId;
                        }

                        locationField.LastModified = DateTime.UtcNow;
                        locationField.LastModifyingUserId = userId;
                    }
                }

                #endregion

                //TODO CR Make extension method on ITrackable to do this
                existingService.LastModified = DateTime.UtcNow;
                existingService.LastModifyingUserId = CoreEntitiesContainer.CurrentUserAccount().Id;
            }
            //the service was generated, insert a new Service and set the appropriate field values
            else
            {
                if (!service.RecurringServiceId.HasValue && !service.ParentServiceTemplateId.HasValue)
                    throw Request.BadRequest("Need to have a parent RecurringServiceId or a ParentServiceTemplateId specified");

                var parentTemplateId = service.RecurringServiceId.HasValue
                                     ? service.RecurringServiceId
                                     : service.ParentServiceTemplateId.Value;

                var generatedService = new Core.Models.CoreEntities.Service
                {
                    Id = service.Id,
                    CreatedDate = DateTime.UtcNow,
                    ServiceDate = service.ServiceDate.Date,
                    ServiceProviderId = service.ServiceProviderId,
                    ClientId = service.ClientId,
                    RecurringServiceId = service.RecurringServiceId,
                    ServiceTemplate = new ServiceTemplate
                    {
                        Id = service.Id,
                        Name = service.Name,
                        ServiceTemplateLevel = ServiceTemplateLevel.ServiceDefined,
                        OwnerServiceTemplateId = parentTemplateId
                    },
                };

                //Add all fields from the generated Service to the Service Template
                foreach (var field in service.Fields)
                    generatedService.ServiceTemplate.Fields.Add(Models.Field.ConvertBack(field));

                CoreEntitiesContainer.Services.AddObject(generatedService);

                if (routeTaskId.HasValue)
                {
                    var routeTask = CoreEntitiesContainer.RouteTasks.FirstOrDefault(rt => rt.Id == routeTaskId.Value);
                    if (routeTask != null)
                    {
                        routeTask.RecurringServiceId = null;
                        routeTask.ServiceId = generatedService.Id;
                    }
                }
            }
            
            //Save any changes that were made
            SaveWithRetry();
        }