-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetRandomSwimComponent.js
More file actions
30 lines (26 loc) · 985 Bytes
/
getRandomSwimComponent.js
File metadata and controls
30 lines (26 loc) · 985 Bytes
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
import { success, failure } from "./libs/response-lib";
import * as dynamoDbLib from "./libs/dynamodb-lib";
export async function main(event, context) {
const params = {
TableName: "swim_components_table",
ProjectionExpression: "username, component_id"
};
const resultList = await dynamoDbLib.call("scan", params);
let randomItemNumber = Math.floor(Math.random() * resultList.Items.length);
let randomUsername = resultList.Items[randomItemNumber].username;
let randomUUID = resultList.Items[randomItemNumber].component_id;
const randomParams = {
TableName: "swim_components_table",
Key: {
username: randomUsername,
component_id: randomUUID
}
};
const result = await dynamoDbLib.call("get", randomParams);
if (result.Item) {
// Return the retrieved item
return success(result.Item);
} else {
return failure({ status: false, error: "Item not found." });
}
}