mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Improved tets and added PUT route for user acc
This commit is contained in:
+18
-16
@@ -1,10 +1,16 @@
|
||||
import { PrismaClient, User } from '@prisma/client'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import express, { RequestHandler } from 'express'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import { z } from 'zod'
|
||||
|
||||
const userRouter = express.Router()
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const editableUserData = z.object({
|
||||
name: z.string().optional(),
|
||||
picture_path: z.string().optional()
|
||||
})
|
||||
|
||||
function parseJwtUserId(jwtoken: string): number | undefined {
|
||||
const payload = jwt.decode(jwtoken)
|
||||
if (payload == undefined) {
|
||||
@@ -15,28 +21,24 @@ function parseJwtUserId(jwtoken: string): number | undefined {
|
||||
return payload.userId
|
||||
}
|
||||
|
||||
async function updateUser(userId: number): Promise<User> {
|
||||
return await prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const userRouterPut: RequestHandler = async (req, res) => {
|
||||
const userId = parseJwtUserId(req.cookies.jwt)
|
||||
if (userId === undefined) {
|
||||
res.status(400).json({
|
||||
message: 'Incorrect JWT payload.'
|
||||
})
|
||||
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
||||
return;
|
||||
}
|
||||
|
||||
await updateUser(userId)
|
||||
try {
|
||||
await prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: editableUserData.parse(req.body)
|
||||
})
|
||||
} catch (error: any) {
|
||||
res.status(400).json({ message: (error.issues ?? error) })
|
||||
return;
|
||||
}
|
||||
|
||||
res.json({
|
||||
message: 'User successfully updated.'
|
||||
})
|
||||
res.json({ message: 'User successfully updated.' })
|
||||
}
|
||||
|
||||
userRouter.put("/", userRouterPut)
|
||||
|
||||
Reference in New Issue
Block a user