private void InitSeedData()
 {
     USER_FOCUS_ENTITY = SHIPPER_ENTITY;
     USER = new UserEntity
     {
         UserId      = USER_ID,
         IdentUserId = USER_ID
     };
     CARRIERS = new List <CarrierEntity>
     {
         new CarrierEntity
         {
             CarrierId    = CARRIER_ID,
             CarrierScacs = new List <CarrierScacEntity>
             {
                 new CarrierScacEntity
                 {
                     Scac = SCAC
                 }
             }
         }
     };
     USER_CARRIER_SCACS = new List <UserCarrierScacEntity>();
     USER_SHIPPERS      = new List <UserShipperEntity>
     {
         new UserShipperEntity
         {
             CustomerId = CUSTOMER_ID,
             UserId     = USER_ID
         }
     };
 }
            public async Task CarrierScac_Admin_AddsUserCarrierScac()
            {
                USER_FOCUS_ENTITY = CARRIER_SCAC_ENTITY;

                var builder = new MockDbBuilder();

                InitDb(builder);
                InitService();

                var result = await _svc.UpdateFocusEntityAsync(USER_ID, USER_FOCUS_ENTITY, USERNAME);

                builder.MockUserCarrierScacs.Verify(x => x.Add(It.IsAny <UserCarrierScacEntity>()), Times.Once);
            }
        public async Task <UserProfileData> UpdateFocusEntityAsync(Guid identUserId, UserFocusEntityData userFocusEntityData, string userName)
        {
            identUserId.NullArgumentCheck(nameof(identUserId));
            userFocusEntityData.NullArgumentCheck(nameof(userFocusEntityData));
            userName.NullArgumentCheck(nameof(userName));

            var userEntity = await _context.Users.SingleOrDefaultAsync(user => user.IdentUserId == identUserId);

            userEntity.NullEntityCheck(identUserId);

            userEntity.PrimaryScac       = null;
            userEntity.PrimaryCustomerId = null;


            switch (userFocusEntityData.Type)
            {
            case UserFocusEntityType.CarrierScac:
                userEntity.PrimaryScac = userFocusEntityData.Id;

                //If admin add a record for the user so we don't have to write special code for admin security checkes thorughout the app
                if (await _securityService.IsAdminAsync() || await _securityService.IsDedicatedAsync())
                {
                    await UpsertCarrier(userFocusEntityData.Id, userEntity);
                }
                break;

            case UserFocusEntityType.Shipper:

                Guid customerId;

                if (Guid.TryParse(userFocusEntityData.Id, out customerId))
                {
                    userEntity.PrimaryCustomerId = customerId;

                    //If admin add a record for the user so we don't have to write special code for admin security checkes thorughout the app
                    if (await _securityService.IsAdminAsync())
                    {
                        await UpsertCustomer(customerId, userEntity);
                    }
                }
                else
                {
                    throw new Exception($"Invalid customer id: {userFocusEntityData.Id}");
                }
                break;
            }

            await _context.SaveChangesAsync(userName);

            return(await GetUserProfileAsync(identUserId));
        }
            public async Task CarrierScac_NotAdmin_DoesNotAddUserCarrierScac()
            {
                USER_FOCUS_ENTITY = CARRIER_SCAC_ENTITY;
                _securityService.Setup(x => x.UserHasRoleAsync(It.IsAny <string[]>())).ReturnsAsync(false);

                var builder = new MockDbBuilder();

                InitDb(builder);
                InitService();

                var result = await _svc.UpdateFocusEntityAsync(USER_ID, USER_FOCUS_ENTITY, USERNAME);

                builder.MockUserCarrierScacs.Verify(x => x.Add(It.IsAny <UserCarrierScacEntity>()), Times.Never);
            }
        public async Task <IActionResult> PutFocusEntity([FromBody] UserFocusEntityData userFocusEntityData)
        {
            try
            {
                if (!_userContext.UserId.HasValue)
                {
                    throw new EmptyUserIdException("UserId is null");
                }

                return(Success(await _userProfileService.UpdateFocusEntityAsync(_userContext.UserId.Value, userFocusEntityData, _userContext.UserName)));
            }
            catch (Exception ex)
            {
                return(Error <ResponseMessage <List <CarrierScacData> > >(ex));
            }
        }
            public void CarrierScac_Admin_NotCarrierIdFound_ThrowsException()
            {
                USER_FOCUS_ENTITY = CARRIER_SCAC_ENTITY;
                CARRIERS.First().CarrierId = null;

                var builder = new MockDbBuilder();

                InitDb(builder);
                InitService();

                var expected = $"Cannot find Carrier for SCAC: {SCAC}";

                _svc.Awaiting(x => x.UpdateFocusEntityAsync(USER_ID, USER_FOCUS_ENTITY, USERNAME))
                .Should().Throw <Exception>()
                .WithMessage(expected);
            }
            public async Task CarrierScac_Admin_HasUserCarrierScacAlready_NoUserCarrierScacAdded()
            {
                USER_FOCUS_ENTITY = CARRIER_SCAC_ENTITY;
                USER_CARRIER_SCACS.Add(new UserCarrierScacEntity
                {
                    UserId    = USER_ID,
                    CarrierId = CARRIER_ID,
                    Scac      = SCAC
                });

                var builder = new MockDbBuilder();

                InitDb(builder);
                InitService();

                var result = await _svc.UpdateFocusEntityAsync(USER_ID, USER_FOCUS_ENTITY, USERNAME);

                builder.MockUserCarrierScacs.Verify(x => x.Add(It.IsAny <UserCarrierScacEntity>()), Times.Never);
            }