private async Task CreateUnionInfoAsync(UnionInfoCreateOrUpdateInput input)
        {
            var nationalCode = input.NationalCode.Replace("-", "");
            var user         = new User
            {
                IsActive = true,
                ShouldChangePasswordOnNextLogin = true,
                UserName     = input.UserName,
                EmailAddress = input.UserName + "@mgnsys.ir",
                Name         = input.Name,
                Surname      = input.Family
            };

            user.Password = _passwordHasher.HashPassword(user, nationalCode);
            CheckErrors(await UserManager.CreateAsync(user));
            await CurrentUnitOfWork.SaveChangesAsync();

            var  officerRole = _roleManager.GetRoleByName(StaticRoleNames.Host.StateAdmin);
            long userId      = user.ToUserIdentifier().UserId;

            user.Roles = new List <UserRole>();
            user.Roles.Add(new UserRole(null, user.Id, officerRole.Id));

            if (userId > 0)
            {
                var unionInfo = ObjectMapper.Map <UnionInfo>(input);
                unionInfo.UserId = userId;
                await _unionInfoRepository.InsertAsync(unionInfo);
            }
            else
            {
                throw new UserFriendlyException(L("AnErrorOccurred"));
            }
        }
        public async Task CreateOrUpdateUnionInfo(UnionInfoCreateOrUpdateInput input)
        {
            await CheckValidation(input);

            if (input.Id.HasValue)
            {
                await UpdateUnionInfoAsync(input);
            }
            else
            {
                await CreateUnionInfoAsync(input);
            }
        }
        private async Task UpdateUnionInfoAsync(UnionInfoCreateOrUpdateInput input)
        {
            var user = await UserManager.GetUserByIdAsync(input.UserId);

            user.Name         = input.Name;
            user.Surname      = input.Family;
            user.UserName     = input.UserName;
            user.EmailAddress = input.UserName + "@mgnsys.ir";
            CheckErrors(await UserManager.UpdateAsync(user));
            await CurrentUnitOfWork.SaveChangesAsync();

            var unionInfo = ObjectMapper.Map <UnionInfo>(input);
            await _unionInfoRepository.UpdateAsync(unionInfo);
        }
        private async Task CheckValidation(UnionInfoCreateOrUpdateInput input)
        {
            var existingObj = (await _unionInfoRepository.GetAll().AsNoTracking()
                               .FirstOrDefaultAsync(l => l.Code == input.Code));

            if (existingObj != null && existingObj.Id != input.Id)
            {
                throw new UserFriendlyException(L("ThisCodeAlreadyExists"));
            }

            existingObj = (await _unionInfoRepository.GetAll().AsNoTracking()
                           .FirstOrDefaultAsync(l => l.UnionName == input.UnionName));
            if (existingObj != null && existingObj.Id != input.Id)
            {
                throw new UserFriendlyException(L("ThisNameAlreadyExists"));
            }

            existingObj = (await _unionInfoRepository.GetAll().AsNoTracking()
                           .FirstOrDefaultAsync(l => l.StateInfoId == input.StateInfoId));
            if (existingObj != null && existingObj.Id != input.Id)
            {
                throw new UserFriendlyException(L("ThisStateAlreadyExists"));
            }
        }