/// <summary>
        /// Validates a list of card records.
        /// </summary>
        /// <param name="cardFileRecords"></param>
        /// <param name="issuerId"></param>
        /// <returns></returns>
        private Dictionary <string, CardFileRecord> validateCardRecords(List <CardFileRecord> cardFileRecords, int issuerId)
        {
            Dictionary <string, CardFileRecord> validCards    = new Dictionary <string, CardFileRecord>(); //<CardNumber, CardFileRecord>
            Dictionary <string, BranchLookup>   validBranches = new Dictionary <string, BranchLookup>();   //<BranchCode, BranchLookup>
            BranchService branchService = new BranchService();

            #region Loop through all the cards, checking valid branchs, Valid BIN's and duplicates in the file
            foreach (CardFileRecord cardFileRecord in cardFileRecords)
            {
                BranchLookup branchLookup;
                //See if we've already checked the DB for the branch. If it's not in the dictionary then do a read from the DB.
                if (!validBranches.TryGetValue(cardFileRecord.BranchCode, out branchLookup))
                {
                    //check if branch exists in the DB
                    branch Branch = branchService.GetBranchByBranchCode(issuerId, cardFileRecord.BranchCode, BranchStatus.ACTIVE);
                    branchLookup = new BranchLookup(cardFileRecord.BranchCode, 0, false);

                    if (Branch != null)
                    {
                        branchLookup = new BranchLookup(Branch.branch_code, Branch.branch_id, true);
                    }

                    validBranches.Add(cardFileRecord.BranchCode, branchLookup);
                }


                //check if branch exist
                //if not, add a record to rejected file
                if (!branchLookup.IsValid)
                {
                    //RegisterNonExistanceBranch(line, validCard.BranchCode, config);
                }
                else
                {
                    //Check if the card has been added to the dictionary already, if it has dont add it again.
                    if (validCards.ContainsKey(cardFileRecord.PsuedoPAN))
                    {
                        //duplicaterecords++;
                        //duplicatesInFileMessage += validCard.PsuedoPAN + ", ";
                    }
                    else
                    {
                        if (CheckIfIsLicenseCardRecord(cardFileRecord.PsuedoPAN))
                        {
                            cardFileRecord.BranchId = branchLookup.BranchId;
                            validCards.Add(cardFileRecord.PsuedoPAN, cardFileRecord);
                        }
                    }
                }
            }
            #endregion

            #region check card records for duplication in indigo
            //check if card record is in the DB
            List <string> results = fileLoaderDAL.ValidateCardsLoaded(new List <string>(validCards.Keys));

            foreach (string result in results)
            {
                //Because the results coming back are based on the keys, the dictionary should have this key, but checking just incase
                if (validCards.ContainsKey(result))
                {
                    //recored is already in DB
                    validCards.Remove(result);
                    //duplicaterecords++;
                    //duplicatesInDB += string.Format("Card Record is a duplicate in Indigo {0},", result);
                    //fileLoadComments += Environment.NewLine + string.Format("Card Record {0} is a duplicate to a previously loaded record. Record will be excluded".ToUpper() + Environment.NewLine, result);
                }
            }
            #endregion

            return(validCards);
        }
