Skip to content
Snippets Groups Projects
Commit 6969a928 authored by chif's avatar chif
Browse files

Merge branch 'feature/user' into 'dev'

[Review]Feature/user

See merge request kszk/devteam/bodysch/bodysch-backend!8
parents 6b87344c 2a3a1dd3
No related branches found
No related tags found
2 merge requests!10Feature/12 dev auto deploy,!8[Review]Feature/user
Showing
with 261 additions and 1 deletion
......@@ -5,6 +5,7 @@ import bodyParser from "body-parser";
import expressSession from "express-session";
import authRoute from "./routes/auth";
import newsRoute from "./routes/news";
import usersRoute from "./routes/user";
mongoose
.connect("mongodb://localhost:27017/bodysch", {
......@@ -46,6 +47,10 @@ authRoute(app);
// Register routes
newsRoute(app);
usersRoute(app);
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
res.status(500).send("Houston, we have a problem!");
......
......@@ -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}`)
.then( (response) => {
req.session!.user = {
id: String(response.data.basic),
id: String(response.data.internal_id),
email: String(response.data.mail),
name: `${response.data.sn} ${response.data.givenName}` ,
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 fields = ['studentCardNumber', 'roomNumber', 'picture'];
fields.forEach(field => {
const value = req.body[field]
if(value)
profile.set(field, 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};
}
next()
});
}
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);
}
next();
});
}
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) {
console.warn(error);
res.status(400);
} else {
res.status(200);
res.data = { profile };
}
next();
});
}
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}
}
next();
})
}
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, response } from 'express';
const responseUser = () => (req: Request, res: Response) => {
res.json(res.data.profile);
}
export default responseUser;
\ No newline at end of file
import { Request, Response, NextFunction, response } from 'express';
const responseUserList = () => (req: Request, res: Response) => {
res.json(res.data.profiles);
}
export default responseUserList;
\ 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 validFields = ['studentCardNumber', 'roomNumber', 'picture'];
Profile.findOne(
{_id : req.params.id},
(error, profile) => {
res.status(200);
if (error) {
res.status(400);
}
else {
if(profile){
validFields.forEach(field => {
const value = req.body[field]
if(value)
profile.set(field, value);
});
profile.save();
res.data = {profile};
}
}
next();
}
);
}
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 { Request, Response } from 'express';
const emptyResponse = () => (req: Request, res: Response) => res.json({});
\ No newline at end of file
import { Document, Schema, model } from 'mongoose';
import { IWarnings, WarningSchema } from './WarningSchema';
import { Admin } from 'mongodb';
export enum Role {
Admin,
Staff,
User,
}
export interface IProfile extends Document {
external_id: string,
studentCardNumber: string,
roomNumber?: string,
picture: string,
role: Role.Admin | Role.Staff | Role.User,
email?: string,
name?: string,
warnings: [IWarnings] | [],
};
const ProfileSchema = new Schema({
external_id: {type: String, required: true, unique : true, dropDups: true },
studentCardNumber: {type: String, required: true },
roomNumber: {type: String},
picture: {type: String},
role: {type: String, required: true},
warnings: [WarningSchema]
});
export default model<IProfile>('Profile', ProfileSchema);
\ No newline at end of file
import { Schema } from 'mongoose';
export interface IWarnings extends Document{
text: string,
date: Date,
given_by: {
_id: string,
name: string,
}
}
export const WarningSchema = new Schema({
text: {type: String, required: true},
date: {type: Date, required: true},
given_by: {required: true, type: {
_id: {type: String, required: true},
name: {String, required: true},
}}
});
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';
import emptyResponse from 'src/middlewares/utils/emptyResponse';
import responseUser from 'src/middlewares/user/responseUser';
import responseUserList from 'src/middlewares/user/responseUserList';
const usersRoute = (app: Application): void => {
app.get('/users', authenticated(), getUsersList(), responseUserList());
app.post('/users', authenticated(), addUser(), responseUser());
app.get('/users/:id', getUser(), responseUser() );
app.get('/users/:id/warnings', getWarningsList() );
app.get('/users/:userId/warnings/:warningId', getWarning() );
app.post('/users/:id/warnings', addWarning());
app.put('/users/:id', updateUser(), responseUser() );
app.put('/users/:userId/warnings/:warningId', updateWarning());
app.delete('/users/:id', deleteUser(), emptyResponse());
app.delete('/users/:userId/warnings/:warningId', deleteWarning());
}
export default usersRoute;
\ No newline at end of file
import { INews } from "../../models/NewsSchema";
import { IProfile } from "src/models/ProfileSchema";
declare global {
namespace Express {
......@@ -6,6 +7,8 @@ declare global {
data: {
news?: 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