-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
204 lines (167 loc) · 7.54 KB
/
Program.cs
File metadata and controls
204 lines (167 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using CsvHelper;
using CsvHelper.Configuration;
using Nager.Country;
using RateConverter.Entities;
using RateConverter.Helpers;
using System.Globalization;
using System.IO;
namespace RateConverter
{
internal class Program
{
static void Main(string[] args)
{
string directoryPath = @"";
string fileName = "PTApp_EUR.csv";
string filePath = Path.Combine(directoryPath, fileName);
WriteToCsv(filePath, "EU", "EUR", "c", "13032025");
}
static void WriteToCsv(string filePath, string originCountryCode, string currency, string minorUnit, string validFrom)
{
try
{
if (!File.Exists(filePath))
{
using (File.Create(filePath)) { }
}
var data = ReadFromCsv(filePath);
if (data != null)
{
data = data.Skip(1).ToList();
List<RateOutputModel> outputs = new List<RateOutputModel>();
List<string> unmatchedDestionations = new List<string>();
RateOutputModel rateOutput = null;
foreach (RateInputModel item in data)
{
var zoneName = item.Destination.Split('-')[0].Trim();
var phone_type_key = item.Destination.Contains("Mobile") ? "Mobile" : "Fixed";
var destinationSplit = item.Destination.Split(new[] { '-' }, 2);
string phoneNumberTypeKeyValue = "";
if (item.Destination.Contains("Mobile"))
{
var dt = "";
phoneNumberTypeKeyValue = item.Destination.Split(new[] { '-' }, 2)[1].Trim();
}
else
{
if (item.Destination.Split('-').Length > 1)
{
phoneNumberTypeKeyValue = "Fixed - " + item.Destination.Split('-')[1].Trim();
}
else
{
phoneNumberTypeKeyValue = "Fixed";
}
}
var countryCode = Helper.GetCountryCode(zoneName);
if (countryCode == "")
{
try
{
ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountryByName(zoneName);
if (countryInfo != null)
{
countryCode = countryInfo.Alpha2Code.ToString();
}
else
{
unmatchedDestionations.Add(zoneName);
countryCode = "";
}
}
catch (Exception ex)
{
unmatchedDestionations.Add(zoneName);
countryCode = "";
}
}
rateOutput = new RateOutputModel()
{
originCountryCode = originCountryCode,
currency = currency,
destinationCountryCode = countryCode != null ? countryCode : "",
destinationKey = zoneName,
minorUnit = minorUnit,
validFrom = DateTime.Now.ToShortDateString(),
connectionFee = "0",
rate = item.Rate,
delimitedCode = item.Codes,
destinationAndDescriptionKey = phone_type_key,
phoneNumberTypeKey = phoneNumberTypeKeyValue
};
outputs.Add(rateOutput);
}
var groupedRateList = outputs.GroupBy(r => r.destinationCountryCode);
foreach (var countryCodeRate in groupedRateList)
{
var minFixed = Convert.ToDecimal(countryCodeRate.Where(x => x.destinationAndDescriptionKey == "Fixed").Min(y => y.rate));
var minMobile = Convert.ToDecimal(countryCodeRate.Where(x => x.destinationAndDescriptionKey == "Mobile").Min(y => y.rate));
foreach (var item in countryCodeRate)
{
if(item.destinationCountryCode == "LV")
{
var latvia = "";
}
if(item.destinationAndDescriptionKey == "Fixed" && Convert.ToDecimal(item.rate) == minFixed)
item.phoneNumberTypeKey = "Fixed";
else if(item.destinationAndDescriptionKey == "Mobile" && Convert.ToDecimal(item.rate) == minMobile)
item.phoneNumberTypeKey = "Mobile";
}
}
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = true
};
var newFile = filePath.Split(".")[0] + "-" + originCountryCode + "-" + currency + "-" + validFrom + ".csv";
using (StreamWriter streamWriter = new StreamWriter(newFile))
{
using (CsvWriter csvWriter = new CsvWriter(streamWriter, config))
{
csvWriter.WriteRecords(outputs);
}
}
if (unmatchedDestionations.Count > 0)
{
Console.WriteLine("The following destinations could not be matched:");
foreach (var item in unmatchedDestionations)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
catch (Exception ex)
{
throw;
}
}
static List<RateInputModel> ReadFromCsv(string filepath)
{
try
{
if (File.Exists(filepath))
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (StreamReader streamReader = new StreamReader(filepath))
{
using (CsvReader csvReader = new CsvReader(streamReader, config))
{
var records = csvReader.GetRecords<RateInputModel>().ToList();
return records;
}
}
}
return null;
}
catch (Exception ex)
{
throw;
}
}
}
}