Skip to content
Snippets Groups Projects
Commit 7fe626eb authored by Bodor Máté's avatar Bodor Máté
Browse files

Create user route and middlewares

parent 50f7bd91
No related branches found
No related tags found
2 merge requests!10Feature/12 dev auto deploy,!8[Review]Feature/user
Showing with 204 additions and 5 deletions
...@@ -5,6 +5,7 @@ import bodyParser from "body-parser"; ...@@ -5,6 +5,7 @@ import bodyParser from "body-parser";
import expressSession from "express-session"; import expressSession from "express-session";
import authRoute from "./routes/auth"; import authRoute from "./routes/auth";
import newsRoute from "./routes/news"; import newsRoute from "./routes/news";
import usersRoute from "./routes/user";
mongoose mongoose
.connect("mongodb://localhost:27017/bodysch", { .connect("mongodb://localhost:27017/bodysch", {
...@@ -46,6 +47,10 @@ authRoute(app); ...@@ -46,6 +47,10 @@ authRoute(app);
// Register routes // Register routes
newsRoute(app); newsRoute(app);
usersRoute(app);
app.use((err: any, req: Request, res: Response, next: NextFunction) => { app.use((err: any, req: Request, res: Response, next: NextFunction) => {
res.status(500).send("Houston, we have a problem!"); res.status(500).send("Houston, we have a problem!");
......
...@@ -15,7 +15,7 @@ const complete = () => async (req: Request, res: Response) => { ...@@ -15,7 +15,7 @@ const complete = () => async (req: Request, res: Response) => {
await axios.get(`https://auth.sch.bme.hu/api/profile/?access_token=${token.access_token}`) await axios.get(`https://auth.sch.bme.hu/api/profile/?access_token=${token.access_token}`)
.then( (response) => { .then( (response) => {
req.session!.user = { req.session!.user = {
id: String(response.data.basic), id: String(response.data.internal_id),
email: String(response.data.mail), email: String(response.data.mail),
name: `${response.data.sn} ${response.data.givenName}` , name: `${response.data.sn} ${response.data.givenName}` ,
token, token,
......
import { Request, Response, NextFunction } from 'express';
import Profile, { Role } from "../../models/ProfileSchema";
const addUser = () => (req: Request, res: Response, next: NextFunction) => {
const profile = new Profile();
const filds = ['studentCardNumber', 'roomNumber', 'picture'];
filds.forEach(fild => {
const value = req.body[fild]
if(value)
profile.set(fild, value);
});
profile.external_id = req.session!.user!.id;
profile.role = Role.User;
profile.save(err => {
if (err) {
res.status(400);
}
else {
res.status(201);
res.data = {profile};
}
res.json(profile);
});
}
export default addUser;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
const addWarning = () => (req: Request, res: Response, next: NextFunction) =>{
res.send("Add warning");
}
export default addWarning;
\ No newline at end of file
import { Request, Response, NextFunction } from 'express';
import Profile from '../../models/ProfileSchema';
const deletUser = () => (req: Request, res: Response, next: NextFunction) => {
Profile.findByIdAndDelete(req.params.id, (error) => {
if (error) {
res.status(400);
} else {
res.status(204);
}
res.json({});
});
}
export default deletUser;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
const deleteWarning = () => (req: Request, res: Response, next: NextFunction) =>{
res.send("Delete warning");
}
export default deleteWarning;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
import Profile from '../../models/ProfileSchema';
const getUser = () => (req: Request, res: Response, next: NextFunction) =>{
Profile.findById(req.params.id, (error, profile) => {
if (!error) {
res.status(200);
res.data = {profile};
} else {
console.warn(error);
res.status(400);
}
res.json(profile);
});
}
export default getUser;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
import Profile from '../../models/ProfileSchema';
const getUsersList = () => (req: Request, res: Response, next: NextFunction) =>{
Profile.find({}, (err, profiles) =>{
if(err) {
res.status(400);
}
else{
res.status(200);
res.data = {profiles}
}
res.json(profiles);
})
}
export default getUsersList;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
const getWarning = () => (req: Request, res: Response, next: NextFunction) =>{
res.send("Get warning");
}
export default getWarning;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
const getWarningsList = () => (req: Request, res: Response, next: NextFunction) =>{
res.send("Get warnings");
}
export default getWarningsList;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
import Profile from '../../models/ProfileSchema';
const updateUser = () => (req: Request, res: Response, next: NextFunction) => {
const validFilds = ['studentCardNumber', 'roomNumber', 'picture'];
Profile.findOne(
{_id : req.params.id},
(error, profile) => {
res.status(400);
if (error) {
res.status(400);
}
else {
if(profile){
validFilds.forEach(fild => {
const value = req.body[fild]
if(value)
profile.set(fild, value);
});
res.status(200);
profile.save();
res.data = {profile};
}
}
res.json(profile);
}
);
}
export default updateUser;
\ No newline at end of file
import {Request, Response, NextFunction} from 'express';
const updateWarning = () => (req: Request, res: Response, next: NextFunction) =>{
res.send("Update warning");
}
export default updateWarning;
\ No newline at end of file
import { Document, Schema, model } from 'mongoose'; import { Document, Schema, model } from 'mongoose';
import { IWarnings, WarningSchema } from './WarningSchema'; import { IWarnings, WarningSchema } from './WarningSchema';
import { Admin } from 'mongodb';
export enum Role {
Admin,
Staff,
User,
}
export interface IProfile extends Document { export interface IProfile extends Document {
external_id: string, external_id: string,
studentCardNumber: string, studentCardNumber: string,
roomNumber?: string, roomNumber?: string,
picture: any, picture: string,
role: string, role: Role.Admin | Role.Staff | Role.User,
email?: string, email?: string,
name?: string, name?: string,
warnings: [IWarnings] | [], warnings: [IWarnings] | [],
}; };
const ProfileSchema = new Schema({ const ProfileSchema = new Schema({
external_id: {type: String, required: true }, external_id: {type: String, required: true, unique : true, dropDups: true },
studentCardNumber: {type: String, required: true }, studentCardNumber: {type: String, required: true },
roomNumber: {type: String}, roomNumber: {type: String},
picture: {type: String}, picture: {type: String},
...@@ -21,4 +28,4 @@ const ProfileSchema = new Schema({ ...@@ -21,4 +28,4 @@ const ProfileSchema = new Schema({
warnings: [WarningSchema] warnings: [WarningSchema]
}); });
export default model<IProfile>('News', ProfileSchema); export default model<IProfile>('Profile', ProfileSchema);
\ No newline at end of file \ No newline at end of file
import { Application } from 'express';
import addWarning from "../middlewares/user/addWarning";
import deleteUser from "../middlewares/user/deleteUser";
import deleteWarning from "../middlewares/user/deleteWarning";
import getUser from "../middlewares/user/getUser";
import getUsersList from "../middlewares/user/getUsersList";
import getWarning from "../middlewares/user/getWarning";
import getWarningsList from "../middlewares/user/getWarningsList";
import updateUser from "../middlewares/user/updateUser";
import updateWarning from "../middlewares/user/updateWarning";
import addUser from '../middlewares/user/addUser';
import authenticated from '../middlewares/auth/authenticated';
const usersRoute = (app: Application): void => {
app.get('/users', authenticated(), getUsersList() );
app.post('/users', authenticated(), addUser() );
app.get('/users/:id', getUser() );
app.get('/users/:id/warnings', getWarningsList() );
app.get('/users/:userId/warnings/:warningId', getWarning() );
app.post('/users/:id/warnings', addWarning());
app.put('/users/:id', updateUser());
app.put('/users/:userId/warnings/:warningId', updateWarning());
app.delete('/users/:id', deleteUser());
app.delete('/users/:userId/warnings/:warningId', deleteWarning());
}
export default usersRoute;
\ No newline at end of file
import { INews } from "../../models/NewsSchema"; import { INews } from "../../models/NewsSchema";
import { IProfile } from "src/models/ProfileSchema";
declare global { declare global {
namespace Express { namespace Express {
...@@ -6,6 +7,8 @@ declare global { ...@@ -6,6 +7,8 @@ declare global {
data: { data: {
news?: INews[] | null; news?: INews[] | null;
newsObject?: INews | null; newsObject?: INews | null;
profile?: IProfile | null;
profiles?: IProfile[] | null;
}; };
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment