Пример #1
0
        public async Task <DeleteSystemUserCommandResponse> Handle(DeleteSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new DeleteSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            var systemUserIds = request.SystemUserIds.Distinct().ToList();
            var systemusers   = await _systemUserRepository.GetSystemUsersByIds(systemUserIds);

            if (systemusers.Count != systemUserIds.Count)
            {
                throw new RulesException("Invalid", @"User not found");
            }

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var sysuser in systemusers)
                {
                    foreach (var sysuserassociatedcountry in sysuser.SystemUserAssociatedCountries.ToList())
                    {
                        sysuser.SystemUserAssociatedCountries.Remove(sysuserassociatedcountry);
                        _systemUserRepository.Delete(sysuserassociatedcountry);
                    }
                    _systemUserRepository.DeleteSystemUser(sysuser);
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userDocs = _context.GetAll(Constants.SystemUsersDiscriminator);
                foreach (var user in systemusers)
                {
                    //foreach (var country in user.SystemUserAssociatedCountries)
                    //{
                    var eventsourcing = new SystemUserCommandEvent()
                    {
                        id            = userDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.SystemUsersDiscriminator,
                        SystemUserId  = user.SystemUserId,
                        PartitionKey  = userDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId).GetPropertyValue <int>("CountryId").ToString()
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventsourcing);

                    //}
                }
                scope.Complete();
            }
            return(response);
        }
