Skip to content

Commit

Permalink
added conversations-clear.ts, groups-clear.ts, users-clear.ts (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlehDulebaEpam authored Dec 18, 2023
1 parent 3555192 commit f6d8d56
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/cdk/AngularCourseStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const angularTaskApi: {
method: HttpMethod.GET,
lambdaName: "users",
},
{
path: "/users/clear",
method: HttpMethod.DELETE,
lambdaName: "users-clear",
},
{
path: "/profile",
method: HttpMethod.GET,
Expand All @@ -75,6 +80,11 @@ const angularTaskApi: {
method: HttpMethod.DELETE,
lambdaName: "delete-personal-conversation",
},
{
path: "/conversations/clear",
method: HttpMethod.DELETE,
lambdaName: "conversations-clear",
},
{
path: "/conversations/read",
method: HttpMethod.GET,
Expand All @@ -100,6 +110,11 @@ const angularTaskApi: {
method: HttpMethod.DELETE,
lambdaName: "groups-delete",
},
{
path: "/groups/clear",
method: HttpMethod.DELETE,
lambdaName: "groups-clear",
},
{
path: "/groups/read",
method: HttpMethod.GET,
Expand Down
49 changes: 49 additions & 0 deletions src/functions/conversations-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { DynamoDBClient, ScanCommand, DeleteItemCommand, DeleteTableCommand } from "@aws-sdk/client-dynamodb";
import {APIGatewayProxyEventV2} from "aws-lambda";

const client = new DynamoDBClient({ region: 'eu-central-1' });

export const handler = async (event: APIGatewayProxyEventV2) => {

const getCommand = new ScanCommand({
TableName: 'rsschool-2023-conversations',
ExpressionAttributeNames: {
"#ID": "id",
"#U1": "user1",
"#U2": "user2",
},
ProjectionExpression: "#ID, #U1, #U2",
});
const result = await client.send(getCommand);

const deleteRequests = result.Items.map(item => {
const conversationID = item.id.S;

const deleteItemCommand = new DeleteItemCommand({
TableName: "rsschool-2023-conversations",
Key: {
id: {
S: conversationID,
},
},
ReturnValues: "NONE",
});
const deleteItemQuery = client.send(deleteItemCommand);

const deleteTableCommand = new DeleteTableCommand({
TableName: `conversation-${conversationID}`,
});
const deleteTableQuery = client.send(deleteTableCommand);


return Promise.allSettled([deleteItemQuery, deleteTableQuery]).then(res => ({conversationID, result: res.status === 'fulfilled' ? res.value : res.reason}));

Check failure on line 39 in src/functions/conversations-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'status' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'.

Check failure on line 39 in src/functions/conversations-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'value' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'. Did you mean 'values'?

Check failure on line 39 in src/functions/conversations-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'reason' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'.
});

const response = await Promise.allSettled(deleteRequests);


return {
statusCode: 200,
body: JSON.stringify(response),
};
};
47 changes: 47 additions & 0 deletions src/functions/groups-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DynamoDBClient, ScanCommand, DeleteItemCommand, DeleteTableCommand } from "@aws-sdk/client-dynamodb";
import {APIGatewayProxyEventV2} from "aws-lambda";

const client = new DynamoDBClient({ region: 'eu-central-1' });

export const handler = async (event: APIGatewayProxyEventV2) => {

const getCommand = new ScanCommand({
TableName: 'rsschool-2023-groups',
ExpressionAttributeNames: {
"#ID": "id",
},
ProjectionExpression: "#ID",
});
const result = await client.send(getCommand);

const deleteRequests = result.Items.map(item => {
const groupID = item.id.S;

const deleteItemCommand = new DeleteItemCommand({
TableName: "rsschool-2023-groups",
Key: {
id: {
S: groupID,
},
},
ReturnValues: "NONE",
});
const deleteItemQuery = client.send(deleteItemCommand);

const deleteTableCommand = new DeleteTableCommand({
TableName: `group-${groupID}`,
});
const deleteTableQuery = client.send(deleteTableCommand);


return Promise.allSettled([deleteItemQuery, deleteTableQuery]).then(res => ({groupID, result: res.status === 'fulfilled' ? res.value : res.reason}));

Check failure on line 37 in src/functions/groups-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'status' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'.

Check failure on line 37 in src/functions/groups-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'value' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'. Did you mean 'values'?

Check failure on line 37 in src/functions/groups-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'reason' does not exist on type '[PromiseSettledResult<DeleteItemCommandOutput>, PromiseSettledResult<DeleteTableCommandOutput>]'.
});

const response = await Promise.allSettled(deleteRequests);


return {
statusCode: 200,
body: JSON.stringify(response),
};
};
2 changes: 1 addition & 1 deletion src/functions/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const handler = async (event: APIGatewayProxyEventV2) => {

if ((err as Error).name === "ConditionalCheckFailedException") {
return {
statusCode: 400,
statusCode: 401,
body: JSON.stringify({
type: "InvalidTokenException",
message: "Current session token is not valid.",
Expand Down
42 changes: 42 additions & 0 deletions src/functions/users-clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { DynamoDBClient, ScanCommand, DeleteItemCommand } from "@aws-sdk/client-dynamodb";
import {APIGatewayProxyEventV2} from "aws-lambda";

const client = new DynamoDBClient({ region: 'eu-central-1' });

export const handler = async (event: APIGatewayProxyEventV2) => {

const getCommand = new ScanCommand({
TableName: 'rsschool-2023-users',
ExpressionAttributeNames: {
"#E": "email",
},
ProjectionExpression: "#E",
});
const result = await client.send(getCommand);

const deleteRequests = result.Items.map(item => {
const email = item.email.S;

const deleteItemCommand = new DeleteItemCommand({
TableName: "rsschool-2023-users",
Key: {
email: {
S: email,
},
},
ReturnValues: "NONE",
});
const deleteItemQuery = client.send(deleteItemCommand);


return deleteItemQuery.then(res => ({email, result: res.status === 'fulfilled' ? res.value : res.reason}));

Check failure on line 32 in src/functions/users-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'status' does not exist on type 'DeleteItemCommandOutput'.

Check failure on line 32 in src/functions/users-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'value' does not exist on type 'DeleteItemCommandOutput'.

Check failure on line 32 in src/functions/users-clear.ts

View workflow job for this annotation

GitHub Actions / Build & Deploy

Property 'reason' does not exist on type 'DeleteItemCommandOutput'.
});

const response = await Promise.allSettled(deleteRequests);


return {
statusCode: 200,
body: JSON.stringify(response),
};
};

0 comments on commit f6d8d56

Please sign in to comment.