Ejemplo n.º 1
0
        public void AddServiceToUserOffice(UserOffice userOffice, string serviceTypeName)
        {
            var serviceProvider = _serviceProviderRepository.GetByType(serviceTypeName);

            if (serviceProvider != null)
            {
                var service = _userServiceRepository.GetSingle(
                    x => x.UserOfficeId.Equals(userOffice.Id) && x.ServiceProvider.Id.Equals(serviceProvider.Id)) ??
                              _userServiceRepository.Create(new UserService
                {
                    ServiceProviderId = serviceProvider.Id,
                    UserOfficeId      = userOffice.Id
                });

                var serviceProviderSettings = (ServiceProviderSettings)_appSettings.Value.ServiceProviderTypeSettings.GetType()
                                              .GetProperty(serviceTypeName)
                                              .GetValue(_appSettings.Value.ServiceProviderTypeSettings);

                if (serviceProviderSettings.UserData != null)
                {
                    foreach (var pair in serviceProviderSettings.UserData)
                    {
                        var userServiceData =
                            _userServiceDataRepository.GetSingle(
                                x => x.UserServiceId.Equals(service.Id) && x.Key.Equals(pair.Key));
                        if (userServiceData == null)
                        {
                            _userServiceDataRepository.Create(new UserServiceData
                            {
                                Key           = pair.Key,
                                Value         = string.IsNullOrEmpty(pair.Value) ? null : pair.Value,
                                UserServiceId = service.Id
                            });
                        }
                        else if (userServiceData.Value != pair.Value && string.IsNullOrEmpty(userServiceData.Value))
                        {
                            userServiceData.Value = pair.Value;
                            _userServiceDataRepository.Update(userServiceData);
                        }
                    }
                }
            }
        }