Example #1
0
        public async Task <ActionResult <CouponDto> > GetCouponByCodeAsync(string code)
        {
            _logger.LogInformation("----- Get coupon {CouponCode}", code);

            var result = _exceptionTrigger.Process(code);

            if (result.shouldFire)
            {
                throw new Exception($"Exception code received: {code}");
            }

            if (result.configured)
            {
                return(NotFound($"CONFIG: {result.message}"));
            }

            var coupon = await _couponRepository.FindCouponByCodeAsync(code);

            if (coupon is null || coupon.Consumed)
            {
                return(NotFound(coupon == null ? "ERROR: The coupon doesn't exist" : "ERROR: The coupon has been redeemed already"));
            }

            var couponDto = _mapper.Translate(coupon);

            return(Ok(couponDto));
        }
Example #2
0
        public async Task <ActionResult <CouponDto> > GetCouponByCodeAsync(string code)
        {
            var coupon = await _couponRepository.FindCouponByCodeAsync(code);

            if (coupon is null || coupon.Consumed)
            {
                return(NotFound());
            }

            var couponDto = _mapper.Translate(coupon);

            return(couponDto);
        }
        private async Task <IntegrationEvent> ProcessIntegrationEventAsync(OrderStatusChangedToAwaitingCouponValidationIntegrationEvent integrationEvent)
        {
            var coupon = await _couponRepository.FindCouponByCodeAsync(integrationEvent.Code);

            Log.Information("----- Coupon \"{CouponCode}\": {@Coupon}", integrationEvent.Code, coupon);

            if (coupon == null || coupon.Consumed)
            {
                return(new OrderCouponRejectedIntegrationEvent(integrationEvent.OrderId, coupon.Code));
            }

            Log.Information("Consumed coupon: {DiscountCode}", integrationEvent.Code);

            await _couponRepository.UpdateCouponConsumedByCodeAsync(integrationEvent.Code, integrationEvent.OrderId);

            return(new OrderCouponConfirmedIntegrationEvent(integrationEvent.OrderId, coupon.Discount));
        }