Skip to content

Commit

Permalink
Merge pull request #413 from ubc-biztech/registration-gsi-v2
Browse files Browse the repository at this point in the history
Switch Registrations service to using DB query with GSI
  • Loading branch information
voctory authored Dec 4, 2024
2 parents 150d56f + 06a45f6 commit aadc5e9
Show file tree
Hide file tree
Showing 14 changed files with 6,811 additions and 3,531 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@
"object-property-newline": ["warn", {}],
"object-curly-newline": ["warn", "always"],
"eqeqeq": ["warn", "always"]
}
},
"overrides": [{
"files": ["scripts/initlocal.js"],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
}
}]
}
31 changes: 11 additions & 20 deletions constants/test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
export const INTEGRATION_TEST_USER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_USER_EMAIL =
"[email protected]";
export const INTEGRATION_TEST_PERSISTENT_USER_EMAIL_2 =
"[email protected]";
export const INTEGRATION_TEST_NON_EXISTANT_USER_EMAIL =
"[email protected]";
export const INTEGRATION_TEST_USER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_USER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_USER_EMAIL_2 = "[email protected]";
export const INTEGRATION_TEST_NON_EXISTANT_USER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_EVENT_ID = "__INTEGRATION_TEST_EVENT_POST";
export const INTEGRATION_TEST_YEAR = 2020;
export const INTEGRATION_TEST_PERSISTENT_EVENT_ID = "__INTEGRATION_TEST_EVENT";
export const INTEGRATION_TEST_PERSISTENT_YEAR = 2020;
export const INTEGRATION_TEST_PERSISTENT_EVENT_ID_2 =
"__INTEGRATION_TEST_EVENT_2";
export const INTEGRATION_TEST_PERSISTENT_EVENT_ID_2 = "__INTEGRATION_TEST_EVENT_2";
export const INTEGRATION_TEST_PERSISTENT_YEAR_2 = 2020;
export const INTEGRATION_TEST_NON_EXISTANT_EVENT_ID =
"someRandomEventThatDoesNotExist123";
export const INTEGRATION_TEST_NON_EXISTANT_EVENT_ID = "someRandomEventThatDoesNotExist123";
export const INTEGRATION_TEST_NON_EXISTANT_YEAR = 1234;
export const INTEGRATION_TEST_PRIZE_ID = "__INTEGRATION_TEST_PRIZE_POST";
export const INTEGRATION_TEST_PERSISTENT_PRIZE_ID = "__INTEGRATION_TEST_PRIZE";
export const INTEGRATION_TEST_NON_EXISTANT_PRIZE_ID =
"someRandomPrizeThatDoesNotExist123";
export const INTEGRATION_TEST_MEMBER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_MEMBER_EMAIL =
"[email protected]";
export const INTEGRATION_TEST_PERSISTENT_MEMBER_EMAIL_2 =
"[email protected]";
export const INTEGRATION_TEST_NON_EXISTENT_MEMBER_EMAIL =
"[email protected]";
export const INTEGRATION_TEST_NON_EXISTANT_PRIZE_ID = "someRandomPrizeThatDoesNotExist123";
export const INTEGRATION_TEST_MEMBER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_MEMBER_EMAIL = "[email protected]";
export const INTEGRATION_TEST_PERSISTENT_MEMBER_EMAIL_2 = "[email protected]";
export const INTEGRATION_TEST_NON_EXISTENT_MEMBER_EMAIL = "[email protected]";

export const INTEGRATION_TEST_PERSISTENT_REGISTRATION_PARAMETERS = {
eventId: INTEGRATION_TEST_PERSISTENT_EVENT_ID_2, // has capacity of "1"
Expand Down
31 changes: 31 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,36 @@ export default {
const errorResponse = this.dynamoErrorResponse(err);
throw errorResponse;
}
},

query: async function (table, indexName, keyCondition) {
try {
const params = {
TableName: table + (process.env.ENVIRONMENT || ""),
KeyConditionExpression: keyCondition.expression,
ExpressionAttributeValues: keyCondition.expressionValues
};

if (keyCondition.expressionNames) {
params.ExpressionAttributeNames = keyCondition.expressionNames;
}

if (indexName) {
params.IndexName = indexName;
}

const command = new QueryCommand(params);
const result = await docClient.send(command);

if (!result) {
console.warn("Query returned no result");
return [];
}

return result.Items || [];
} catch (err) {
console.error("DYNAMO DB ERROR", err);
return null;
}
}
};
55 changes: 30 additions & 25 deletions lib/testHelpers.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import {
Lambda
LambdaClient, InvokeCommand
} from "@aws-sdk/client-lambda";

const OPTIONS = {
region: "us-west-2"
};
const API_PREFIX = "biztechApi";
const lambda = new Lambda(OPTIONS);
const lambdaClient = new LambdaClient(OPTIONS);

export default {

/**
* Create a promise that invokes specified lambda function for integration testing
* @param {*} functionName - the lambda function name
* @param {*} payload - JSON string with function payload. Empty by default
* Invokes specified lambda function for integration testing
* @param {string} service - the service name
* @param {string} functionName - the lambda function name
* @param {string} payload - JSON string with function payload. Empty by default
* @returns {Promise<[number, object]>} Promise resolving to [statusCode, responseBody]
*/
invokeLambda: function(service, functionName, payload = "") {
invokeLambda: async function(service, functionName, payload = "") {
const serviceName = service === "" ? API_PREFIX : `${API_PREFIX}-${service}`;

let params = {
const params = {
FunctionName: `${serviceName}-dev-${functionName}`,
Payload: payload ? Buffer.from(payload) : undefined
};
if (payload) {
params.Payload = payload;
}
return new Promise((resolve, reject) => {
lambda.invoke(params, function(err, data) {
if (err) {
// something went wrong when invoking function!
console.error(err);
reject(err);
}

const payload = JSON.parse(data["Payload"]);
if (!payload.hasOwnProperty("statusCode") && !payload.hasOwnProperty("body")) {
resolve([null, null]);
}
resolve([payload.statusCode, JSON.parse(payload.body)]);
});
});
try {
const command = new InvokeCommand(params);
const response = await lambdaClient.send(command);

// Convert Uint8Array to string and parse JSON
const responsePayload = JSON.parse(new TextDecoder().decode(response.Payload));

if (!responsePayload.hasOwnProperty("statusCode") && !responsePayload.hasOwnProperty("body")) {
return [null, null];
}

return [
responsePayload.statusCode,
JSON.parse(responsePayload.body)
];
} catch (error) {
console.error("Lambda invocation error:", error);
throw error;
}
},
};
Loading

0 comments on commit aadc5e9

Please sign in to comment.