-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (130 loc) · 4.03 KB
/
index.js
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
//insira o account da loja
var account = "nomedaloja";
//inserir a appkey e apptoken da loija
var appkey = "x-vtex-api-appkey";
var apptoken = "x-vtex-api-apptoken";
var campos = {campoDesejado: 'atributo do campo'};
//colocar as informações do usuario para cadastro ou update
var array = [
{
document: "11111111111",
firstName: "Teste1",
lastName: "Aprovado1",
email: "teste2@teste.com"
},
{
document: "22222222222",
firstName: "Teste2",
lastName: "Aprovado2",
email: "teste2@teste.com"
}
];
//variaveis de retorno
var emailNotFound = [];
var emailFound = [];
var emailUpdate = [];
var userCreate = [];
async function addClient(user) {
let urlUserMasterData = `https://${account}.myvtex.com/api/dataentities/CL/documents`;
const data = {
...user,
...campos
}
try {
await fetch(`${urlUserMasterData}`, {
method: 'PATCH',
headers: {
Accept: "application/vnd.vtex.ds.v10+json",
"x-vtex-api-appkey": `${appkey}`,
"x-vtex-api-apptoken": `${apptoken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data)
});
}
catch (err) {
console.error(err);
}
finally {
userCreate.push(data);
}
}
async function updateClient(user) {
let urlUserMasterData = `https://${account}.myvtex.com/api/dataentities/CL/documents`;
const data = {
...user,
...campos
}
try {
await fetch(`${urlUserMasterData}`, {
method: 'PATCH',
headers: {
Accept: "application/vnd.vtex.ds.v10+json",
"x-vtex-api-appkey": `${appkey}`,
"x-vtex-api-apptoken": `${apptoken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data)
});
}
catch (err) {
console.error(err);
}
finally {
emailUpdate.push(data);
}
}
async function getClient(user, index) {
let urlUserGet = `https://${account}.myvtex.com/api/checkout/pub/profiles?email=${user.email}&_fields=_all`;
try {
const infoUser = await fetch(`${urlUserGet}`, {
method: "GET",
crossDomain: !0,
headers: {
Accept: "application/vnd.vtex.ds.v10+json",
"x-vtex-api-appkey": `${appkey}`,
"x-vtex-api-apptoken": `${apptoken}`,
"Content-Type": "application/json",
}
})
.then(
(response) => {
return response.json();
}
)
.catch(
(error) => {
console.error("error: ", error);
}
)
return infoUser;
}
catch (error) {
console.error("index: " + index + ", email: " + user.email);
console.error("error: ", error);
}
finally {
console.log('Get user success')
}
}
array.forEach(item => item.email = item.email.toLowerCase());
array.map(async(user, index) => {
const response = await getClient(user, index)
let urlUserGet = `https://${account}.myvtex.com/api/checkout/pub/profiles?email=${user.email}&_fields=_all`;
if (response.userProfileId === null) {
addClient(user)
emailNotFound.push({...user,urlUserGet,response});
} else {
updateClient(user)
emailFound.push({...user,urlUserGet,response})
}
});
console.log("|-------------------------------------------------|");
console.info("Emails que existe registro: ", emailFound);
console.log("|-------------------------------------------------|");
console.warn("Emails que não existe registro: ", emailNotFound);
console.log("|-------------------------------------------------|");
console.info("Emails Atualizados: ", emailUpdate);
console.log("|-------------------------------------------------|");
console.info("Usuarios Criados: ", userCreate);
console.log("|-------------------------------------------------|");