From 01e5685f957636a7197a95336c5345d90a6d4023 Mon Sep 17 00:00:00 2001
From: Matt Kirby <MattJKirby@outlook.com>
Date: Sun, 16 Apr 2023 15:03:03 +0100
Subject: [PATCH] Implemented new userlist endpoint

---
 .../user-service/controllers/appController.js | 31 ++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/backend-services/user-service/controllers/appController.js b/backend-services/user-service/controllers/appController.js
index 73558768..ddecf556 100644
--- a/backend-services/user-service/controllers/appController.js
+++ b/backend-services/user-service/controllers/appController.js
@@ -70,7 +70,17 @@ export async function register(req,res){
 
                           // return save result as a response
                           user.save()
-                              .then(result => res.status(201).send({ msg: "User Registered Successfully"}))
+                              .then(async (result) => {
+                                const user = await UserModel.findOne({username: username})
+                                
+                                // create jwt token
+                                const token = jwt.sign({
+                                  userId: user._id,
+                                  username : user.username
+                              }, ENV.JWT_SECRET , { expiresIn : "24h"});
+
+                                res.status(201).send({ msg: "User Registered Successfully", username: user.username, token })
+                              })
                               .catch(error => res.status(500).send({error}))
 
                       }).catch(error => {
@@ -195,3 +205,22 @@ export async function updateUser(req,res){
     }
 }
 
+/**
+ * GET /userlist
+ * This DOES NOT return emails and passwords
+ * @param {*} req 
+ * @param {*} res 
+ * @returns 
+ * 
+ * body: {userIdList: []}
+ */
+export const GetUserList = async (req,res) => {
+  try {
+    const {userIdList} = req.body;
+    const users = await UserModel.find({ '_id': { $in: userIdList } }, {password: 0, email: 0});
+    return res.status(201).send({ userList: users});
+  } catch(error){
+    return res.status(401).send({ error });
+  }
+}
+
-- 
GitLab