-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderHook.js
128 lines (116 loc) · 3.56 KB
/
orderHook.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
const fetch = require('node-fetch');
const AWS = require('aws-sdk');
AWS.config.update({
region: 'us-east-2'
});
// Instância DynamoDB
const dynamodb = new AWS.DynamoDB.DocumentClient();
// DynamoDB Table
const dynamoTableName = "pedidos";
// Rotas
const order = "/order";
const order_created = "/order/new";
const get_order = "/order/getuserdata";
// Tratamento de Rotas
exports.handler = async function(event){
console.log('Request de Evento', event);
let response;
switch(true){
// Checa se as rotas estão funcionando
case event.httpMethod === 'GET' && event.path === order:
const body = "OK";
response = buildResponse(200, body);
break;
case event.httpMethod === 'POST' && event.path === order:
response = await createOrder(JSON.parse(event.body));
break;
case event.httpMethod === 'POST' && event.path === order_created:
response = newOrder(JSON.parse(event.body));
break;
case event.httpMethod === 'POST' && event.path === get_order:
response = newOrder(JSON.parse(event.body));
break;
}
return response;
};
async function createOrder(requestContent){
const data = {
"orderId": requestContent.OrderId,
"state": requestContent.State,
"last_change": requestContent.LastChange
};
const params = {
TableName: dynamoTableName,
Item: data
};
return await dynamodb.put(params).promise().then(()=>{
const body = {
Operation: "Cadastro de Pedido",
Message: "Salvo com Sucesso :D",
Order: data
};
return newOrder(data);
},
(error) =>{
console.error("Incapaz de armazenar Order", error);
});
}
async function newOrder(requestBody){
return await getOrderDetails(requestBody);
}
async function getOrderDetails(requestBody){
const order_data = requestBody.orderId.toString();
let email = '';
const url = `https://hiringcoders202128.vtexcommercestable.com.br/api/oms/pvt/orders/${order_data}/conversation-message`;
const options = {
method: 'GET',
headers: {
Accept: '*/*',
'X-VTEX-API-AppKey': 'vtexappkey-hiringcoders202128-OLBOEK',
'X-VTEX-API-AppToken': 'YMYOVCEOIZAOCZUMDOWWRDDCWTYTCEOUUQMBONEHQJVYQYLNWKJKSZMVIWRCEKMDLFTLMTYQKOGHHFMYILXSVECVDRSSWCDGXRMKEZACMSADTFNDJSUSMUIHCYIVBFUH'
}
};
await fetch(url, options)
.then(res => res.json())
.then(json => {
email = json[0].to[0].email;
})
.catch(err => console.error('error:' + err));
return await modifyLead(email);
}
async function modifyLead(email){
const today = new Date();
const date = today.getFullYear()+'/'+(today.getMonth()+1).toString().padStart(2, 0)+'/'+today.getDate();
const params = {
TableName: 'acct',
Key: {
'email': email
},
UpdateExpression: `set customer_date = :date`,
ExpressionAttributeValues: {
':date': date,
},
ReturnValues: 'UPDATED_NEW'
};
return await dynamodb.update(params).promise().then((response) => {
const body = {
Operation: 'Atualização de Cliente',
Message: "Atualizado com sucesso!",
Item: response,
Email: email
};
return buildResponse(200, body);
},(error) =>{
console.error("Incapaz de atualizar Lead", error);
});
}
function buildResponse(statusCode, body){
return {
statusCode: statusCode,
headers: {
'Content-type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
};
}