Beispiel #1
0
        public static Transaction ValidateAndCreate(TransactionRawData data, string dateFormat)
        {
            if (data == null)
            {
                ItemValidationException.Throw("Data is null, cannot validate");
            }
            if (data.TransactionId.Length > TRANSACTIONID_MAX_LENGTH)
            {
                ItemValidationException.Throw($"Id is longer than {TRANSACTIONID_MAX_LENGTH}");
            }

            decimal amount;

            // Check decimal is valid
            if (!decimal.TryParse(data.Amount, NumberStyles.Any, CultureInfo.InvariantCulture, out amount))
            {
                ItemValidationException.Throw("Amount is not decimal");
            }

            if (!CurrencyISOCodeInfo.ContainsCurrencyCode(data.CurrencyCode))
            {
                ItemValidationException.Throw("Currency Code is not correct");
            }

            // Date
            var dateResult = DateTime.Now;
            var provider   = CultureInfo.InvariantCulture;

            try
            {
                // Check against format
                dateResult = DateTime.ParseExact(data.TransactionDate, dateFormat, provider);
            }
            catch
            {
                ItemValidationException.Throw("Date is not in the correct format.");
            }

            var status = TransactionStatus.GetStatusCode(data.Status);

            if (string.IsNullOrEmpty(status))
            {
                ItemValidationException.Throw("Status is incorrect");
            }

            return(new Transaction
            {
                TransactionId = data.TransactionId,
                Amount = amount,
                CurrencyCode = data.CurrencyCode,
                TransactionDate = dateResult,
                Status = status
            });
        }
Beispiel #2
0
        public override FileParsingResult ParseTransactionFile()
        {
            var results = new FileParsingResult();

            try
            {
                using (var parser = new TextFieldParser(FilePath))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    while (!parser.EndOfData)
                    {
                        //Process row
                        var fields  = parser.ReadFields();
                        var rawData = new TransactionRawData
                        {
                            TransactionId   = StringHelper.StripQuotes(fields[0]),
                            Amount          = StringHelper.StripQuotes(fields[1]),
                            CurrencyCode    = StringHelper.StripQuotes(fields[2]),
                            TransactionDate = StringHelper.StripQuotes(fields[3]),
                            Status          = StringHelper.StripQuotes(fields[4])
                        };

                        var parsedItem = new Transaction();

                        try
                        {
                            parsedItem = TransactionItemValidator.ValidateAndCreate(rawData, DateFormat());
                        }
                        catch (ItemValidationException ex)
                        {
                            results.InvalidItems.Add(rawData.TransactionId + ", " + ex.Message);
                            continue;
                        }
                        // Add to results
                        results.Results.Add(parsedItem.TransactionId, parsedItem);
                    }
                }
            }
            catch (Exception e)
            {
                results.InvalidItems.Add(e.Message);
            }

            return(results);
        }
Beispiel #3
0
        public override FileParsingResult ParseTransactionFile()
        {
            var results = new FileParsingResult();

            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(FilePath);
                XmlElement xRoot = xDoc.DocumentElement;

                var transactions = xRoot.SelectNodes("/Transactions/Transaction");

                foreach (XmlNode xn in transactions)
                {
                    var rawData = new TransactionRawData();

                    // Get Transaction ID
                    if (xn.Attributes.Count > 0)
                    {
                        XmlNode attr = xn.Attributes.GetNamedItem("id");
                        if (attr != null)
                        {
                            rawData.TransactionId = attr.Value;
                        }
                    }

                    foreach (XmlNode childnode in xn.ChildNodes)
                    {
                        if (childnode.Name == "TransactionDate")
                        {
                            rawData.TransactionDate = StringHelper.StripQuotes(childnode.InnerText);
                        }
                        if (childnode.Name == "Status")
                        {
                            rawData.Status = StringHelper.StripQuotes(childnode.InnerText);
                        }

                        if (childnode.Name == "PaymentDetails")
                        {
                            foreach (XmlNode innerNode in childnode.ChildNodes)
                            {
                                if (innerNode.Name == "Amount")
                                {
                                    rawData.Amount = StringHelper.StripQuotes(innerNode.InnerText);
                                }
                                if (innerNode.Name == "CurrencyCode")
                                {
                                    rawData.CurrencyCode = StringHelper.StripQuotes(innerNode.InnerText);
                                }
                            }
                        }
                    }

                    var parsedItem = new Transaction();

                    try
                    {
                        parsedItem = TransactionItemValidator.ValidateAndCreate(rawData, DateFormat());
                    }
                    catch (ItemValidationException ex)
                    {
                        results.InvalidItems.Add(rawData.TransactionId + ", " + ex.Message);
                        continue;
                    }
                    // Add to results
                    results.Results.Add(parsedItem.TransactionId, parsedItem);
                }
            }
            catch (Exception e)
            {
                results.InvalidItems.Add(e.Message);
            }

            return(results);
        }