Skip to content

Commit

Permalink
Sticker Service Refactor + Constants (#388)
Browse files Browse the repository at this point in the history
* Refactor + removed legacy tests

* Refactor
  • Loading branch information
kevinxiao27 authored Oct 16, 2024
1 parent 8349c94 commit f117db4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 211 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"serverless-bundle": "^6.1.0",
"serverless-domain-manager": "^5.1.0",
"serverless-mocha-plugin": "^1.12.0",
"serverless-offline": "3.20.2",
"serverless-prune-plugin": "^2.0.2"
},
"pre-commit": [
Expand Down
17 changes: 17 additions & 0 deletions services/stickers/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const STICKER_TYPE_GOLDEN = "golden";
export const VOTER_ROLE = "voter";
export const ADMIN_ROLE = "admin";
export const STATE_KEY = "STATE";
export const ACTION_TYPES = {
score: "score",
state: "state",
sticker: "sticker",
sync: "sync",
error: "error"
};

export const ADMIN_EVENTS = {
start: "start",
end: "end",
changeTeam: "changeTeam"
};
86 changes: 47 additions & 39 deletions services/stickers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import {
} from "../../constants/tables";
import { QueryCommand } from "@aws-sdk/lib-dynamodb";
import docClient from "../../lib/docClient";
import {
ACTION_TYPES,
ADMIN_EVENTS,
ADMIN_ROLE,
STATE_KEY,
STICKER_TYPE_GOLDEN,
VOTER_ROLE
} from "./constants";

/**
* Connection handler
Expand All @@ -32,7 +40,7 @@ export const connectHandler = async (event, ctx, callback) => {
const connectionID = event.requestContext.connectionId;
const obj = {
connectionID,
role: "voter",
role: VOTER_ROLE,
userID: ""
};

Expand Down Expand Up @@ -65,7 +73,7 @@ export const disconnectHandler = async (event, ctx, callback) => {
* id: string
* }`
*
* to sync connection to admin role and state, set id = "admin"
* to sync connection to admin role and state, set id = ADMIN_ROLE
*/
export const syncHandler = async (event, ctx, callback) => {
const body = JSON.parse(event.body);
Expand All @@ -83,14 +91,14 @@ export const syncHandler = async (event, ctx, callback) => {
let state = await fetchState();
const { isVoting, teamName } = state.Item;

if (body.id === "admin") {
if (body.id === ADMIN_ROLE) {
return await syncAdmin(event, teamName, isVoting);
}

if (!isVoting) {
await sendMessage(event, {
status: 200,
action: "sync",
action: ACTION_TYPES.sync,
data: {
isVoting,
teamName,
Expand Down Expand Up @@ -138,7 +146,7 @@ export const adminHandler = async (event, ctx, callback) => {
}
const action = body.event;

if (action === "changeTeam" && !body.hasOwnProperty("team")) {
if (action === ACTION_TYPES.changeTeam && !body.hasOwnProperty("team")) {
const errMessage = checkPayloadProps(body, {
team: {
required: true,
Expand All @@ -153,37 +161,37 @@ export const adminHandler = async (event, ctx, callback) => {
try {
let payload;
switch (action) {
case "start": {
case ADMIN_EVENTS.start: {
let state = {
isVoting: true
};
payload = await updateSocket(state, "STATE");
await notifyAdmins(state, "state", event);
await notifyVoters(state, "state", event);
payload = await updateSocket(state, STATE_KEY);
await notifyAdmins(state, ACTION_TYPES.state, event);
await notifyVoters(state, ACTION_TYPES.state, event);
return {
statusCode: 200
};
}

case "end": {
case ADMIN_EVENTS.end: {
let state = {
isVoting: false
};
payload = await updateSocket(state, "STATE");
await notifyAdmins(state, "state", event);
await notifyVoters(state, "state", event);
payload = await updateSocket(state, STATE_KEY);
await notifyAdmins(state, ACTION_TYPES.state, event);
await notifyVoters(state, ACTION_TYPES.state, event);
return {
statusCode: 200
};
}

case "changeTeam": {
case ADMIN_EVENTS.changeTeam: {
let state = {
teamName: body.team
};
payload = updateSocket(state, "STATE");
await notifyAdmins(state, "state", event);
await notifyVoters(state, "state", event);
payload = await updateSocket(state, STATE_KEY);
await notifyAdmins(state, ACTION_TYPES.state, event);
await notifyVoters(state, ACTION_TYPES.state, event);
return {
statusCode: 200
};
Expand All @@ -192,7 +200,7 @@ export const adminHandler = async (event, ctx, callback) => {
default: {
await sendMessage(event, {
status: "400",
action: "error",
action: ACTION_TYPES.error,
message: "unrecognized event type"
});
return {
Expand All @@ -201,10 +209,10 @@ export const adminHandler = async (event, ctx, callback) => {
}
}
} catch (error) {
console.log(error);
console.error(error);
await sendMessage(event, {
status: "500",
action: "error",
action: ACTION_TYPES.error,
message: "Internal Server Error"
});
return {
Expand Down Expand Up @@ -235,7 +243,7 @@ export const stickerHandler = async (event, ctx, callback) => {
if (!isVoting) {
await sendMessage(event, {
status: 400,
action: "error",
action: ACTION_TYPES.error,
message: "voting is not open"
});
return {
Expand All @@ -261,12 +269,12 @@ export const stickerHandler = async (event, ctx, callback) => {
const { id, stickerName } = body;

let isGoldenInDB;
if (stickerName === "golden") {
if (stickerName === STICKER_TYPE_GOLDEN) {
try {
const command = new QueryCommand({
IndexName: "stickerName",
ExpressionAttributeValues: {
":sname": "golden",
":sname": STICKER_TYPE_GOLDEN,
":uid": id
},
KeyConditionExpression: "stickerName = :sname",
Expand All @@ -278,13 +286,13 @@ export const stickerHandler = async (event, ctx, callback) => {
isGoldenInDB = response.Items.length > 0;
} catch (error) {
let errResponse = db.dynamoErrorResponse(error);
console.log(errResponse);
console.error(errResponse);
}

if (isGoldenInDB) {
await sendMessage(event, {
status: 400,
action: "error",
action: ACTION_TYPES.error,
message: "Already submitted golden sticker."
});
return {
Expand Down Expand Up @@ -316,10 +324,10 @@ export const stickerHandler = async (event, ctx, callback) => {
true
);
} catch (error) {
console.log(error);
console.error(error);
await sendMessage(event, {
status: 500,
action: "error",
action: ACTION_TYPES.error,
message: "Failed to create sticker"
});
return {
Expand All @@ -330,7 +338,7 @@ export const stickerHandler = async (event, ctx, callback) => {

await sendMessage(event, {
status: 200,
action: "sticker",
action: ACTION_TYPES.sticker,
message: stickerName + " was created for user " + id,
data: {
teamName,
Expand All @@ -352,7 +360,7 @@ export const stickerHandler = async (event, ctx, callback) => {
userID: id
}
},
"sticker",
ACTION_TYPES.sticker,
event
);
return {
Expand All @@ -374,7 +382,7 @@ export const stickerHandler = async (event, ctx, callback) => {
} else {
await sendMessage(event, {
status: 400,
action: "error",
action: ACTION_TYPES.error,
message: "Used up all " + sticker.limit + " stickers"
});
return {
Expand All @@ -390,13 +398,13 @@ export const stickerHandler = async (event, ctx, callback) => {
stickerName,
userID: id
},
"sticker",
ACTION_TYPES.sticker,
event
);

await sendMessage(event, {
status: 200,
action: "sticker",
action: ACTION_TYPES.sticker,
data: sticker
});
return {
Expand Down Expand Up @@ -453,7 +461,7 @@ export const scoreHandler = async (event, ctx, callback) => {
true
);
} catch (error) {
console.log(error.message);
console.error(error.message);
await sendMessage(event, {
status: 500,
message: "Failed to store score"
Expand All @@ -462,13 +470,13 @@ export const scoreHandler = async (event, ctx, callback) => {

await sendMessage(event, {
status: 200,
action: "score",
action: ACTION_TYPES.score,
message: "Stored score"
});
return {
status: 200,
action: "score",
message: "Stored score successfully"
action: ACTION_TYPES.score,
message: "Stored score"
};
};

Expand All @@ -481,11 +489,11 @@ export const defaultHandler = async (event, ctx, callback) => {
try {
await sendMessage(event, {
status: 400,
action: "error",
action: ACTION_TYPES.error,
message: "unknown action"
});
} catch (error) {
console.log(error);
console.error(error);
callback(null, error);
return null;
}
Expand All @@ -503,7 +511,7 @@ export const getScores = async (event, ctx, callback) => {
try {
res = await db.scan(SCORE_TABLE);
} catch (error) {
console.log(error);
console.error(error);
res = createResponse(500, {
message: "failed to fetch scores"
});
Expand Down
Loading

0 comments on commit f117db4

Please sign in to comment.