Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: user preferences backend #22

Merged
merged 6 commits into from
Jul 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion @types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Interview, InterviewStatus } from "@prisma/client";
import { Interview, InterviewStatus, User } from "@prisma/client";

export interface JWTToken {
id: string;
Expand Down Expand Up @@ -35,6 +35,10 @@ export interface UpdateInterviewRequest {
status: InterviewStatus;
}

export type UserPreferences = Omit<User, "id" | "name" | "email" | "imageUrl">;

export type UpdateUserPreferencesRequest = UserPreferences;

declare global {
namespace Express {
interface User {
Expand Down
36 changes: 36 additions & 0 deletions controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PrismaClient, QuestionDifficulty, QuestionType, CodingLanguage } from ".prisma/client";
import express, { Request, Response } from "express";
import { UpdateUserPreferencesRequest, UserPreferences } from "../@types";
import UserRepository from "../models/UserRepository";

export default function userController(prismaClient: PrismaClient) {
const userRouter = express.Router();
const userRepository = new UserRepository(prismaClient);

userRouter.post("/", async (req: Request<{}, {}, UpdateUserPreferencesRequest>, res: Response) => {
const userId = req.user?.id;
const timeZone = (req.query.timezone as string) || null;
const questionTypes = (req.query.questionTypes as QuestionType[]) || null;
const questionDifficulty = (req.query.questionDifficulty as QuestionDifficulty) || null;
const codingLanguage = (req.query.codingLanguage as CodingLanguage[]) || null;

let prefs: Partial<UserPreferences> = {};
if (timeZone) prefs.timeZone = timeZone;
if (questionTypes) prefs.questionTypes = questionTypes;
if (questionDifficulty) prefs.questionDifficulty = questionDifficulty;
if (codingLanguage) prefs.codingLanguage = codingLanguage;

try {
if (!timeZone && !questionTypes && !questionDifficulty && !codingLanguage)
return res.status(400).send({ error: "Empty Request." });
joshmhanson marked this conversation as resolved.
Show resolved Hide resolved

if (!userId) return res.status(401).send({ error: "Not Authenticated." });
await userRepository.updateUserById(userId, prefs);
res.send(200).json(prefs);
joshmhanson marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
res.status(500).send({ error: e.message });
}
});

return userRouter;
}
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Socket } from "socket.io";

import authController from "./controllers/authController";
import interviewController from "./controllers/interviewController";
import userController from "./controllers/userController";

const port = process.env.PORT || 8000;
const app = express();
Expand All @@ -28,6 +29,7 @@ app.use(

app.use("/api/auth", authController(prismaClient));
app.use("/api/interviews", interviewController(prismaClient));
app.use("/api/users", userController(prismaClient));

app.use((err: Error, _req: Request, res: Response) => {
if (err.name === "UnauthorizedError") res.status(401).send("Unauthorized token");
Expand Down
6 changes: 6 additions & 0 deletions models/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PrismaClient, User } from ".prisma/client";
import { CodingLanguage, QuestionDifficulty, QuestionType } from "@prisma/client";
import { TokenPayload } from "google-auth-library";
import { UserPreferences } from "../@types";

export default class UserRepository {
prisma: PrismaClient;
Expand All @@ -24,4 +26,8 @@ export default class UserRepository {
return newUser;
}
}

async updateUserById(id: string, preferences: Partial<UserPreferences>): Promise<void> {
await this.prisma.user.update({ where: { id }, data: { ...preferences } });
joshmhanson marked this conversation as resolved.
Show resolved Hide resolved
}
}