private void ValidateFeeStructure(CustomerProfileData customerProfile)
 {
     if (customerProfile != null)
     {
         if (customerProfile.InNetworkFlatFee < 0)
         {
             throw new ValidationException("In Network Flat Fee must be $0.00 or more.");
         }
         if (customerProfile.InNetworkPercentFee < 0)
         {
             throw new ValidationException("In Network Flat Fee must be 0.00% or more.");
         }
         if (customerProfile.InNetworkPercentFee >= 10)
         {
             throw new ValidationException("In Network Flat Fee must be less than 1,000%.");
         }
         if (customerProfile.OutNetworkFlatFee < 0)
         {
             throw new ValidationException("Out Network Flat Fee must be $0.00 or more.");
         }
         if (customerProfile.OutNetworkPercentFee < 0)
         {
             throw new ValidationException("Out Network Flat Fee must be 0.00% or more.");
         }
         if (customerProfile.OutNetworkPercentFee >= 10)
         {
             throw new ValidationException("Out Network Flat Fee must be less than 1,000%.");
         }
     }
 }
            public void DoesntSendFeeChangeEmail()
            {
                var customerId = new Guid("11111111-1111-1111-1111-111111111111");
                var customer   = new CustomerEntity
                {
                    CustomerId                   = customerId,
                    Name                         = "Test Customer",
                    InNetworkFlatFee             = 10.00m,
                    CustomerContacts             = new List <CustomerContactEntity>(),
                    CustomerCarrierScacContracts = new List <CustomerCarrierScacContractEntity>()
                };
                var updatedCustomer = new CustomerProfileData()
                {
                    CustomerId                     = customerId,
                    CustomerLoadTypeId             = (int)CustomerLoadTypeEnum.HighPriority,
                    CustomerLoadTypeExpirationDate = null,
                    InNetworkFlatFee               = 10.00m,
                    CustomerContacts               = new List <CustomerContactData>(),
                    CustomerCarrierScacs           = new List <string>()
                };

                _db = new MockDbBuilder()
                      .WithCustomer(customer)
                      .Build();
                _userContext.SetupGet(_ => _.FirstName).Returns("fname");
                _userContext.SetupGet(_ => _.LastName).Returns("lname");
                _svc = BuildService();

                _svc.UpdateShipper(updatedCustomer, USERNAME);

                _notificationService.Verify(_ => _.SendShipperFeeChangeEmail(customer, updatedCustomer, "fname lname"), Times.Never);
            }
 public IActionResult UpdateShipper([FromBody] CustomerProfileData customer)
 {
     try
     {
         return(Success(_shipperAdminService.UpdateShipper(customer, _userContext.UserName)));
     }
     catch (ValidationException exception)
     {
         return(Error <CustomerProfileData>(exception));
     }
 }
 /// <summary>
 /// Validates customer order type
 /// </summary>
 /// <param name="customerProfile"></param>
 /// <returns></returns>
 public void ValidateCustomerOrderType(CustomerProfileData customerProfile)
 {
     if (customerProfile != null && customerProfile.CustomerLoadTypeId.HasValue)
     {
         if (customerProfile.CustomerLoadTypeId.Value == (int)CustomerLoadTypeEnum.NewShipper &&
             !customerProfile.CustomerLoadTypeExpirationDate.HasValue)
         {
             throw new ValidationException("Expiration Date is required for New Shipper type.");
         }
     }
 }
 private void SendFeeChangeEmail(CustomerEntity dbCustomer, CustomerProfileData customer, string changeUser)
 {
     if (dbCustomer == null ||
         dbCustomer.InNetworkFlatFee != customer.InNetworkFlatFee ||
         dbCustomer.InNetworkPercentFee != customer.InNetworkPercentFee ||
         dbCustomer.InNetworkFeeAdd != customer.InNetworkFeeAdd ||
         dbCustomer.OutNetworkFlatFee != customer.OutNetworkFlatFee ||
         dbCustomer.OutNetworkPercentFee != customer.OutNetworkPercentFee ||
         dbCustomer.OutNetworkFeeAdd != customer.OutNetworkFeeAdd
         )
     {
         _notificationService.SendShipperFeeChangeEmail(dbCustomer, customer, $"{_userContext.FirstName} {_userContext.LastName}");
     }
 }
            public void SendsFeeChangeEmail()
            {
                var customerId      = new Guid("11111111-1111-1111-1111-111111111111");
                var updatedCustomer = new CustomerProfileData()
                {
                    CustomerId                     = customerId,
                    CustomerLoadTypeId             = (int)CustomerLoadTypeEnum.HighPriority,
                    CustomerLoadTypeExpirationDate = null,
                    InNetworkFlatFee               = 10.00m,
                    CustomerContacts               = new List <CustomerContactData>(),
                    CustomerCarrierScacs           = new List <string>()
                };

                _userContext.SetupGet(_ => _.FirstName).Returns("fname");
                _userContext.SetupGet(_ => _.LastName).Returns("lname");
                _svc = BuildService();

                _svc.AddShipper(updatedCustomer, USERNAME);

                _notificationService.Verify(_ => _.SendShipperFeeChangeEmail(null, updatedCustomer, "fname lname"));
            }
        public CustomerProfileData AddShipper(CustomerProfileData customer, string username)
        {
            _securityService.GuardAction(SecurityActions.Loadshop_Ui_System_Shipper_Add_Edit);

            if (customer != null && !customer.CustomerLoadTypeId.HasValue)
            {
                customer.CustomerLoadTypeExpirationDate = null;
            }

            ValidateCustomerOrderType(customer);
            ValidateFeeStructure(customer);

            SendFeeChangeEmail(null, customer, "Test");

            var dbCustomer = _mapper.Map <CustomerEntity>(customer);

            dbCustomer.DataSource = "LOADSHOP";

            _context.Customers.Add(dbCustomer);

            if (customer.CustomerCarrierScacs.Any())
            {
                dbCustomer.CustomerCarrierScacContracts = new List <CustomerCarrierScacContractEntity>();
            }

            foreach (var scac in customer.CustomerCarrierScacs)
            {
                var dbCustomerCarrierScac = new CustomerCarrierScacContractEntity
                {
                    Scac = scac
                };
                dbCustomer.CustomerCarrierScacContracts.Add(dbCustomerCarrierScac);
            }

            _context.SaveChanges(username);
            return(_mapper.Map <CustomerProfileData>(GetDbShipper(dbCustomer.CustomerId)));
        }
 public async Task <IActionResult> SetupCustomerApi([FromBody] CustomerProfileData customer)
 {
     return(Success(await _shipperAdminService.CreateCustomerUser(customer.CustomerId.Value, _userContext.UserName)));
 }
        public CustomerProfileData UpdateShipper(CustomerProfileData customer, string username)
        {
            _securityService.GuardAction(SecurityActions.Loadshop_Ui_System_Shipper_Add_Edit);

            if (customer != null && !customer.CustomerLoadTypeId.HasValue)
            {
                customer.CustomerLoadTypeExpirationDate = null;
            }

            if (!customer.CustomerId.HasValue)
            {
                throw new Exception("Customer Id cannot be null");
            }

            ValidateCustomerOrderType(customer);
            ValidateFeeStructure(customer);

            var dbCustomer = GetDbShipper(customer.CustomerId.Value);

            SendFeeChangeEmail(dbCustomer, customer, "Test");

            dbCustomer.Name                           = customer.Name;
            dbCustomer.DefaultCommodity               = customer.DefaultCommodity;
            dbCustomer.UseFuelRerating                = customer.UseFuelRerating;
            dbCustomer.FuelReratingNumberOfDays       = Math.Abs(customer.FuelReratingNumberOfDays.GetValueOrDefault(0));
            dbCustomer.AllowZeroFuel                  = customer.AllowZeroFuel;
            dbCustomer.AllowEditingFuel               = customer.AllowEditingFuel;
            dbCustomer.TopsOwnerId                    = customer.TopsOwnerId;
            dbCustomer.SuccessManagerUserId           = customer.SuccessManagerUserId;
            dbCustomer.SuccessSpecialistUserId        = customer.SuccessSpecialistUserId;
            dbCustomer.Comments                       = customer.Comments;
            dbCustomer.CustomerLoadTypeId             = customer.CustomerLoadTypeId;
            dbCustomer.CustomerLoadTypeExpirationDate = customer.CustomerLoadTypeExpirationDate;
            dbCustomer.AutoPostLoad                   = customer.AutoPostLoad;
            dbCustomer.ValidateUniqueReferenceLoadIds = customer.ValidateUniqueReferenceLoadIds;
            dbCustomer.AllowManualLoadCreation        = customer.AllowManualLoadCreation;
            dbCustomer.InNetworkFlatFee               = customer.InNetworkFlatFee;
            dbCustomer.InNetworkPercentFee            = customer.InNetworkPercentFee;
            dbCustomer.OutNetworkFlatFee              = customer.OutNetworkFlatFee;
            dbCustomer.OutNetworkPercentFee           = customer.OutNetworkPercentFee;
            dbCustomer.InNetworkFeeAdd                = customer.InNetworkFeeAdd;
            dbCustomer.OutNetworkFeeAdd               = customer.OutNetworkFeeAdd;
            dbCustomer.RequireMarginalAnalysis        = customer.RequireMarginalAnalysis;

            // Customer Contacts
            foreach (var customerContact in customer.CustomerContacts)
            {
                var dbCustomerContact = _context.CustomerContacts
                                        .SingleOrDefault(x => x.CustomerContactId == customerContact.CustomerContactId);

                if (dbCustomerContact == null)
                {
                    dbCustomerContact = _mapper.Map <CustomerContactEntity>(customerContact);
                    dbCustomer.CustomerContacts.Add(dbCustomerContact);
                }
                else
                {
                    dbCustomerContact.FirstName   = customerContact.FirstName;
                    dbCustomerContact.LastName    = customerContact.LastName;
                    dbCustomerContact.Position    = customerContact.Position;
                    dbCustomerContact.PhoneNumber = customerContact.PhoneNumber;
                    dbCustomerContact.Email       = customerContact.Email;
                }
            }
            foreach (var dbCustomerContact in dbCustomer.CustomerContacts.Where(x => x.CustomerContactId != Guid.Empty))
            {
                if (customer.CustomerContacts.All(x => x.CustomerContactId != dbCustomerContact.CustomerContactId))
                {
                    _context.CustomerContacts.Remove(dbCustomerContact);
                }
            }

            //Only update if user has access
            if (_securityService.UserHasAction(SecurityActions.Loadshop_Ui_System_Shipper_Contracted_Carriers_Add_Edit))
            {
                // Customer Carrier Scacs
                var dbCustomerCarrierScacs = _context.CustomerCarrierScacContracts.Where(x => x.CustomerId == customer.CustomerId).ToList();
                foreach (var scac in customer.CustomerCarrierScacs)
                {
                    var dbCustomerCarrierScac = dbCustomerCarrierScacs.SingleOrDefault(x => x.Scac == scac);

                    if (dbCustomerCarrierScac == null)
                    {
                        dbCustomerCarrierScac = new CustomerCarrierScacContractEntity
                        {
                            Scac = scac
                        };
                        dbCustomerCarrierScacs.Add(dbCustomerCarrierScac);
                        dbCustomer.CustomerCarrierScacContracts.Add(dbCustomerCarrierScac);
                    }
                }
                foreach (var dbScac in dbCustomer.CustomerCarrierScacContracts.Where(x => x.CustomerCarrierContractId != Guid.Empty))
                {
                    if (customer.CustomerCarrierScacs.All(x => x != dbScac.Scac))
                    {
                        _context.CustomerCarrierScacContracts.Remove(dbScac);
                    }
                }
            }

            _context.SaveChanges(username);
            return(_mapper.Map <CustomerProfileData>(GetDbShipper(dbCustomer.CustomerId)));
        }