Skip to content

Commit

Permalink
updated avatar mapping, enums (#155)
Browse files Browse the repository at this point in the history
* updated avatar mapping, enums

* fix format

* modified to avatarId

* tests

* test

* removed extra format, removed mapped

* fix format

* format again
  • Loading branch information
lasyaneti authored Jan 18, 2024
1 parent 9dd618e commit a528446
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export enum RegistrationTemplates {
REGISTRATION_SUBMISSION = "registration_confirmation",
}

export enum Avatars {
BUNNY = "bunny",
SQUIRREL = "squirrel",
GOBLIN = "goblin",
CHESTER = "chester",
CAT = "cat",
MUSHROOM = "mushroom",
FISHERCAT = "fishercat",
AXOLOTL = "axolotl",
}

function requireEnv(name: string): string {
const value = env[name];

Expand Down Expand Up @@ -93,6 +104,7 @@ const Config = {
DEFAULT_POINT_VALUE: 0,
DEFAULT_FOOD_WAVE: 0,
DEFAULT_COIN_VALUE: 0,
DEFAULT_AVATAR: "goblin",

/* Limits */
LEADERBOARD_QUERY_LIMIT: 25,
Expand Down
2 changes: 1 addition & 1 deletion src/services/profile/profile-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("POST /profile", () => {
const response = await postAsUser("/profile/")
.send({
displayName: 123,
avatarUrl: 1,
avatarId: 1,
discordTag: "test",
})
.expect(StatusCode.ClientErrorBadRequest);
Expand Down
29 changes: 16 additions & 13 deletions src/services/profile/profile-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Request, Router } from "express";
import { NextFunction, Response } from "express-serve-static-core";

import Config from "../../config.js";
import { Avatars } from "../../config.js";
import { isValidLimit, updatePoints, updateCoins } from "./profile-lib.js";
import { AttendeeMetadata, AttendeeProfile } from "../../database/attendee-db.js";
import Models from "../../database/models.js";
Expand Down Expand Up @@ -94,7 +95,7 @@ profileRouter.get("/leaderboard/", async (req: Request, res: Response, next: Nex
* @apiSuccess (200: Success) {string} userID ID of the user
* @apiSuccess (200: Success) {string} displayName Publicly-visible display name for the user
* @apiSuccess (200: Success) {string} discordTag Discord tag for the user
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user avatar
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user's selected avatar
* @apiSuccess (200: Success) {number} points Points that the user has
* @apiSuccess (200: Success) {number} coins Coins that the user has
* @apiSuccessExample Example Success Response:
Expand Down Expand Up @@ -144,7 +145,7 @@ profileRouter.get("/", strongJwtVerification, async (_: Request, res: Response,
* @apiSuccess (200: Success) {string} userID ID of the user
* @apiSuccess (200: Success) {string} displayName Publicly-visible display name for the user
* @apiSuccess (200: Success) {string} discordTag Discord tag for the user
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user avatar
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user's selected avatar
* @apiSuccess (200: Success) {number} points Points that the user has
* @apiSuccess (200: Success) {number} coins Coins that the user has
*
Expand Down Expand Up @@ -199,15 +200,14 @@ profileRouter.get("/id", (_: Request, res: Response) => {
* @apiGroup Profile
* @apiDescription Create a user profile based on their authentication.
*
* @apiBody {String} firstName User's first name.
* @apiBody {String} lastName User's last name.
* @apiBody {String} discord User's Discord username.
* @apiBody {String} avatarUrl User's avatar URL.
* @apiBody {String} displayName User's displayName.
* @apiBody {String} discordTag User's Discord username.
* @apiBody {String} avatarId User's requested avatar.
*
* @apiSuccess (200: Success) {string} userID ID of the user
* @apiSuccess (200: Success) {string} displayName Publicly-visible display name for the user
* @apiSuccess (200: Success) {string} discordTag Discord tag for the user
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user avatar
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user selected avatar. If invalid avatar is passed, default avatar is assigned.
* @apiSuccess (200: Success) {number} points Points that the user has
* @apiSuccess (200: Success) {number} coins Coins that the user has
*
Expand All @@ -217,8 +217,8 @@ profileRouter.get("/id", (_: Request, res: Response) => {
* "_id": "abc12345",
* "userId": "github12345",
* "displayName": "Hack",
* "discord": "HackIllinois",
* "avatarUrl": "na",
* "discordTag": "HackIllinois",
* "avatarUrl": "https://hackillinois.org/mushroom.png",
* "points": 0,
* "coins": 0
* }
Expand All @@ -229,14 +229,17 @@ profileRouter.get("/id", (_: Request, res: Response) => {
* HTTP/1.1 400 Bad Request
* {"error": "UserAlreadyExists"}
*
* @apiErrorExample Example Error Response (InternalError):
* HTTP/1.1 500 Internal Server Error
* {"error": "InternalError"}
*/
profileRouter.post("/", strongJwtVerification, async (req: Request, res: Response, next: NextFunction) => {
const avatarId: string = (Object.values(Avatars) as string[]).includes(req.body.avatarId as string)
? (req.body.avatarId as string)
: Config.DEFAULT_AVATAR;

const profile: ProfileFormat = req.body as ProfileFormat;
profile.points = Config.DEFAULT_POINT_VALUE;
profile.coins = Config.DEFAULT_COIN_VALUE;
profile.avatarUrl = `https://raw.githubusercontent.com/HackIllinois/adonix-metadata/main/avatars/${avatarId}.png`;
console.log(profile.avatarUrl);

const payload: JwtPayload = res.locals.payload as JwtPayload;
profile.userId = payload.id;
Expand Down Expand Up @@ -304,7 +307,7 @@ profileRouter.delete("/", strongJwtVerification, async (_: Request, res: Respons
* @apiSuccess (200: Success) {string} userID ID of the user
* @apiSuccess (200: Success) {string} displayName Publicly-visible display name for the user
* @apiSuccess (200: Success) {string} discordTag Discord tag for the user
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user avatar
* @apiSuccess (200: Success) {string} avatarUrl URL that contains the user's selected avatar
* @apiSuccess (200: Success) {number} points Points that the user has
* @apiSuccess (200: Success) {number} coins Coins that the user has
*
Expand Down
2 changes: 1 addition & 1 deletion src/testTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const TESTER = {
id: "bob-the-tester101010101011",
email: "bob-the-tester@hackillinois.org",
name: "Bob Tester",
avatarUrl: "https://www.hackillinois.org",
avatarUrl: "https://hackillinois.org/mushroom.png",
discordTag: "hackillinoistest",
userName: "bobster_the_mobster",
};
Expand Down

0 comments on commit a528446

Please sign in to comment.