Skip to content

Commit

Permalink
Merge pull request #432 from ubc-biztech/qr-script-update
Browse files Browse the repository at this point in the history
Create Extra NFCs
  • Loading branch information
voctory authored Jan 19, 2025
2 parents fcf09c7 + bd8134c commit 66f0381
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 40 deletions.
23 changes: 22 additions & 1 deletion scripts/generateNFCsForEventRegistrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const generateNFCsForEventRegistrations = async (eventID, year) => {
registrationID: registration.id,
profileID: profileID
}
}, "biztechQRs" + (process.env.ENVIRONMENT || ""));
}, "biztechQRs");

// Get existing profile
const existingProfile = await client.send(new GetCommand({
Expand Down Expand Up @@ -125,4 +125,25 @@ const generateNFCsForEventRegistrations = async (eventID, year) => {

console.log(`created ${count} NFCs`);
};

/*
This function is used to create backup NFCs in our DB so that we have extra QRs/NFCs day of.
*/
const createExtraNFCs = async (eventID, year, count) => {
for (let i = 0; i < count; i++) {
const nfc = await create({
id: humanId(),
"eventID;year": `${eventID};${year}`,
type: "NFC_ATTENDEE",
isUnlimitedScans: true,
data: {
registrationID: "DEFAULT",
profileID: "DEFAULT"
}
}, "biztechQRs");
}
};


generateNFCsForEventRegistrations("blueprint", 2025);
// createExtraNFCs("blueprint", 2025, 15);
34 changes: 18 additions & 16 deletions scripts/generateQuestsForEventRegistrations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { PutCommand, QueryCommand } from "@aws-sdk/lib-dynamodb";
import { QUESTS_TABLE, USER_REGISTRATIONS_TABLE } from "../constants/tables.js";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
PutCommand, QueryCommand
} from "@aws-sdk/lib-dynamodb";
import {
QUESTS_TABLE, USER_REGISTRATIONS_TABLE
} from "../constants/tables.js";
import {
DynamoDBClient
} from "@aws-sdk/client-dynamodb";

const awsConfig = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
Expand Down Expand Up @@ -40,19 +46,15 @@ const QUESTS = [
];

const create = async (item, table) => {
try {
const params = {
Item: item,
TableName: table + (process.env.ENVIRONMENT || ""),
ConditionExpression: "attribute_not_exists(id)"
};
const params = {
Item: item,
TableName: table + (process.env.ENVIRONMENT || ""),
ConditionExpression: "attribute_not_exists(id)"
};

const command = new PutCommand(params);
const res = await docClient.send(command);
return res;
} catch (err) {
throw err;
}
const command = new PutCommand(params);
const res = await docClient.send(command);
return res;
};

const userQuestsArray = (id, eventString) => {
Expand Down Expand Up @@ -119,4 +121,4 @@ const questsForEventRegistrations = async (eventID, year) => {
console.log(`Created ${count} Quest Entries for ${users} Users`);
};

await questsForEventRegistrations("blueprint", 2025);
questsForEventRegistrations("blueprint", 2025);
43 changes: 31 additions & 12 deletions services/profiles/handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import db from "../../lib/db.js";
import helpers from "../../lib/handlerHelpers.js";
import { isEmpty } from "../../lib/utils.js";
import { humanId } from "human-id";
import {
isEmpty
} from "../../lib/utils.js";
import {
humanId
} from "human-id";

const PROFILES_TABLE = "biztechProfiles";
const REGISTRATIONS_TABLE = "biztechRegistrations";
Expand All @@ -10,15 +14,26 @@ const QRS_TABLE = "biztechQRs";
export const createProfile = async (event, ctx, callback) => {
try {
const data = JSON.parse(event.body);

// Validate input
helpers.checkPayloadProps(data, {
email: { required: true, type: "string" },
eventID: { required: true, type: "string" },
year: { required: true, type: "number" }
email: {
required: true,
type: "string"
},
eventID: {
required: true,
type: "string"
},
year: {
required: true,
type: "number"
}
});

const { email, eventID, year } = data;
const {
email, eventID, year
} = data;
const eventIDAndYear = `${eventID};${year}`;

// Check if profile already exists
Expand Down Expand Up @@ -143,7 +158,9 @@ export const getProfile = async (event, ctx, callback) => {
throw helpers.missingPathParamResponse("profileID");
}

const { profileID } = event.pathParameters;
const {
profileID
} = event.pathParameters;

// Query using the GSI
const result = await db.query(
Expand All @@ -163,7 +180,7 @@ export const getProfile = async (event, ctx, callback) => {

// Filter to only include public fields
const publicProfile = filterPublicProfileFields(result[0]);

const response = helpers.createResponse(200, publicProfile);
callback(null, response);
return response;
Expand All @@ -180,7 +197,9 @@ export const getProfileByEmail = async (event, ctx, callback) => {
throw helpers.missingPathParamResponse("email, eventID, or year");
}

const { email, eventID, year } = event.pathParameters;
const {
email, eventID, year
} = event.pathParameters;
const eventIDAndYear = `${eventID};${year}`;

// Get profile by email and eventID;year
Expand All @@ -195,12 +214,12 @@ export const getProfileByEmail = async (event, ctx, callback) => {
const response = helpers.createResponse(200, {
profileID: profile.profileID
});

callback(null, response);
return response;
} catch (err) {
console.error(err);
callback(null, err);
return null;
}
};
};
43 changes: 32 additions & 11 deletions services/profiles/test/profileTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

const mochaPlugin = require("serverless-mocha-plugin");
const expect = mochaPlugin.chai.expect;
const { mockClient } = require("aws-sdk-client-mock");
const { DynamoDBDocumentClient, GetCommand, PutCommand } = require("@aws-sdk/lib-dynamodb");
const {
mockClient
} = require("aws-sdk-client-mock");
const {
DynamoDBDocumentClient, GetCommand, PutCommand
} = require("@aws-sdk/lib-dynamodb");
const sinon = require("sinon");

const ddbMock = mockClient(DynamoDBDocumentClient);
Expand Down Expand Up @@ -66,32 +70,49 @@ describe("Profiles Service", () => {
ddbMock.on(GetCommand).callsFake((params) => {
if (params.TableName.includes("biztechRegistrations")) {
if (params.Key.id === testEmail && params.Key["eventID;year"] === `${testEventId};${testYear}`) {
return { Item: mockRegistration };
return {
Item: mockRegistration
};
}
return { Item: undefined };
return {
Item: undefined
};
} else if (params.TableName.includes("biztechProfiles")) {
if (params.Key.email === testEmail && params.Key["eventID;year"] === `${testEventId};${testYear}`) {
return { Item: mockProfile };
return {
Item: mockProfile
};
}
return { Item: undefined };
return {
Item: undefined
};
}
return { Item: undefined };
return {
Item: undefined
};
});

// Mock DynamoDB PutCommand
ddbMock.on(PutCommand).callsFake((params) => {
if (params.TableName.includes("biztechProfiles")) {
return { Item: params.Item };
return {
Item: params.Item
};
}
return {};
return {
};
});

// Stub db module functions
sinon.stub(require("../../../lib/db"), "getOne").callsFake(async (id, table, sortKey) => {
const params = {
TableName: table,
Key: {
...(table.includes("biztechRegistrations") ? { id } : { email: id }),
...(table.includes("biztechRegistrations") ? {
id
} : {
email: id
}),
...sortKey
}
};
Expand Down Expand Up @@ -243,4 +264,4 @@ describe("Profiles Service", () => {
expect(body).to.deep.include(mockProfile);
});
});
});
});

0 comments on commit 66f0381

Please sign in to comment.