public async Task <bool> Handle(AwardRewardPointsCommand request, CancellationToken cancellationToken) { if (request.Order == null) { throw new ArgumentNullException("order"); } var customer = await _customerService.GetCustomerById(request.Order.CustomerId); var currency = await _currencyService.GetCurrencyByCode(request.Order.CustomerCurrencyCode); var amount = await _currencyService.ConvertToPrimaryStoreCurrency(request.Order.OrderTotal - request.Order.OrderShippingInclTax, currency); var points = await _mediator.Send(new CalculateRewardPointsCommand() { Customer = customer, Amount = amount }); if (points <= 0) { return(false); } //add reward points await _rewardPointsService.AddRewardPointsHistory(customer.Id, points, request.Order.StoreId, string.Format(_localizationService.GetResource("RewardPoints.Message.EarnedForOrder"), request.Order.OrderNumber)); request.Order.CalcRewardPoints += points; //assign to order await _orderService.UpdateOrder(request.Order); return(true); }
public async Task <bool> Handle(ReduceRewardPointsCommand request, CancellationToken cancellationToken) { if (request.Order == null) { throw new ArgumentNullException("order"); } var customer = await _customerService.GetCustomerById(request.Order.CustomerId); int points = await _mediator.Send(new CalculateRewardPointsCommand() { Customer = customer, Amount = request.Order.OrderTotal - request.Order.OrderShippingInclTax }); if (points <= 0) { return(false); } if (request.Order.CalcRewardPoints < points) { return(false); } //reduce reward points await _rewardPointsService.AddRewardPointsHistory(customer.Id, -points, request.Order.StoreId, string.Format(_localizationService.GetResource("RewardPoints.Message.ReducedForOrder"), request.Order.OrderNumber)); request.Order.CalcRewardPoints -= points; //assign to order await _orderService.UpdateOrder(request.Order); return(true); }
public async Task <bool> Handle(ReturnBackRedeemedRewardPointsCommand request, CancellationToken cancellationToken) { if (request.Order == null) { throw new ArgumentNullException("customer"); } //were some points redeemed when placing an order? if (request.Order.RedeemedRewardPointsEntry == null) { return(false); } //return back await _rewardPointsService.AddRewardPointsHistory(request.Order.CustomerId, -request.Order.RedeemedRewardPointsEntry.Points, request.Order.StoreId, string.Format(_localizationService.GetResource("RewardPoints.Message.ReturnedForOrder"), request.Order.OrderNumber)); return(true); }
/// <summary> /// Register customer /// </summary> /// <param name="request">Request</param> /// <returns>Result</returns> public virtual async Task <CustomerRegistrationResult> RegisterCustomer(CustomerRegistrationRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Customer == null) { throw new ArgumentException("Can't load current customer"); } var result = new CustomerRegistrationResult(); if (request.Customer.IsSearchEngineAccount()) { result.AddError("Search engine can't be registered"); return(result); } if (request.Customer.IsBackgroundTaskAccount()) { result.AddError("Background task account can't be registered"); return(result); } if (request.Customer.IsRegistered()) { result.AddError("Current customer is already registered"); return(result); } if (String.IsNullOrEmpty(request.Email)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailIsNotProvided")); return(result); } if (!CommonHelper.IsValidEmail(request.Email)) { result.AddError(_localizationService.GetResource("Common.WrongEmail")); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.PasswordIsNotProvided")); return(result); } if (_customerSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameIsNotProvided")); return(result); } } //validate unique user if (await _customerService.GetCustomerByEmail(request.Email) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailAlreadyExists")); return(result); } if (_customerSettings.UsernamesEnabled) { if (await _customerService.GetCustomerByUsername(request.Username) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameAlreadyExists")); return(result); } } //event notification await _mediator.CustomerRegistrationEvent(result, request); //return if exist errors if (result.Errors.Any()) { return(result); } //at this point request is valid request.Customer.Username = request.Username; request.Customer.Email = request.Email; request.Customer.PasswordFormat = request.PasswordFormat; request.Customer.StoreId = request.StoreId; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.Customer.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.Customer.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.Customer.PasswordSalt = saltKey; request.Customer.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _customerSettings.HashedPasswordFormat); } break; default: break; } await _customerService.InsertCustomerPassword(request.Customer); request.Customer.Active = request.IsApproved; await _customerService.UpdateActive(request.Customer); //add to 'Registered' role var registeredRole = await _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Registered); if (registeredRole == null) { throw new GrandException("'Registered' role could not be loaded"); } request.Customer.CustomerRoles.Add(registeredRole); registeredRole.CustomerId = request.Customer.Id; await _customerService.InsertCustomerRoleInCustomer(registeredRole); //remove from 'Guests' role var guestRole = request.Customer.CustomerRoles.FirstOrDefault(cr => cr.SystemName == SystemCustomerRoleNames.Guests); if (guestRole != null) { request.Customer.CustomerRoles.Remove(guestRole); guestRole.CustomerId = request.Customer.Id; await _customerService.DeleteCustomerRoleInCustomer(guestRole); } //Add reward points for customer registration (if enabled) if (_rewardPointsSettings.Enabled && _rewardPointsSettings.PointsForRegistration > 0) { await _rewardPointsService.AddRewardPointsHistory(request.Customer.Id, _rewardPointsSettings.PointsForRegistration, request.StoreId, _localizationService.GetResource("RewardPoints.Message.EarnedForRegistration"), "", 0); } request.Customer.PasswordChangeDateUtc = DateTime.UtcNow; await _customerService.UpdateCustomer(request.Customer); return(result); }