Example #1
0
        private async Task DeleteFromNationalSociety(int nationalSocietyId, int technicalAdvisorId, bool allowHeadManagerDeletion = false)
        {
            var technicalAdvisor = await _nationalSocietyUserService.GetNationalSocietyUserIncludingNationalSocieties <TechnicalAdvisorUser>(technicalAdvisorId);

            var userNationalSocieties = technicalAdvisor.UserNationalSocieties;

            var nationalSocietyReferenceToRemove = userNationalSocieties.SingleOrDefault(uns => uns.NationalSocietyId == nationalSocietyId);

            if (nationalSocietyReferenceToRemove == null)
            {
                throw new ResultException(ResultKey.User.Registration.UserIsNotAssignedToThisNationalSociety);
            }

            _dataContext.UserNationalSocieties.Remove(nationalSocietyReferenceToRemove);

            await HandleHeadManagerStatus(technicalAdvisor, nationalSocietyReferenceToRemove, allowHeadManagerDeletion);

            var isUsersLastNationalSociety = userNationalSocieties.Count == 1;

            if (isUsersLastNationalSociety)
            {
                _nationalSocietyUserService.DeleteNationalSocietyUser(technicalAdvisor);
                await _identityUserRegistrationService.DeleteIdentityUser(technicalAdvisor.IdentityUserId);
            }
        }
Example #2
0
        private async Task DeleteFromDb(int managerId, bool allowHeadManagerDeletion = false)
        {
            var manager = await _nationalSocietyUserService.GetNationalSocietyUserIncludingNationalSocieties <ManagerUser>(managerId);

            await HandleHeadManagerStatus(manager, allowHeadManagerDeletion);

            _nationalSocietyUserService.DeleteNationalSocietyUser(manager);
            await _identityUserRegistrationService.DeleteIdentityUser(manager.IdentityUserId);
        }
Example #3
0
        public async Task <Result> Delete(int nationalSocietyId, int dataConsumerId)
        {
            try
            {
                await _deleteService.EnsureCanDeleteUser(dataConsumerId, Role.DataConsumer);

                using var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

                var dataConsumerUser = await _nationalSocietyUserService.GetNationalSocietyUserIncludingNationalSocieties <DataConsumerUser>(dataConsumerId);

                var userNationalSocieties = dataConsumerUser.UserNationalSocieties;

                var nationalSocietyReferenceToRemove = userNationalSocieties.SingleOrDefault(uns => uns.NationalSocietyId == nationalSocietyId);

                if (nationalSocietyReferenceToRemove == null)
                {
                    return(Error(ResultKey.User.Registration.UserIsNotAssignedToThisNationalSociety));
                }

                var isUsersLastNationalSociety = userNationalSocieties.Count == 1;
                _dataContext.UserNationalSocieties.Remove(nationalSocietyReferenceToRemove);

                if (isUsersLastNationalSociety)
                {
                    _nationalSocietyUserService.DeleteNationalSocietyUser(dataConsumerUser);
                    await _identityUserRegistrationService.DeleteIdentityUser(dataConsumerUser.IdentityUserId);
                }

                await _dataContext.SaveChangesAsync();

                transactionScope.Complete();
                return(Success());
            }
            catch (ResultException e)
            {
                _loggerAdapter.Debug(e);
                return(e.Result);
            }
        }