forked from Superformula/node-backend-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.js
38 lines (35 loc) · 1.21 KB
/
delete.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
import { success, failure } from "./libs/response-lib";
import * as dynamoDbLib from "./libs/dynamodb-lib";
import User from './model/user';
import HttpStatus from 'http-status-codes';
import { UUID_V1 } from './validators/user';
/**
* Delete API Lambda for User model.
*/
export async function main(event, context) {
try {
if (!('pathParameters' in event)) {
return failure(HttpStatus.UNPROCESSABLE_ENTITY, {error : "ID Path Parameter required."});
}
const id = event.pathParameters.id;
if (!id) {
return failure(HttpStatus.UNPROCESSABLE_ENTITY, {error : "ID Path Parameter required."});
} else if (!id.match(UUID_V1)) {
throw { message : "Required UUID V1 format." };
}
let user = new User(dynamoDbLib);
try {
const exists = await user.read(id);
if (!exists) {
return failure(HttpStatus.NOT_FOUND, {});
}
await user.delete(id);
return success(HttpStatus.NO_CONTENT, {});
} catch (e) {
return failure(HttpStatus.INTERNAL_SERVER_ERROR, {});
}
} catch (e) {
console.info("Invalid Request, Path Parameters: [" + JSON.stringify(event.pathParameters) + "]", e.message);
return failure(HttpStatus.UNPROCESSABLE_ENTITY, {});
}
}