-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment-handler.js
66 lines (59 loc) · 2.71 KB
/
comment-handler.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
'use strict';
const CommentHelper = require('./helpers/comment')
const Status = require('@pbnj-xintern/xintern-commons/util/status')
const TOKEN_SECRET = process.env.TOKEN_SECRET
const AuthHelper = require('@pbnj-xintern/xintern-commons/util/auth_checker')
const middy = require('middy')
const Comment = require('@pbnj-xintern/xintern-commons/models/Comment')
//--------------- LAMBDA FUNCTIONS ---------------
module.exports.getUpvotedCommentsByUserId = middy(async (event) => {
let payload = (event.body instanceof Object) ? event.body : JSON.parse(event.body)
let decodedJWT = false
if (event.headers && event.headers.Authorization) {
decodedJWT = AuthHelper.decodeJWT(event.headers.Authorization.replace("Bearer ", ""));
}
if (decodedJWT) {
let user_id = decodedJWT.userId;
return await CommentHelper.getUpvotedCommentsByUserId(user_id)
}
return Status.createErrorResponse(403, "Invalid Bearer Token")
}).use(AuthHelper.verifyJWT(TOKEN_SECRET))
module.exports.getDownvotedCommentsByUserId = middy(async (event) => {
let payload = (event.body instanceof Object) ? event.body : JSON.parse(event.body)
let decodedJWT = false
if (event.headers && event.headers.Authorization) {
decodedJWT = AuthHelper.decodeJWT(event.headers.Authorization.replace("Bearer ", ""));
}
if (decodedJWT) {
let user_id = decodedJWT.userId;
return await CommentHelper.getDownvotedCommentsByUserId(user_id)
}
return Status.createErrorResponse(403, "Invalid Bearer Token")
}).use(AuthHelper.verifyJWT(TOKEN_SECRET))
module.exports.createComment = middy(async (event) => {
let reviewId = event.pathParameters.review_id
let payload = (event.body instanceof Object) ? event.body : JSON.parse(event.body)
let decodedJWT = false
if (event.headers && event.headers.Authorization) {
decodedJWT = AuthHelper.decodeJWT(event.headers.Authorization.replace("Bearer ", ""));
}
if (decodedJWT) {
payload.author = decodedJWT.userId;
return await CommentHelper.createComment(reviewId, payload)
}
return Status.createErrorResponse(401, "Invalid Bearer Token")
}).use(AuthHelper.verifyJWT(TOKEN_SECRET))
//deleteComment
module.exports.deleteComment = middy(async (event) => {
let commentId = event.pathParameters.comment_id
return await CommentHelper.deleteComment(commentId)
}).use(AuthHelper.verifyJWT(TOKEN_SECRET))
//updateComment
module.exports.updateComment = middy(async (event) => {
let commentId = event.pathParameters.comment_id
let payload = (event.body instanceof Object) ? event.body : JSON.parse(event.body)
return await CommentHelper.updateComment(commentId, payload)
}).use(AuthHelper.verifyJWT(TOKEN_SECRET))
module.exports.getPopulatedComments = async event => {
return await CommentHelper.getPopulatedComments(event.pathParameters.review_id)
}