-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleads.js
164 lines (152 loc) · 4.26 KB
/
leads.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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 = "acct";
// Rotas
const test = "/test";
const leads = "/leads";
const lead = "/lead";
const order = "/order";
// 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 === test:
const body = {
"Operation": "Teste API",
"Message": "Tudo OK :D"
};
response = buildResponse(200, body);
break;
// Lista de todos os Leads cadastrados
case event.httpMethod === 'GET' && event.path === leads:
response = await getLeads();
break;
// Request de um lead específico
case event.httpMethod === 'GET' && event.path === lead:
response = await getLead(event.queryStringParameters.email);
break;
// Cadastro e Atualização de Item
case event.httpMethod === 'PUT' && event.path === lead:
response = await createEditLead(JSON.parse(event.body));
break;
// Remoção de Lead
case event.httpMethod === 'DELETE' && event.path === lead:
response = await deleteLead(JSON.parse(event.body).email);
break;
case event.httpMethod === 'POST' && event.path === order:
response = await createOrder(JSON.parse(event.body));
break;
case event.httpMethod === 'GET' && event.path === order:
response = buildResponse(200, {
"Operation": 200,
"Message": "OK"
});
break;
}
return response;
};
async function createOrder(requestBody){
const params = {
TableName: dynamoTableName,
Item: requestBody
};
return await dynamodb.put(params).promise().then(()=>{
const body = {
Operation: "Cadastro de Prospecto",
Message: "Salvo com Sucesso :D",
Lead: requestBody
};
return buildResponse(200, body);
}, (error) =>{
console.error("Incapaz de armazenar Lead", error);
});
//return buildResponse(200, requestBody)
}
async function createEditLead(requestBody){
const params = {
TableName: dynamoTableName,
Item: requestBody
};
return await dynamodb.put(params).promise().then(()=>{
const body = {
Operation: "Cadastro de Prospecto",
Message: "Salvo com Sucesso :D",
Lead: requestBody
};
return buildResponse(200, body);
}, (error) =>{
console.error("Incapaz de armazenar Lead", error);
});
}
async function getLeads(){
const params = {
TableName: dynamoTableName
};
const allSubs = await scanDynamoRecords(params, []);
const body = {
prospectos: allSubs
};
return buildResponse(200, body);
}
async function scanDynamoRecords(scanParams, itemArray){
try{
const dynamoData = await dynamodb.scan(scanParams).promise();
itemArray = itemArray.concat(dynamoData.Items);
if(dynamoData.LastEvaluatedKey){
scanParams.ExclusiveStartKey = dynamoData.LastEvaluatedKey;
return await scanDynamoRecords(scanParams, itemArray);
}
return itemArray;
}catch(error){
console.error("Incapaz de encontrar Leads", error);
}
}
async function getLead(email){
const params = {
TableName: dynamoTableName,
Key: {
'email': email
}
};
return await dynamodb.get(params).promise().then((response) =>{
return buildResponse(200, response.Item);
},(error) =>{
console.error("Incapaz de encontrar Lead", error);
});
}
async function deleteLead(email){
const params = {
TableName: dynamoTableName,
Key: {
'email': email
},
ReturnValues: 'ALL_OLD'
};
return await dynamodb.delete(params).promise().then((response)=>{
const body = {
Operation: "Exclusão de Prospecto",
Message: "Prospecto removido com sucesso",
Item: response
};
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)
};
}