Ejemplo n.º 1
0
        public override async Task <PriceCard> Run(PriceCardSnapshotArgument arg, CommercePipelineExecutionContext context)
        {
            Condition.Requires(arg).IsNotNull(Name + ": The argument can not be null");
            Condition.Requires(arg.PriceSnapshot).IsNotNull(Name + ": The price snapshot can not be null");
            Condition.Requires(arg.PriceSnapshot.BeginDate).IsNotNull(Name + ": The price snapshot begin date can not be null");
            Condition.Requires(arg.PriceCard).IsNotNull(Name + ": The price card can not be null");
            context.CommerceContext.AddUniqueObjectByType(arg);

            PriceCard card = arg.PriceCard;
            PriceSnapshotComponent snapshot = arg.PriceSnapshot;

            snapshot.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Draft, ""));
            PriceSnapshotComponent           snapshotComponent = card.Snapshots.OrderByDescending(s => s.BeginDate).FirstOrDefault(s => s.IsApproved(context.CommerceContext));
            CommercePipelineExecutionContext executionContext;

            if (snapshotComponent != null && snapshotComponent.BeginDate.CompareTo(snapshot.BeginDate) >= 0)
            {
                executionContext = context;
                CommerceContext commerceContext = context.CommerceContext;
                string          validationError = context.GetPolicy <KnownResultCodes>().ValidationError;
                string          commerceTermKey = "PriceSnapshotCannotBeAddedOrEdited";
                object[]        args            = new object[1]
                {
                    card.FriendlyId
                };
                string defaultMessage = "Price snapshot can not be added to/modified in price card " + card.FriendlyId + " because its begin date is not greater than existing snapshots.";
                executionContext.Abort(await commerceContext.AddMessage(validationError, commerceTermKey, args, defaultMessage), context);
                executionContext = null;
                return(card);
            }
            if (card.Snapshots.Any(s => DateTimeOffset.Compare(s.BeginDate, snapshot.BeginDate) == 0))
            {
                executionContext = context;
                CommerceContext commerceContext = context.CommerceContext;
                string          validationError = context.GetPolicy <KnownResultCodes>().ValidationError;
                string          commerceTermKey = "PriceSnapshotAlreadyExists";
                object[]        args            = new object[1]
                {
                    card.FriendlyId
                };
                string defaultMessage = "Price snapshot with specified date already exists for price card '" + card.FriendlyId + "'.";
                executionContext.Abort(await commerceContext.AddMessage(validationError, commerceTermKey, args, defaultMessage), context);
                executionContext = null;
                return(card);
            }

            snapshot.Id = Guid.NewGuid().ToString("N");
            card.Snapshots.Add(snapshot);
            PriceSnapshotAdded priceSnapshotAdded = new PriceSnapshotAdded(snapshot.Id);

            priceSnapshotAdded.Name = snapshot.Name;
            context.CommerceContext.AddModel(priceSnapshotAdded);
            return(card);
        }
        /// <summary>
        ///     Creates the variants card.
        /// </summary>
        /// <param name="book">The book.</param>
        /// <param name="context">The context.</param>
        private async Task CreateVariantsCard(PriceBook book, CommercePipelineExecutionContext context)
        {
            context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded);

            DateTimeOffset date = DateTimeOffset.UtcNow;

            // VARIANTS CARD
            PriceCard adventureVariantsCard = await _addPriceCardPipeline
                                              .Run(new AddPriceCardArgument(book, "Habitat_VariantsPriceCard"), context)
                                              .ConfigureAwait(false);

            // READY FOR APPROVAL SNAPSHOT
            adventureVariantsCard = await _addPriceSnapshotPipeline
                                    .Run(
                new PriceCardSnapshotArgument(adventureVariantsCard,
                                              new PriceSnapshotComponent(date.AddMinutes(-10))), context)
                                    .ConfigureAwait(false);

            PriceSnapshotComponent readyForApprovalSnapshot =
                adventureVariantsCard.Snapshots.FirstOrDefault(s =>
                                                               s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId,
                                                                           StringComparison.OrdinalIgnoreCase));

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, readyForApprovalSnapshot,
                                                  new PriceTier("USD", 1, 2000M)), context).ConfigureAwait(false);

            context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded);

            // FIRST APPROVED SNAPSHOT
            adventureVariantsCard = await _addPriceSnapshotPipeline
                                    .Run(
                new PriceCardSnapshotArgument(adventureVariantsCard,
                                              new PriceSnapshotComponent(date.AddHours(-1))), context).ConfigureAwait(false);

            PriceSnapshotComponent firstSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s =>
                                                                                                  s.Id.Equals(
                                                                                                      context.CommerceContext
                                                                                                      .GetModel <PriceSnapshotAdded>()
                                                                                                      ?.PriceSnapshotId,
                                                                                                      StringComparison.OrdinalIgnoreCase));

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("USD", 1, 9M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("USD", 5, 6M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("USD", 10, 3M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("CAD", 1, 7M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("CAD", 5, 4M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("CAD", 10, 2M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot,
                                                  new PriceTier("EUR", 1, 2M)), context).ConfigureAwait(false);

            context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded);

            // DRAFT SNAPSHOT
            adventureVariantsCard = await _addPriceSnapshotPipeline
                                    .Run(
                new PriceCardSnapshotArgument(adventureVariantsCard,
                                              new PriceSnapshotComponent(date)), context).ConfigureAwait(false);

            PriceSnapshotComponent draftSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s =>
                                                                                                  s.Id.Equals(
                                                                                                      context.CommerceContext
                                                                                                      .GetModel <PriceSnapshotAdded>()
                                                                                                      ?.PriceSnapshotId,
                                                                                                      StringComparison.OrdinalIgnoreCase));

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, draftSnapshot,
                                                  new PriceTier("USD", 1, 1000M)), context).ConfigureAwait(false);

            context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded);

            // SECOND APPROVED SNAPSHOT
            adventureVariantsCard = await _addPriceSnapshotPipeline
                                    .Run(
                new PriceCardSnapshotArgument(adventureVariantsCard,
                                              new PriceSnapshotComponent(date.AddDays(30))), context).ConfigureAwait(false);

            PriceSnapshotComponent secondSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s =>
                                                                                                   s.Id.Equals(
                                                                                                       context.CommerceContext
                                                                                                       .GetModel <PriceSnapshotAdded>()
                                                                                                       ?.PriceSnapshotId,
                                                                                                       StringComparison.OrdinalIgnoreCase));

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("USD", 1, 8M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("USD", 5, 4M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("USD", 10, 2M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("CAD", 1, 7M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("CAD", 5, 3M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("CAD", 10, 1M)), context).ConfigureAwait(false);

            adventureVariantsCard = await _addPriceTierPipeline
                                    .Run(
                new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot,
                                                  new PriceTier("EUR", 1, 2M)), 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(adventureVariantsCard), context).ConfigureAwait(false);
        }