public async Task <Result> InsertOrganisation(ScopeOptions scope, OrganisationEdit organisation)
        {
            var validator = new OrganisationValidator(_context, true, organisation.Id);
            var result    = validator.Validate(organisation).GetResult();

            if (!result.Success)
            {
                return(result);
            }

            //Only for super admins
            if (!scope.IgnoreScope)
            {
                return(new Result());
            }

            var entity = MapModelToEntity(organisation);
            await _context.Organisation.AddAsync(entity);

            await _context.SaveChangesAsync();

            organisation.Id = entity.Id;
            result.Tag      = organisation;

            return(result);
        }
        public async Task <Result> UpdateOrganisation(ScopeOptions scope, OrganisationEdit organisation)
        {
            var validator = new OrganisationValidator(_context, false, organisation.Id);
            var result    = validator.Validate(organisation).GetResult();

            if (!result.Success)
            {
                return(result);
            }

            //Only organisation scope
            if (scope.Scope == Scope.Branch || scope.Scope == Scope.User)
            {
                return(new Result());
            }

            var entity = await ScopeQuery
                         .GetOrganisationEntityQuery(_context, scope)
                         .FirstOrDefaultAsync(o => o.Id == organisation.Id);

            if (entity == null)
            {
                return(new Result());
            }

            entity = MapModelToEntity(organisation, entity);
            await _context.SaveChangesAsync();

            return(result);
        }
        public void Validate_Admin_Delete_Completes(string role)
        {
            var ctx = Creator.GetContext(role: role);

            _context.Setup(c => c.HttpContext).Returns(ctx);
            var validator = new OrganisationValidator(_context.Object);

            var valres = validator.Validate(new Organisation(), ruleSet: nameof(IOrganisationLogic.Delete));

            valres.Errors.Should().BeEmpty();
        }
        public void Validate_NonAdmin_Update_Returns_Error(string role)
        {
            var ctx = Creator.GetContext(role: role);

            _context.Setup(c => c.HttpContext).Returns(ctx);
            var validator = new OrganisationValidator(_context.Object);

            var valres = validator.Validate(new Organisation(), ruleSet: nameof(IOrganisationLogic.Update));

            valres.Errors.Count().Should().Be(1);
        }