public async Task<Result<AddUpdateCustomerModel>> AddUpdateCustomer( [FromBody] AddUpdateCustomerModel addUpdateCustomerModel) { if (!Validate(addUpdateCustomerModel)) return null; var customer = await _customerMapper.FromModelAsync(addUpdateCustomerModel, (int) CustomerType.Retail); var sUserId = _userManager.GetUserId(User); int userId; if (int.TryParse(sUserId, out userId)) { customer.IdEditedBy = userId; foreach (var address in customer.ShippingAddresses) { address.IdEditedBy = userId; } customer.ProfileAddress.IdEditedBy = userId; } customer.IdEditedBy = userId; if (customer.Id > 0) { customer = await _customerService.UpdateAsync(customer); } else { customer = await _customerService.InsertAsync(customer); if (customer.StatusCode != (int)CustomerStatus.Suspended && !String.IsNullOrEmpty(customer.Email)) { await _storefrontUserService.SendActivationAsync(customer.Id); } } var toReturn = await _customerMapper.ToModelAsync<AddUpdateCustomerModel>(customer); toReturn.IsConfirmed = addUpdateCustomerModel.IsConfirmed; toReturn.PublicUserId = addUpdateCustomerModel.PublicUserId; await PrepareCustomerNotes(customer, toReturn); if (!string.IsNullOrEmpty(toReturn.Email)) { var dbProductReviewEmailEnabled = !await _notificationService.IsEmailUnsubscribedAsync(EmailConstants.ProductReviewIdNewsletter, toReturn.Email); if (addUpdateCustomerModel.ProductReviewEmailEnabled && !dbProductReviewEmailEnabled) { await _notificationService.UpdateUnsubscribeEmailAsync(EmailConstants.ProductReviewIdNewsletter, toReturn.Email, false); } if (!addUpdateCustomerModel.ProductReviewEmailEnabled && dbProductReviewEmailEnabled) { await _notificationService.UpdateUnsubscribeEmailAsync(EmailConstants.ProductReviewIdNewsletter, toReturn.Email, true); } } toReturn.ProductReviewEmailEnabled = addUpdateCustomerModel.ProductReviewEmailEnabled; toReturn.ForReview = (await _customerService.GetCustomersForReviewAsync(new int[] { toReturn.Id })).Any(); return toReturn; }
public async Task PrepareCustomerNotes(CustomerDynamic dynamic, AddUpdateCustomerModel model) { var noteIds = dynamic.CustomerNotes.Where(x => x.IdEditedBy.HasValue).Select(x => x.IdEditedBy.Value).ToList(); noteIds.AddRange(dynamic.CustomerNotes.Where(x => x.IdAddedBy.HasValue).Select(x => x.IdAddedBy.Value).ToList()); var adminProfileCondition = new AdminProfileQuery().IdInRange(noteIds); var adminProfiles = await _adminProfileService.QueryAsync(adminProfileCondition); foreach (var customerNote in model.CustomerNotes) { customerNote.EditedBy = adminProfiles.SingleOrDefault( y => y.Id == dynamic.CustomerNotes.Single(z => z.Id == customerNote.Id).IdEditedBy)? .AgentId; customerNote.AddedBy = adminProfiles.SingleOrDefault( y => y.Id == dynamic.CustomerNotes.Single(z => z.Id == customerNote.Id).IdAddedBy)? .AgentId; } model.CustomerNotes = model.CustomerNotes.OrderByDescending(x => x.DateEdited).ToList(); }