Exemplo n.º 1
0
        public async Task <ValidIdentityNumber> AddAsync(ValidIdentityNumber validIdentityNumber)
        {
            using (var file = File.Open(Path.Combine(FileDirectory, FileName), FileMode.Append))
                using (var streamWriter = new StreamWriter(file))
                    using (var csvWriter = new CsvWriter(streamWriter))
                    {
                        csvWriter.Configuration.RegisterClassMap <ValidIdentityNumberMap>();
                        csvWriter.WriteRecord(validIdentityNumber);
                        csvWriter.NextRecord();

                        return(await Task.FromResult(validIdentityNumber));
                    }
        }
        public async Task <Tuple <IEnumerable <ValidIdentityNumber>, IEnumerable <InvalidIdentityNumber> > > ValidateAsync(
            List <string> identityNumbers)
        {
            Validate(identityNumbers);

            var validIdentityNumbers   = new List <ValidIdentityNumber>();
            var invalidIdentityNumbers = new List <InvalidIdentityNumber>();
            var tasks = new ConcurrentBag <string>(identityNumbers).Select(async identityNumber =>
            {
                if (string.IsNullOrWhiteSpace(identityNumber))
                {
                    return;
                }

                var reasonsFailed = new List <string>();

                if (!long.TryParse(identityNumber, out var longIdentityNumber))
                {
                    reasonsFailed.Add("Identity Number has invalid format.");
                }

                if (identityNumber.Trim().Length != 13)
                {
                    reasonsFailed.Add("Identity Number has incorrect length.");
                }

                if (reasonsFailed.Any())
                {
                    var invalidIdentityNumber = new InvalidIdentityNumber
                    {
                        IdentityNumber = identityNumber,
                        ReasonsFailed  = string.Join(" | ", reasonsFailed)
                    };

                    await _invalidIdentityNumberRepository.AddAsync(invalidIdentityNumber);

                    invalidIdentityNumbers.Add(invalidIdentityNumber);

                    return;
                }

                int.TryParse(identityNumber.Substring(0, 2), out var twoDigitYear);
                int.TryParse(identityNumber.Substring(2, 2), out var month);
                int.TryParse(identityNumber.Substring(4, 2), out var day);

                var fourDigitYear = CultureInfo.CurrentCulture.Calendar.ToFourDigitYear(twoDigitYear);
                var birthDate     = new DateTime(fourDigitYear, month, day);

                if (birthDate.Year <= 0)
                {
                    reasonsFailed.Add("Year of birth is invalid");
                }

                if (birthDate.Month <= 0)
                {
                    reasonsFailed.Add("Month of birth is invalid");
                }

                if (birthDate.Day <= 0)
                {
                    reasonsFailed.Add("Day of birth is invalid");
                }

                int.TryParse(identityNumber.Substring(6, 4), out var gender);
                int.TryParse(identityNumber.Substring(10, 1), out var citizenship);

                if (!identityNumber.ToString().LuhnCheck())
                {
                    reasonsFailed.Add("Identity number check is invalid");
                }

                if (reasonsFailed.Any())
                {
                    var invalidIdentityNumber = new InvalidIdentityNumber
                    {
                        IdentityNumber = identityNumber,
                        ReasonsFailed  = string.Join(" | ", reasonsFailed)
                    };

                    await _invalidIdentityNumberRepository.AddAsync(invalidIdentityNumber);

                    invalidIdentityNumbers.Add(invalidIdentityNumber);

                    return;
                }

                var validIdentityNumber = new ValidIdentityNumber
                {
                    IdentityNumber = longIdentityNumber,
                    BirthDate      = birthDate,
                    Gender         = gender < 4999 ? "Female" : "Male",
                    Citizenship    = citizenship == 0 ? "SA Citizen" : "Permanent Resident"
                };

                await _validIdentityNumberRepository.AddAsync(validIdentityNumber);

                validIdentityNumbers.Add(validIdentityNumber);
            });

            await Task.WhenAll(tasks);

            return(new Tuple <IEnumerable <ValidIdentityNumber>, IEnumerable <InvalidIdentityNumber> >(validIdentityNumbers,
                                                                                                       invalidIdentityNumbers));
        }