Skip to content

Commit

Permalink
Add DetectaFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Jul 23, 2023
1 parent af2d177 commit 5434afd
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 19 deletions.
13 changes: 13 additions & 0 deletions BancosBrasileiros.MergeTool/Dto/Bank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ public bool? CreditDocument
[Display(Name = "Legal Cheque")]
public bool LegalCheque { get; set; }

/// <summary>
/// Gets or sets a value indicating whether [detecta flow].
/// </summary>
/// <value><c>true</c> if [detecta flow]; otherwise, <c>false</c>.</value>
[JsonProperty("DetectaFlow")]
[XmlElement("DetectaFLow")]
[Display(Name = "Detecta Flow")]
public bool DetectaFlow { get; set; }

/// <summary>
/// Gets or sets the salary portability.
/// </summary>
Expand Down Expand Up @@ -353,6 +362,7 @@ public bool Equals(Bank other)
&& string.Equals(Network, other.Network, StringComparison.InvariantCultureIgnoreCase)
&& string.Equals(PixType, other.PixType, StringComparison.InvariantCultureIgnoreCase)
&& LegalCheque == other.LegalCheque
&& DetectaFlow == other.DetectaFlow
//&& Equals(Products, other.Products)
&& string.Equals(
SalaryPortability,
Expand Down Expand Up @@ -416,6 +426,7 @@ public override int GetHashCode()
hashCode.Add(PixType ?? string.Empty, StringComparer.InvariantCultureIgnoreCase);
//hashCode.Add(Products);
hashCode.Add(LegalCheque);
hashCode.Add(DetectaFlow);
hashCode.Add(
SalaryPortability ?? string.Empty,
StringComparer.InvariantCultureIgnoreCase
Expand Down Expand Up @@ -464,6 +475,8 @@ public override string ToString()

strBuilder.Append($"Legal cheque: {LegalCheque} | ");

strBuilder.Append($"Detecta FLow: {DetectaFlow} | ");

if (!string.IsNullOrWhiteSpace(SalaryPortability))
strBuilder.Append($"Salary portability: {SalaryPortability} | ");

Expand Down
6 changes: 6 additions & 0 deletions BancosBrasileiros.MergeTool/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ internal static class Constants
/// </summary>
public const string CqlUrl =
"https://www2.nuclea.com.br/SAP/Rela%C3%A7%C3%A3o%20de%20Participantes%20CQL.pdf";

/// <summary>
/// The DetectaFlow URL
/// </summary>
public const string DetectaFlowUrl =
"https://www2.nuclea.com.br/SAP/Rela%C3%A7%C3%A3o%20de%20Participantes%20-%20Detecta%20Flow.pdf";
}
13 changes: 13 additions & 0 deletions BancosBrasileiros.MergeTool/Helpers/Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,23 @@ internal static class Patterns
TimeSpan.FromSeconds(5)
);

/// <summary>
/// The CQL pattern
/// </summary>
public static readonly Regex CqlPattern =
new(
@"^(?<code>\d{1,3})\s(?<nome>.+?)\s(?<ispb>\d{7,8})\s(?<tipo>.+?)$",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromSeconds(5)
);

/// <summary>
/// The detecta flow pattern
/// </summary>
public static readonly Regex DetectaFlowPattern =
new(
@"^(?<code>\d{1,3})\s(?<nome>.+?)\s(?<cnpj>\d{1,2}\.\d{3}\.\d{3}(?:.|\/)\d{4}([-|·|\.|\s]{1,2})\d{2})\s+(?<ispb>\d{7,8})$",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Compiled,
TimeSpan.FromSeconds(5)
);
}
85 changes: 81 additions & 4 deletions BancosBrasileiros.MergeTool/Helpers/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

namespace BancosBrasileiros.MergeTool.Helpers;

using CrispyWaffle.Serialization;
using Dto;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using CrispyWaffle.Serialization;
using Dto;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

/// <summary>
/// Class Reader.
Expand Down Expand Up @@ -56,6 +56,11 @@ internal class Reader
/// </summary>
private int _countingCql;

/// <summary>
/// The counting detecta flow
/// </summary>
private int _countingDetectaFlow;

/// <summary>
/// Downloads the string.
/// </summary>
Expand All @@ -69,6 +74,13 @@ private static string DownloadString(string url)
return content.ReadAsStringAsync().Result;
}

