/// <summary> /// Parse the Response file /// </summary> /// <param name="fileName"> /// Name of the file being parsed /// </param> /// <param name="stream"> /// Stream of data capturing the contents /// </param> /// <returns> /// Instance of Response File /// </returns> public OfferRegistrationResponseFile Parse(string fileName, Stream stream) { OfferRegistrationResponseFile responseFile = new OfferRegistrationResponseFile(); string recordType; OfferRegistrationResponseHeader header = null; OfferRegistrationResponseTrailer trailer = null; Collection <OfferRegistrationResponseDetail> detailRecords = new Collection <OfferRegistrationResponseDetail>(); if (stream != null) { using (StreamReader reader = new StreamReader(stream)) { string headerStr = reader.ReadLine(); header = new OfferRegistrationResponseHeader(headerStr); while ((Line = reader.ReadLine()) != null) { recordType = Line.Substring(0, 1); if (recordType == "D") { OfferRegistrationResponseDetail details = new OfferRegistrationResponseDetail(Line); detailRecords.Add(details); } if (recordType == "T") { trailer = new OfferRegistrationResponseTrailer(Line); // should be last line break; } } } } // verify integrity if (trailer.TrailerCount != detailRecords.Count) { Log.Warning("Number of Records suggested by trailer Amex Offer Registration Response file \"{0}\" do not match.", (int)ResultCode.FileMissingExpectedRecord, fileName); } responseFile.Header = header; responseFile.Trailer = trailer; foreach (OfferRegistrationResponseDetail detailRecord in detailRecords) { responseFile.ResponseRecords.Add(detailRecord); } return(responseFile); }
/// <summary> /// Parse the Response file /// </summary> /// <param name="fileName"> /// Name of the file being parsed /// </param> /// <param name="stream"> /// Stream of data capturing the contents /// </param> /// <returns> /// Instance of Response File /// </returns> public OfferRegistrationResponseFile Parse(string fileName, Stream stream) { OfferRegistrationResponseFile responseFile = new OfferRegistrationResponseFile(); OfferRegistrationResponseHeader header = null; OfferRegistrationResponseTrailer trailer = null; Collection <OfferRegistrationResponseDetail> detailRecords = new Collection <OfferRegistrationResponseDetail>(); if (stream != null) { using (StreamReader reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { string recordType = line.Substring(0, 1); switch (recordType) { case "H": header = new OfferRegistrationResponseHeader(line); break; case "D": detailRecords.Add(new OfferRegistrationResponseDetail(line)); break; case "T": trailer = new OfferRegistrationResponseTrailer(line); break; } } } } // verify integrity if (trailer != null && trailer.TrailerCount != detailRecords.Count) { Log.Warning("Number of Records suggested by trailer Amex Offer Registration Response file \"{0}\" do not match.", (int)ResultCode.FileMissingExpectedRecord, fileName); } responseFile.Header = header; responseFile.Trailer = trailer; foreach (OfferRegistrationResponseDetail detailRecord in detailRecords) { responseFile.ResponseRecords.Add(detailRecord); } return(responseFile); }
/// <summary> /// Process the response file /// </summary> /// <returns> /// Async Task Wrapper /// </returns> public virtual async Task <bool> ProcessAsync() { OfferRegistrationResponseFileParser parser = new OfferRegistrationResponseFileParser(Context.Log); OfferRegistrationResponseFile responseFile = parser.Parse(ResponseFileName, ResponseFileStream); bool submissionValid = true; if (responseFile != null) { if (responseFile.Header.ResponseCode == "A") { foreach (OfferRegistrationResponseDetail record in responseFile.ResponseRecords) { Context[Key.PartnerDealId] = record.OfferId; Context[Key.Partner] = Partner.Amex; IDealOperations dealOperations = CommerceOperationsFactory.DealOperations(Context); Guid? discountId = dealOperations.RetrieveDiscountIdFromPartnerDealId(); Context[Key.GlobalDealId] = discountId.Value; SharedDealLogic dealLogic = new SharedDealLogic(Context, CommerceOperationsFactory.DealOperations(Context)); Deal deal = dealLogic.RetrieveDeal(); // for each record - check the status and process accordingly if (record.ResponseCode == "A") { if (record.ActionCode == OfferRegistrationActionCodeType.Add) { // Possible Race condition in this part of the code // By time time we check whether all partners are registered, things could change in DB // this is not a concern right now but we need to figure it out. bool allOtherPartnersRegistered = true; foreach (PartnerDealInfo partnerDealInfo in deal.PartnerDealInfoList) { if (partnerDealInfo.PartnerId != Partner.Amex) { if (partnerDealInfo.PartnerDealRegistrationStatusId != PartnerDealRegistrationStatus.Complete) { allOtherPartnersRegistered = false; break; } } } // now update deal status deal.DealStatusId = DealStatus.PendingAutoLinking; foreach (PartnerDealInfo partnerDealInfo in deal.PartnerDealInfoList) { if (partnerDealInfo.PartnerId == Partner.Amex) { partnerDealInfo.PartnerDealRegistrationStatusId = PartnerDealRegistrationStatus.Complete; } } Context[Key.Deal] = deal; dealOperations.RegisterDeal(); if (allOtherPartnersRegistered) { // schedule autolinking ConcurrentDictionary <string, string> payload = new ConcurrentDictionary <string, string>(); payload[Key.GlobalDealId.ToString()] = deal.GlobalId.ToString(); ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails { JobId = Guid.NewGuid(), JobType = ScheduledJobType.ClaimDiscountForExistingCards, Orchestrated = true, StartTime = DateTime.UtcNow, Payload = payload }; await Scheduler.ScheduleJobAsync(scheduledJobDetails).ConfigureAwait(false); } } else if (record.ActionCode == OfferRegistrationActionCodeType.Update) { // previously registered, and update was successful. foreach (PartnerDealInfo partnerDealInfo in deal.PartnerDealInfoList) { if (partnerDealInfo.PartnerId == Partner.Amex) { partnerDealInfo.PartnerDealRegistrationStatusId = PartnerDealRegistrationStatus.Complete; } } Context[Key.Deal] = deal; dealOperations.RegisterDeal(); // TODO:Tell Deal Server we are done. // ConcurrentDictionary<string, string> payload = new ConcurrentDictionary<string, string>(); // payload[Key.DealId.ToString()] = deal.Id.ToString(); // ScheduledJobDetails scheduledJobDetails = new ScheduledJobDetails // { // JobId = Guid.NewGuid(), // JobType = ScheduledJobType.DiscountActivationJob, // Orchestrated = false, // StartTime = DateTime.UtcNow, // Payload = payload // }; // await Scheduler.ScheduleJobAsync(scheduledJobDetails).ConfigureAwait(false); } } else { Context.Log.Warning("Attempt to register a deal with Amex failed\r\nOffer Id {0}\r\n Reason {1}", (int)ResultCode.SubmissionRejected, record.OfferId, record.ResponseCodeMessage); // update the deal to reflect error foreach (PartnerDealInfo partnerDealInfo in deal.PartnerDealInfoList) { if (partnerDealInfo.PartnerId == Partner.Amex) { partnerDealInfo.PartnerDealRegistrationStatusId = PartnerDealRegistrationStatus.Error; } } Context[Key.Deal] = deal; dealOperations.RegisterDeal(); } } } else { // file submission was rejected. submissionValid = false; } } return(submissionValid); }