Exemple #2
0
 private branch CheckDBForBranch(int issuerId, string branchCode)
 {
     return(branchService.GetBranchByBranchCode(issuerId, branchCode, BranchStatus.ACTIVE));
 }
        /// <summary>
        /// Validates a list of card records.
        /// </summary>
        /// <param name="cardFileRecords"></param>
        /// <param name="issuerId"></param>
        /// <returns></returns>
        private FileStatus validateCardRecords(List <CardFileRecord> cardFileRecords, int issuerId, out Dictionary <string, CardFileRecord> validCardRecords, ref List <FileCommentsObject> fileComments)
        {
            validCardRecords = new Dictionary <string, CardFileRecord>();
            Dictionary <string, CardFileRecord> validCards    = new Dictionary <string, CardFileRecord>(); //<CardNumber, CardFileRecord>
            Dictionary <string, BranchLookup>   validBranches = new Dictionary <string, BranchLookup>();   //<BranchCode, BranchLookup>
            List <issuer_product> validProducts = new List <issuer_product>();
            BranchService         branchService = new BranchService();

            //get a list of valid bins

            #region Loop through all the cards, checking valid branchs, Valid BIN's and duplicates in the file
            foreach (CardFileRecord cardFileRecord in cardFileRecords)
            {
                BranchLookup branchLookup;
                //See if we've already checked the DB for the branch. If it's not in the dictionary then do a read from the DB.
                if (!validBranches.TryGetValue(cardFileRecord.BranchCode.Trim().ToUpper(), out branchLookup))
                {
                    //check if branch exists in the DB
                    branch Branch = branchService.GetBranchByBranchCode(issuerId, cardFileRecord.BranchCode.Trim().ToUpper(), BranchStatus.ACTIVE);
                    branchLookup = new BranchLookup(cardFileRecord.BranchCode.Trim().ToUpper(), 0, false);

                    if (Branch != null)
                    {
                        branchLookup = new BranchLookup(Branch.branch_code.Trim().ToUpper(), Branch.branch_id, true);
                    }

                    validBranches.Add(cardFileRecord.BranchCode.Trim().ToUpper(), branchLookup);
                }


                //check if branch exist
                //if not, add a record to rejected file
                if (!branchLookup.IsValid)
                {
                    log.Error("No Valid Branch found in Indigo:\t" + branchLookup.BranchCode);
                    fileComments.Add(new FileCommentsObject("No Valid Branch found in Indigo:\t" + branchLookup.BranchCode));
                    return(FileStatus.NO_ACTIVE_BRANCH_FOUND);
                }
                else
                {
                    //Check if the card has been added to the dictionary already, if it has dont add it again.
                    if (validCards.ContainsKey(cardFileRecord.PsuedoPAN.Trim()))
                    {
                        fileComments.Add(new FileCommentsObject("Duplicate card found in card file:\t" + cardFileRecord.PsuedoPAN));
                        log.Error("Duplicate card found in card file:\t" + cardFileRecord.PsuedoPAN);
                        return(FileStatus.DUPLICATE_CARDS_IN_FILE);
                    }
                    else
                    {
                        if (CheckIfIsLicenseCardRecord(cardFileRecord.PAN.Trim()))
                        {
                            //See if we can link this card to a product
                            int lookupTries = 0;
                            int productId   = -1;

                            do
                            {
                                int charCount = 0;
                                foreach (var product in validProducts)
                                {
                                    //Check the actual PAN, not PSUDO pan... Product bin can be up to 9 characters.
                                    if (cardFileRecord.PAN.Trim().StartsWith(product.product_bin_code))
                                    {
                                        //Need to find the bincode with the least amount of characters, as this result may have more than one result.
                                        //E.G. Card number could be 123456-88771199 but the result may contain a record for products with:
                                        // 123456 and 123456999 becuse the search is only on the first 6 characters.
                                        if (product.product_bin_code.Length > charCount)
                                        {
                                            charCount = product.product_bin_code.Length;
                                            productId = product.product_id;
                                        }
                                    }
                                }

                                if (productId == -1)//Call the DB to see if we can get valid product for the user
                                {
                                    var result = fileLoaderDAL.FetchIssuerProduct(cardFileRecord.PsuedoPAN, branchLookup.BranchId);
                                    validProducts.AddRange(result);
                                }

                                lookupTries++;
                            }while (productId == -1 && lookupTries < 3);

                            if (productId > -1)
                            {
                                cardFileRecord.BranchId  = branchLookup.BranchId;
                                cardFileRecord.ProductId = productId;
                                validCards.Add(cardFileRecord.PsuedoPAN.Trim(), cardFileRecord);
                            }
                            else
                            {
                                //TODO; could not find product for card.
                                //throw new Exception("Could not find product for card.");
                                log.Error("Could not find product for card.");
                                fileComments.Add(new FileCommentsObject("Could not find product for card."));
                                return(FileStatus.NO_PRODUCT_FOUND_FOR_CARD);
                            }
                        }
                        else
                        {
                            log.Error("Unlicensed BIN in card file.");
                            fileComments.Add(new FileCommentsObject("Unlicensed BIN in card file."));
                            return(FileStatus.UNLICENSED_BIN);
                        }
                    }
                }
            }
            #endregion

            #region check card records for duplication in indigo
            //check if card record is in the DB
            List <string> results = fileLoaderDAL.ValidateCardsLoaded(new List <string>(validCards.Keys));

            foreach (string result in results)
            {
                CardFileRecord value;
                //Because the results coming back are based on the keys, the dictionary should have this key, but checking just incase
                if (validCards.TryGetValue(result.Trim(), out value))
                {
                    //card is already in DB
                    validCards.Remove(result.Trim());
                    log.Error("Duplicate cards in database.");
                    fileComments.Add(new FileCommentsObject("Duplicate cards in database."));
                    return(FileStatus.DUPLICATE_CARDS_IN_DATABASE);
                }
            }
            #endregion

            validCardRecords = validCards;
            return(FileStatus.VALID_CARDS);
        }