Example #1
0
        /// <summary>
        /// Attempts to award the reward defined by the offer
        /// </summary>
        /// <param name="validatedAgainst">
        /// The constrain by.
        /// </param>
        /// <param name="customer">
        /// The customer.
        /// </param>
        /// <param name="applyConstraints">
        /// Optional parameter indicating whether or not to apply constraints before attempting to award the reward.
        /// Defaults to true.
        /// </param>
        /// <returns>
        ///
        /// The <see cref="Attempt"/>.
        /// </returns>
        internal Attempt <IOfferResult <object, object> > TryToAward(object validatedAgainst, ICustomerBase customer, bool applyConstraints = true)
        {
            var result = new OfferResult <object, object>
            {
                Customer = customer,
                Messages = new List <string>()
            };

            // ensure the offer is valid
            var ensureOffer = this.EnsureValidOffer(result);

            if (!ensureOffer.Success)
            {
                return(ensureOffer);
            }
            result = ensureOffer.Result as OfferResult <object, object>;
            if (result == null)
            {
                throw new NullReferenceException("EnsureValidOffer returned a null Result");
            }

            Attempt <object> awardAttempt;

            if (applyConstraints)
            {
                // apply the constraints
                var constraintAttempt = this.TryApplyConstraints(validatedAgainst, customer);
                result = PopulateConstraintOfferResult(result, constraintAttempt) as OfferResult <object, object>;
                if (result == null)
                {
                    throw new NullReferenceException("PopulateConstraintOfferResult returned null");
                }

                if (!constraintAttempt.Success)
                {
                    return(Attempt <IOfferResult <object, object> > .Fail(result, constraintAttempt.Exception));
                }

                awardAttempt            = OfferProcessor.TryAward(constraintAttempt.Result, customer);
                result.ValidatedAgainst = constraintAttempt.Result;
            }
            else
            {
                awardAttempt            = OfferProcessor.TryAward(validatedAgainst, customer);
                result.ValidatedAgainst = validatedAgainst;
            }

            // get the reward
            result.Award = awardAttempt.Result;
            if (!awardAttempt.Success)
            {
                return(Attempt <IOfferResult <object, object> > .Fail(result, awardAttempt.Exception));
            }

            result.Messages.Add("AWARD - Success");
            return(Attempt <IOfferResult <object, object> > .Succeed(result));
        }
Example #2
0
        /// <summary>
        /// The ensure offer is valid.
        /// </summary>
        /// <param name="customer">
        /// The customer.
        /// </param>
        /// <typeparam name="TConstraint">
        /// The type of constraint
        /// </typeparam>
        /// <typeparam name="TAward">
        /// The type of award
        /// </typeparam>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        internal Attempt <IOfferResult <TConstraint, TAward> > EnsureOfferIsValid <TConstraint, TAward>(ICustomerBase customer)
            where TConstraint :
        class
            where TAward : class
        {
            var result = new OfferResult <object, object>
            {
                Customer = customer,
                Messages = new List <string>()
            };

            return(this.EnsureValidOffer(result).As <TConstraint, TAward>());
        }
Example #3
0
        /// <summary>
        /// Attempts to apply the constraints against the offer.
        /// </summary>
        /// <param name="validatedAgainst">
        /// The validated against.
        /// </param>
        /// <param name="customer">
        /// The customer.
        /// </param>
        /// <typeparam name="TConstraint">
        /// The type of constraint
        /// </typeparam>
        /// <typeparam name="TAward">
        /// The type of offer award
        /// </typeparam>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public Attempt <IOfferResult <TConstraint, TAward> > TryApplyConstraints <TConstraint, TAward>(object validatedAgainst, ICustomerBase customer)
            where TConstraint : class
            where TAward : class
        {
            var result = new OfferResult <object, object>
            {
                Customer = customer,
                Messages = new List <string>()
            };

            // ensure the offer is valid
            var ensureOffer = this.EnsureValidOffer(result);

            if (!ensureOffer.Success)
            {
                return(ensureOffer.As <TConstraint, TAward>());
            }
            result = ensureOffer.Result as OfferResult <object, object>;
            if (result == null)
            {
                throw new NullReferenceException("EnsureValidOffer returned a null Result");
            }

            // attempt to validate against any constraints that have been configured
            var constraintAttempt = TryApplyConstraints(validatedAgainst, customer);

            result = PopulateConstraintOfferResult(result, constraintAttempt) as OfferResult <object, object>;
            if (result == null)
            {
                throw new NullReferenceException("EnsureValidOffer returned a null Result");
            }

            if (!constraintAttempt.Success)
            {
                var exception = new Exception("Offer constraint validation failed.");
                return(Attempt <IOfferResult <object, object> > .Fail(result, exception).As <TConstraint, TAward>());
            }

            result.ValidatedAgainst = validatedAgainst as TConstraint;
            result.Messages.Add("PASSED - Constraint validation");
            return(Attempt <IOfferResult <object, object> > .Succeed(result).As <TConstraint, TAward>());
        }