Exemple #1
0
        public async Task <ReferralHotel> ConfirmAsync(string confirmationToken)
        {
            var referralHotelEncrypted = await _referralHotelsRepository.GetByConfirmationTokenAsync(confirmationToken);

            if (referralHotelEncrypted == null)
            {
                throw new ReferralDoesNotExistException();
            }

            if (referralHotelEncrypted.State == ReferralHotelState.Confirmed)
            {
                return(await DecryptAsync(referralHotelEncrypted));
            }

            if (await ConfirmedReferralExistsAsync(referralHotelEncrypted.EmailHash))
            {
                throw new ReferralAlreadyConfirmedException();
            }

            if (ReferralExpired(referralHotelEncrypted))
            {
                referralHotelEncrypted.State = ReferralHotelState.Expired;

                await _referralHotelsRepository.UpdateAsync(referralHotelEncrypted);

                throw new ReferralExpiredException(referralHotelEncrypted.Id);
            }

            referralHotelEncrypted.State = ReferralHotelState.Confirmed;

            referralHotelEncrypted = await _referralHotelsRepository.UpdateAsync(referralHotelEncrypted);

            _log.Info("Referral hotel confirmed", context: $"referralHotelId: {referralHotelEncrypted.Id}");

            return(await DecryptAsync(referralHotelEncrypted));
        }
        public async Task <ReferralHotel> ConfirmReferralHotelAsync(string confirmationToken)
        {
            ReferralHotel confirmReferralHotelAsync = null;

            try
            {
                var referralHotelEncrypted = await _referralHotelsRepository.GetByConfirmationTokenAsync(confirmationToken);

                if (referralHotelEncrypted == null)
                {
                    throw new ReferralDoesNotExistException();
                }

                if (referralHotelEncrypted.State == ReferralHotelState.Used)
                {
                    confirmReferralHotelAsync = await DecryptAsync(referralHotelEncrypted);

                    return(confirmReferralHotelAsync);
                }

                referralHotelEncrypted.State = ReferralHotelState.Used;
                referralHotelEncrypted       = await _referralHotelsRepository.UpdateAsync(referralHotelEncrypted);

                await PublishHotelUsedEvent(referralHotelEncrypted);

                confirmReferralHotelAsync = await DecryptAsync(referralHotelEncrypted);

                _log.Info($"PublishHotelUsedEvent '{confirmationToken}'");

                return(confirmReferralHotelAsync);
            }
            catch (Exception e)
            {
                _log.Error("Demo service failed to process the request.", e);
            }

            return(confirmReferralHotelAsync);
        }