Ejemplo n.º 1
0
        public InsertOrUpdateDoNotCallPhoneResponse InsertOrUpdateDoNotCallPhone(InsertOrUpdateDoNotCallPhoneRequest request)
        {
            string methodName = $"{nameof(DoNotCallInterfaceService)}.{nameof(InsertOrUpdateDoNotCallPhone)}";
            var    response   = new InsertOrUpdateDoNotCallPhoneResponse();
            bool   success    = false;
            string detail     = string.Empty;

            try
            {
                _doNotCallFacade = new DoNotCallFacade();
                // Validate input data
                DoNotCallInterfaceValidateResult validateResult = ValidateInsertUpdatePhoneRequest(request);
                if (!validateResult.IsValid)
                {
                    response.ResponseStatusInfo = GetInvalidInputResponse <InsertResponseStatusInfo>(validateResult.ErrorMessage);
                    _logger.DebugFormat("-- XMLResponse --\n{0}", response.SerializeObject());
                    detail = validateResult.ErrorMessage;
                    return(response);
                }

                response.Header = GetResponseHeader(request.Header, methodName);

                // Authenticate user
                if (!ValidateServiceRequest(request.Header))
                {
                    response.ResponseStatusInfo = GetInvalidLoginResponse <InsertResponseStatusInfo>();
                    detail = "Invalid login";
                    return(response);
                }
                _logger.DebugFormat("-- XMLRequest --\n{0}", request.SerializeObject());

                // Map model to insert
                DoNotCallByTelephoneEntity entity = MapTelephoneEntity(request, validateResult);
                // Insert data

                int transactionId = _doNotCallFacade.SavePhone(entity);
                if (transactionId > 0)
                {
                    response.ResponseStatusInfo = GetSuccessResponseStatusInfo <InsertResponseStatusInfo>();
                    response.ResponseStatusInfo.TransactionId = transactionId;
                    success = true;
                }

                return(response);
            }
            catch (Exception ex)
            {
                response.ResponseStatusInfo = GetErrorResponse <InsertResponseStatusInfo>(ex);
                detail = ex.Message;
                return(response);
            }
            finally
            {
                InsertAuditLog(methodName, success, detail ?? response.ResponseStatusInfo.ResponseMessage);
            }
        }
Ejemplo n.º 2
0
        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);
        }