public async Task AddOrUpdateCampaignInCache(CampaignModel campaign)
        {
            var key = GetCampaignKeyFromPattern(campaign.Id);

            var campaignCached = await _db.StringGetAsync(key);

            if (campaignCached.HasValue)
            {
                await DeleteCampaignFromCache(campaign.Id);
            }

            foreach (var type in campaign.Conditions)
            {
                await AddOrUpdateBonusType(type.BonusType.Type, campaign.Id);
            }

            var serialized = JsonConvert.SerializeObject(campaign);

            await _db.StringSetAsync(key, serialized);
        }
        private async Task ProcessCampaignForCustomerAsync(MAVN.Service.BonusEngine.Domain.Models.Campaign campaign,
                                                           CampaignCompletion campaignCompletion, Guid operationId, IReadOnlyDictionary <string, string> data)
        {
            var customerId = campaignCompletion.CustomerId;
            var campaignId = campaign.Id;

            var conditionCompletions = await _conditionCompletionService
                                       .GetConditionCompletionsAsync(customerId, campaignId);

            if (IsCampaignCompleted(conditionCompletions, campaign) == false)
            {
                return;
            }

            var reward = await _bonusCalculatorService.CalculateRewardAmountAsync(campaign, customerId, conditionCompletions);

            var bonusOperation = new BonusOperation
            {
                CustomerId          = customerId,
                CampaignId          = campaignId,
                ExternalOperationId = operationId,
                Reward             = reward,
                TimeStamp          = DateTime.UtcNow,
                BonusOperationType = BonusOperationType.CampaignReward
            };

            if (data.TryGetValue(ReferralId, out string referralId))
            {
                bonusOperation.ReferralId = referralId;
            }

            await _bonusOperationService.AddBonusOperationAsync(bonusOperation);

            _log.Info("Customer completed all condition in campaign.",
                      context: $"operationId: {operationId}; campaignId: {campaignId}; customerId: {customerId}");

            await _campaignCompletionService.IncreaseCompletionCountAsync(campaignCompletion, campaign,
                                                                          conditionCompletions);
        }
 private static bool IsCampaignCompleted(IReadOnlyCollection <ConditionCompletion> conditionCompletions,
                                         MAVN.Service.BonusEngine.Domain.Models.Campaign campaign)
 {
     return(conditionCompletions.Count == campaign.Conditions.Count &&
            conditionCompletions.All(c => c.IsCompleted));
 }
Пример #4
0
        private void CreateConditionTestData()
        {
            BonusType = new BonusType
            {
                Type        = BonusTypeName,
                DisplayName = BonusTypeName
            };

            ConditionModel = new Condition()
            {
                CampaignId      = CampaignId,
                CompletionCount = 1,
                BonusType       = BonusType,
                Id = ConditionId,
                ImmediateReward = 10
            };

            ConditionModels = new List <Condition>()
            {
                ConditionModel
            };

            NewConditionCompletion = new ConditionCompletion
            {
                CustomerId   = CustomerId,
                ConditionId  = ConditionId,
                Id           = ConditionCompletionsId,
                CurrentCount = 0,
                IsCompleted  = false
            };

            ConditionCompletions = new List <ConditionCompletion>();

            NewConditionBonusOperation = new BonusOperation
            {
                CustomerId         = CustomerId,
                Reward             = ConditionModel.ImmediateReward,
                BonusOperationType = BonusOperationType.CampaignReward
            };

            CampaignModel = new CampaignModel()
            {
                Name            = "SignUp Campaign",
                Reward          = 20,
                Id              = CampaignId,
                CompletionCount = 1,
                Conditions      = new List <Condition>()
                {
                    ConditionModel
                }
            };

            CampaignModels = new List <CampaignModel>()
            {
                CampaignModel
            };

            CampaignCompletion = new CampaignCompletion()
            {
                CustomerId = CustomerId,
                CampaignId = CampaignId,
                CampaignCompletionCount = 0
            };

            CampaignCompletions = new List <CampaignCompletion>()
            {
                CampaignCompletion
            };

            NewCampaignBonusOperation = new BonusOperation
            {
                CustomerId         = CustomerId,
                Reward             = CampaignModel.Reward,
                BonusOperationType = BonusOperationType.CampaignReward
            };
        }