/// <summary>
/// Downloads the and parse PDF.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="system">The system.</param>
/// <param name="callback">The callback.</param>
/// <returns>List&lt;Bank&gt;.</returns>
private static List<Bank> DownloadAndParsePdf(
string url,
string system,
Expand Down Expand Up @@ -598,19 +610,33 @@ private Bank ParseLinePcps(string line)
};
}

/// <summary>
/// Loads the CQL.
/// </summary>
/// <returns>List&lt;Bank&gt;.</returns>
public List<Bank> LoadCql()
{
_countingCql = 0;
return DownloadAndParsePdf(Constants.CqlUrl, "CQL", ParsePageCql);
}

/// <summary>
/// Parses the page CQL.
/// </summary>
/// <param name="page">The page.</param>
/// <returns>IEnumerable&lt;Bank&gt;.</returns>
private IEnumerable<Bank> ParsePageCql(string page)
{
var lines = page.Split("\n");

return lines.Select(ParseLineCql).Where(bank => bank != null).ToList();
}

/// <summary>
/// Parses the line CQL.
/// </summary>
/// <param name="line">The line.</param>
/// <returns>Bank.</returns>
private Bank ParseLineCql(string line)
{
if (!Patterns.CqlPattern.IsMatch(line))
Expand All @@ -630,4 +656,55 @@ private Bank ParseLineCql(string line)
LegalCheque = true
};
}

/// <summary>
/// Loads the detecta flow.
/// </summary>
/// <returns>List&lt;Bank&gt;.</returns>
public List<Bank> LoadDetectaFlow()
{
_countingDetectaFlow = 0;
return DownloadAndParsePdf(Constants.DetectaFlowUrl, "DetectaFlow", ParsePageDetectaFlow);
}

/// <summary>
/// Parses the page detecta flow.
/// </summary>
/// <param name="page">The page.</param>
/// <returns>IEnumerable&lt;Bank&gt;.</returns>
private IEnumerable<Bank> ParsePageDetectaFlow(string page)
{
var lines = page.Split("\n");

return lines.Select(ParseLineDetectaFlow).Where(bank => bank != null).ToList();
}

/// <summary>
/// Parses the line detecta flow.
/// </summary>
/// <param name="line">The line.</param>
/// <returns>Bank.</returns>
private Bank ParseLineDetectaFlow(string line)
{
if (!Patterns.DetectaFlowPattern.IsMatch(line))
return null;

var match = Patterns.DetectaFlowPattern.Match(line);
var code = Convert.ToInt32(match.Groups["code"].Value.Trim());

_countingDetectaFlow++;
if (_countingDetectaFlow != code)
Logger.Log(
$"DetectaFlow | Counting: {_countingDetectaFlow++} | Code: {code}",
ConsoleColor.DarkYellow
);

return new Bank
{
Document = match.Groups["cnpj"].Value.Trim(),
IspbString = match.Groups["ispb"].Value.Trim(),
LongName = match.Groups["nome"].Value.Replace("\"", "").Trim(),
DetectaFlow = true
};
}
}
52 changes: 51 additions & 1 deletion BancosBrasileiros.MergeTool/Helpers/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ public Seeder SeedPcps(IEnumerable<Bank> items)
/// Seeds the CQL.
/// </summary>
/// <param name="items">The items.</param>
public void SeedCql(IEnumerable<Bank> items)
/// <returns>Seeder.</returns>
public Seeder SeedCql(IEnumerable<Bank> items)
{
var found = 0;
var upToDate = 0;
Expand Down Expand Up @@ -747,5 +748,54 @@ public void SeedCql(IEnumerable<Bank> items)
$"\r\nCQL | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n",
ConsoleColor.DarkYellow
);

return this;
}

