Exemple #1
0
        /// <inheritdoc />
        public async Task <Guid> Create(CreateDeviceRequest request, string token)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(token))
            {
                throw new ArgumentNullException(nameof(token));
            }

            var devices = await _deviceRepository.GetAsync();

            if (devices.Any(x => x.Udid == request.Udid))
            {
                throw new ApplicationException($"Device with udid: {request.Udid} already exist");
            }

            var scopeArray       = new [] { Const.Permissions.Device.AdminCreate };
            var correctCompanyId = await _validationHelper.GetCompanyIdByPermission(token, scopeArray, request.CompanyId);

            if (!correctCompanyId.HasValue)
            {
                throw new ArgumentException("Invalid companyId", nameof(request.CompanyId));
            }
            request.CompanyId = correctCompanyId.Value;

            await _validationHelper.ValidateCompanyAndBranch(request.CompanyId, request.BranchId, token);

            var device = request.Adapt <Device>();

            device.CreatedOn = DateTime.UtcNow;
            return(await _deviceRepository.AddAsync(device));
        }
Exemple #2
0
        public async Task ShouldReturnFailIfCompanyNotExistWhenValidateCompanyAndBranch()
        {
            // Arrange
            int    companyId = 5;
            string token     = _baseTest.Fixture.Create <string>();
            Guid   branchId  = Guid.Parse("596e029a-7eb7-44b5-a724-5099ead0f70a");

            _httpServiceMock.Setup(x => x.GetCompanyById(companyId, token)).ReturnsAsync((CompanyModel)null);
            // Act
            Func <Task> result = async() => await _validationHelper.ValidateCompanyAndBranch(companyId, branchId, token);

            //Assert
            await result.Should().ThrowAsync <ApplicationException>();
        }