Пример #1
0
        public void Mapper_assigns_brand_to_new_template()
        {
            var template = _bonusMapper.MapModelToTemplate(_model);

            template.Info.Brand.Id.Should().Be(_brand.Id);
        }
Пример #2
0
        public TemplateIdentifier AddUpdateTemplate(CreateUpdateTemplate model)
        {
            var validationResult = _bonusQueries.GetValidationResult(model);

            if (validationResult.IsValid == false)
            {
                throw new RegoException(string.Join("/n", validationResult.Errors.Select(failure => failure.ErrorMessage)));
            }

            var template = _bonusMapper.MapModelToTemplate(model);

            template.Info.Brand = _repository.Brands.Single(brand => brand.Id == template.Info.Brand.Id);

            if (template.Id == Guid.Empty)
            {
                template.Id        = Guid.NewGuid();
                template.CreatedOn = DateTimeOffset.Now.ToBrandOffset(template.Info.Brand.TimezoneId);
                template.CreatedBy = _actorInfoProvider.Actor.UserName;

                _repository.Templates.Add(template);
                _repository.SaveChanges();
            }
            else
            {
                var firstTemplateVersion = _repository.Templates.Where(t => t.Id == template.Id).Single(t => t.Version == 0);
                template.CreatedOn = firstTemplateVersion.CreatedOn;
                template.CreatedBy = firstTemplateVersion.CreatedBy;
                template.UpdatedOn = DateTimeOffset.Now.ToBrandOffset(template.Info.Brand.TimezoneId);
                template.UpdatedBy = _actorInfoProvider.Actor.UserName;
                if (template.Status == TemplateStatus.Complete)
                {
                    var bonusesUsingThisTemplate = _bonusQueries.GetBonusesUsingTemplate(template).ToList();

                    template.Version++;
                    _eventBus.Publish(new BonusTemplateUpdated
                    {
                        AggregateId  = template.Id,
                        Description  = template.Info.Description,
                        EventCreated = template.UpdatedOn.Value
                    });

                    _repository.Templates.Add(template);
                    _repository.SaveChanges();

                    foreach (var bonus in bonusesUsingThisTemplate)
                    {
                        bonus.Template = template;
                        UpdateBonus(bonus);
                    }
                }
                else
                {
                    if (template.Notification != null)
                    {
                        template.Status = TemplateStatus.Complete;
                        _eventBus.Publish(new BonusTemplateCreated
                        {
                            AggregateId  = template.Id,
                            Description  = template.Info.Description,
                            EventCreated = template.CreatedOn
                        });
                    }
                    _repository.Templates.Remove(firstTemplateVersion);

                    _repository.Templates.Add(template);
                    _repository.SaveChanges();
                }
            }

            return(new TemplateIdentifier
            {
                Id = template.Id,
                Version = template.Version
            });
        }