Пример #5
0
        public async Task ShouldCompleteStakableCampaignsWhereStakingEnabledOnConditions()
        {
            // Arrange
            var fixtureData = new CampaignServiceBonusProcessingTestsFixture();

            var partnerId       = Guid.NewGuid();
            var secondBonusType = new BonusType
            {
                CreationDate   = DateTime.UtcNow,
                DisplayName    = "secondbonustype",
                IsAvailable    = true,
                IsHidden       = false,
                IsStakeable    = true,
                RewardHasRatio = false,
                Type           = "secondbonustype"
            };

            var campaignId2 = Guid.NewGuid().ToString("D");
            var campaign2   = new CampaignModel
            {
                CompletionCount = 1,
                Id   = campaignId2,
                Name = "SignUp2",

                Conditions = new List <Condition>
                {
                    new Condition
                    {
                        CampaignId      = campaignId2,
                        CompletionCount = 1,
                        Id = Guid.NewGuid().ToString("D"),
                        ImmediateReward = 1,
                        BonusType       = fixtureData.BonusType,
                        HasStaking      = true
                    },
                    new Condition
                    {
                        CampaignId      = campaignId2,
                        CompletionCount = 1,
                        Id = Guid.NewGuid().ToString("D"),
                        ImmediateReward = 1,
                        BonusType       = secondBonusType
                    }
                }
            };

            var campaignId3 = Guid.NewGuid().ToString("D");
            var campaign3   = new CampaignModel
            {
                CompletionCount = 1,
                Id         = campaignId3,
                Name       = "SignUp3",
                Conditions = new List <Condition>
                {
                    new Condition
                    {
                        CampaignId      = campaignId3,
                        CompletionCount = 1,
                        Id = Guid.NewGuid().ToString("D"),
                        ImmediateReward = 1,
                        BonusType       = fixtureData.BonusType,
                        HasStaking      = true
                    },
                    new Condition
                    {
                        CampaignId      = campaignId3,
                        CompletionCount = 1,
                        Id = Guid.NewGuid().ToString("D"),
                        ImmediateReward = 1,
                        BonusType       = secondBonusType
                    }
                }
            };

            fixtureData.CampaignModel.Conditions = new List <Condition>
            {
                fixtureData.ConditionModel,
                new Condition
                {
                    CampaignId      = fixtureData.CampaignModel.Id,
                    CompletionCount = 1,
                    Id = Guid.NewGuid().ToString("D"),
                    ImmediateReward = 1,
                    BonusType       = secondBonusType
                }
            };

            fixtureData.CampaignModels.Add(campaign2);
            fixtureData.CampaignModels.Add(campaign3);

            fixtureData.CampaignCompletions.Add(new CampaignCompletion
            {
                CustomerId = fixtureData.CustomerId,
                CampaignId = campaignId2,
                CampaignCompletionCount = 0,
                IsCompleted             = false,
                Id = Guid.NewGuid().ToString()
            });
            fixtureData.CampaignCompletions.Add(new CampaignCompletion
            {
                CustomerId = fixtureData.CustomerId,
                CampaignId = campaignId3,
                CampaignCompletionCount = 0,
                IsCompleted             = false,
                Id = Guid.NewGuid().ToString()
            });

            fixtureData.SetupAllMocks();

            fixtureData.BonusCalculatorServiceMock.Setup(b =>
                                                         b.CalculateConditionRewardAmountAsync(It.IsAny <Condition>(), It.IsAny <ConditionCompletion>()))
            .ReturnsAsync(10);

            var data = new Dictionary <string, string>()
            {
                { "StakedCampaignId", campaignId3 }
            };

            // Execute
            await fixtureData.CampaignServiceInstance.ProcessEventForCustomerAsync(
                fixtureData.CustomerId,
                null,
                null,
                data,
                fixtureData.BonusTypeName);

            await fixtureData.CampaignServiceInstance.ProcessEventForCustomerAsync(
                fixtureData.CustomerId,
                null,
                null,
                new Dictionary <string, string>(),
                secondBonusType.Type);

            // Assert
            fixtureData.BonusOperationServiceMock.VerifyAll();

            fixtureData.BonusOperationServiceMock.Verify(c =>
                                                         c.AddBonusOperationAsync(It.IsAny <BonusOperation>()), Times.Exactly(3));
        }
Пример #6
0
        public async Task ShouldCompleteCampaign_WhenPartnerIdExistAndAllOtherRequirementsAreFulfilled()
        {
            // Arrange
            var fixtureData = new CampaignServiceBonusProcessingTestsFixture();

            var partnerId  = Guid.NewGuid();
            var locationId = Guid.NewGuid();

            var campaignId2 = Guid.NewGuid().ToString("D");
            var campaign2   = new CampaignModel
            {
                CompletionCount = 2,
                Id         = campaignId2,
                Name       = "SignUp2",
                Conditions = new List <Condition>
                {
                    new Condition
                    {
                        CampaignId      = Guid.NewGuid().ToString("D"),
                        CompletionCount = 3,
                        Id = campaignId2,
                        ImmediateReward = 1,
                        BonusType       = fixtureData.BonusType,
                        PartnerIds      = new Guid[]
                        {
                            partnerId,
                            Guid.NewGuid(),
                            Guid.NewGuid(),
                        }
                    }
                }
            };

            fixtureData.CampaignModels.Add(campaign2);

            fixtureData.CampaignCompletions.Add(new CampaignCompletion
            {
                IsCompleted = true,
                CampaignId  = campaignId2,
                CustomerId  = campaignId2,
                Id          = Guid.NewGuid().ToString("D")
            });

            fixtureData.ConditionCompletions.Add(fixtureData.NewConditionCompletion);

            fixtureData.SetupAllMocks();

            fixtureData.BonusCalculatorServiceMock.Setup(b =>
                                                         b.CalculateConditionRewardAmountAsync(It.IsAny <Condition>(), It.IsAny <ConditionCompletion>()))
            .ReturnsAsync(10);
            // Execute
            await fixtureData.CampaignServiceInstance.ProcessEventForCustomerAsync(
                fixtureData.CustomerId,
                partnerId.ToString(""),
                locationId.ToString(""),
                fixtureData.EventDataEmpty,
                fixtureData.BonusTypeName);

            // Assert
            fixtureData.BonusOperationServiceMock.VerifyAll();

            fixtureData.BonusOperationServiceMock.Verify(c =>
                                                         c.AddBonusOperationAsync(It.IsAny <BonusOperation>()), Times.Exactly(2));

            fixtureData.BonusOperationServiceMock.Verify(x =>
                                                         x.AddBonusOperationAsync(It.Is <BonusOperation>(p => p.Reward == fixtureData.CampaignModel.Reward)));

            fixtureData.BonusOperationServiceMock.Verify(x =>
                                                         x.AddBonusOperationAsync(It.Is <BonusOperation>(p => p.Reward == fixtureData.ConditionModel.ImmediateReward)));
        }