-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-list.js
44 lines (41 loc) · 1.24 KB
/
delete-list.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
39
40
41
42
43
44
import * as dynamoDbLib from "./libs/dynamodb-lib";
import { success, failure } from "./libs/response-lib";
const TABLE_NAME = process.env.DATABASE_NAME
export async function main(event, context) {
const listParams = {
TableName:TABLE_NAME,
KeyConditionExpression: "userId = :userId AND begins_with(wordId,:languageCode)",
ExpressionAttributeValues: {
":userId": event.requestContext.authorizer.principalId,
":languageCode": event.queryStringParameters.languageCode
}
}
try {
const list = await dynamoDbLib.call("query", listParams);
let failed = []
// TODO we should do this in batches of 25 using batchWrite
if (list.Count > 0) {
for (let item of list.Items) {
let params = {
TableName: TABLE_NAME,
Key: {
userId: event.requestContext.authorizer.principalId,
wordId: item.wordId
}
};
try {
let result = await dynamoDbLib.call("delete", params)
} catch (e) {
failed.push(item)
}
}
if (failed.length > 0) {
return failure({ status: false });
}
}
return success({ status: true });
} catch (e) {
console.log(e)
return failure({ status: false });
}
}