-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletePost.ts
108 lines (97 loc) · 2.78 KB
/
deletePost.ts
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
// delete post code here
import { lambda, sdk } from '@pulumi/aws';
import { getToken } from '../../auth';
import type { lambdaEvent } from '#utils/util';
import { PostsTable, TagsTable } from '#tables/index';
import {
currentEndpoint,
CUSTOM_ERROR_CODES,
makeCustomError,
decodeJWT,
populateResponse,
STATUS_CODES,
} from '#utils/util';
/**
* Delete a post
* @description
* - The post is deleted from the database
* - The lambda is triggered by a DELETE request to /posts/delete/{postID}
*
* @see https://www.pulumi.com/docs/guides/crosswalk/aws/api-gateway/#lambda-request-handling
*/
export const deletePost = new lambda.CallbackFunction<
lambdaEvent,
{
body: string;
statusCode: number;
}
>('deletePost', {
runtime: lambda.Runtime.NodeJS16dX,
callback: async event => {
const { postID } = event.pathParameters!;
const userID = decodeJWT(getToken(event)).data?.id;
if (!userID) {
return populateResponse(
STATUS_CODES.UNAUTHORIZED,
makeCustomError('You are not authorized', CUSTOM_ERROR_CODES.USER_NOT_AUTHORIZED),
);
}
const client = new sdk.DynamoDB.DocumentClient(currentEndpoint);
try {
const { Items } = await client
.query({
TableName: TagsTable.get(),
IndexName: 'postID',
KeyConditionExpression: 'postID = :postID',
ExpressionAttributeValues: {
':postID': postID,
},
})
.promise();
if (!Items?.length)
return populateResponse(
STATUS_CODES.NOT_FOUND,
makeCustomError('Post not found', CUSTOM_ERROR_CODES.POST_ERROR),
);
const oldTag = Items[0].tag;
await client
.transactWrite({
TransactItems: [
{
Delete: {
TableName: TagsTable.get(),
Key: {
postID,
tag: oldTag,
},
},
},
{
Delete: {
TableName: PostsTable.get(),
Key: {
userID,
postID,
},
ConditionExpression: 'attribute_exists(postID)',
},
},
],
})
.promise();
return populateResponse(STATUS_CODES.OK, 'Post deleted');
} catch (error) {
if ((error as any).code === 'ConditionalCheckFailedException') {
return populateResponse(
STATUS_CODES.NOT_FOUND,
makeCustomError('You cannot delete this post', CUSTOM_ERROR_CODES.RESOURCE_NOT_FOUND),
);
}
console.error(error);
return populateResponse(
STATUS_CODES.INTERNAL_SERVER_ERROR,
makeCustomError('Internal Server Error', CUSTOM_ERROR_CODES.POST_ERROR),
);
}
},
});