/// <summary>
/// Seeds the detecta flow.
/// </summary>
/// <param name="items">The items.</param>
public void SeedDetectaFlow(IEnumerable<Bank> items)
{
var found = 0;
var upToDate = 0;
var notFound = 0;

Logger.Log("DetectaFlow\r\n", ConsoleColor.DarkYellow);

foreach (var detectaFlow in items)
{
var bank = _source.SingleOrDefault(b => b.Ispb.Equals(detectaFlow.Ispb));

if (bank == null)
{
Logger.Log(
$"Detecta FLow | Bank not found: {detectaFlow.LongName} | {detectaFlow.Document.Trim()}",
ConsoleColor.DarkRed
);
notFound++;
continue;
}

if (bank.DetectaFlow)
{
Logger.Log(
$"Detecta Flow | Detecta FLow updated: {detectaFlow.LongName}",
ConsoleColor.DarkGreen
);
upToDate++;
continue;
}

bank.DetectaFlow = true;
bank.DateUpdated = DateTimeOffset.UtcNow;
found++;
}

Logger.Log(
$"\r\nDetecta Flow | Found: {found} | Not found: {notFound} | Up to date: {upToDate}\r\n",
ConsoleColor.DarkYellow
);
}
}
5 changes: 5 additions & 0 deletions BancosBrasileiros.MergeTool/Helpers/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ internal enum Source
/// The CQL.
/// </summary>
Cql,

/// <summary>
/// The detecta flow
/// </summary>
DetectaFlow,
}
7 changes: 3 additions & 4 deletions BancosBrasileiros.MergeTool/Helpers/Writer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace BancosBrasileiros.MergeTool.Helpers;
using Dto;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -81,7 +80,7 @@ private static void SaveCsv(IList<Bank> banks)
lines.AddRange(
banks.Select(
bank =>
$"{bank.Compe:000},{bank.Ispb:00000000},{bank.Document},{bank.LongName.Replace(",", "")},{bank.ShortName.Replace(",", "")},{bank.Network},{bank.Type},{bank.PixType},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "" : bank.ChargeStr.ToCamelCase())},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "" : bank.CreditDocumentStr.ToCamelCase())},{(bank.LegalCheque ? "Sim" : "Não")},{bank.SalaryPortability},{(bank.Products == null ? "" : string.Join("; ", bank.Products))},{bank.Url},{bank.DateOperationStarted},{bank.DatePixStarted},{bank.DateRegistered:O},{bank.DateUpdated:O}"
$"{bank.Compe:000},{bank.Ispb:00000000},{bank.Document},{bank.LongName.Replace(",", "")},{bank.ShortName.Replace(",", "")},{bank.Network},{bank.Type},{bank.PixType},{(string.IsNullOrWhiteSpace(bank.ChargeStr) ? "" : bank.ChargeStr.ToCamelCase())},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) ? "" : bank.CreditDocumentStr.ToCamelCase())},{(bank.LegalCheque ? "Sim" : "Não")},{(bank.DetectaFlow ? "Sim" : "Não")},{bank.SalaryPortability},{(bank.Products == null ? "" : string.Join("; ", bank.Products))},{bank.Url},{bank.DateOperationStarted},{bank.DatePixStarted},{bank.DateRegistered:O},{bank.DateUpdated:O}"
)
);

Expand All @@ -105,7 +104,7 @@ private static void SaveMarkdown(IList<Bank> banks)
lines.AddRange(
banks.Select(
bank =>
$"{bank.Compe:000} | {bank.Ispb:00000000} | {bank.Document} | {bank.LongName} | {bank.ShortName} | {(string.IsNullOrWhiteSpace(bank.Network) ? "-" : bank.Network)} | {(string.IsNullOrWhiteSpace(bank.Type) ? "-" : bank.Type)} | {(string.IsNullOrWhiteSpace(bank.PixType) ? "-" : bank.PixType)} | {(string.IsNullOrWhiteSpace(bank.ChargeStr) || !bank.Charge.HasValue ? "-" : (bank.Charge.Value ? "Sim" : "Não"))} | {(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) || !bank.CreditDocument.HasValue ? "-" : (bank.CreditDocument.Value ? "Sim" : "Não"))} | {(bank.LegalCheque ? "Sim" : "Não")} | {(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "-" : bank.SalaryPortability)} | {(bank.Products == null ? "-" : string.Join(",", bank.Products))} | {(string.IsNullOrWhiteSpace(bank.Url) ? "-" : bank.Url)} | {(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "-" : bank.DateOperationStarted)} | {(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "-" : bank.DatePixStarted)} | {bank.DateRegistered:O} | {bank.DateUpdated:O}"
$"{bank.Compe:000} | {bank.Ispb:00000000} | {bank.Document} | {bank.LongName} | {bank.ShortName} | {(string.IsNullOrWhiteSpace(bank.Network) ? "-" : bank.Network)} | {(string.IsNullOrWhiteSpace(bank.Type) ? "-" : bank.Type)} | {(string.IsNullOrWhiteSpace(bank.PixType) ? "-" : bank.PixType)} | {(string.IsNullOrWhiteSpace(bank.ChargeStr) || !bank.Charge.HasValue ? "-" : (bank.Charge.Value ? "Sim" : "Não"))} | {(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) || !bank.CreditDocument.HasValue ? "-" : (bank.CreditDocument.Value ? "Sim" : "Não"))} | {(bank.LegalCheque ? "Sim" : "Não")} | {(bank.DetectaFlow ? "Sim" : "Não")} | {(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "-" : bank.SalaryPortability)} | {(bank.Products == null ? "-" : string.Join(",", bank.Products))} | {(string.IsNullOrWhiteSpace(bank.Url) ? "-" : bank.Url)} | {(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "-" : bank.DateOperationStarted)} | {(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "-" : bank.DatePixStarted)} | {bank.DateRegistered:O} | {bank.DateUpdated:O}"
)
);

