public async Task UpdateAsync([FromBody] SmartVoucherCampaignEditRequest request)
        {
            var result = await _smartVouchersClient.CampaignsApi.UpdateAsync(_mapper.Map <VoucherCampaignEditModel>(request));

            if (result != UpdateVoucherCampaignErrorCodes.None)
            {
                throw LykkeApiErrorException.BadRequest(new LykkeApiErrorCode(result.ToString()));
            }
        }
Exemplo n.º 2
0
        public async Task UpdateAsync([FromBody] SmartVoucherCampaignEditRequest model)
        {
            #region Filter

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

            if (permissionLevel.HasValue && permissionLevel.Value == PermissionLevel.PartnerEdit)
            {
                var existingCampaign = await _smartVouchersClient.CampaignsApi.GetByIdAsync(model.Id);

                // filter data for current _requestContext.UserId
                if (existingCampaign != null &&
                    existingCampaign.CreatedBy != _requestContext.UserId)
                {
                    throw LykkeApiErrorException.Forbidden(new LykkeApiErrorCode(nameof(HttpStatusCode.Forbidden)));
                }
            }

            #endregion

            var campaign = _mapper.Map <VoucherCampaignEditModel>(model);

            var mobileContents = new List <VoucherCampaignContentEditModel>();

            foreach (var mobileContent in model.MobileContents)
            {
                Enum.TryParse <SmartVouchers.Client.Models.Enums.Localization>(mobileContent.MobileLanguage.ToString(), out var mobileLanguage);

                if (!string.IsNullOrEmpty(mobileContent.Title))
                {
                    mobileContents.Add(new VoucherCampaignContentEditModel
                    {
                        Id           = mobileContent.TitleId,
                        ContentType  = VoucherCampaignContentType.Name,
                        Localization = mobileLanguage,
                        Value        = mobileContent.Title
                    });
                }

                mobileContents.Add(new VoucherCampaignContentEditModel
                {
                    Id           = mobileContent.DescriptionId,
                    ContentType  = VoucherCampaignContentType.Description,
                    Localization = mobileLanguage,
                    Value        = string.IsNullOrEmpty(mobileContent.Description) ? null : mobileContent.Description
                });

                mobileContents.Add(new VoucherCampaignContentEditModel
                {
                    Id           = mobileContent.ImageId,
                    ContentType  = VoucherCampaignContentType.ImageUrl,
                    Localization = mobileLanguage,
                    Value        = mobileContent.ImageBlobUrl
                });
            }

            campaign.LocalizedContents = mobileContents;

            UpdateVoucherCampaignErrorCodes response;

            try
            {
                response = await _smartVouchersClient.CampaignsApi.UpdateAsync(campaign);
            }
            catch (ClientApiException exception)
            {
                throw new ValidationApiException(exception.ErrorResponse);
            }

            if (response != UpdateVoucherCampaignErrorCodes.None)
            {
                throw LykkeApiErrorException.BadRequest(new LykkeApiErrorCode(response.ToString()));
            }
        }