/// <summary> /// Updates an existing partner offer. /// </summary> /// <param name="partnerOfferUpdate">The partner offer to update.</param> /// <returns>The updated partner offer.</returns> public async Task <PartnerOffer> UpdateAsync(PartnerOffer partnerOfferUpdate) { partnerOfferUpdate.AssertNotNull(nameof(partnerOfferUpdate)); IList <PartnerOffer> allPartnerOffers = new List <PartnerOffer>(await RetrieveAsync().ConfigureAwait(false)); new PartnerOfferNormalizer().Normalize(partnerOfferUpdate); PartnerOffer existingPartnerOffer = allPartnerOffers.FirstOrDefault(offer => offer.Id == partnerOfferUpdate.Id); if (existingPartnerOffer == null) { throw new PartnerDomainException(ErrorCode.PartnerOfferNotFound, Resources.OfferNotFound); } if (existingPartnerOffer.MicrosoftOfferId != partnerOfferUpdate.MicrosoftOfferId) { // we do not allow changing the Microsoft offer association since there may be existing purchases that purchased the original Microsoft offer throw new PartnerDomainException(ErrorCode.MicrosoftOfferImmutable, Resources.MicrosoftOfferImmutableErrorMessage); } allPartnerOffers[allPartnerOffers.IndexOf(existingPartnerOffer)] = partnerOfferUpdate; await UpdateAsync(allPartnerOffers).ConfigureAwait(false); return(partnerOfferUpdate); }
/// <summary> /// Applies business rules to a partner offer. /// </summary> /// <param name="partnerOffer">The partner offer to normalize.</param> public void Normalize(PartnerOffer partnerOffer) { partnerOffer.AssertNotNull(nameof(partnerOffer)); // ensure the Microsoft offer ID and other required properties are set Guid offerId; if (!Guid.TryParse(partnerOffer.Id, out offerId)) { throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.IdMustBeAValidGUID).AddDetail("Field", "Id"); } if (string.IsNullOrWhiteSpace(partnerOffer.MicrosoftOfferId)) { throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.MicrosoftOfferIdMustBeSet).AddDetail("Field", "MicrosoftOfferId"); } partnerOffer.Title.AssertNotEmpty("Offer title"); if (partnerOffer.Price <= 0) { throw new PartnerDomainException(ErrorCode.InvalidInput, Resources.OfferPriceShouldBeMoreThanZero).AddDetail("Field", "Price"); } // flatten the offer price based on locale decimal settings. partnerOffer.Price = Math.Round(partnerOffer.Price, Resources.Culture.NumberFormat.CurrencyDecimalDigits, MidpointRounding.AwayFromZero); partnerOffer.Features = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Features); partnerOffer.Summary = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Summary); }
/// <summary> /// Applies business rules to a partner offer. /// </summary> /// <param name="partnerOffer">The partner offer to normalize.</param> public void Normalize(PartnerOffer partnerOffer) { partnerOffer.AssertNotNull(nameof(partnerOffer)); // ensure the Microsoft offer ID and other required properties are set Guid offerId; if (!Guid.TryParse(partnerOffer.Id, out offerId)) { throw new PartnerDomainException(ErrorCode.InvalidInput, "Id must be a valid GUID").AddDetail("Field", "Id"); } if (string.IsNullOrWhiteSpace(partnerOffer.MicrosoftOfferId)) { throw new PartnerDomainException(ErrorCode.InvalidInput, "MicrosoftOfferId must be set").AddDetail("Field", "MicrosoftOfferId"); } partnerOffer.Title.AssertNotEmpty("Offer title"); if (partnerOffer.Price <= 0) { throw new PartnerDomainException(ErrorCode.InvalidInput, "Offer price should be more than zero").AddDetail("Field", "Price"); } partnerOffer.Features = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Features); partnerOffer.Summary = PartnerOfferNormalizer.CleanupEmptyEntries(partnerOffer.Summary); }
/// <summary> /// Initializes a new instance of the <see cref="PurchaseLineItemWithOffer"/> class. /// </summary> /// <param name="purchaseLineItem">The purchase line item.</param> /// <param name="partnerOffer">The partner offer.</param> public PurchaseLineItemWithOffer(PurchaseLineItem purchaseLineItem, PartnerOffer partnerOffer) { purchaseLineItem.AssertNotNull(nameof(purchaseLineItem)); partnerOffer.AssertNotNull(nameof(partnerOffer)); this.PurchaseLineItem = purchaseLineItem; this.PartnerOffer = partnerOffer; }
/// <summary> /// Adds a new partner offer to the repository. /// </summary> /// <param name="newPartnerOffer">The partner offer to add.</param> /// <returns>The added partner offer.</returns> public async Task <PartnerOffer> AddAsync(PartnerOffer newPartnerOffer) { newPartnerOffer.AssertNotNull(nameof(newPartnerOffer)); newPartnerOffer.Id = Guid.NewGuid().ToString(); ICollection <PartnerOffer> allPartnerOffers = new List <PartnerOffer>(await RetrieveAsync().ConfigureAwait(false)); new PartnerOfferNormalizer().Normalize(newPartnerOffer); allPartnerOffers.Add(newPartnerOffer); await UpdateAsync(allPartnerOffers).ConfigureAwait(false); return(newPartnerOffer); }