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

accept user picture

parent fc38e946
No related branches found
No related tags found
2 merge requests!29version 0.9,!28API and Schema rework
Pipeline #5656 passed
import { NextFunction, Request, Response } from "express";
import File from "../../models/FileSchema";
import Profile from "../../models/ProfileSchema";
import fs from "fs";
/**
* Deletes the file if exists. Careful, doesn't remove reference!
*/
const deleteFile = () => async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
if (!res.data.value) return next();
const oldFile = await File.findByIdAndRemove(res.data.value).lean().exec();
if (oldFile) fs.unlinkSync(oldFile!.path);
next();
} catch (err) {
next(err);
}
};
export default deleteFile;
import { NextFunction, Request, Response } from "express";
import Profile, { IProfile } from "../../models/ProfileSchema";
/**
* userId -> updates the user
*/
const acceptPicture = () => async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
if (!res.data.profile?.picture)
return res.status(400).json({
message: "The User doesn't have any unaccepted Profile picture!",
});
await Profile.updateOne(
{ _id: req.params.userId },
{
$set: {
picture: undefined,
acceptedPicture: res.data.profile?.picture,
},
},
{ upsert: true, runValidators: true }
)
.lean()
.exec();
next();
} catch (err) {
next(err);
}
};
export default acceptPicture;
import { Application } from "express";
import acceptPicture from "../middlewares/user/acceptPicture";
import createdResponse from "../middlewares/utils/createdResponse";
import deleteFile from "../middlewares/files/deleteFile";
import deleteUser from "../middlewares/user/deleteUser";
import example from "../middlewares/example";
import getFieldValue from "../middlewares/utils/getFieldValue";
import getUser from "../middlewares/user/getUser";
import getUserWarningsList from "../middlewares/user/getUserWarningsList";
import getUsersList from "../middlewares/user/getUsersList";
......@@ -93,7 +96,16 @@ const usersRoute = (prefix: string, app: Application): void => {
noContentResponse()
);
// Accept/Reject a users picture TODO
app.post(`${prefix}/user/:userId/picture/accept`, example());
app.post(
`${prefix}/user/:userId/picture/accept`,
isRegistered(),
isStaffOrAdmin(),
getUser(),
getFieldValue("profile", "acceptedPicture"),
deleteFile(),
acceptPicture(),
noContentResponse()
);
app.post(`${prefix}/user/:userId/picture/reject`, example());
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment