-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportMCCDataset.Codeunit.al
142 lines (124 loc) · 4.27 KB
/
ImportMCCDataset.Codeunit.al
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
codeunit 50181 "Import MCC Dataset"
{
var
DatasetUrl: Label 'https://raw.githubusercontent.com/Oleksios/Merchant-Category-Codes/main/With%20groups/mcc.json';
procedure GetDataset()
var
RestClient: HttpClient;
Response: HttpResponseMessage;
Headers: HttpHeaders;
Content: HttpContent;
Result: Text;
Address: Text;
ResultJson: JsonObject;
JObject: JsonObject;
JToken: JsonToken;
JArray: JsonArray;
i: Integer;
ClientId: Text;
ClientName: Text;
begin
if RestClient.Get(DatasetUrl, Response) then begin
Response.Content().ReadAs(Result);
if not Response.IsSuccessStatusCode then
Error(Result);
//ResultJson.ReadFrom(Result);
JToken.ReadFrom(Result);
if JToken.IsArray() then begin
JArray := JToken.AsArray();
for i := 0 to JArray.Count() - 1 do begin
JArray.Get(i, JToken);
JObject := JToken.AsObject();
ProcessMccEntry(JObject);
end;
end;
end else begin
//process error
Error(GetLastErrorText);
end;
end;
local procedure ProcessMccEntry(MccJObject: JsonObject)
var
MCC: Record "Merchant Category Code";
MccGroup: Record "Merchant Category Group";
DescJObject: JsonObject;
GroupJObject: JsonObject;
MccId: Integer;
MaskedCardNumber: Text;
RecordExists: Boolean;
JToken: JsonToken;
JArray: JsonArray;
i: Integer;
begin
MccId := GetTokenValue(MccJObject, 'mcc').AsInteger();
if MCC.Get(MccId) then begin
RecordExists := true
end else begin
MCC.Init();
MCC.Code := MccId;
end;
if MccJObject.Get('fullDescription', JToken) and JToken.IsObject() then begin
DescJObject := JToken.AsObject();
MCC."Description UA" := GetTextValue(DescJObject, 'uk');
MCC."Description EN" := GetTextValue(DescJObject, 'en');
end;
if MccJObject.Get('shortDescription', JToken) and JToken.IsObject() then begin
DescJObject := JToken.AsObject();
MCC."Short Description UA" := GetTextValue(DescJObject, 'uk');
MCC."Short Description EN" := GetTextValue(DescJObject, 'en');
end;
if MccJObject.Get('group', JToken) and JToken.IsObject() then begin
GroupJObject := JToken.AsObject();
MccGroup := ProcessMccGroup(GroupJObject);
MCC."Group Code" := MccGroup.Code;
end;
if RecordExists then
MCC.Modify(true)
else
MCC.Insert(true);
end;
local procedure ProcessMccGroup(GroupJObject: JsonObject) MccGroup: Record "Merchant Category Group"
var
DescJObject: JsonObject;
GroupId: code[10];
MaskedCardNumber: Text;
RecordExists: Boolean;
JToken: JsonToken;
JArray: JsonArray;
i: Integer;
begin
GroupId := GetTokenValue(GroupJObject, 'type').AsText();
if MccGroup.Get(GroupId) then begin
RecordExists := true
end else begin
MccGroup.Init();
MccGroup.Code := GroupId;
end;
if GroupJObject.Get('description', JToken) and JToken.IsObject() then begin
DescJObject := JToken.AsObject();
MccGroup."Description UA" := GetTextValue(DescJObject, 'uk');
MccGroup."Description EN" := GetTextValue(DescJObject, 'en');
end;
if RecordExists then
MccGroup.Modify(true)
else
MccGroup.Insert(true);
end;
local procedure GetTokenValue(JObject: JsonObject; PropertyName: Text): JsonValue
var
JToken: JsonToken;
begin
JObject.get(PropertyName, JToken);
if JToken.IsValue() then
exit(JToken.AsValue());
end;
local procedure GetTextValue(JObject: JsonObject; PropertyName: Text): Text
var
JToken: JsonToken;
JValue: JsonValue;
begin
if JObject.get(PropertyName, JToken) then
if JToken.IsValue() then
exit(JToken.AsValue().AsText());
end;
}