Skip to content

Commit

Permalink
Upgrade to AWS SDK v3 (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmartin-coforma authored Mar 7, 2024
1 parent afbdc11 commit 2f14557
Show file tree
Hide file tree
Showing 50 changed files with 2,502 additions and 1,240 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/destroy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Destroy

on:
on:
delete:
workflow_dispatch:
inputs:
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ The Kafka Queues we link to are in the BigMac account and are currently not bein

`postKafkaData`: Fires when an update to the database happens and syncs kafka to reflect the current state of the database.

`forceKafkaSync`: This can be manually triggered to force kafka to reflect the current state of the database.

### Utilities

---
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@types/yargs": "^15.0.10",
"@typescript-eslint/eslint-plugin": "5.18.0",
"@typescript-eslint/parser": "5.18.0",
"aws-sdk": "^2.1310.0",
"dotenv": "^8.2.0",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
Expand Down
5 changes: 3 additions & 2 deletions services/app-api/handlers/banners/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import handler from "../../libs/handler-lib";
import dynamoDb from "../../libs/dynamodb-lib";
import { Errors, StatusCodes } from "../../utils/constants/constants";
import { Banner } from "../../types";

export const fetchBanner = handler(async (event, _context) => {
if (!event?.pathParameters?.bannerId!) {
Expand All @@ -12,8 +13,8 @@ export const fetchBanner = handler(async (event, _context) => {
key: event?.pathParameters?.bannerId!,
},
};
const response = await dynamoDb.get(params);
const response = await dynamoDb.get<Banner>(params);

const status = StatusCodes.SUCCESS;
return { status: status, body: response };
return { status: status, body: { Item: response } };
});
20 changes: 4 additions & 16 deletions services/app-api/handlers/banners/tests/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { createBanner } from "../create";
import { APIGatewayProxyEvent } from "aws-lambda";
import { APIGatewayProxyEvent } from "../../../types";
import { testBanner, proxyEvent } from "./proxyEvent";
import dynamoDb from "../../../libs/dynamodb-lib";
import { Errors, StatusCodes } from "../../../utils/constants/constants";
import { mockDocumentClient } from "../../../utils/testing/setupJest";

const mockHasRolePermissions = jest.fn();
jest.mock("../../../libs/authorization", () => ({
isAuthenticated: jest.fn().mockReturnValue(true),
hasRolePermissions: () => mockHasRolePermissions(),
}));

jest.mock("../../../libs/debug-lib", () => ({
init: jest.fn(),
flush: jest.fn(),
}));

const testEvent: APIGatewayProxyEvent = {
...proxyEvent,
httpMethod: "PUT",
Expand All @@ -28,15 +22,9 @@ const testEventNoTitle: APIGatewayProxyEvent = {
body: JSON.stringify({ ...testBanner, title: "" }),
};

jest.spyOn(dynamoDb, "put").mockImplementation(
mockDocumentClient.put.promise.mockReturnValue({
Item: {
...testBanner,
createdAt: new Date().getTime(),
lastAltered: new Date().getTime(),
},
})
);
jest.mock("../../../libs/dynamodb-lib", () => ({
put: jest.fn().mockResolvedValue("yep i put that thing!"),
}));

describe("Test createBanner API method", () => {
beforeEach(() => {
Expand Down
18 changes: 4 additions & 14 deletions services/app-api/handlers/banners/tests/delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import { deleteBanner } from "../delete";
import { APIGatewayProxyEvent } from "aws-lambda";
import { APIGatewayProxyEvent } from "../../../types";
import { testBanner, proxyEvent } from "./proxyEvent";
import dynamoDb from "../../../libs/dynamodb-lib";
import { Errors, StatusCodes } from "../../../utils/constants/constants";
import { mockDocumentClient } from "../../../utils/testing/setupJest";

const mockHasRolePermissions = jest.fn();
jest.mock("../../../libs/authorization", () => ({
isAuthenticated: jest.fn().mockReturnValue(true),
hasRolePermissions: () => mockHasRolePermissions(),
}));

jest.mock("../../../libs/debug-lib", () => ({
init: jest.fn(),
flush: jest.fn(),
}));

const testEvent: APIGatewayProxyEvent = {
...proxyEvent,
httpMethod: "DEL",
};

jest.spyOn(dynamoDb, "delete").mockImplementation(
mockDocumentClient.delete.promise.mockReturnValue({
Item: {
...testBanner,
},
})
);
jest.mock("../../../libs/dynamodb-lib", () => ({
delete: jest.fn().mockResolvedValue("DELETED"),
}));

describe("Test deleteBanner API method", () => {
beforeEach(() => {
Expand Down
58 changes: 27 additions & 31 deletions services/app-api/handlers/banners/tests/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
import { fetchBanner } from "../fetch";
import { APIGatewayProxyEvent } from "aws-lambda";
import { proxyEvent, testBanner } from "./proxyEvent";
import { APIGatewayProxyEvent, EventParameters } from "../../../types";
import dynamoDb from "../../../libs/dynamodb-lib";
import { Errors, StatusCodes } from "../../../utils/constants/constants";
import { mockDocumentClient } from "../../../utils/testing/setupJest";

jest.mock("../../../libs/authorization", () => ({
isAuthenticated: jest.fn().mockReturnValue(true),
hasPermissions: jest.fn().mockReturnValue(true),
}));

jest.mock("../../../libs/debug-lib", () => ({
init: jest.fn(),
flush: jest.fn(),
jest.mock("../../../libs/dynamodb-lib", () => ({
get: jest.fn().mockResolvedValue({
title: "test banner",
description: "test description",
link: "https://www.example.com",
startDate: 1000,
endDate: 2000,
createdAt: new Date().getTime(),
lastAltered: new Date().getTime(),
}),
}));

jest.spyOn(dynamoDb, "get").mockImplementation(
mockDocumentClient.get.promise.mockReturnValue({
Item: {
...testBanner,
createdAt: new Date().getTime(),
lastAltered: new Date().getTime(),
},
})
);

const testEvent: APIGatewayProxyEvent = {
...proxyEvent,
headers: { "cognito-identity-id": "test" },
pathParameters: { bannerId: "testKey" },
};
const testEvent = {
headers: {
"cognito-identity-id": "test",
} as EventParameters,
pathParameters: {
bannerId: "testKey",
} as EventParameters,
} as APIGatewayProxyEvent;

describe("Test fetchBanner API method", () => {
test("Test Successful empty Banner Fetch", async () => {
jest.spyOn(dynamoDb, "get").mockImplementation(
mockDocumentClient.get.promise.mockReturnValueOnce({
Item: undefined,
})
);
(dynamoDb.get as jest.Mock).mockResolvedValueOnce({
Item: undefined,
});
const res = await fetchBanner(testEvent, null);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
});
Expand All @@ -46,11 +42,11 @@ describe("Test fetchBanner API method", () => {
const res = await fetchBanner(testEvent, null);
expect(res.statusCode).toBe(StatusCodes.SUCCESS);
const parsedBody = JSON.parse(res.body);
expect(parsedBody.Item.title).toEqual(testBanner.title);
expect(parsedBody.Item.description).toEqual(testBanner.description);
expect(parsedBody.Item.startDate).toEqual(testBanner.startDate);
expect(parsedBody.Item.endDate).toEqual(testBanner.endDate);
expect(parsedBody.Item.link).toEqual(testBanner.link);
expect(parsedBody.Item.title).toEqual("test banner");
expect(parsedBody.Item.description).toEqual("test description");
expect(parsedBody.Item.startDate).toEqual(1000);
expect(parsedBody.Item.endDate).toEqual(2000);
expect(parsedBody.Item.link).toEqual("https://www.example.com");
});

test("Test bannerKey not provided throws 500 error", async () => {
Expand Down
57 changes: 7 additions & 50 deletions services/app-api/handlers/banners/tests/proxyEvent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIGatewayProxyEvent } from "aws-lambda";
import { APIGatewayProxyEvent, EventParameters } from "../../../types";

export const testBanner = {
title: "test banner",
Expand All @@ -10,60 +10,17 @@ export const testBanner = {

export const proxyEvent: APIGatewayProxyEvent = {
body: "{}",
headers: { "x-api-key": "test" },
headers: { "x-api-key": "test" } as EventParameters,
httpMethod: "GET",
isBase64Encoded: false,
multiValueHeaders: {},
multiValueQueryStringParameters: {},
multiValueHeaders: {} as EventParameters,
multiValueQueryStringParameters: {} as EventParameters,
path: "",
pathParameters: { bannerId: "admin-banner-id" },
pathParameters: { bannerId: "admin-banner-id" } as EventParameters,
resource: "",
stageVariables: null,
queryStringParameters: { bannerId: "testKey" },
queryStringParameters: { bannerId: "testKey" } as EventParameters,
requestContext: {
accountId: "",
apiId: "",
authorizer: () => {},
httpMethod: "",
path: "",
protocol: "",
requestId: "",
requestTimeEpoch: 0,
resourceId: "",
resourcePath: "",
stage: "",
connectedAt: 0,
connectionId: "",
domainName: "",
domainPrefix: "",
eventType: "",
extendedRequestId: "",
messageDirection: "",
messageId: "",
requestTime: "",
routeKey: "",
identity: {
accessKey: "",
accountId: "",
apiKey: "",
apiKeyId: "",
caller: "",
cognitoAuthenticationProvider: "",
cognitoAuthenticationType: "",
cognitoIdentityId: "",
cognitoIdentityPoolId: "",
principalOrgId: "",
sourceIp: "",
user: "",
userAgent: "",
userArn: "",
clientCert: {
clientCertPem: "",
issuerDN: "",
serialNumber: "",
subjectDN: "",
validity: { notAfter: "", notBefore: "" },
},
},
/* nope */
},
};
4 changes: 2 additions & 2 deletions services/app-api/handlers/coreSets/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const createCoreSet = handler(async (event, context) => {
},
};

await dynamoDb.post(params);
await dynamoDb.put(params);
return { status: StatusCodes.SUCCESS, body: params };
});

Expand Down Expand Up @@ -114,7 +114,7 @@ const createDependentMeasures = async (
},
};

const result = await dynamoDb.post(params);
const result = await dynamoDb.put(params);
dependentMeasures.push(result);
}
};
4 changes: 2 additions & 2 deletions services/app-api/handlers/coreSets/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const deleteDependentMeasures = async (
coreSet: string
) => {
const measures = await getMeasures(state, year, coreSet);
const Items = measures.Items || [];
const Items = measures;

for await (const { measure } of Items) {
const dynamoKey = `${state}${year}${coreSet}${measure}`;
Expand All @@ -67,6 +67,6 @@ const getMeasures = async (state: string, year: number, coreSet: string) => {
"list"
),
};
const queryValue = await dynamoDb.scan<Measure>(params);
const queryValue = await dynamoDb.scanAll<Measure>(params);
return queryValue;
};
30 changes: 18 additions & 12 deletions services/app-api/handlers/coreSets/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
hasRolePermissions,
hasStatePermissions,
} from "../../libs/authorization";
import * as Types from "../../types";
import { Errors, StatusCodes } from "../../utils/constants/constants";
import { CoreSet, CoreSetAbbr, UserRoles } from "../../types";

export const coreSetList = handler(async (event, context) => {
// action limited to any admin type user and state users from corresponding state
const isStateUser = hasRolePermissions(event, [Types.UserRoles.STATE_USER]);
const isStateUser = hasRolePermissions(event, [UserRoles.STATE_USER]);
if (isStateUser) {
const isFromCorrespondingState = hasStatePermissions(event);
if (!isFromCorrespondingState) {
Expand All @@ -35,15 +35,15 @@ export const coreSetList = handler(async (event, context) => {
),
};

const results = await dynamoDb.scan<Types.CoreSet>(params);
const results = await dynamoDb.scanAll<CoreSet>(params);
// if the query value contains no results
if (results.Count === 0) {
if (results.length === 0) {
// add an adult coreset and requery the db
const createCoreSetEvent = {
...event,
pathParameters: {
...event.pathParameters,
coreSet: Types.CoreSetAbbr.ACS,
coreSet: CoreSetAbbr.ACS,
},
};
try {
Expand All @@ -52,10 +52,12 @@ export const coreSetList = handler(async (event, context) => {
context
);
if (createCoreSetResult.statusCode === 200) {
const res = await dynamoDb.scan(params);
const res = await dynamoDb.scanAll<CoreSet>(params);
return {
status: StatusCodes.SUCCESS,
body: res,
body: {
Items: res,
},
};
} else {
throw new Error("Creation failed");
Expand All @@ -66,18 +68,20 @@ export const coreSetList = handler(async (event, context) => {
}
} else {
// Update the progress measure numComplete
const updatedCoreSetProgressResults =
(await updateCoreSetProgress(results, event, context)) || results;
await updateCoreSetProgress(results, event, context);

return {
status: StatusCodes.SUCCESS,
body: updatedCoreSetProgressResults,
body: {
Items: results,
},
};
}
});

export const getCoreSet = handler(async (event, context) => {
// action limited to any admin type user and state users from corresponding state
const isStateUser = hasRolePermissions(event, [Types.UserRoles.STATE_USER]);
const isStateUser = hasRolePermissions(event, [UserRoles.STATE_USER]);
if (isStateUser) {
const isFromCorrespondingState = hasStatePermissions(event);
if (!isFromCorrespondingState) {
Expand All @@ -99,6 +103,8 @@ export const getCoreSet = handler(async (event, context) => {
const queryValue = await dynamoDb.get(params);
return {
status: StatusCodes.SUCCESS,
body: queryValue,
body: {
Item: queryValue,
},
};
});
Loading

0 comments on commit 2f14557

Please sign in to comment.