public Guid Add(AddLicenseeData data)
        {
            using (var scope = CustomTransactionScope.GetTransactionScope())
            {
                var validationResult = _licenseeQueries.ValidateCanAdd(data);

                if (!validationResult.IsValid)
                {
                    throw new RegoValidationException(validationResult);
                }

                var licensee = new Licensee
                {
                    Id                  = data.Id ?? Guid.NewGuid(),
                    Name                = data.Name,
                    CompanyName         = data.CompanyName,
                    AffiliateSystem     = data.AffiliateSystem,
                    ContractStart       = data.ContractStart,
                    ContractEnd         = data.ContractEnd,
                    Email               = data.Email,
                    AllowedBrandCount   = data.BrandCount,
                    AllowedWebsiteCount = data.WebsiteCount,
                    TimezoneId          = data.TimeZoneId,
                    Status              = LicenseeStatus.Inactive,
                    DateCreated         = DateTimeOffset.UtcNow,
                    CreatedBy           = _actorInfoProvider.Actor.UserName,
                    Contracts           = new List <Contract>
                    {
                        new Contract
                        {
                            Id                = Guid.NewGuid(),
                            StartDate         = data.ContractStart,
                            EndDate           = data.ContractEnd,
                            IsCurrentContract = true
                        }
                    }
                };

                if (data.Products != null)
                {
                    EnumerableExtensions.ForEach(data.Products, x =>
                                                 licensee.Products.Add(new LicenseeProduct
                    {
                        ProductId = new Guid(x)
                    }));
                }

                EnumerableExtensions.ForEach(data.Currencies, x => licensee.Currencies.Add(_repository.Currencies.Single(y => y.Code == x)));
                EnumerableExtensions.ForEach(data.Countries, x => licensee.Countries.Add(_repository.Countries.Single(y => y.Code == x)));
                EnumerableExtensions.ForEach(data.Languages, x => licensee.Cultures.Add(_repository.Cultures.Single(y => y.Code == x)));

                _repository.Licensees.Add(licensee);
                _repository.SaveChanges();

                _eventBus.Publish(new LicenseeCreated
                {
                    Id              = licensee.Id,
                    Name            = licensee.Name,
                    CompanyName     = licensee.CompanyName,
                    Email           = licensee.Email,
                    AffiliateSystem = licensee.AffiliateSystem,
                    ContractStart   = licensee.ContractStart,
                    ContractEnd     = licensee.ContractEnd,
                    Languages       = licensee.Cultures.Select(c => c.Code)
                });

                scope.Complete();

                return(licensee.Id);
            }
        }
Exemple #2
0
 public ValidationResult ValidateCanAdd(AddLicenseeData data)
 {
     return(new AddLicenseeValidator(_repository).Validate(data));
 }