private async Task <PriceCard> UpdateOrCreatePriceCard(CommercePipelineExecutionContext context, PriceBook priceBook, string priceCardName, string displayName = "", string description = "") { var cardId = string.Format("{0}{1}-{2}", CommerceEntity.IdPrefix <PriceCard>(), priceBook.Name, priceCardName); var priceCard = await _findEntityPipeline.Run(new FindEntityArgument(typeof(PriceCard), cardId), context).ConfigureAwait(false) as PriceCard; if (priceCard != null) { return(priceCard); } var priceCardArgument = new AddPriceCardArgument(priceBook, priceCardName) { Description = description, DisplayName = displayName }; var createdPriceCard = await _addPriceCardPipeline.Run(priceCardArgument, context) .ConfigureAwait(false); if (createdPriceCard != null) { priceCard = createdPriceCard; } return(priceCard); }
/// <summary> /// Creates the products card. /// </summary> /// <param name="book">The book.</param> /// <param name="context">The context.</param> private async Task CreateProductsCard(PriceBook book, CommercePipelineExecutionContext context) { context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); var date = DateTimeOffset.UtcNow; // CARD var adventureCard = await _addPriceCardPipeline.Run(new AddPriceCardArgument(book, "Habitat_PriceCard"), context).ConfigureAwait(false); // READY FOR APPROVAL SNAPSHOT adventureCard = await _addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddMinutes(-10))), context).ConfigureAwait(false); var readyForApprovalSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, readyForApprovalSnapshot, new PriceTier("USD", 1, 2000M)), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // CARD FIRST SNAPSHOT adventureCard = await _addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddHours(-1))), context).ConfigureAwait(false); var firstSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 1, 10M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 5, 5M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 10, 1M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 1, 15M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 5, 10M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 10, 5M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("EUR", 1, 1M)), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // DRAFT SNAPSHOT adventureCard = await _addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date)), context).ConfigureAwait(false); var draftSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, draftSnapshot, new PriceTier("USD", 1, 1000M)), context).ConfigureAwait(false); adventureCard = await _addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(adventureCard, draftSnapshot, new Tag("new pricing")), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // CARD SECOND SNAPSHOT adventureCard = await _addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddDays(30))), context).ConfigureAwait(false); var secondSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 1, 7M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 5, 4M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 10, 3M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 1, 6M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 5, 3M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 10, 2M)), context).ConfigureAwait(false); adventureCard = await _addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("EUR", 1, 1M)), context).ConfigureAwait(false); adventureCard = await _addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(adventureCard, secondSnapshot, new Tag("future pricing")), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); readyForApprovalSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().ReadyForApproval)); firstSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); secondSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); await _persistEntityPipeline.Run(new PersistEntityArgument(adventureCard), context).ConfigureAwait(false); }