Example #1
0
        public async Task <SmartVoucherCampaignDetailsResponse> GetByIdAsync(Guid campaignId)
        {
            var campaign = await _smartVouchersClient.CampaignsApi.GetByIdAsync(campaignId);

            if (campaign == null)
            {
                throw LykkeApiErrorException.BadRequest(new LykkeApiErrorCode("SmartVoucherCampaignDoesNotExist",
                                                                              "Smart voucher campaign with this id does not exist"));
            }

            #region Filter

            var permissionLevel = await _requestContext.GetPermissionLevelAsync(PermissionType.VoucherManager);

            if (permissionLevel.HasValue && permissionLevel.Value == PermissionLevel.PartnerEdit)
            {
                // filter data for current _requestContext.UserId
                if (campaign.CreatedBy != _requestContext.UserId)
                {
                    throw LykkeApiErrorException.Forbidden(new LykkeApiErrorCode(nameof(HttpStatusCode.Forbidden)));
                }
            }

            #endregion

            var result = _mapper.Map <SmartVoucherCampaignDetailsResponse>(campaign);

            // dictionary by Localization
            var mobileContentsDictionary = new Dictionary <string, MobileContentResponse>();

            foreach (var content in campaign.LocalizedContents)
            {
                if (mobileContentsDictionary.TryGetValue(content.Localization.ToString(), out var existingMobileContent))
                {
                    FillMobileContent(existingMobileContent);
                }
                else
                {
                    Enum.TryParse <MobileLocalization>(content.Localization.ToString(), out var mobileLanguage);

                    var newMobileContent = new MobileContentResponse {
                        MobileLanguage = mobileLanguage
                    };

                    FillMobileContent(newMobileContent);

                    mobileContentsDictionary.TryAdd(content.Localization.ToString(), newMobileContent);
                }

                void FillMobileContent(MobileContentResponse mobileContent)
                {
                    switch (content.ContentType)
                    {
                    case VoucherCampaignContentType.Name:
                        mobileContent.Title   = content.Value;
                        mobileContent.TitleId = content.Id;
                        break;

                    case VoucherCampaignContentType.Description:
                        mobileContent.Description   = content.Value;
                        mobileContent.DescriptionId = content.Id;
                        break;

                    case VoucherCampaignContentType.ImageUrl:
                        mobileContent.ImageId      = content.Id;
                        mobileContent.ImageBlobUrl = content.Value;
                        mobileContent.Image        = new ImageResponse
                        {
                            Id            = content.Image?.Id,
                            RuleContentId = content.Id.ToString(),
                            ImageBlobUrl  = content.Image?.BlobUrl
                        };
                        break;
                    }
                }
            }

            result.MobileContents = mobileContentsDictionary.ToList().OrderBy(x => x.Key).Select(x => x.Value).ToList();

            return(result);
        }
        public async Task <BurnRuleModel> GetBurnRuleByIdAsync([FromQuery] string id)
        {
            if (!Guid.TryParse(id, out var idGuid))
            {
                throw LykkeApiErrorException.BadRequest(ApiErrorCodes.Service.InvalidBurnRuleId);
            }

            var burnRuleResponse = await _campaignsClient.BurnRules.GetByIdAsync(idGuid);

            ThrowIfError(burnRuleResponse.ErrorCode, burnRuleResponse.ErrorMessage);

            var result = _mapper.Map <BurnRuleModel>(burnRuleResponse);

            if (burnRuleResponse.Vertical.HasValue &&
                Enum.TryParse <Vertical>(burnRuleResponse.Vertical.Value.ToString(), out var parsedVertical) &&
                parsedVertical == Vertical.Retail)
            {
                var spendRuleVouchers = await _vouchersClient.Reports.GetSpendRuleVouchersAsync(Guid.Parse(id));

                result.VouchersCount        = spendRuleVouchers.Total;
                result.VouchersInStockCount = spendRuleVouchers.InStock;
            }

            // dictionary by Localization
            var mobileContentsDictionary = new Dictionary <Localization, MobileContentResponse>();

            foreach (var content in burnRuleResponse.BurnRuleContents)
            {
                if (mobileContentsDictionary.TryGetValue(content.Localization, out var existingMobileContent))
                {
                    FillMobileContent(existingMobileContent);
                }
                else
                {
                    Enum.TryParse <MobileLocalization>(content.Localization.ToString(), out var mobileLanguage);

                    var newMobileContent = new MobileContentResponse {
                        MobileLanguage = mobileLanguage
                    };

                    FillMobileContent(newMobileContent);

                    mobileContentsDictionary.TryAdd(content.Localization, newMobileContent);
                }

                void FillMobileContent(MobileContentResponse mobileContent)
                {
                    switch (content.RuleContentType)
                    {
                    case RuleContentType.Title:
                        mobileContent.Title   = content.Value;
                        mobileContent.TitleId = content.Id;
                        break;

                    case RuleContentType.Description:
                        mobileContent.Description   = content.Value;
                        mobileContent.DescriptionId = content.Id;
                        break;

                    case RuleContentType.UrlForPicture:
                        mobileContent.ImageId      = content.Id;
                        mobileContent.ImageBlobUrl = content.Value;
                        mobileContent.Image        = new ImageResponse
                        {
                            Id            = content.Image?.Id,
                            RuleContentId = content.Id.ToString(),
                            ImageBlobUrl  = content.Image?.BlobUrl
                        };
                        break;
                    }
                }
            }

            result.MobileContents = mobileContentsDictionary.ToList().OrderBy(x => x.Key).Select(x => x.Value).ToList();

            return(result);
        }
        public async Task <EarnRuleModel> GetEarnRuleByIdAsync([FromQuery] string id)
        {
            var campaign = await _campaignsClient.Campaigns.GetByIdAsync(id);

            ThrowIfError(campaign.ErrorCode, campaign.ErrorMessage);

            var asset = _settingsService.GetTokenName();

            // order conditions to have optional after first condition
            if (campaign.Conditions.Count > 0)
            {
                var orderedConditions = new List <Lykke.Service.Campaign.Client.Models.Condition.ConditionModel>();
                var specialCondition  = campaign.Conditions.FirstOrDefault(x => x.Type.Equals(ReferToRealEstateBonusType));

                if (specialCondition != null)
                {
                    orderedConditions.Add(specialCondition);
                }

                orderedConditions.AddRange(campaign.Conditions.Where(x => !x.Type.Equals(ReferToRealEstateBonusType)));
            }

            var result = _mapper.Map <EarnRuleModel>(campaign);

            result.Asset = asset;

            // dictionary by Localization
            var mobileContentsDictionary = new Dictionary <Localization, MobileContentResponse>();

            foreach (var content in campaign.Contents)
            {
                if (mobileContentsDictionary.TryGetValue(content.Localization, out var existingMobileContent))
                {
                    FillMobileContent(existingMobileContent);
                }
                else
                {
                    var newMobileContent = new MobileContentResponse
                    {
                        MobileLanguage = content.Localization
                    };

                    FillMobileContent(newMobileContent);

                    mobileContentsDictionary.TryAdd(content.Localization, newMobileContent);
                }

                void FillMobileContent(MobileContentResponse mobileContent)
                {
                    switch (content.RuleContentType)
                    {
                    case RuleContentType.Title:
                        mobileContent.Title   = content.Value;
                        mobileContent.TitleId = content.Id;
                        break;

                    case RuleContentType.Description:
                        mobileContent.Description   = content.Value;
                        mobileContent.DescriptionId = content.Id;
                        break;

                    case RuleContentType.UrlForPicture:
                        mobileContent.ImageId      = content.Id;
                        mobileContent.ImageBlobUrl = content.Value;
                        mobileContent.Image        = new ImageResponse
                        {
                            Id            = content.Image?.Id,
                            RuleContentId = content.Id.ToString(),
                            ImageBlobUrl  = content.Image?.BlobUrl
                        };
                        break;
                    }
                }
            }

            result.MobileContents = mobileContentsDictionary.ToList().OrderBy(x => x.Key).Select(x => x.Value).ToList();

            return(result);
        }