Esempio n. 1
0
        public async Task <MemoryStream> DownloadFeedFileAsync(RewardNetworkFeedInformation rnFeedInformation)
        {
            feedFile = string.Format(rnFeedInformation.FeedFileName, DateTime.UtcNow.AddDays(-1).ToString("yyyyMMdd"));
            Log.Info($"Downloading reward network feed file {feedFile} from {rnFeedInformation.Url}");
            MemoryStream      rewardNetworkDataStream = new MemoryStream();
            DefaultSftpClient sftpClient = new DefaultSftpClient(rnFeedInformation.UserName, rnFeedInformation.Password, rnFeedInformation.Url);
            await sftpClient.DownloadFileAsync(feedFile, rewardNetworkDataStream, rnFeedInformation.FeedFolder);

            Log.Info($"Finished downloading reward network feed file {feedFile} from {rnFeedInformation.Url}");

            return(rewardNetworkDataStream);
        }
Esempio n. 2
0
        public async Task ExecuteAsync(ScheduledJobInfo scheduledJobInfo)
        {
            IDictionary <string, string> payload = scheduledJobInfo.JobPayload;

            if (payload == null)
            {
                throw new ArgumentNullException($"VisaMerchantLookupJob {scheduledJobInfo.JobId} has an empty payload");
            }
            if (!payload.ContainsKey(ProviderId))
            {
                throw new ArgumentException(
                          $"Payload for VisaMerchantLookupJob {scheduledJobInfo.JobId} is missing the provider id");
            }

            if (string.IsNullOrWhiteSpace(payload[ProviderId]))
            {
                throw new ArgumentException($"ProviderId is empty in VisaMerchantLookupJob {scheduledJobInfo.JobId}");
            }

            provider = await EarnRepository.Instance.GetProviderAsync(payload[ProviderId]).ConfigureAwait(false);

            if (provider == null)
            {
                throw new Exception($"Provider id {payload[ProviderId]} not found");
            }
            string feedInformation = null;

            if (provider.ExtendedAttributes != null && provider.ExtendedAttributes.ContainsKey(JobConstants.FeedInformation))
            {
                feedInformation = provider.ExtendedAttributes[JobConstants.FeedInformation];
            }
            if (string.IsNullOrEmpty(feedInformation))
            {
                throw new Exception($"Reward network feed information is missing in {provider.Id}");
            }
            RewardNetworkFeedInformation rnFeedInformation = JsonConvert.DeserializeObject <RewardNetworkFeedInformation>(feedInformation);

            if (string.IsNullOrWhiteSpace(rnFeedInformation.Url))
            {
                throw new Exception($"Feed information is missing the reward network url");
            }
            if (string.IsNullOrWhiteSpace(rnFeedInformation.UserName))
            {
                throw new Exception($"Feed information is missing the reward network username");
            }
            if (string.IsNullOrWhiteSpace(rnFeedInformation.Password))
            {
                throw new Exception($"Feed information is missing the reward network password");
            }

            MemoryStream ms = await DownloadFeedFileAsync(rnFeedInformation).ConfigureAwait(false);

            IList <Merchant> lstMerchants = await ParseFeedFileAsync(ms).ConfigureAwait(false);

            if (lstMerchants != null)
            {
                IList <Merchant> lstNewMerchants = await LookForNewMerchantsAsync(provider, lstMerchants).ConfigureAwait(false);

                if (lstNewMerchants.Any())
                {
                    await EnrichMerchantInformationAsync(lstMerchants, rnFeedInformation).ConfigureAwait(false);

                    await EarnRepository.Instance.AddMerchantsInBatchAsync(lstNewMerchants).ConfigureAwait(false);

                    Log.Info("Finished adding reward network merchants to db");
                }
            }
            scheduledJobInfo.JobCompletedTime = DateTime.UtcNow;
        }
Esempio n. 3
0
        public async Task EnrichMerchantInformationAsync(IList <Merchant> lstMerchants, RewardNetworkFeedInformation rnFeedInformation)
        {
            string accessToken = GetAccessToken(rnFeedInformation.AuthTokenUrl, rnFeedInformation.ApiKey);

            if (!string.IsNullOrWhiteSpace(accessToken))
            {
                foreach (var newMerchant in lstMerchants)
                {
                    Uri rewardNetworkUri             = new Uri(string.Format(rnFeedInformation.MerchantInfoUrl, newMerchant.PartnerMerchantId));
                    RewardNetworkMerchant rnMerchant = await GetRestaurantInfoAsync(rewardNetworkUri, accessToken).ConfigureAwait(false);

                    if (rnMerchant?.merchantContent?.photos.Count() > 0)
                    {
                        foreach (string imageFileName in rnMerchant.merchantContent.photos)
                        {
                            if (newMerchant.Images == null)
                            {
                                newMerchant.Images = new List <Image>();
                            }
                            newMerchant.Images.Add(new Image
                            {
                                Url    = string.Format(rnFeedInformation.ImageUrl, imageFileName),
                                Status = ImageStatusType.GoodImage
                            });
                        }
                    }
                    if (rnMerchant?.merchantDetails?.cuisines.Count() > 0 && !newMerchant.ExtendedAttributes.ContainsKey(MerchantConstants.RewardNetworkCuisine))
                    {
                        newMerchant.ExtendedAttributes.Add(MerchantConstants.RewardNetworkCuisine,
                                                           string.Join("|", rnMerchant.merchantDetails.cuisines));
                    }
                }
            }
        }