public override async Task <SaveCustomerPreferencesResponse> Handle(SaveCustomerPreferencesRequest request, CancellationToken cancellationToken)
        {
            var optedInPreferences = request.CustomerPreferences
                                     .Where(customerPreference => customerPreference.Value == true);
            var optedOutPreferences = request.CustomerPreferences
                                      .Where(customerPreference => customerPreference.Value == false);
            var invalidPreferences = new List <string>();
            var preferences        = await _cacheProvider.GetPreferences(cancellationToken);

            var customerPreferences = await _customerPreferenceService
                                      .GetCustomerPreferences(request.CustomerId, request.FromDate, cancellationToken);

            var savedCustomerPreferences = new List <CustomerPreference>();

            foreach (var preference in request.CustomerPreferences)
            {
                Preference foundPreference;

                if ((foundPreference = _preferenceService.GetPreference(preferences, preference.Key)) == null)
                {
                    invalidPreferences.Add(preference.Key);
                    continue;
                }

                var customerPreference = _customerPreferenceService.GetCustomerPreference(customerPreferences, foundPreference.Id);

                if (customerPreference == null)
                {
                    customerPreference = new CustomerPreference
                    {
                        CustomerId   = request.CustomerId,
                        PreferenceId = foundPreference.Id
                    }
                }
                ;

                customerPreference.OptInDate       = request.FromDate;
                customerPreference.NextCheckInDate = request.NextCheckInDate;

                savedCustomerPreferences.Add(await _customerPreferenceService
                                             .Save(customerPreference, false, false, cancellationToken));
            }

            if (savedCustomerPreferences.Count > 0)
            {
                await _customerPreferenceService
                .CommitChanges(cancellationToken);
            }

            return(Response.Success <SaveCustomerPreferencesResponse>(savedCustomerPreferences.ToArray(), model => {
                model.InalidPreferences = invalidPreferences.ToArray();
                model.TotalOptedIn = optedInPreferences.Count();
                model.TotalOptedOut = optedOutPreferences.Count();
            }));
        }
    }
Exemplo n.º 2
0
        public override async Task <GetCustomerPreferenceResponse> Handle(GetCustomerPreferencesRequest request, CancellationToken cancellationToken)
        {
            var encryptedCustomer = await Encryption.Encrypt <CustomerDto, Customer>(new CustomerDto { EmailAddress = request.EmailAddress });

            var customer = await _customerService.GetCustomerByEmailAddress(encryptedCustomer.EmailAddress, cancellationToken);

            if (customer == null)
            {
                Response.Failed <GetCustomerPreferenceResponse>(new ValidationFailure(nameof(request.EmailAddress),
                                                                                      $"Customer preferences for '{ request.EmailAddress }' not found"));
            }

            var customerPreferences = await _customerPreferenceService.GetCustomerPreferences(customer.Id, request.ToDate, cancellationToken);

            return(Response.Success <GetCustomerPreferenceResponse>(customerPreferences));
        }