Expand All @@ -124,7 +123,7 @@ private static void SaveSql(IList<Bank> banks)
lines.AddRange(
banks.Select(
bank =>
$"{prefix}'{bank.Compe:000}','{bank.Ispb:00000000}','{bank.Document}','{bank.LongName.Replace("'", "''")}','{bank.ShortName}',{(string.IsNullOrWhiteSpace(bank.Type) ? "NULL" : $"'{bank.Type}'")},{(string.IsNullOrWhiteSpace(bank.PixType) ? "NULL" : $"'{bank.PixType}'")},{(string.IsNullOrWhiteSpace(bank.Network) ? "NULL" : $"'{bank.Network}'")},{(string.IsNullOrWhiteSpace(bank.ChargeStr) || !bank.Charge.HasValue ? "NULL" : $"{(bank.Charge.Value ? 1 : 0)}")},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) || !bank.CreditDocument.HasValue ? "NULL" : $"{(bank.CreditDocument.Value ? 1 : 0)}")},'{(bank.LegalCheque ? 1 : 0)}',{(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "NULL" : $"'{bank.SalaryPortability}'")},{(bank.Products == null ? "NULL" : $"'{string.Join(",", bank.Products)}'")},{(string.IsNullOrWhiteSpace(bank.Url) ? "NULL" : $"'{bank.Url}'")},{(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "NULL" : $"'{bank.DateOperationStarted}'")},{(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "NULL" : $"'{bank.DatePixStarted}'")},'{bank.DateRegistered:O}','{bank.DateUpdated:O}');"
$"{prefix}'{bank.Compe:000}','{bank.Ispb:00000000}','{bank.Document}','{bank.LongName.Replace("'", "''")}','{bank.ShortName}',{(string.IsNullOrWhiteSpace(bank.Type) ? "NULL" : $"'{bank.Type}'")},{(string.IsNullOrWhiteSpace(bank.PixType) ? "NULL" : $"'{bank.PixType}'")},{(string.IsNullOrWhiteSpace(bank.Network) ? "NULL" : $"'{bank.Network}'")},{(string.IsNullOrWhiteSpace(bank.ChargeStr) || !bank.Charge.HasValue ? "NULL" : $"{(bank.Charge.Value ? 1 : 0)}")},{(string.IsNullOrWhiteSpace(bank.CreditDocumentStr) || !bank.CreditDocument.HasValue ? "NULL" : $"{(bank.CreditDocument.Value ? 1 : 0)}")},{(bank.LegalCheque ? 1 : 0)},{(bank.DetectaFlow ? 1 : 0)},{(string.IsNullOrWhiteSpace(bank.SalaryPortability) ? "NULL" : $"'{bank.SalaryPortability}'")},{(bank.Products == null ? "NULL" : $"'{string.Join(",", bank.Products)}'")},{(string.IsNullOrWhiteSpace(bank.Url) ? "NULL" : $"'{bank.Url}'")},{(string.IsNullOrWhiteSpace(bank.DateOperationStarted) ? "NULL" : $"'{bank.DateOperationStarted}'")},{(string.IsNullOrWhiteSpace(bank.DatePixStarted) ? "NULL" : $"'{bank.DatePixStarted}'")},'{bank.DateRegistered:O}','{bank.DateUpdated:O}');"
)
);

Expand Down
Loading

0 comments on commit 5434afd

Please sign in to comment.