-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
60 lines (52 loc) · 1.66 KB
/
handler.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
import 'source-map-support/register';
import { middyfy } from '@libs/lambda';
import { createLogger } from '@libs/logger'
import type { APIGatewayProxyEvent, APIGatewayProxyResult, APIGatewayProxyHandler } from "aws-lambda"
import { getUserId } from '@libs/getUserId';
import { getItem , deleteItem} from '@libs/database'
import {deleteBucket} from '@libs/storage'
const logger = createLogger('deleteTodos');
const deleteTodos: APIGatewayProxyHandler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
// TODO: Get all TODO items for a current user
const user = getUserId(event);
const todoId = event.pathParameters.todoId;
logger.info(
`Request to delete todo item ${todoId} for user ${user} `
);
// check if todo item exists
const todoQuery = await getItem(todoId, user);
if (todoQuery.Count === 0) {
logger.info(`Got invalid todoId ${todoId} for user ${user}`);
return {
statusCode: 404,
body: JSON.stringify({
error: "TODO item does not exist",
}),
};
}
const todoEntry = todoQuery.Items[0]
// check if an S3 bucket was created for this item and delete if so
if (todoEntry.hasOwnProperty('attachmentUrl')) {
logger.info('will be deleting S3 bucket')
try {
deleteBucket(todoEntry.attachmentUrl)
} catch (e) {
logger.info('Delete s3: Got error',e)
}
}
logger.info('database entry to delete', todoEntry)
// delete the todo entry
const timestamp = todoEntry.timestamp
try {
deleteItem(timestamp, user)
} catch (e) {
logger.info('Got error',e)
}
return {
statusCode: 200,
body: ''
}
}
export const main = middyfy(deleteTodos);