forked from aws-samples/serverless-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (87 loc) · 3.1 KB
/
index.js
File metadata and controls
100 lines (87 loc) · 3.1 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
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
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb');
// Initialize AWS clients
const dynamodbClient = new DynamoDBClient({});
const dynamodb = DynamoDBDocumentClient.from(dynamodbClient);
/**
* Status query function for webhook processing
* Allows real-time status tracking via REST API
*/
exports.handler = async (event, context) => {
const executionToken = event.pathParameters?.executionToken;
const eventsTableName = process.env.EVENTS_TABLE_NAME;
console.log(`Querying status for execution token: ${executionToken}`);
if (!executionToken) {
return {
statusCode: 400,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error: 'Missing executionToken parameter'
})
};
}
try {
// Query execution state from DynamoDB
const result = await dynamodb.send(new GetCommand({
TableName: eventsTableName,
Key: { executionToken }
}));
if (!result.Item) {
return {
statusCode: 404,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error: 'Execution token not found',
executionToken: executionToken
})
};
}
// Format response based on current status
const execution = result.Item;
const response = {
executionToken: executionToken,
status: execution.status,
timestamp: execution.timestamp,
currentStep: execution.currentStep || 'unknown'
};
// Add additional fields based on status
if (execution.status === 'COMPLETED') {
response.result = execution.result;
response.completedAt = execution.completedAt;
}
if (execution.status === 'FAILED') {
response.error = execution.error;
}
if (execution.payload) {
response.originalPayload = execution.payload;
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(response)
};
} catch (error) {
console.error(`Error querying status for ${executionToken}:`, error.message);
return {
statusCode: 500,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({
error: 'Failed to query execution status',
executionToken: executionToken,
message: error.message
})
};
}
};