-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
196 lines (168 loc) · 5.03 KB
/
handler.js
File metadata and controls
196 lines (168 loc) · 5.03 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const AWS = require('aws-sdk'); //requires AWS
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2019.11.21' }); //requires DynamoDB
const uuid = require('uuid/v4'); //requires UUID, the thing that generates the IDs for us
//gets table info from the config file (the yml)
const orgsTable = process.env.ORGS_TABLE;
//helper function to create the response sent:
function response(statusCode, message) {
return {
statusCode: statusCode,
headers: {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'GET, OPTIONS, POST, PUT',
'Content-Type': 'application/json',
},
body: JSON.stringify(message),
};
}
function sortByDate(a, b) {
if (a.createdAt > b.createdAt) {
return -1;
} else {
return 1;
}
}
//--------POST NEW ORG:--------
module.exports.createOrg = (event, context, callback) => {
//context holds env variables, AWS stuff, etc.
//callback sends response or error
const reqBody = JSON.parse(event.body); //request body
//validation - must have name:
if (!reqBody.orgName || reqBody.orgName.trim() === '') {
return callback(null, response(400, { error: 'Org must have name' }));
}
const org = {
id: uuid(),
createdAt: new Date().toISOString(),
orgName: reqBody.orgName,
category: reqBody.category,
briefBio: reqBody.briefBio,
opportunities: reqBody.opportunities,
qualities: reqBody.qualities,
contactName: reqBody.contactName,
contactDetails: reqBody.contactDetails,
img: reqBody.img,
uid: reqBody.uid,
link: reqBody.link,
};
return db
.put({
TableName: orgsTable,
Item: org,
})
.promise()
.then(() => {
callback(null, response(200, org)); //uses helper function defined above to pass 201 (or 200 - changed it to this to bug-fix CORS) as the status code and the org itself as the message
})
.catch((err) => response(null, response(err.statusCode, err)));
};
//--------GET ALL ORGS:--------
module.exports.getAllOrgs = (event, context, callback) => {
return db
.scan({
TableName: orgsTable,
})
.promise()
.then((res) => callback(null, response(200, res.Items.sort(sortByDate))))
.catch((err) => callback(null, response(err.statusCode, err)));
};
//--------GET ORG BY ID:--------
module.exports.getOrg = (event, context, callback) => {
//gets the id out of the url parameters:
const id = event.pathParameters.id;
//sets up the params to tell the db which table and that the key will be the id grabbed from the url:
const params = {
Key: {
id: id,
},
TableName: orgsTable,
};
return db
.get(params)
.promise()
.then((res) => {
//checks if there's an org with that id; if so, it's stored in res.Item
if (res.Item) callback(null, response(200, res.Item));
else
callback(null, response(404, { error: 'No org with that name found' }));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
//--------UPDATE ORG - ALL ATTRIBUTES:--------
module.exports.updateOrg = (event, context, callback) => {
const id = event.pathParameters.id;
const reqBody = JSON.parse(event.body);
const org = {
id: id,
createdAt: new Date().toISOString(),
orgName: reqBody.orgName,
category: reqBody.category,
briefBio: reqBody.briefBio,
opportunities: reqBody.opportunities,
qualities: reqBody.qualities,
contactName: reqBody.contactName,
contactDetails: reqBody.contactDetails,
img: reqBody.img,
uid: reqBody.uid,
link: reqBody.link,
};
return db
.put({
TableName: orgsTable,
Item: org,
})
.promise()
.then((res) => {
callback(null, response(200, res));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
//--------UPDATE ORG - SINGLE ATTRIBUTE:--------
module.exports.updateOrgAttribute = (event, context, callback) => {
const id = event.pathParameters.id;
const body = JSON.parse(event.body);
const paramName = body.paramName;
const paramValue = body.paramValue;
const params = {
Key: {
id: id,
},
TableName: orgsTable,
ConditionExpression: 'attribute_exists(id)',
UpdateExpression: `set ${paramName} = :v`,
ExpressionAttributeValues: {
':v': paramValue,
},
ReturnValue: 'ALL_NEW',
};
return db
.update(params)
.promise()
.then((res) => {
callback(null, response(200, { paramName, paramValue }));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
//--------DELETE ORG:--------
module.exports.deleteOrg = (event, context, callback) => {
const id = event.pathParameters.id;
const params = {
Key: {
id: id,
},
TableName: orgsTable,
};
return db
.delete(params)
.promise()
.then(() =>
callback(
null,
response(200, { message: `Org ${id} deleted successfully` })
)
)
.catch((err) => callback(null, response(err.statusCode, err)));
};