Skip to content

Commit ad66b5c

Browse files
committed
Add logger to various endpoints
Add legit admin emails as well as test ones Add debugger for local devlopment in packagejson
1 parent c33ab2a commit ad66b5c

File tree

7 files changed

+376
-62
lines changed

7 files changed

+376
-62
lines changed

Controllers/feedbackController.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const feedbackValidator = require("../utility/inputValidator/feedbackValidator")
1111
const {
1212
studentNotification,
1313
} = require("../utility/notifications/flockNotification");
14+
const logger = require('../utility/logger/logger');
1415

1516
/*This controller allows the interns to request for feedback using the request
1617
feedback forms.
@@ -23,8 +24,8 @@ const submitFeedBack = async (req, res) => {
2324
const { errors, validationCheck } = feedbackrequestValidator(req.body);
2425

2526
if (!validationCheck) {
26-
res.status(400).json(errors);
27-
return;
27+
logger.error(`error validating inputs: ${JSON.stringify(errors)}`)
28+
return res.status(400).json(errors);
2829
}
2930

3031
let fullName = await User.findOne({
@@ -58,12 +59,14 @@ const submitFeedBack = async (req, res) => {
5859
};
5960
await exerciseInfo.create(add_User_To_ExerciseInfo_Table);
6061
await FeedbackRequest.create(feedBackRequestData);
61-
// studentNotification(feedBackRequestData);
62+
// await studentNotification(feedBackRequestData);
6263
}
6364

6465
res.status(200).json({ data: feedBackRequestData });
66+
logger.info(`User submitted request successfully`, {log: JSON.stringify(feedBackRequestData)});
6567
} catch (err) {
6668
res.status(400).json({ msg: err.message });
69+
logger.error(`error submitting request`, {error: JSON.stringify(err)});
6770
}
6871
};
6972

@@ -89,6 +92,7 @@ const getAllFeedBackRequestsForms = async (req, res) => {
8992
});
9093
res.status(200).json({ data: feedBackrequests });
9194
}
95+
//Not sure why this was added. Might not be useful. Will revisit to make sure it's needed.
9296
if (!isMentor) {
9397
const feedBackrequests = await FeedbackRequest.findAll({
9498
where: {
@@ -102,6 +106,7 @@ const getAllFeedBackRequestsForms = async (req, res) => {
102106
}
103107
} catch (err) {
104108
res.status(500).json({ error: "Internal Server Error" });
109+
logger.error(`Server Error`, {error: JSON.stringify(err)})
105110
}
106111
};
107112

@@ -115,8 +120,10 @@ const getUserFeedBackRequestForms = async (req, res) => {
115120
where: { userId: id },
116121
});
117122
res.status(200).json({ data: singleFeedBack });
123+
logger.info(`Feedback Requests was Fetached Successfully`)
118124
} catch (err) {
119125
res.status(500).json({ msg: "Internal Server Error" });
126+
logger.error(`error is ${JSON.stringify(err)}`)
120127
}
121128
};
122129

@@ -132,6 +139,7 @@ const getMentorFeedback = async (req, res) => {
132139
});
133140

134141
if (!feedbackRequest) {
142+
logger.error(`Feedback request not found for mentor`, {error: JSON.stringify(feedbackRequest)});
135143
return res.status(404).json({ error: "Feedback request not found" });
136144
}
137145

@@ -143,6 +151,7 @@ const getMentorFeedback = async (req, res) => {
143151
res.json({ data: allFeedbackOnFeedbackRequest });
144152
} catch (err) {
145153
res.status(500).json({ error: "Internal server error" });
154+
logger.error(`Feedback was not fetched due to error`, {error: JSON.stringify(error)})
146155
}
147156
};
148157

@@ -168,8 +177,10 @@ const flockNotification = async (req, res) => {
168177

169178
studentNotification(data);
170179
res.status(200).json({ message: "Notification was sent successfully" });
180+
logger.info(`Notification was sent successfully`, {log: JSON.stringify(data)})
171181
} catch (err) {
172182
res.status(500).json({ msg: err });
183+
logger.error(`error sending notification`, {error: JSON.stringify(err)})
173184
}
174185
};
175186

@@ -189,6 +200,7 @@ const addFeedBack = async (req, res) => {
189200
const { id } = jwt.verify(authToken, process.env.JWT_SECRET);
190201

191202
if (!validationCheck) {
203+
logger.error(`Bad Input:`, {log: JSON.stringify(errors)})
192204
return res.status(400).json(errors);
193205
}
194206

@@ -201,6 +213,7 @@ const addFeedBack = async (req, res) => {
201213
});
202214

203215
if (!isMentor) {
216+
logger.error(`Unauthorized user`, {log: JSON.stringify(isMentor)})
204217
return res.status(401).json({ msg: "Unauthorized user" });
205218
}
206219

@@ -214,8 +227,8 @@ const addFeedBack = async (req, res) => {
214227
});
215228

216229
if (!feedbackRequest) {
217-
res.status(404).json({ msg: "Feedback request not found" });
218-
return;
230+
logger.error(`Feeback request not found`, {log: JSON.stringify(feedbackRequest)})
231+
return res.status(404).json({ msg: "not found" });;
219232
}
220233

221234
// Create the feedback and associate it with the feedback request and mentor
@@ -227,11 +240,14 @@ const addFeedBack = async (req, res) => {
227240
};
228241

229242
const createdFeedback = await Feedbacks.create(feedBackData);
243+
244+
logger.info(`Feedback added successfully`, {log: JSON.stringify(createdFeedback)})
230245
res.status(200).json({
231246
msg: "Feedback added successfully",
232247
data: createdFeedback,
233248
});
234249
} catch (err) {
250+
logger.error(`An error occurred adding feedback`, {log: JSON.stringify(err)})
235251
res.status(500).json({ error: "An error occurred adding feedback" });
236252
}
237253
};
@@ -253,8 +269,8 @@ const assignFeedBackToMentor = async (req, res) => {
253269
});
254270

255271
if (!isMentor) {
256-
res.status(401).json({ msg: "Unauthorized user" });
257-
return;
272+
logger.error(`Unauthorized user`, {log: JSON.stringify(isMentor)})
273+
return res.status(401).json({ msg: "Unauthorized user" });
258274
}
259275

260276
// Find the specific feedback request record based on feedbackrequestId
@@ -263,16 +279,19 @@ const assignFeedBackToMentor = async (req, res) => {
263279
});
264280

265281
if (!feedbackRecord) {
266-
res.status(404).json({ msg: "Feedback record not found" });
267-
return;
282+
logger.error(`Feedback record not found`, {log: JSON.stringify(feedbackRecord)})
283+
return res.status(404).json({ msg: "Feedback record not found" });
268284
}
269285

270286
feedbackRecord.isAssigned = true;
271287
feedbackRecord.mentorId = isMentor.mentorId;
272288
feedbackRecord.whoisAssigned = isMentor.fName;
273289
await feedbackRecord.save();
290+
291+
logger.info(`Feedback assigned to mentor`, {log: JSON.stringify()})
274292
res.json({ msg: "Feedback assigned to mentor" });
275293
} catch (err) {
294+
logger.error(`An error occurred while updating feedback`, {log: JSON.stringify(err)})
276295
res
277296
.status(500)
278297
.json({ error: "An error occurred while updating feedback" });
@@ -300,6 +319,7 @@ const getAssignedFeedBacks = async (req, res) => {
300319
});
301320

302321
if (!isMentor) {
322+
logger.error(`Unauthorized user`, {log: JSON.stringify(isMentor)});
303323
res.status(401).json({ msg: "Unauthorized user" });
304324
return;
305325
}
@@ -312,6 +332,8 @@ const getAssignedFeedBacks = async (req, res) => {
312332
},
313333
},
314334
});
335+
336+
logger.info(`List fetched successfully`, {log: JSON.stringify(assignedList)})
315337
res.status(200).json({ data: assignedList });
316338
} catch (err) {
317339
console.error(err);
@@ -330,18 +352,15 @@ const getSelectedFeedback = async (req, res) => {
330352
});
331353

332354
if (!feedbackRequest) {
355+
logger.error(`Feedback request not found`, {log: JSON.stringify(feedbackRequest)})
333356
res.status(404).json({ msg: "Feedback request not found" });
334357
return;
335358
}
336-
359+
360+
logger.info(`Fetched feedback successfully`, {log: JSON.stringify(feedbackRequest)})
337361
res.json({ data: feedbackRequest });
338362
} catch (error) {
339-
console.error(error);
340-
if (error.name === "JsonWebTokenError") {
341-
res.status(401).json({ msg: "Invalid authentication token" });
342-
} else {
343-
res.status(500).json({ msg: "Internal Server Error" });
344-
}
363+
logger.error(`error:`, {log: JSON.stringify(error)});
345364
}
346365
};
347366

@@ -360,6 +379,7 @@ const markFeedbackRequestComplete = async (req, res) => {
360379
});
361380

362381
if (!isMentor) {
382+
logger.error(`Unauthorized user`, {log: isMentor})
363383
res.status(401).json({ msg: "Unauthorized user" });
364384
return;
365385
}
@@ -369,6 +389,7 @@ const markFeedbackRequestComplete = async (req, res) => {
369389
});
370390

371391
if (!markAsComplete) {
392+
logger.error(`FeedbackRequest not found`, {log: markAsComplete})
372393
return res.status(404).json({ message: "FeedbackRequest not found" });
373394
}
374395

@@ -381,6 +402,7 @@ const markFeedbackRequestComplete = async (req, res) => {
381402
});
382403

383404
if (!exerciseInfo) {
405+
logger.error(`ExerciseInfo not found`, {log: exerciseInfo})
384406
return res.status(404).json({ message: "ExerciseInfo not found" });
385407
}
386408

@@ -389,9 +411,9 @@ const markFeedbackRequestComplete = async (req, res) => {
389411

390412
// Update the status attribute of the FeedbackRequest record
391413
await markAsComplete.update({ status: true });
392-
393414
res.status(200).json({ msg: "Exercise Marked As Complete" });
394415
} catch (err) {
416+
logger.error(`Error:`, {log: JSON.stringify(err)})
395417
res.status(500).json({ msg: err });
396418
}
397419
};

0 commit comments

Comments
 (0)