From 9da05637bda2d1d5952081729266553277d4186b Mon Sep 17 00:00:00 2001
From: FALKE JANOS <janos.falke@etu.unistra.fr>
Date: Mon, 29 Nov 2021 20:25:36 +0100
Subject: [PATCH] Add pay_adhesion

---
 backend/models/user.js                       |  2 +-
 backend/routes/auth.js                       | 20 +++++++++++++++++++-
 frontend/src/App.js                          | 12 ++++--------
 frontend/src/actions/Login.actions.js        |  7 +++++++
 frontend/src/containers/Profile.container.js |  2 ++
 5 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/backend/models/user.js b/backend/models/user.js
index af22d66..4b99879 100644
--- a/backend/models/user.js
+++ b/backend/models/user.js
@@ -42,6 +42,7 @@ const UserSchema = new mongoose.Schema({
     },
     student_number: {
         type: Number,
+        unique: true,
         required: true,
         minlength: 8,
         maxlength: 8,
@@ -53,7 +54,6 @@ const UserSchema = new mongoose.Schema({
     },
     date_subscription: {
         type: String,
-        select: false,
         default: null,
     },
     url_photo: {
diff --git a/backend/routes/auth.js b/backend/routes/auth.js
index 5ad9797..a5f4e78 100644
--- a/backend/routes/auth.js
+++ b/backend/routes/auth.js
@@ -70,10 +70,28 @@ router.post('/login', async (req, res) => {
         token: `Bearer ${token}` });
 });
 
+router.post('/pay_adhesion', async (req, res) => {
+    const student_number = req.body.student_number;
+
+    if (!student_number)
+        return res.status(400);
+
+    const dbUser = await User.findOne({student_number: req.body.student_number});
+
+    if (dbUser){
+        dbUser.date_subscription = "" + (new Date()).toISOString().split('T')[0];
+
+        dbUser.save();
+        return res.status(200).json({ success: "Le payement de la adhesion a été prise en compte." });        
+    } else {
+        return res.status(400).json({error : e});
+    }
+});
+
 router.get('/me', passport.authenticate('jwt', {session: false}), async function(req, res, next) {
     const username = req.user.username;
     const dbUser = await User.findOne({ username });
-    res.status(200).json(dbUser);
+    return res.status(200).json(dbUser);
 });
 
 router.post('/me/update', passport.authenticate('jwt', {session: false}), async function(req, res, next) {
diff --git a/frontend/src/App.js b/frontend/src/App.js
index 1b71b85..89532ce 100644
--- a/frontend/src/App.js
+++ b/frontend/src/App.js
@@ -38,9 +38,6 @@ class App extends Component {
               <Navbar bg="dark" expand="lg" variant="dark">
                 <Navbar.Brand>STUDENT</Navbar.Brand>
                 <Nav className="mr-auto">
-                  <Nav.Link as={Link} to="/">
-                    Utilisateurs
-                  </Nav.Link>
                   {!this.props.loginState.loggedIn &&
                     <Nav.Link as={Link} to="/signup/">
                       S'inscrire
@@ -52,8 +49,8 @@ class App extends Component {
                     </Nav.Link>
                   }
                   { this.props.loginState.loggedIn &&
-                    <Nav.Link as={Link} to="/profile/">
-                      Profil
+                    <Nav.Link as={Link} to="/">
+                      Profile
                     </Nav.Link>
                   }
                   { this.props.loginState.loggedIn &&
@@ -101,11 +98,10 @@ class App extends Component {
                 </Modal>
               }
 
-              <Route path="/" exact component={Users} />
+              <PrivateRoute path="/" exact component={Profile} />
+              <Route path="/login" exact component={Login} />
               <Route path="/signup/" component={SignUp} />
-              <Route path="/login/" component={Login} />
               <Route path="/privacy/" component={Privacy} />
-              <PrivateRoute path="/profile/" component={Profile} />
               <PrivateRoute path="/bank/" component={Bank} />
             </div>
           </Router>
diff --git a/frontend/src/actions/Login.actions.js b/frontend/src/actions/Login.actions.js
index 44c4a25..23ba633 100644
--- a/frontend/src/actions/Login.actions.js
+++ b/frontend/src/actions/Login.actions.js
@@ -56,8 +56,15 @@ export const login = (loginData, ownProps) => {
                 userService.setToken(data.token);
                 dispatch(fetchUserData());
                 ownProps.history.push('/');
+
+
                 // TODO: do another request to know if user paid adhesion
+                // make request to get user data 
+                
+
                 var paid=true;
+                
+                
                 if(paid){
                     userService.setAdhesion(true);
                     dispatch(loginSuccesPaymentDone(data));
diff --git a/frontend/src/containers/Profile.container.js b/frontend/src/containers/Profile.container.js
index 9632eba..18bd369 100644
--- a/frontend/src/containers/Profile.container.js
+++ b/frontend/src/containers/Profile.container.js
@@ -9,6 +9,8 @@ export class Profile extends React.Component {
 
   constructor(props) {
     super(props);
+    this.props.fetchUserData();
+    this.props.reinitializeState();
 
     this.state = {
       readOnly: true
-- 
GitLab