Пример #2
0
        public async Task <UpdateSystemUserCommandResponse> Handle(UpdateSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new UpdateSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            var userExists = _systemUserRepository.UserExists(request.Email, request.SystemUserId);

            if (!userExists)
            {
                throw new RulesException("email", $"User with SystemuserId: {request.SystemUserId} and email address: {request.Email} does not exists");
            }

            //var user = _mapper.Map<SystemUsers>(request);
            var user = (await _systemUserRepository.GetSystemUsersByIds(new List <int> {
                request.SystemUserId
            })).FirstOrDefault();
            var countriesToDelete = new List <int>();

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                user.FirstName         = request.FirstName;
                user.LastName          = request.LastName;
                user.Level             = request.Level;
                user.Role              = request.Role;
                user.WorkPhoneNumber   = request.WorkPhoneNumber;
                user.MobilePhoneNumber = request.MobilePhoneNumber;
                user.Location          = request.Location;
                user.Email             = request.Email;
                user.UpdatedBy         = "CMS Admin";
                user.UpdatedDate       = DateTime.UtcNow;

                var homeCountry = user.SystemUserAssociatedCountries.FirstOrDefault(c => c.CountryId == request.HomeCountry);
                if (homeCountry == null)
                {
                    homeCountry = new SystemUserAssociatedCountries
                    {
                        CountryId    = request.HomeCountry,
                        IsPrimary    = true,
                        SystemUserId = user.SystemUserId
                    };
                    _systemUserRepository.Add(homeCountry);
                }
                else
                {
                    homeCountry.IsPrimary    = true;
                    homeCountry.SystemUserId = user.SystemUserId;
                    homeCountry.CountryId    = request.HomeCountry;
                    _systemUserRepository.Update(homeCountry);
                }

                foreach (var country in request.Countries.Where(x => !x.Equals(request.HomeCountry)))
                {
                    if (user.SystemUserAssociatedCountries.FirstOrDefault(c => c.CountryId == country) == null)
                    {
                        var associatedCountries = new SystemUserAssociatedCountries
                        {
                            CountryId    = country,
                            SystemUserId = user.SystemUserId,
                            IsPrimary    = false
                        };
                        user.SystemUserAssociatedCountries.Add(associatedCountries);
                        _systemUserRepository.Add(associatedCountries);
                    }
                }
                foreach (var associatedCountry in user.SystemUserAssociatedCountries.ToList())
                {
                    if (request.Countries.Where(c => c == associatedCountry.CountryId).Count() == 0)
                    {
                        countriesToDelete.Add(associatedCountry.SystemUserAssociatedCountryId);
                        user.SystemUserAssociatedCountries.Remove(associatedCountry);
                        _systemUserRepository.Delete(associatedCountry);
                    }
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var systemuserDocs = _context.GetAll(Constants.SystemUsersDiscriminator);
                foreach (var content in user.SystemUserAssociatedCountries)
                {
                    var doc = systemuserDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId &&
                                                            d.GetPropertyValue <int>("SystemUserAssociatedCountryId") == content.SystemUserAssociatedCountryId);
                    var eventSourcing = new SystemUserCommandEvent()
                    {
                        id = doc != null?doc.GetPropertyValue <Guid>("id") : Guid.NewGuid(),
                                 EventType         = doc != null ? ServiceBusEventType.Update : ServiceBusEventType.Create,
                                 Discriminator     = Constants.SystemUsersDiscriminator,
                                 SystemUserId      = user.SystemUserId,
                                 CreatedBy         = user.CreatedBy ?? string.Empty,
                                 CreatedDate       = user.CreatedDate,
                                 UpdatedBy         = user.UpdatedBy,
                                 UpdatedDate       = user.UpdatedDate,
                                 FirstName         = user.FirstName,
                                 LastName          = user.LastName,
                                 WorkPhoneNumber   = user.WorkPhoneNumber,
                                 MobilePhoneNumber = user.MobilePhoneNumber,
                                 Level             = user.Level,
                                 Role      = user.Role,
                                 Location  = user.Location,
                                 Email     = user.Email,
                                 TimeZone  = user.TimeZone,
                                 CountryId = content.CountryId,
                                 IsPrimary = content.IsPrimary,
                                 SystemUserAssociatedCountryId = content.SystemUserAssociatedCountryId,
                                 PartitionKey = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                foreach (int i in countriesToDelete)
                {
                    var deleteEvt = new SystemUserCommandEvent()
                    {
                        id = systemuserDocs.FirstOrDefault(d => d.GetPropertyValue <int>("SystemUserId") == user.SystemUserId &&
                                                           d.GetPropertyValue <int>("SystemUserAssociatedCountryId") == i).GetPropertyValue <Guid>("id"),
                        EventType     = ServiceBusEventType.Delete,
                        Discriminator = Constants.SystemUsersDiscriminator,
                        PartitionKey  = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(deleteEvt);
                }
                scope.Complete();
            }
            return(response);
        }
        public async Task <CreateSystemUserCommandResponse> Handle(CreateSystemUserCommand request, CancellationToken cancellationToken)
        {
            var response = new CreateSystemUserCommandResponse()
            {
                IsSuccessful = false
            };

            // Check User Exists
            var userExists = _systemUserRepository.UserExists(request.Email, 0);

            if (userExists)
            {
                throw new RulesException("email", $"This email address: {request.Email} already exists");
            }

            var user = _mapper.Map <SystemUsers>(request);

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                user.CreatedBy   = "CMS Admin";
                user.CreatedDate = DateTime.UtcNow;
                user.UpdatedBy   = "CMS Admin";
                user.UpdatedDate = DateTime.UtcNow;


                _systemUserRepository.Add(user);
                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();


                var homeCountry = new SystemUserAssociatedCountries();
                homeCountry.CountryId    = request.HomeCountry;
                homeCountry.IsPrimary    = true;
                homeCountry.SystemUserId = user.SystemUserId;
                _systemUserRepository.Add(homeCountry);

                var countryPermissionList = request.Countries.Distinct();
                foreach (var country in countryPermissionList.Where(x => !x.Equals(request.HomeCountry)))
                {
                    var associatedCountry = new SystemUserAssociatedCountries();
                    associatedCountry.CountryId    = country;
                    associatedCountry.SystemUserId = user.SystemUserId;
                    associatedCountry.IsPrimary    = false;
                    _systemUserRepository.Add(associatedCountry);
                }

                await _systemUserRepository.UnitOfWork.SaveEntitiesAsync();

                response.IsSuccessful = true;
                scope.Complete();
            }
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                foreach (var content in user.SystemUserAssociatedCountries)
                {
                    var eventSourcing = new SystemUserCommandEvent()
                    {
                        EventType         = ServiceBusEventType.Create,
                        Discriminator     = Constants.SystemUsersDiscriminator,
                        SystemUserId      = user.SystemUserId,
                        CreatedBy         = user.CreatedBy,
                        CreatedDate       = user.CreatedDate,
                        UpdatedBy         = user.UpdatedBy,
                        UpdatedDate       = user.UpdatedDate,
                        FirstName         = user.FirstName,
                        LastName          = user.LastName,
                        WorkPhoneNumber   = user.WorkPhoneNumber,
                        MobilePhoneNumber = user.MobilePhoneNumber,
                        Level             = user.Level,
                        Role      = user.Role,
                        Location  = user.Location,
                        Email     = user.Email,
                        TimeZone  = user.TimeZone,
                        CountryId = content.CountryId,
                        IsPrimary = content.IsPrimary,
                        SystemUserAssociatedCountryId = content.SystemUserAssociatedCountryId,
                        PartitionKey = ""
                    };
                    await _eventcontext.PublishThroughEventBusAsync(eventSourcing);
                }
                scope.Complete();
            }
            return(response);
        }