private DoNotCallInterfaceValidateResult ValidateInsertUpdateCustomerRequest(InsertOrUpdateDoNotCallCustomerRequest request) { var result = new DoNotCallInterfaceValidateResult(); bool valid = ValidateRequestModel(request, result); if (valid) // validate with database { // check item alreayd exist (get id and update date) DoNotCallTransactionInfo info = _doNotCallFacade.GetCustomerTransaction(request.CardNo, request.SubscriptTypeCode); bool isExistingTransaction = info != null && info.TransactionId > 0; if (isExistingTransaction && info.UpdateDate > request.UpdateDate) { result.ErrorMessage = $"Transaction already exists (transaction ID: {info.TransactionId})"; } else if (isExistingTransaction && info.IsBlockAllInfoProduct && request.InformationBlockInfo.BlockProducts.Count > 0) { result.ErrorMessage = $"Cannot add block product to Information block product list. The current setting is block ALL products."; } else if (isExistingTransaction && info.IsBlockAllSalesProduct && request.SalesBlockInfo.BlockProducts.Count > 0) { result.ErrorMessage = $"Cannot add block product to Sales block product list. The current setting is block ALL products."; } else { if (isExistingTransaction) // For Update { result.IsBlockAllInfoProducts = info.IsBlockAllInfoProduct; result.IsBlockAllSalesProducts = info.IsBlockAllSalesProduct; // remove duplicated email if (request.EmailList != null && request.EmailList.Count > 0 && info.Emails.Count > 0) { var removeItemList = new List <BlockEmail>(); foreach (var inputItem in request.EmailList) { var existingItems = info.Emails.Where(x => x.Email.Equals(inputItem.Email, StringComparison.InvariantCultureIgnoreCase)).ToList(); bool itemAlreadyExists = existingItems != null && existingItems.Count > 0; if (itemAlreadyExists) { bool inputItemIsActive = inputItem.IsActive == Constants.FlagY; var activeItem = existingItems.FirstOrDefault(x => !x.IsDeleted); bool haveExistingActiveItem = activeItem != null; bool haveExistingDeletedItem = existingItems.Any(x => x.IsDeleted); bool itemAlreadyActive = haveExistingActiveItem && inputItemIsActive; bool itemAlreadyDeleted = !haveExistingActiveItem && haveExistingDeletedItem && !inputItemIsActive; if (itemAlreadyActive || itemAlreadyDeleted) { removeItemList.Add(inputItem); } else if (haveExistingActiveItem && !inputItemIsActive) // have item to delete { inputItem.Id = activeItem.Id; } } } // remove items request.EmailList = request.EmailList.Except(removeItemList).ToList(); } // remove duplicated phone no if (request.TelephoneList != null && request.TelephoneList.Count > 0 && info.Telephones.Count > 0) { var removeItemList = new List <BlockPhoneNo>(); foreach (var inputItem in request.TelephoneList) { var existingItems = info.Telephones.Where(x => x.PhoneNo.Equals(inputItem.PhoneNo, StringComparison.InvariantCultureIgnoreCase)).ToList(); bool itemAlreadyExists = existingItems != null && existingItems.Count > 0; if (itemAlreadyExists) { bool inputItemIsActive = inputItem.IsActive == Constants.FlagY; var activeItem = existingItems.FirstOrDefault(x => !x.IsDeleted); bool haveExistingActiveItem = activeItem != null; bool haveExistingDeletedItem = existingItems.Any(x => x.IsDeleted); bool itemAlreadyActive = haveExistingActiveItem && inputItemIsActive; bool itemAlreadyDeleted = !haveExistingActiveItem && haveExistingDeletedItem && !inputItemIsActive; if (itemAlreadyActive || itemAlreadyDeleted) { removeItemList.Add(inputItem); } else if (haveExistingActiveItem && !inputItemIsActive) // have item to delete { inputItem.Id = activeItem.Id; } } } // remove items request.TelephoneList = request.TelephoneList.Except(removeItemList).ToList(); } } result.TransactionId = info?.TransactionId; // check subscription type code exisst _commonFacade = new CommonFacade(); string subscriptTypeCode = request.SubscriptTypeCode; int? subTypeId = _commonFacade.GetSubscriptTypeByCode(subscriptTypeCode)?.SubscriptTypeId; if (!subTypeId.HasValue) { result.ErrorMessage = $"Subscript type code {subscriptTypeCode} not found"; } else { // check product exists result.SubscriptionTypeId = subTypeId.Value; ValidateProductCodes(request, result); if (result.IsValid && isExistingTransaction) { // Sales var removeSalesProductList = new List <DoNotCallActivityProductInput>(); foreach (var inputItem in result.ActivityProducts) { var existingItems = info.SalesProducts.Where(x => x.ProductId == inputItem.ProductId).ToList(); bool itemAlreadyExists = existingItems != null && existingItems.Count > 0; if (itemAlreadyExists) { bool inputItemIsActive = inputItem.IsActive == Constants.FlagY; var activeItem = existingItems.FirstOrDefault(x => !x.IsDeleted); bool haveExistingActiveItem = activeItem != null; bool haveExistingDeletedItem = existingItems.Any(x => x.IsDeleted); bool itemAlreadyActive = haveExistingActiveItem && inputItemIsActive; bool itemAlreadyDeleted = !haveExistingActiveItem && haveExistingDeletedItem && !inputItemIsActive; if (itemAlreadyActive || itemAlreadyDeleted) { removeSalesProductList.Add(inputItem); } else if (haveExistingActiveItem && !inputItemIsActive) // have item to delete { inputItem.ProductId = activeItem.ProductId; } } } // Info var removeInfoProductList = new List <DoNotCallActivityProductInput>(); foreach (var inputItem in result.ActivityProducts) { var existingItems = info.InfoProducts.Where(x => x.ProductId == inputItem.ProductId).ToList(); bool itemAlreadyExists = existingItems != null && existingItems.Count > 0; if (itemAlreadyExists) { bool inputItemIsActive = inputItem.IsActive == Constants.FlagY; var activeItem = existingItems.FirstOrDefault(x => !x.IsDeleted); bool haveExistingActiveItem = activeItem != null; bool haveExistingDeletedItem = existingItems.Any(x => x.IsDeleted); bool itemAlreadyActive = haveExistingActiveItem && inputItemIsActive; bool itemAlreadyDeleted = !haveExistingActiveItem && haveExistingDeletedItem && !inputItemIsActive; if (itemAlreadyActive || itemAlreadyDeleted) { removeInfoProductList.Add(inputItem); } else if (haveExistingActiveItem && !inputItemIsActive) // have item to delete { inputItem.ProductId = activeItem.ProductId; } } } // remove items result.ActivityProducts = result.ActivityProducts.Except(removeInfoProductList).ToList(); } } } } result.IsValid = string.IsNullOrWhiteSpace(result.ErrorMessage); return(result); }
private DoNotCallInterfaceValidateResult ValidateInsertUpdatePhoneRequest(InsertOrUpdateDoNotCallPhoneRequest request) { var result = new DoNotCallInterfaceValidateResult(); bool valid = ValidateRequestModel(request, result); if (valid) // validate with database { // check item alreayd exist (get id and update date) if (request.TransactionId != 0) { int transactionId = request.TransactionId; DoNotCallTransactionInfo info = _doNotCallFacade.GetTelephoneTransactionById(transactionId); if (info == null) { result.ErrorMessage = $"Transaction ID: {transactionId} not found."; } else if (info.UpdateDate > request.UpdateDate) { result.ErrorMessage = $"Transaction already updated (Transaction ID: {info.TransactionId})"; } else if (info.IsBlockAllInfoProduct && request.InformationBlockInfo.BlockProducts.Count > 0) { result.ErrorMessage = $"Cannot add block product to Information block product list. The current setting is block ALL products."; } else if (info.IsBlockAllSalesProduct && request.SalesBlockInfo.BlockProducts.Count > 0) { result.ErrorMessage = $"Cannot add block product to Sales block product list. The current setting is block ALL products."; } else { result.IsBlockAllInfoProducts = info.IsBlockAllInfoProduct; result.IsBlockAllSalesProducts = info.IsBlockAllSalesProduct; // remove duplicated email if (request.EmailList != null && request.EmailList.Count > 0 && info.Emails.Count > 0) { request.EmailList.RemoveAll(email => info.Emails .Any(y => y.Email.Equals(email.Email, StringComparison.InvariantCultureIgnoreCase) && y.IsDeleted == email.IsDeleted)); } // remove duplicated phone no if (request.TelephoneList != null && request.TelephoneList.Count > 0 && info.Telephones.Count > 0) { request.TelephoneList.RemoveAll(phoneNo => info.Telephones .Any(y => y.PhoneNo.Equals(phoneNo.PhoneNo, StringComparison.InvariantCultureIgnoreCase) && y.IsDeleted == phoneNo.IsDeleted)); } result.TransactionId = info.TransactionId; // check subscription type code exisst if (!string.IsNullOrWhiteSpace(request.SubscriptTypeCode)) { _commonFacade = new CommonFacade(); string subscriptTypeCode = request.SubscriptTypeCode; int? subTypeId = _commonFacade.GetSubscriptTypeByCode(subscriptTypeCode)?.SubscriptTypeId; if (!subTypeId.HasValue) { result.ErrorMessage = $"Subscript type code {subscriptTypeCode} not found"; } else { // check product exists result.SubscriptionTypeId = subTypeId.Value; } } ValidateProductCodes(request, result); if (result.IsValid) { // check duplicated sales product result.ActivityProducts.RemoveAll(x => x.BlockType == Constants.ActivityProductTypeSales && info.SalesProducts.Any(y => y.ProductId == x.ProductId && x.IsDeleted == y.IsDeleted)); // check duplicated info product result.ActivityProducts.RemoveAll(x => x.BlockType == Constants.ActivityProductTypeInformation && info.InfoProducts.Any(y => y.ProductId == x.ProductId && x.IsDeleted == y.IsDeleted)); } } } else { // check subscription type code exisst if (!string.IsNullOrWhiteSpace(request.SubscriptTypeCode)) { _commonFacade = new CommonFacade(); string subscriptTypeCode = request.SubscriptTypeCode; int? subTypeId = _commonFacade.GetSubscriptTypeByCode(subscriptTypeCode)?.SubscriptTypeId; if (!subTypeId.HasValue) { result.ErrorMessage = $"Subscript type code {subscriptTypeCode} not found"; } else { // check product exists result.SubscriptionTypeId = subTypeId.Value; } } ValidateProductCodes(request, result); } } result.IsValid = string.IsNullOrWhiteSpace(result.ErrorMessage); return(result); }