Skip to content
Snippets Groups Projects
Commit ccc99af2 authored by Rafael László's avatar Rafael László :speech_balloon:
Browse files

getFieldValue middleware

parent bfa48199
No related branches found
No related tags found
2 merge requests!29version 0.9,!28API and Schema rework
import { NextFunction, Request, Response, response } from "express";
const example = () => (req: Request, res: Response) => {
console.log(res.data.value);
res.status(200).json({ message: "Example" });
};
......
import { NextFunction, Request, Response } from "express";
import Profile from "../../models/ProfileSchema";
import Term from "../../models/TermSchema";
/**
* set res.data.value to res.data[objectName][fieldName]
*/
const getFieldValue = (
objectName: "profile" | "term",
fieldName: "picture" | "acceptedPicture" | "backgroundFile"
) => async (req: Request, res: Response, next: NextFunction) => {
try {
const object: Record<string, any> = res.data[objectName]!;
if (object) {
res.data.value = object[fieldName];
}
next();
} catch (err) {
next(err);
}
};
export default getFieldValue;
......@@ -2,6 +2,11 @@ import { Application } from "express";
import cardImageStorage from "../middlewares/files/card/cardImageStorage";
import example from "../middlewares/example";
import { fileFilter } from "../middlewares/files/fileFilter";
import getFieldValue from "../middlewares/utils/getFieldValue";
import getUser from "../middlewares/user/getUser";
import isLoggedIn from "../middlewares/auth/isLoggedIn";
import isRegistered from "../middlewares/auth/isRegistered";
import isStaffOrAdmin from "../middlewares/auth/isStaffOrAdmin";
import multer from "multer";
import profilePictureStorage from "../middlewares/files/profile/profilePictureStorage";
......@@ -17,7 +22,14 @@ const CardImageUpload = multer({
const filesRoute = (prefix: string, app: Application): void => {
// Get a users profile picture
app.get(`${prefix}/user/:userId/picture`, example());
app.get(
`${prefix}/user/:userId/picture`,
isRegistered(),
isStaffOrAdmin(),
getUser(),
getFieldValue("profile", "picture"),
example()
);
// Get a users accepted picture
app.get(`${prefix}/user/:userId/picture/accepted`, example());
// Get own picture
......
......@@ -2,6 +2,7 @@ import { INews } from "../../models/NewsSchema";
import { IProfile } from "../../models/ProfileSchema";
import { IFile } from "../../models/FileSchema";
import { IWarning } from "../../models/WarningSchema";
import { ITerm } from "../../models/TermSchema";
declare global {
namespace Express {
......@@ -10,7 +11,8 @@ declare global {
profile?: Partial<IProfile> | null;
warnings?: Partial<IWarning>[] | null;
newObjectId?: string | null;
value?: string | null;
term?: Partial<ITerm> | null;
news?: Partial<INews>[] | null;
newsObject?: Partial<INews> | null;
profiles?: Partial<IProfile>[] | null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment