Skip to content

Commit a0af527

Browse files
committed
importation erabliere et contact
1 parent ae87838 commit a0af527

File tree

8 files changed

+98
-63
lines changed

8 files changed

+98
-63
lines changed

erabliereapi/ErabliereList.al

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,10 @@ page 50102 "Erabliere List"
3737

3838
trigger OnAction()
3939
var
40-
ErablieresArray: JsonArray;
41-
ErabliereToken: JsonToken;
4240
ErabliereAPI: CodeUnit "ErabliereAPI";
4341
ErabliereRec: Record "Erablieres";
44-
propVal: Text;
45-
i: Integer;
46-
count: Integer;
4742
begin
48-
ErablieresArray := ErabliereAPI.GetAdminErabliere();
49-
count := ErablieresArray.Count;
50-
Dialog.Open(count);
51-
for i := 0 to ErablieresArray.Count - 1 do begin
52-
53-
ErablieresArray.Get(i, ErabliereToken);
54-
55-
propVal := Json.GetText(ErabliereToken, 'id');
56-
57-
// Si l'érable n'existe pas, on le crée
58-
if not ErabliereRec.Get(propVal) then begin
59-
ErabliereRec.Init();
60-
61-
ErabliereRec.Validate("Erabliere ID", propVal);
62-
63-
propVal := Json.GetText(ErabliereToken, 'nom');
64-
65-
ErabliereRec.Validate("Description", propVal);
66-
67-
ErabliereRec.Insert();
68-
69-
Dialog.Update(i + 1, propVal + ' inséré');
70-
end
71-
else begin
72-
propVal := Json.GetText(ErabliereToken, 'nom');
73-
74-
Dialog.Update(i + 1, propVal + ' existe déjà');
75-
end;
76-
end;
43+
ImportErablieres(ErabliereRec);
7744
Dialog.PrettyMessage('Importation terminée');
7845
end;
7946
}
@@ -86,30 +53,71 @@ page 50102 "Erabliere List"
8653

8754
trigger OnAction()
8855
var
89-
ErablieresArray: JsonArray;
90-
ErabliereToken: JsonToken;
91-
ErabliereAPI: CodeUnit "ErabliereAPI";
92-
ErabliereRec: Record "Erablieres";
93-
propVal: Text;
94-
i: Integer;
95-
count: Integer;
56+
ErabliereRec: Record "Erablieres" temporary;
57+
previewPage: Page "Erabliere List";
9658
begin
97-
ErablieresArray := ErabliereAPI.GetAdminErabliere();
98-
count := ErablieresArray.Count;
99-
Dialog.Open(count);
100-
for i := 0 to count - 1 do begin
101-
Sleep(125);
102-
ErablieresArray.Get(i, ErabliereToken);
103-
propVal := Json.GetText(ErabliereToken, 'nom');
104-
Dialog.Update(i + 1, propVal);
105-
end;
106-
Sleep(625);
107-
Dialog.PrettyMessage('Prévisualisation terminée');
59+
ImportErablieres(ErabliereRec);
60+
if ErabliereRec.FindSet() then begin
61+
Page.Run(Page::"Erabliere List", ErabliereRec);
62+
end
63+
else
64+
Dialog.PrettyMessage('Nothing to import');
10865
end;
10966
}
11067
}
11168
}
11269

70+
local procedure ImportErablieres(var ErabliereRec: Record Erablieres)
71+
var
72+
ErablieresArray: JsonArray;
73+
ErabliereToken: JsonToken;
74+
ErabliereAPI: CodeUnit "ErabliereAPI";
75+
propVal: Text;
76+
i: Integer;
77+
count: Integer;
78+
begin
79+
ErablieresArray := ErabliereAPI.GetAdminErabliere();
80+
count := ErablieresArray.Count;
81+
Dialog.Open(count);
82+
for i := 0 to count - 1 do begin
83+
ErablieresArray.Get(i, ErabliereToken);
84+
85+
GetOrCreateErabliere(ErabliereRec, ErabliereToken, i);
86+
end;
87+
Dialog.Close();
88+
end;
89+
90+
local procedure GetOrCreateErabliere(var ErabliereRec: Record "Erablieres"; var ErabliereToken: JsonToken; var i: Integer)
91+
var
92+
propVal: Text;
93+
begin
94+
propVal := Json.GetText(ErabliereToken, 'id');
95+
96+
// Si l'érable n'existe pas, on le crée
97+
if not ErabliereRec.Get(propVal) then begin
98+
ErabliereRec.Init();
99+
100+
ErabliereRec.Validate("Erabliere ID", propVal);
101+
102+
propVal := Json.GetText(ErabliereToken, 'nom');
103+
104+
ErabliereRec.Validate("Description", propVal);
105+
106+
ErabliereRec.Insert(true);
107+
108+
Dialog.Update(i + 1, propVal + ' inséré');
109+
end
110+
else begin
111+
propVal := Json.GetText(ErabliereToken, 'nom');
112+
113+
ErabliereRec.Validate(Description, propVal);
114+
115+
ErabliereRec.Modify(true);
116+
117+
Dialog.Update(i + 1, propVal + ' existe déjà');
118+
end;
119+
end;
120+
113121
var
114122
Dialog: Codeunit "DynDialog";
115123
Json: Codeunit "Json";

erabliereapi/Pages/ContactPageExt.al

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pageextension 50120 ContactPageExt extends "Contact List"
22
{
33
layout
44
{
5-
// Add changes to page layout here
5+
66
}
77

88
actions
@@ -42,13 +42,15 @@ pageextension 50120 ContactPageExt extends "Contact List"
4242
uniqueName: Text;
4343
i: Integer;
4444
count: Integer;
45+
modified: Boolean;
4546
begin
4647
ContactsArray := ErabliereAPI.GetAdminContact();
4748
count := ContactsArray.Count;
4849
Dialog.Open(count);
4950
for i := 0 to count - 1 do begin
5051
ContactsArray.Get(i, ContactToken);
5152
uniqueName := Json.GetText(ContactToken, 'uniqueName');
53+
modified := false;
5254

5355
if uniqueName.Contains('@') then begin
5456
ContactRec.SetFilter("ErabliereAPI Unique Name", uniqueName);
@@ -57,26 +59,44 @@ pageextension 50120 ContactPageExt extends "Contact List"
5759
SynErabliereAPIContact(ContactRec, ContactToken);
5860
if not Preview then
5961
ContactRec.Modify(true);
60-
Dialog.Update(i + 1, StrSubstNo('Contact %1 was updated', uniqueName));
61-
end
62-
else begin
62+
Dialog.Update(i + 1, StrSubstNo('Contact %1 %2 was updated', ContactRec."No.", uniqueName));
63+
modified := true;
64+
end;
65+
66+
ContactRec.SetFilter("ErabliereAPI Unique Name", '');
67+
ContactRec.SetFilter("E-Mail", Json.GetText(ContactToken, 'email'));
68+
69+
if ContactRec.FindFirst() then begin
70+
SynErabliereAPIContact(ContactRec, ContactToken);
71+
if not Preview then
72+
ContactRec.Modify(true);
73+
Dialog.Update(i + 1, StrSubstNo('Contact %1 %2 updated from an existing customer', ContactRec."No.", uniqueName));
74+
modified := true;
75+
end;
76+
77+
if not modified then begin
6378
ContactRec.Init();
6479
ContactRec.Type := ContactRec.Type::Person;
6580
SynErabliereAPIContact(ContactRec, ContactToken);
6681
ContactRec.CheckDuplicates();
67-
if not Preview then
82+
if not Preview then begin
83+
ContactRec."No." := ContactRec.CreateCustomer();
6884
ContactRec.Insert(true);
69-
Dialog.Update(i + 1, StrSubstNo('Contact %1 was created', uniqueName));
85+
end;
86+
87+
Dialog.Update(i + 1, StrSubstNo('Contact %1 created', ContactRec."No." + ' ' + uniqueName));
7088
end;
7189
end
7290
else begin
7391
Dialog.Update(i + 1, StrSubstNo('Device %1 was not imported as a contact', uniqueName));
7492
end;
7593
end;
7694
if Preview then
77-
Dialog.PrettyConfirm('Do you confirm the import data?')
95+
exit(Dialog.PrettyConfirm('Do you confirm the import data?'))
7896
else
7997
Dialog.PrettyMessage('Importation terminée');
98+
99+
exit(true);
80100
end;
81101

82102
procedure SynErabliereAPIContact(var ContactRec: Record "Contact"; var ContactToken: JsonToken)

erabliereapi/Pages/EAPISetupList.al

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
page 50134 "EAPI Setup List"
22
{
3+
ApplicationArea = All;
34
PageType = List;
4-
UsageCategory = Lists;
5+
UsageCategory = Administration;
56
SourceTable = "EAPI Setup";
67
CardPageId = "EAPI Setup List";
78

erabliereapi/Pages/EAPISetupPage.al

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ page 50132 "EAPI Setup Page"
22
{
33
PageType = Card;
44
ApplicationArea = All;
5-
UsageCategory = Tasks;
5+
UsageCategory = Administration;
66
SourceTable = "EAPI Setup";
7+
CardPageId = "EAPI Setup Page";
78

89
layout
910
{

erabliereapi/Tables/EAPISetup.al

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ table 50125 "EAPI Setup"
7272
{
7373
DataClassification = SystemMetadata;
7474
}
75+
field(26; "Force Token Renewal"; Boolean)
76+
{
77+
DataClassification = CustomerContent;
78+
}
7579
}
7680

7781
keys

erabliereapi/Utils/APIWebService.al

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ codeunit 50126 "API Web Service"
101101
Error('API Web Service Error. Error Read JSON');
102102

103103
TokenResponseText := GetJsonToken(JObjectResult, 'access_token').AsValue().AsText();
104-
TokenExpiry := CurrentDateTime() + GetJsonToken(JObjectResult, 'ext_expires_in').AsValue().AsInteger();
104+
TokenExpiry := CurrentDateTime() + GetJsonToken(JObjectResult, 'ext_expires_in').AsValue().AsInteger() * 1000;
105105

106106
end else
107107
Error('API Web Service Error. Reason: %1 Message: %2', ResponseMessage.ReasonPhrase, ResponseText);

erabliereapi/Utils/DynDialog.al

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ codeunit 50130 DynDialog
55
dialogTemplate: Text;
66
i: Integer;
77
begin
8+
Texts.RemoveRange(1, Texts.Count);
89
for i := 1 to n do begin
910
dialogTemplate += '#' + Format(i) + ' ';
1011
Texts.Add('');

erabliereapi/Utils/ErabliereAPI.al

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ codeunit 50140 ErabliereAPI
2727
begin
2828
APIConfig.FindSet();
2929

30-
Token := HttpAuthUtils.GetAuthenticationToken(APIConfig, true);
30+
Token := HttpAuthUtils.GetAuthenticationToken(APIConfig, APIConfig."Force Token Renewal");
3131

3232
HttpClient.DefaultRequestHeaders.Add('Authorization', 'Bearer ' + Token);
3333

0 commit comments

Comments
 (0)