Beispiel #1
0
        public async Task <CustomerBonuses> UpdateAsync(CustomerBonuses customerBonuses)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var entity = await context.CustomerBonuses.SingleOrDefaultAsync(o => o.CustomerId == customerBonuses.CustomerId);

                _mapper.Map(customerBonuses, entity);

                context.Update(entity);

                await context.SaveChangesAsync();

                return(customerBonuses);
            }
        }
Beispiel #2
0
        public async Task <CustomerBonuses> CreateAsync(CustomerBonuses customerBonuses)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var entity = _mapper.Map <CustomerBonusesEntity>(customerBonuses);

                entity.Id = Guid.NewGuid();

                context.CustomerBonuses.Add(entity);

                await context.SaveChangesAsync();

                return(customerBonuses);
            }
        }
Beispiel #3
0
        public async Task UpdateAsync(Guid customerId, Money18 bonusAmount)
        {
            try
            {
                await _sync.WaitAsync();

                var customerBonuses = await _customerBonusesRepository.GetAsync(customerId);

                if (customerBonuses == null)
                {
                    customerBonuses = new CustomerBonuses
                    {
                        CustomerId          = customerId,
                        TotalAwardedBonuses = bonusAmount
                    };

                    await _customerBonusesRepository.CreateAsync(customerBonuses);
                }
                else
                {
                    customerBonuses.TotalAwardedBonuses += bonusAmount;

                    await _customerBonusesRepository.UpdateAsync(customerBonuses);
                }

                var tier = await _tiersService.GetByAmountAsync(customerBonuses.TotalAwardedBonuses);

                if (tier == null)
                {
                    throw new FailedOperationException("No matching tier found.");
                }

                var tierId = await _customerTiersRepository.GetTierByCustomerIdAsync(customerId);

                if (tierId.HasValue)
                {
                    if (tierId.Value == tier.Id)
                    {
                        _log.Info("Customer current tier corresponds to amount.");
                        return;
                    }

                    await _customerTiersRepository.UpdateAsync(customerId, tier.Id);
                }
                else
                {
                    await _customerTiersRepository.InsertAsync(customerId, tier.Id);
                }

                _log.Info("Customer reward tier updated",
                          context: $"customerId: {customerId}; tierId: {tier.Id}");

                var evt = new CustomerTierChangedEvent(customerId, tier.Id);
                await _customerTierChangedPublisher.PublishAsync(evt);

                _log.Info("Customer tier changed event published",
                          context: $"customerId: {customerId}, tierId: {tierId}");
            }
            finally
            {
                _sync.Release();
            }
        }