/// <summary> /// Executes job tear down tasks. /// For the current job. once all autolinking taks are successfully done, we do the following: /// 1. Retrieve the deal by id /// 2. Update the deal stauts to : AutoLinkingComplete /// 3. Updat the deal /// 4. Schedule the job to which will tell Deal Server that deal is active /// </summary> /// <param name="executionResult"> /// The result of job startup tasks. /// </param> /// <param name="jobResult"> /// The result of job. /// </param> /// <returns> /// The result of the execution of the tear down tasks. /// </returns> public OrchestratedExecutionResult TearDown(OrchestratedExecutionResult executionResult) { Log.Information("AutoLinking Job Complete for Deal :{0}", DealId); Log.Information("Job Result :{0}", executionResult); if (executionResult == OrchestratedExecutionResult.Success) { CommerceContext context = new CommerceContext("Claim Discount For Existing Cards Job Context", CommerceWorkerConfig.Instance); // get the deal context[Key.GlobalDealId] = DealId; SharedDealLogic sharedDealLogic = new SharedDealLogic(context, CommerceOperationsFactory.DealOperations(context)); Deal deal = sharedDealLogic.RetrieveDeal(); //now upate deal status to mark autolinking complete context[Key.InitialDeal] = new Deal(deal); context[Key.Deal] = deal; deal.DealStatusId = DealStatus.AutoLinkingComplete; context[Key.PreviouslyRegistered] = true; sharedDealLogic.RegisterDeal(); //TODO:schedule the job to tell DS that deal should be activated, but now do it right here context[Key.GlobalDealId] = DealId; deal = sharedDealLogic.RetrieveDeal(); context[Key.InitialDeal] = new Deal(deal); context[Key.Deal] = deal; deal.DealStatusId = DealStatus.Activated; context[Key.PreviouslyRegistered] = true; sharedDealLogic.RegisterDeal(); } return(OrchestratedExecutionResult.Success); }
/// <summary> /// Executes the Claim Deal API invocation. /// </summary> public async Task Execute() { try { SharedUserLogic sharedUserLogic = new SharedUserLogic(Context, CommerceOperationsFactory.UserOperations(Context)); SharedCardLogic sharedCardLogic = new SharedCardLogic(Context, CommerceOperationsFactory.CardOperations(Context)); SharedDealLogic sharedDealLogic = new SharedDealLogic(Context, CommerceOperationsFactory.DealOperations(Context)); ClaimDealConcluder claimDealConcluder = new ClaimDealConcluder(Context); ResultCode extractionResult = ExtractClaimDealPayload(); if (extractionResult == ResultCode.Success) { Deal deal = sharedDealLogic.RetrieveDeal(); Context[Key.Deal] = deal; if (deal != null) { Context[Key.DealDiscountSummary] = deal.DiscountSummary; User user = sharedUserLogic.RetrieveUser(); Context[Key.User] = user; if (user != null) { Card card = sharedCardLogic.RetrieveCard(); Context[Key.Card] = card; if (card != null) { // If the deal and card have common ground, claim the deal for the card. if (MustClaim(deal, card) == true) { Context[Key.MerchantName] = deal.MerchantName; Context.Log.Verbose("Trying to claim the deal for the user with the appropriate partner."); ClaimDealInvoker claimDealInvoker = new ClaimDealInvoker(Context); await claimDealInvoker.Invoke(); } else { Context.Log.Verbose("It is not necessary to claim this deal for this card."); ((ResultSummary)Context[Key.ResultSummary]).SetResultCode(ResultCode.Success); RestResponder.BuildAsynchronousResponse(Context); } } else { claimDealConcluder.Conclude(ResultCode.UnregisteredCard); } } else { claimDealConcluder.Conclude(ResultCode.UnexpectedUnregisteredUser); } } else { claimDealConcluder.Conclude(ResultCode.UnactionableDealId); } } else { claimDealConcluder.Conclude(extractionResult); } } catch (Exception ex) { ((ResultSummary)Context[Key.ResultSummary]).SetResultCode(ResultCode.UnknownError); RestResponder.BuildAsynchronousResponse(Context, ex); } }
/// <summary> /// Executes the Register deal API invocation. /// </summary> public void Execute() { SharedDealLogic sharedDealLogic = new SharedDealLogic(Context, CommerceOperationsFactory.DealOperations(Context)); sharedDealLogic.Execute(); }
/// <summary> /// Ctor for getting discounts /// </summary> /// <param name="context"> /// Commerce Context /// </param> public GetActiveDiscountsExecutor(CommerceContext context) { Context = context; DealOperations = CommerceOperationsFactory.DealOperations(context); }
/// <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); }