From 872b061e90718cd95fbcae39e226fe6ad2e51b31 Mon Sep 17 00:00:00 2001 From: David <62899351+davidclaveau@users.noreply.github.com> Date: Mon, 29 Jul 2024 07:46:10 -0700 Subject: [PATCH] wip - update to missing v3 files --- arSam/handlers/activity/POST/index.js | 18 ++++--- .../activity/__tests__/activity.test.js | 2 - arSam/handlers/export-variance/GET/index.js | 24 +++++---- .../handlers/export-variance/GET/package.json | 11 ---- .../export-variance/invokable/index.js | 17 ++++--- .../export-variance/invokable/package.json | 9 ---- arSam/handlers/export/GET/index.js | 22 ++++---- arSam/handlers/export/GET/package.json | 11 ---- arSam/handlers/export/invokable/index.js | 4 +- arSam/handlers/export/invokable/package.json | 1 - arSam/handlers/fiscalYearEnd/POST/index.js | 9 ++-- .../handlers/fiscalYearEnd/POST/package.json | 1 - .../nameUpdate/__tests__/name-update.test.js | 2 +- arSam/handlers/nameUpdate/index.js | 22 +++++--- arSam/handlers/nameUpdate/package.json | 3 +- arSam/handlers/park/POST/index.js | 9 +++- arSam/handlers/subArea/DELETE/index.js | 22 +++++--- arSam/handlers/subArea/DELETE/package.json | 9 ---- arSam/handlers/subArea/POST/index.js | 5 +- .../subArea/__tests__/subArea.test.js | 37 +------------- arSam/handlers/variance/PUT/index.js | 4 +- arSam/layers/baseLayer/baseLayer.js | 51 ++++++++++++++----- arSam/layers/baseLayer/nodejs/package.json | 3 ++ arSam/layers/functionsLayer/Makefile | 6 +-- arSam/layers/functionsLayer/functionsLayer.js | 5 +- .../layers/functionsLayer/nodejs/package.json | 5 +- arSam/package.json | 1 + 27 files changed, 147 insertions(+), 166 deletions(-) delete mode 100644 arSam/handlers/export-variance/GET/package.json delete mode 100644 arSam/handlers/export-variance/invokable/package.json delete mode 100644 arSam/handlers/export/GET/package.json delete mode 100644 arSam/handlers/subArea/DELETE/package.json diff --git a/arSam/handlers/activity/POST/index.js b/arSam/handlers/activity/POST/index.js index 697f31b..27c9da2 100644 --- a/arSam/handlers/activity/POST/index.js +++ b/arSam/handlers/activity/POST/index.js @@ -1,7 +1,9 @@ -const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb'); const { DateTime } = require('luxon'); const { - dynamodb, + dynamoClient, + PutItemCommand, + DeleteItemCommand, + UpdateItemCommand, runQuery, TABLE_NAME, getOne, @@ -9,7 +11,9 @@ const { TIMEZONE, sendResponse, logger, - calculateVariance + calculateVariance, + marshall, + unmarshall, } = require('/opt/baseLayer'); const { EXPORT_VARIANCE_CONFIG } = require('/opt/constantsLayer'); @@ -132,7 +136,7 @@ async function deleteVariance(body) { logger.info('Deleting variance record:', params); - await dynamodb.deleteItem(params); + await dynamoClient.send(new DeleteItemCommand(params)); } async function checkVarianceTrigger(body) { @@ -228,7 +232,7 @@ async function createVariance(body, fields) { TableName: TABLE_NAME, Item: newObject, }; - await dynamodb.putItem(putObj); + await dynamoClient.send(new PutItemCommand(putObj)); } catch (e) { logger.error(e); } @@ -318,7 +322,7 @@ async function handleLockUnlock(record, lock, context) { ReturnValues: 'ALL_NEW', }; try { - const res = await dynamodb.updateItem(updateObj); + const res = await dynamoClient.send(new UpdateItemCommand(updateObj)); logger.info(`Updated record pk: ${record.pk}, sk: ${record.sk} `); const s = lock ? 'locked' : 'unlocked'; return sendResponse(200, { @@ -380,7 +384,7 @@ async function handleActivity(body, lock = false, context) { Item: newObject, }; - await dynamodb.putItem(putObject); + await dynamoClient.send(new PutItemCommand(putObject)); logger.info('Activity Updated.'); return sendResponse(200, body, context); } catch (err) { diff --git a/arSam/handlers/activity/__tests__/activity.test.js b/arSam/handlers/activity/__tests__/activity.test.js index 4553254..a7b4cdf 100644 --- a/arSam/handlers/activity/__tests__/activity.test.js +++ b/arSam/handlers/activity/__tests__/activity.test.js @@ -235,8 +235,6 @@ describe("Activity Test", () => { const doc = await dynamoClient.send(command); expect(doc?.Item).toBe(undefined); - - // Change year and create a new record const secondResponse = await activityPOST.handlePost( { diff --git a/arSam/handlers/export-variance/GET/index.js b/arSam/handlers/export-variance/GET/index.js index 78e12db..440fea8 100644 --- a/arSam/handlers/export-variance/GET/index.js +++ b/arSam/handlers/export-variance/GET/index.js @@ -1,10 +1,3 @@ -const { Lambda } = require("@aws-sdk/client-lambda"); -const { getSignedUrl } = require("@aws-sdk/s3-request-presigner"); -const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3"); -const { marshall } = require('@aws-sdk/util-dynamodb'); - -const defaultRegion = process.env.AWS_REGION || "ca-central-1"; -const s3Client = new S3Client({ region: defaultRegion }); const bucket = process.env.S3_BUCKET_DATA || "parks-ar-assets-tools"; const IS_OFFLINE = @@ -17,9 +10,18 @@ if (IS_OFFLINE) { options.endpoint = "http://localhost:3002"; } -const lambda = new Lambda(options); - -const { runQuery, TABLE_NAME, dynamodb, sendResponse, logger } = require("/opt/baseLayer"); +const { runQuery, + TABLE_NAME, + dynamoClient, + PutItemCommand, + marshall, + lambda, + s3Client, + GetObjectCommand, + getSignedUrl, + sendResponse, + logger +} = require("/opt/baseLayer"); const { createHash } = require('node:crypto'); const VARIANCE_EXPORT_FUNCTION_NAME = @@ -151,7 +153,7 @@ exports.handler = async (event, context) => { logger.debug('Creating new job:', varianceExportPutObj); let newJob; try { - newJob = await dynamodb.putItem(varianceExportPutObj); + newJob = await dynamoClient.send(new PutItemCommand(varianceExportPutObj)); // Check if there's already a report being generated. // If there are is no instance of a job or the job is 100% complete, generate a report. logger.debug('New job created:', newJob); diff --git a/arSam/handlers/export-variance/GET/package.json b/arSam/handlers/export-variance/GET/package.json deleted file mode 100644 index 3396848..0000000 --- a/arSam/handlers/export-variance/GET/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "varianceExportGetFunction", - "version": "1.0.0", - "author": "Digitalspace ", - "dependencies": { - "@aws-sdk/client-lambda": "^3.568.0", - "@aws-sdk/client-s3": "^3.568.0", - "@aws-sdk/util-dynamodb": "^3.614.0", - "@aws-sdk/s3-request-presigner": "^3.568.0" - } -} diff --git a/arSam/handlers/export-variance/invokable/index.js b/arSam/handlers/export-variance/invokable/index.js index efc7009..d8c83ac 100644 --- a/arSam/handlers/export-variance/invokable/index.js +++ b/arSam/handlers/export-variance/invokable/index.js @@ -1,10 +1,15 @@ -const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3'); -const s3Client = new S3Client({}); -const { marshall } = require('@aws-sdk/util-dynamodb'); const fs = require('fs'); - const { VARIANCE_CSV_SCHEMA, VARIANCE_STATE_DICTIONARY } = require("/opt/constantsLayer"); -const { getParks, TABLE_NAME, dynamodb, runQuery, logger } = require("/opt/baseLayer"); +const { getParks, + TABLE_NAME, + dynamoClient, + PutItemCommand, + marshall, + s3Client, + PutObjectCommand, + runQuery, + logger +} = require("/opt/baseLayer"); const FILE_PATH = process.env.FILE_PATH || "/tmp/"; const FILE_NAME = process.env.FILE_NAME || "A&R_Variance_Report"; @@ -139,7 +144,7 @@ async function updateJobEntry(jobObj) { TableName: TABLE_NAME, Item: marshall(jobObj) } - await dynamodb.putItem(putObj); + await dynamoClient.send(new PutItemCommand(putObj)); } async function getVarianceRecords(fiscalYearEnd, roles) { diff --git a/arSam/handlers/export-variance/invokable/package.json b/arSam/handlers/export-variance/invokable/package.json deleted file mode 100644 index 30be9ac..0000000 --- a/arSam/handlers/export-variance/invokable/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "varianceExportInvokableFunction", - "version": "1.0.0", - "author": "Digitalspace ", - "dependencies": { - "@aws-sdk/client-s3": "^3.568.0", - "@aws-sdk/util-dynamodb": "^3.614.0" - } -} diff --git a/arSam/handlers/export/GET/index.js b/arSam/handlers/export/GET/index.js index faeb4d0..ab03466 100644 --- a/arSam/handlers/export/GET/index.js +++ b/arSam/handlers/export/GET/index.js @@ -1,10 +1,4 @@ -const { Lambda } = require("@aws-sdk/client-lambda"); -const { getSignedUrl } = require("@aws-sdk/s3-request-presigner"); -const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3"); -const { marshall } = require('@aws-sdk/util-dynamodb'); -const defaultRegion = process.env.AWS_REGION || "ca-central-1"; -const s3Client = new S3Client({ region: defaultRegion }); const bucket = process.env.S3_BUCKET_DATA || "parks-ar-assets-tools"; const IS_OFFLINE = @@ -16,9 +10,19 @@ if (IS_OFFLINE) { // For local we use port 3002 because we're hitting an invokable options.endpoint = "http://localhost:3002"; } -const lambda = new Lambda(options); -const { runQuery, dynamodb, TABLE_NAME, sendResponse, logger } = require("/opt/baseLayer"); +const { runQuery, + dynamoClient, + PutItemCommand, + GetObjectCommand, + getSignedUrl, + s3Client, + marshall, + lambda, + TABLE_NAME, + sendResponse, + logger +} = require("/opt/baseLayer"); const { convertRolesToMD5 } = require("/opt/functionsLayer"); const EXPORT_FUNCTION_NAME = @@ -134,7 +138,7 @@ exports.handler = async (event, context) => { logger.debug(putObject); let newJob; try { - newJob = await dynamodb.putItem(putObject); + newJob = await dynamoClient.send(new PutItemCommand(putObject)); // Check if there's already a report being generated. // If there are is no instance of a job or the job is 100% complete, generate a report. logger.debug("Creating a new export job.", newJob); diff --git a/arSam/handlers/export/GET/package.json b/arSam/handlers/export/GET/package.json deleted file mode 100644 index 9f2c2d8..0000000 --- a/arSam/handlers/export/GET/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "exportGetFunction", - "version": "1.0.0", - "author": "Digitalspace ", - "dependencies": { - "@aws-sdk/client-lambda": "^3.568.0", - "@aws-sdk/client-s3": "^3.568.0", - "@aws-sdk/util-dynamodb": "^3.614.0", - "@aws-sdk/s3-request-presigner": "^3.568.0" - } -} diff --git a/arSam/handlers/export/invokable/index.js b/arSam/handlers/export/invokable/index.js index dbcfb90..8e04578 100644 --- a/arSam/handlers/export/invokable/index.js +++ b/arSam/handlers/export/invokable/index.js @@ -1,5 +1,3 @@ -const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); -const s3Client = new S3Client({}); const fs = require("fs"); const writeXlsxFile = require("write-excel-file/node"); const { @@ -8,6 +6,8 @@ const { getSubAreas, getRecords, logger, + s3Client, + PutObjectCommand } = require("/opt/baseLayer"); const { EXPORT_NOTE_KEYS, diff --git a/arSam/handlers/export/invokable/package.json b/arSam/handlers/export/invokable/package.json index 74d8990..81419b2 100644 --- a/arSam/handlers/export/invokable/package.json +++ b/arSam/handlers/export/invokable/package.json @@ -3,7 +3,6 @@ "version": "1.0.0", "author": "Digitalspace ", "dependencies": { - "@aws-sdk/client-s3": "^3.568.0", "write-excel-file": "^1.3.16" } } diff --git a/arSam/handlers/fiscalYearEnd/POST/index.js b/arSam/handlers/fiscalYearEnd/POST/index.js index b343429..9d6d6ee 100644 --- a/arSam/handlers/fiscalYearEnd/POST/index.js +++ b/arSam/handlers/fiscalYearEnd/POST/index.js @@ -1,11 +1,12 @@ -const { marshall } = require('@aws-sdk/util-dynamodb'); const { TABLE_NAME, - dynamodb, + dynamoClient, + PutItemCommand, TIMEZONE, FISCAL_YEAR_FINAL_MONTH, sendResponse, - logger + logger, + marshall } = require("/opt/baseLayer"); const { DateTime } = require("luxon"); @@ -93,7 +94,7 @@ async function putFiscalYear(isLocked, params) { TableName: TABLE_NAME, Item: marshall(newObject), }; - await dynamodb.putItem(putObj); + await dynamoClient.send(new PutItemCommand(putObj)); logger.debug("Updated fiscalYearEnd Object:", newObject); return newObject; } catch (err) { diff --git a/arSam/handlers/fiscalYearEnd/POST/package.json b/arSam/handlers/fiscalYearEnd/POST/package.json index c9bf047..d55698d 100644 --- a/arSam/handlers/fiscalYearEnd/POST/package.json +++ b/arSam/handlers/fiscalYearEnd/POST/package.json @@ -3,7 +3,6 @@ "version": "1.0.0", "author": "Digitalspace ", "dependencies": { - "@aws-sdk/util-dynamodb": "^3.614.0", "luxon": "^3.2.1" } } diff --git a/arSam/handlers/nameUpdate/__tests__/name-update.test.js b/arSam/handlers/nameUpdate/__tests__/name-update.test.js index 9e4a8ea..aa0895f 100644 --- a/arSam/handlers/nameUpdate/__tests__/name-update.test.js +++ b/arSam/handlers/nameUpdate/__tests__/name-update.test.js @@ -158,7 +158,7 @@ describe("Name Update Tests", () => { // Run the update await nameUpdateHandler.handler({}, null); - // Fetch the updated cache and check that it has been udpated + // Fetch the updated cache and check that it has been updated const cachedDocumentRes = await dynamoClient.send(new GetItemCommand(CACHED_DOCUMENT)); const cachedDocument = unmarshall(cachedDocumentRes.Item); expect(cachedDocument.displayName).toBe('Strathcona Park'); diff --git a/arSam/handlers/nameUpdate/index.js b/arSam/handlers/nameUpdate/index.js index 3c6a4a6..16b106e 100644 --- a/arSam/handlers/nameUpdate/index.js +++ b/arSam/handlers/nameUpdate/index.js @@ -1,6 +1,16 @@ const axios = require('axios'); -const { marshall } = require('@aws-sdk/util-dynamodb'); -const { runQuery, runScan, NAME_CACHE_TABLE_NAME, TABLE_NAME, ORCS_INDEX, dynamodb, logger } = require("/opt/baseLayer"); +const { runQuery, + runScan, + NAME_CACHE_TABLE_NAME, + TABLE_NAME, + ORCS_INDEX, + dynamoClient, + PutItemCommand, + BatchWriteItemCommand, + UpdateItemCommand, + marshall, + logger +} = require("/opt/baseLayer"); const DATA_REGISTER_NAME_ENDPOINT = process.env.DATA_REGISTER_NAME_ENDPOINT || 'https://zloys5cfvf.execute-api.ca-central-1.amazonaws.com/api/parks/names?status=established'; const DATA_REGISTER_NAME_API_KEY = process.env.DATA_REGISTER_NAME_API_KEY; const ESTABLISHED_STATE = 'established'; @@ -82,7 +92,7 @@ async function updateLocalCache(item) { TableName: NAME_CACHE_TABLE_NAME, Item: marshall(item, { removeUndefinedValues: true }) }; - await dynamodb.putItem(putItem); + await dynamoClient.send(new PutItemCommand(putItem)); logger.info("Update complete") } @@ -104,7 +114,7 @@ async function batchWriteCache(records) { if (batch.RequestItems[NAME_CACHE_TABLE_NAME].length === 25) { batchCount++; // Write the current batch and reset the batch - await dynamodb.batchWriteItem(batch); + await dynamoClient.send(new BatchWriteItemCommand(batch)); process.stdout.write(`.`); batch.RequestItems[NAME_CACHE_TABLE_NAME] = []; } @@ -114,7 +124,7 @@ async function batchWriteCache(records) { if (batch.RequestItems[NAME_CACHE_TABLE_NAME].length > 0) { batchCount++; logger.info(`writing final batch #${batchCount}`); - await dynamodb.batchWriteItem(batch); + await dynamoClient.send(new BatchWriteItemCommand(batch)); logger.info(`Complete.`); } } @@ -161,7 +171,7 @@ async function updateRecords(recordsToUpdate, updateObj) { try { process.stdout.write(`.`); - await dynamodb.updateItem(params); + await dynamoClient.send(new UpdateItemCommand(params)); } catch (e) { logger.info(e); // TODO: Fall through, but record error somehow? diff --git a/arSam/handlers/nameUpdate/package.json b/arSam/handlers/nameUpdate/package.json index efe8065..0f0ba16 100644 --- a/arSam/handlers/nameUpdate/package.json +++ b/arSam/handlers/nameUpdate/package.json @@ -3,8 +3,7 @@ "version": "1.0.0", "author": "Digitalspace ", "dependencies": { - "axios": "^0.21.1", - "@aws-sdk/util-dynamodb": "^3.614.0" + "axios": "^0.21.1" } } \ No newline at end of file diff --git a/arSam/handlers/park/POST/index.js b/arSam/handlers/park/POST/index.js index 030ed34..c1bc697 100644 --- a/arSam/handlers/park/POST/index.js +++ b/arSam/handlers/park/POST/index.js @@ -1,4 +1,9 @@ -const { dynamodb, TABLE_NAME, logger, sendResponse } = require("/opt/baseLayer"); +const { dynamoClient, + PutItemCommand, + TABLE_NAME, + logger, + sendResponse +} = require("/opt/baseLayer"); exports.handler = async (event, context) => { logger.debug("Park POST:", event); @@ -38,7 +43,7 @@ exports.handler = async (event, context) => { }; logger.debug("Creating park:", postObj); - const res = await dynamodb.putItem(postObj); + const res = await dynamoClient.send(new PutItemCommand(postObj)); logger.info("Park Created"); logger.debug("Result:", res); return sendResponse(200, res); diff --git a/arSam/handlers/subArea/DELETE/index.js b/arSam/handlers/subArea/DELETE/index.js index f74bbe3..4301130 100644 --- a/arSam/handlers/subArea/DELETE/index.js +++ b/arSam/handlers/subArea/DELETE/index.js @@ -1,6 +1,14 @@ -const { marshall } = require('@aws-sdk/util-dynamodb'); const { requirePermissions } = require("/opt/permissionLayer"); -const { getOne, TABLE_NAME, dynamodb, runQuery, sendResponse, logger } = require("/opt/baseLayer"); +const { getOne, + TABLE_NAME, + dynamoClient, + UpdateItemCommand, + DeleteItemCommand, + marshall, + runQuery, + sendResponse, + logger +} = require("/opt/baseLayer"); exports.handler = async (event, context) => { logger.info("SubArea delete"); @@ -64,7 +72,7 @@ async function deleteActivityRecords(subAreaId, activities, context) { } }; logger.info("Deleting activity config:", params); - const response = await dynamodb.deleteItem(params); + const response = await dynamoClient.send(new DeleteItemCommand(params)); logger.info("Response:", response); } @@ -99,7 +107,7 @@ async function deleteActivityRecord(pk, sk) { logger.info("Deleting activity record:", params); - const response = await dynamodb.deleteItem(params); + const response = await dynamoClient.send(new DeleteItemCommand(params)); logger.info("Response:", response); return response; @@ -116,7 +124,7 @@ async function deleteSubAreaRecords(subAreaId, orcs, context) { ReturnValues: 'ALL_OLD' }; logger.info("Deleting subArea records:", params); - const response = await dynamodb.deleteItem(params); + const response = await dynamoClient.send(new DeleteItemCommand(params)); logger.info("Activities deleted:", response.Attributes?.activities.SS); return response.Attributes?.activities.SS; } @@ -137,7 +145,7 @@ async function archiveSubAreaRecord(subAreaId, orcs, context) { ReturnValues: 'ALL_NEW' }; logger.info("Archiving subArea records:", params); - const response = await dynamodb.updateItem(params); + const response = await dynamoClient.send(new UpdateItemCommand(params)); logger.info("response:", response); return response; } @@ -170,7 +178,7 @@ async function deleteSubAreaFromPark(subAreaId, orcs, context) { ReturnValues: 'ALL_NEW' }; - const response = await dynamodb.updateItem(updateParkObject); + const response = await dynamoClient.send(new UpdateItemCommand(updateParkObject)); logger.info("Park Object after update:", response.Attributes); return response.Attributes; } diff --git a/arSam/handlers/subArea/DELETE/package.json b/arSam/handlers/subArea/DELETE/package.json deleted file mode 100644 index 684e735..0000000 --- a/arSam/handlers/subArea/DELETE/package.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "subAreaDeleteFunction", - "version": "1.0.0", - "author": "Digitalspace ", - "dependencies": { - "@aws-sdk/util-dynamodb": "^3.614.0" - } -} - \ No newline at end of file diff --git a/arSam/handlers/subArea/POST/index.js b/arSam/handlers/subArea/POST/index.js index 8b2e3b7..d9f453e 100644 --- a/arSam/handlers/subArea/POST/index.js +++ b/arSam/handlers/subArea/POST/index.js @@ -1,5 +1,6 @@ const { - dynamodb, + dynamoClient, + TransactWriteItemsCommand, incrementAndGetNextSubAreaID, getOne, logger, @@ -92,7 +93,7 @@ exports.handler = async (event, context) => { }); } - const res = await dynamodb.transactWriteItems(transactionObj); + const res = await dynamoClient.send(new TransactWriteItemsCommand(transactionObj)); logger.debug('res:', res); // Add Keycloak role diff --git a/arSam/handlers/subArea/__tests__/subArea.test.js b/arSam/handlers/subArea/__tests__/subArea.test.js index c88eec7..de3b661 100644 --- a/arSam/handlers/subArea/__tests__/subArea.test.js +++ b/arSam/handlers/subArea/__tests__/subArea.test.js @@ -186,41 +186,6 @@ describe("Sub Area Test", () => { expect(config2.lastID).toBeGreaterThan(lastID); }); - test("Handler - 403 Sub Area POST Unauthenticated", async () => { - const axios = require("axios"); - jest.mock("axios"); - axios.post.mockImplementation(() => - Promise.resolve({ statusCode: 200, data: {} }) - ); - - const subAreaPOST = require("../POST/index"); - const response = await subAreaPOST.handler( - { - headers: { - Authorization: "Bearer " + token, - }, - requestContext: { - authorizer: { - roles: "[\"public\"]", - isAdmin: false, - isAuthenticated: false, - }, - }, - body: JSON.stringify({ - activities: ["Day Use"], - orcs: "0041", - managementArea: "South Fraser", - section: "South Coast", - region: "South Coast", - bundle: "South Fraser", - subAreaName: "Clear Creek", - }), - }, - null - ); - expect(response.statusCode).toBe(403); - }); - test("Handler - 403 Sub Area POST Unauthenticated Invalid User", async () => { const axios = require("axios"); jest.mock("axios"); @@ -256,7 +221,7 @@ describe("Sub Area Test", () => { expect(response.statusCode).toBe(403); }); - test("Handler - 403 Sub Area POST Unauthenticated", async () => { + test("Handler - 403 Sub Area POST Unauthenticated ", async () => { const axios = require("axios"); jest.mock("axios"); axios.post.mockImplementation(() => diff --git a/arSam/handlers/variance/PUT/index.js b/arSam/handlers/variance/PUT/index.js index a3f370e..71137bd 100644 --- a/arSam/handlers/variance/PUT/index.js +++ b/arSam/handlers/variance/PUT/index.js @@ -1,4 +1,4 @@ -const { dynamodb, TABLE_NAME, logger, sendResponse } = require("/opt/baseLayer"); +const { dynamoClient, UpdateItemCommand, TABLE_NAME, logger, sendResponse } = require("/opt/baseLayer"); exports.handler = async (event, context) => { logger.debug("Variance PUT:", event); @@ -66,7 +66,7 @@ exports.handler = async (event, context) => { params.UpdateExpression = `SET ${updateExpressions.join(', ')}`; } - const res = await dynamodb.updateItem(params); + const res = await dynamoClient.send(new UpdateItemCommand(params)); logger.info("Variance updated"); logger.debug("Result:", res); return sendResponse(200, res); diff --git a/arSam/layers/baseLayer/baseLayer.js b/arSam/layers/baseLayer/baseLayer.js index a9c04f7..3a140c9 100644 --- a/arSam/layers/baseLayer/baseLayer.js +++ b/arSam/layers/baseLayer/baseLayer.js @@ -86,8 +86,20 @@ function calculateVariance( } // DynamoUtils -const { DynamoDB } = require('@aws-sdk/client-dynamodb'); +const { DynamoDBClient, + GetItemCommand, + QueryCommand, + PutItemCommand, + UpdateItemCommand, + BatchWriteItemCommand, + TransactWriteItemsCommand, + ScanCommand, + DeleteItemCommand +} = require('@aws-sdk/client-dynamodb'); const { marshall, unmarshall } = require('@aws-sdk/util-dynamodb'); +const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3"); +const { getSignedUrl } = require("@aws-sdk/s3-request-presigner"); +const { Lambda } = require("@aws-sdk/client-lambda"); const TABLE_NAME = process.env.TABLE_NAME || "ParksAr-tests"; const ORCS_INDEX = process.env.ORCS_INDEX || "orcs-index"; @@ -130,9 +142,9 @@ const RECORD_ACTIVITY_LIST = [ "Boating", ]; -const dynamodb = new DynamoDB(options); - -exports.dynamodb = new DynamoDB(); +const dynamoClient = new DynamoDBClient(options); +const s3Client = new S3Client(options); +const lambda = new Lambda(options); // simple way to return a single Item by primary key. async function getOne(pk, sk) { @@ -141,7 +153,7 @@ async function getOne(pk, sk) { TableName: TABLE_NAME, Key: marshall({ pk, sk }), }; - let item = await dynamodb.getItem(params); + let item = await dynamoClient.send(new GetItemCommand(params)); if (item?.Item) { return unmarshall(item.Item); } @@ -151,17 +163,18 @@ async function getOne(pk, sk) { // (1MB) unless they are explicitly specified to retrieve more. // TODO: Ensure the returned object has the same structure whether results are paginated or not. async function runQuery(query, paginated = false) { - logger.debug("query:", query); + logger.info('query:', query); let data = []; let pageData = []; let page = 0; - + const command = new QueryCommand(query); + do { page++; if (pageData?.LastEvaluatedKey) { - query.ExclusiveStartKey = pageData.LastEvaluatedKey; + command.input.ExclusiveStartKey = pageData.LastEvaluatedKey; } - pageData = await dynamodb.query(query); + pageData = await dynamoClient.send(command); data = data.concat( pageData.Items.map((item) => { return unmarshall(item); @@ -203,7 +216,7 @@ async function runScan(query, paginated = false) { if (pageData?.LastEvaluatedKey) { query.ExclusiveStartKey = pageData.LastEvaluatedKey; } - pageData = await dynamodb.scan(query); + pageData = await dynamoClient.send(new ScanCommand(query)); data = data.concat( pageData.Items.map((item) => { return unmarshall(item); @@ -276,7 +289,7 @@ async function batchWrite(items, action = 'put') { } } try { - await dynamodb.batchWriteItem(batchChunk); + await dynamoClient.send(new BatchWriteItemCommand(batchChunk)); } catch (err) { for (const item of items) { logger.info('item.fields:', item.fields); @@ -350,7 +363,7 @@ async function incrementAndGetNextSubAreaID() { }, ReturnValues: "UPDATED_NEW", }; - const response = await dynamodb.updateItem(configUpdateObj); + const response = await dynamoClient.send(new UpdateItemCommand(configUpdateObj)); return response?.Attributes?.lastID?.N; } @@ -371,7 +384,19 @@ module.exports = { TABLE_NAME, ORCS_INDEX, NAME_CACHE_TABLE_NAME, - dynamodb, + dynamoClient, + PutItemCommand, + UpdateItemCommand, + DeleteItemCommand, + BatchWriteItemCommand, + TransactWriteItemsCommand, + s3Client, + PutObjectCommand, + GetObjectCommand, + marshall, + unmarshall, + getSignedUrl, + lambda, runQuery, runScan, getOne, diff --git a/arSam/layers/baseLayer/nodejs/package.json b/arSam/layers/baseLayer/nodejs/package.json index 812e823..caad788 100644 --- a/arSam/layers/baseLayer/nodejs/package.json +++ b/arSam/layers/baseLayer/nodejs/package.json @@ -6,6 +6,9 @@ "dependencies": { "@aws-sdk/client-dynamodb": "^3.614.0", "@aws-sdk/util-dynamodb": "^3.614.0", + "@aws-sdk/s3-request-presigner": "^3.568.0", + "@aws-sdk/client-s3": "^3.568.0", + "@aws-sdk/client-lambda": "^3.568.0", "winston": "^3.8.0" } } diff --git a/arSam/layers/functionsLayer/Makefile b/arSam/layers/functionsLayer/Makefile index f0e0178..d97c863 100644 --- a/arSam/layers/functionsLayer/Makefile +++ b/arSam/layers/functionsLayer/Makefile @@ -3,9 +3,5 @@ build-FunctionsLayer: cp -r * "$(ARTIFACTS_DIR)" # remove makefile in artifacts directory rm "$(ARTIFACTS_DIR)/Makefile" - # install dependencies in artifacts directory - cd nodejs && npm install --prefix "$(ARTIFACTS_DIR)"/nodejs - # symlink to node_modules folder for testing purposes - cd "$(ARTIFACTS_DIR)" && ln -s "$(ARTIFACTS_DIR)"/nodejs/node_modules node_modules # remove package.json to avoid rebuilding when changes don't relate to dependencies - rm "$(ARTIFACTS_DIR)/nodejs/package.json" + rm "$(ARTIFACTS_DIR)/nodejs/package.json" diff --git a/arSam/layers/functionsLayer/functionsLayer.js b/arSam/layers/functionsLayer/functionsLayer.js index e461d05..2dab6f7 100644 --- a/arSam/layers/functionsLayer/functionsLayer.js +++ b/arSam/layers/functionsLayer/functionsLayer.js @@ -1,5 +1,4 @@ -const { marshall } = require('@aws-sdk/util-dynamodb'); -const { dynamodb } = require("/opt/baseLayer"); +const { dynamoClient, PutItemCommand, marshall } = require("/opt/baseLayer"); const { createHash } = require("node:crypto"); function convertRolesToMD5(roles, prefix = "") { @@ -24,7 +23,7 @@ async function updateJobEntry(jobObj, tableName) { TableName: tableName, Item: newObject, }; - await dynamodb.putItem(putObject); + await dynamoClient.send(new PutItemCommand(putObject)); } module.exports = { diff --git a/arSam/layers/functionsLayer/nodejs/package.json b/arSam/layers/functionsLayer/nodejs/package.json index 415e89d..6f5617d 100644 --- a/arSam/layers/functionsLayer/nodejs/package.json +++ b/arSam/layers/functionsLayer/nodejs/package.json @@ -2,8 +2,5 @@ "name": "functionsLayer", "version": "0.0.1", "author": "Digitalspace ", - "license": "MIT", - "dependencies": { - "@aws-sdk/util-dynamodb": "^3.614.0" - } + "license": "MIT" } diff --git a/arSam/package.json b/arSam/package.json index 3150a36..68482de 100644 --- a/arSam/package.json +++ b/arSam/package.json @@ -7,6 +7,7 @@ }, "jest": { "verbose": true, + "testTimeout": 10000, "modulePathIgnorePatterns": [ "/__tests__", "/.aws-sam/"