Skip to content

Commit

Permalink
backend changes for implement user dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
canams committed Jan 20, 2024
1 parent 5b31eea commit 10e1df0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
60 changes: 60 additions & 0 deletions packages/backend/controllers/quiz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import express from "express"
import SkinProfile from "../models/SkinProfile"
import authenticate from "../middleware/authenticate"

const router = express.Router()

// TODO: implement
// router.get(
// "/",
// authenticate,
// async (req: express.Request, res: express.Response) => {
// const { user } = req.body
// console.log(user)
// res.status(200).send({
// name: user.name,
// email: user.email,
// skinProfile: user.skin_profile ?? null,
// })
// }
// )

router.post("/", authenticate, async (req, res) => {

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
const { type, concerns, prevRoutine, complexity, budget, user } = req.body

if (!type || !concerns || !prevRoutine || !complexity || !budget) {
return res.status(400).json({
error:
"Invalid request body. Must contain type, concerns, prevRoutine, complexity and budget.",
})
}

const profile = new SkinProfile({
type: type,
concerns: concerns,
prevRoutine: prevRoutine,
complexity: complexity,
budget: budget,
})

try {
const filter = { _id: user._id }
const userCollection = req.app.locals.db.collection("user")
await userCollection.updateOne(
filter,
{ $set: { skin_profile: profile } },
(err: Error) => {
if (err) throw err
console.log("1 document updated")
}
)

return res.status(200).json({ success: "New skin profile added" })
} catch (err: any) {
console.error(err.message)
return res.status(500).json({ error: "Server error. Please try again" })
}
})

export default router

2 changes: 0 additions & 2 deletions packages/backend/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import User from "../models/User"

const router = express.Router()

// TODO: implement
router.get(
"/",
authenticate,
async (req: express.Request, res: express.Response) => {
const { user } = req.body
console.log(user)
res.status(200).send({
name: user.name,
email: user.email,
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express"
import helmet from "helmet"
import cors from "cors"
import userRouter from "./controllers/user"
import quizRouter from "./controllers/quiz"
import dotenv from "dotenv"
import connectToDatabase from "./services/db"

Expand Down Expand Up @@ -34,6 +35,7 @@ const PORT = process.env.PORT || 5000
connectToDatabase(uri, app)
.then(() => {
app.use("/user", userRouter)
app.use("/profile", quizRouter)

app.listen(PORT, () => {
console.log(`Server started at http://localhost:${PORT}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/middleware/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default async function (
return res.sendStatus(401)
}

req.body = { user: user }
req.body = { ...req.body, user: user }

next()
} catch (err) {
Expand Down
7 changes: 3 additions & 4 deletions packages/backend/models/SkinProfile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import mongoose from "mongoose"

const skinProfileSchema = new mongoose.Schema({
user_id: String,
type: String,
concerns: String,
sensitivities: String,
concerns: Array,
prevRoutine: Array,
budget: String,
complexity: String,
})
Expand All @@ -16,5 +15,5 @@ skinProfileSchema.set("toJSON", {
},
})

export const skinProfileModel = mongoose.model("SkinProfile", skinProfileSchema)
export default mongoose.model("SkinProfile", skinProfileSchema)

1 change: 0 additions & 1 deletion packages/backend/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mongoose from "mongoose"
import { skinProfileModel } from "./SkinProfile"

const userSchema = new mongoose.Schema({
name: String,
Expand Down

0 comments on commit 10e1df0

Please sign in to comment.