public async Task ProcessCoupon(AffiliateCoupon affiliateCoupon) { if (affiliateCoupon == null) { throw new ArgumentNullException(nameof(affiliateCoupon)); } var couponsMatches = await _matchesRepository.GetAllAsync(); var categoriesMatches = await _categoryRepository.GetAllAsync(); var storesMatches = await _storeRepository.GetAllAsync(); var matchedCoupon = couponsMatches.FirstOrDefault(matched => matched.AffiliateProgram == affiliateCoupon.AffiliateProgram && matched.AffiliateCouponId == affiliateCoupon.CouponId); if (matchedCoupon != null) { var coupons = await _couponRepository.GetAllAsync(); var couponToChange = coupons.FirstOrDefault(c => c.CouponId == matchedCoupon.AdvertiseCouponId); UpdateProperties(couponToChange, affiliateCoupon, storesMatches, categoriesMatches, false); await _couponRepository.SaveAsync(couponToChange); return; } var newCoupon = Coupon.Create(); UpdateProperties(newCoupon, affiliateCoupon, storesMatches, categoriesMatches); matchedCoupon = AffiliateCouponMatch.Create(newCoupon, affiliateCoupon); await _matchesRepository.SaveAsync(matchedCoupon); await _couponRepository.SaveAsync(newCoupon); }