-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
53 lines (46 loc) · 1.84 KB
/
index.ts
File metadata and controls
53 lines (46 loc) · 1.84 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
import { Context, Handler, APIGatewayEvent } from 'aws-lambda';
import * as AWS from "aws-sdk"
const table = process.env.TABLE_NAME || 'SttTickets';
const endpoint = process.env.AWS_SAM_LOCAL ? "http://dynamodb:8000/" : undefined;
console.log(`Connecting to ${table} at endpoint: ${endpoint}`)
export const handler: Handler = async (event = {}, context: Context): Promise<any> => {
console.log('Received event:', JSON.stringify(event, null, 2));
if (!event.body) {
throw new Error("Recieved Event with no body")
}
let body = JSON.parse(event.body);
const docClient = new AWS.DynamoDB.DocumentClient({
endpoint
});
const params = {
TableName: table, //we get table name from env variable.
Item: {
"id": new Date().toISOString(),
"description": body.description,
"assigned": body.assigned,
"priority": body.priority,
"status": body.status,
"createdBy": body.createdBy,
"createdOn": new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
}
};
console.log("Adding a new ticket..." + JSON.stringify(params));
try {
const data = await docClient.put(params).promise();
console.log("Added ticket:", JSON.stringify(data, null, 2));
return {
statusCode: '200',
headers: {
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify(params.Item)
};
} catch (err) {
console.error("Unable to add ticket. Error JSON:", JSON.stringify(err, null, 2));
return {
statusCode: '500',
body: 'Unable to add ticket'
}
}
}