Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request for infino-3e2318 #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions backend/db/Application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const { diag } = require('@opentelemetry/api');

let schema = new mongoose.Schema(
{
Expand Down Expand Up @@ -37,7 +38,9 @@ let schema = new mongoose.Schema(
validate: [
{
validator: function (value) {
return this.dateOfApplication <= value;
const isValid = this.dateOfApplication <= value;
diag.debug('Validating dateOfJoining:', { dateOfApplication: this.dateOfApplication, dateOfJoining: value, isValid });
return isValid;
},
msg: "dateOfJoining should be greater than dateOfApplication",
},
Expand All @@ -47,7 +50,10 @@ let schema = new mongoose.Schema(
type: String,
validate: {
validator: function (v) {
return v.split(" ").filter((ele) => ele != "").length <= 250;
const wordCount = v.split(" ").filter((ele) => ele != "").length;
const isValid = wordCount <= 250;
diag.debug('Validating SOP word count:', { sop: v, wordCount, isValid });
return isValid;
},
msg: "Statement of purpose should not be greater than 250 words",
},
Expand Down
8 changes: 8 additions & 0 deletions backend/db/Job.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const { diag } = require('@opentelemetry/api');

let schema = new mongoose.Schema(
{
Expand All @@ -19,6 +20,7 @@ let schema = new mongoose.Schema(
},
{
validator: function (value) {
diag.info(`Validating maxApplicants: ${value}`);
return value > 0;
},
msg: "maxApplicants should greater than 0",
Expand All @@ -34,6 +36,7 @@ let schema = new mongoose.Schema(
},
{
validator: function (value) {
diag.info(`Validating maxPositions: ${value}`);
return value > 0;
},
msg: "maxPositions should greater than 0",
Expand All @@ -50,6 +53,7 @@ let schema = new mongoose.Schema(
},
{
validator: function (value) {
diag.info(`Validating activeApplications: ${value}`);
return value >= 0;
},
msg: "activeApplications should greater than equal to 0",
Expand All @@ -66,6 +70,7 @@ let schema = new mongoose.Schema(
},
{
validator: function (value) {
diag.info(`Validating acceptedCandidates: ${value}`);
return value >= 0;
},
msg: "acceptedCandidates should greater than equal to 0",
Expand All @@ -81,6 +86,7 @@ let schema = new mongoose.Schema(
validate: [
{
validator: function (value) {
diag.info(`Validating deadline: ${value} against dateOfPosting: ${this.dateOfPosting}`);
return this.dateOfPosting < value;
},
msg: "deadline should be greater than dateOfPosting",
Expand Down Expand Up @@ -111,6 +117,7 @@ let schema = new mongoose.Schema(
},
{
validator: function (value) {
diag.info(`Validating salary: ${value}`);
return value >= 0;
},
msg: "Salary should be positive",
Expand All @@ -123,6 +130,7 @@ let schema = new mongoose.Schema(
default: -1.0,
validate: {
validator: function (v) {
diag.info(`Validating rating: ${v}`);
return v >= -1.0 && v <= 5.0;
},
msg: "Invalid rating",
Expand Down
14 changes: 12 additions & 2 deletions backend/db/JobApplicant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const mongoose = require("mongoose");
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');

// Initialize the logger
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

let schema = new mongoose.Schema(
{
Expand Down Expand Up @@ -30,7 +34,9 @@ let schema = new mongoose.Schema(
{ validator: Number.isInteger, msg: "Year should be an integer" },
{
validator: function (value) {
return this.startYear <= value;
const isValid = this.startYear <= value;
diag.info(`Validating endYear: ${value}, startYear: ${this.startYear}, isValid: ${isValid}`);
return isValid;
},
msg: "End year should be greater than or equal to Start year",
},
Expand All @@ -45,7 +51,9 @@ let schema = new mongoose.Schema(
default: -1.0,
validate: {
validator: function (v) {
return v >= -1.0 && v <= 5.0;
const isValid = v >= -1.0 && v <= 5.0;
diag.info(`Validating rating: ${v}, isValid: ${isValid}`);
return isValid;
},
msg: "Invalid rating",
},
Expand All @@ -60,4 +68,6 @@ let schema = new mongoose.Schema(
{ collation: { locale: "en" } }
);

diag.info('Schema for JobApplicantInfo created');

module.exports = mongoose.model("JobApplicantInfo", schema);
8 changes: 8 additions & 0 deletions backend/db/Rating.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const { diag } = require('@opentelemetry/api');

let schema = new mongoose.Schema(
{
Expand All @@ -21,6 +22,7 @@ let schema = new mongoose.Schema(
default: -1.0,
validate: {
validator: function (v) {
diag.debug(`Validating rating: ${v}`);
return v >= -1.0 && v <= 5.0;
},
msg: "Invalid rating",
Expand All @@ -30,6 +32,12 @@ let schema = new mongoose.Schema(
{ collation: { locale: "en" } }
);

diag.debug('Schema created with collation: en');

schema.index({ category: 1, receiverId: 1, senderId: 1 }, { unique: true });

diag.debug('Index created on schema with fields: category, receiverId, senderId');

module.exports = mongoose.model("ratings", schema);

diag.debug('Mongoose model "ratings" exported');
7 changes: 6 additions & 1 deletion backend/db/Recruiter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const mongoose = require("mongoose");
const { diag } = require('@opentelemetry/api');

let schema = new mongoose.Schema(
{
Expand All @@ -14,7 +15,9 @@ let schema = new mongoose.Schema(
type: String,
validate: {
validator: function (v) {
return v !== "" ? /\+\d{1,3}\d{10}/.test(v) : true;
const isValid = v !== "" ? /\+\d{1,3}\d{10}/.test(v) : true;
diag.debug(`Validating contact number: ${v}, isValid: ${isValid}`);
return isValid;
},
msg: "Phone number is invalid!",
},
Expand All @@ -26,4 +29,6 @@ let schema = new mongoose.Schema(
{ collation: { locale: "en" } }
);

diag.debug('Schema created with collation locale: en');

module.exports = mongoose.model("RecruiterInfo", schema);
9 changes: 9 additions & 0 deletions backend/db/User.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const { diag } = require('@opentelemetry/api');
require("mongoose-type-email");

let schema = new mongoose.Schema(
Expand All @@ -26,16 +27,20 @@ let schema = new mongoose.Schema(
// Password hashing
schema.pre("save", function (next) {
let user = this;
diag.debug('Pre-save hook triggered for user:', user.email);

// if the data is not modified
if (!user.isModified("password")) {
diag.debug('Password not modified for user:', user.email);
return next();
}

bcrypt.hash(user.password, 10, (err, hash) => {
if (err) {
diag.error('Error hashing password for user:', user.email, err);
return next(err);
}
diag.debug('Password hashed successfully for user:', user.email);
user.password = hash;
next();
});
Expand All @@ -44,15 +49,19 @@ schema.pre("save", function (next) {
// Password verification upon login
schema.methods.login = function (password) {
let user = this;
diag.debug('Login method called for user:', user.email);

return new Promise((resolve, reject) => {
bcrypt.compare(password, user.password, (err, result) => {
if (err) {
diag.error('Error comparing password for user:', user.email, err);
reject(err);
}
if (result) {
diag.debug('Password verification successful for user:', user.email);
resolve();
} else {
diag.debug('Password verification failed for user:', user.email);
reject();
}
});
Expand Down
14 changes: 14 additions & 0 deletions backend/lib/authKeys.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
To add logging to the given code using the OpenTelemetry library, we need to first set up OpenTelemetry and then add appropriate log messages. Here's how you can instrument the code:

```javascript
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');

// Set up the OpenTelemetry logger
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

module.exports = {
jwtSecretKey: "jwt_secret",
};

// Log the jwtSecretKey value
diag.info(`JWT Secret Key is set to: ${module.exports.jwtSecretKey}`);
```

This code sets up a basic logger using OpenTelemetry and logs the value of `jwtSecretKey`.
4 changes: 4 additions & 0 deletions backend/lib/jwtAuth.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const passport = require("passport");
const { diag } = require('@opentelemetry/api');

const jwtAuth = (req, res, next) => {
passport.authenticate("jwt", { session: false }, function (err, user, info) {
if (err) {
diag.error('Authentication error:', err);
return next(err);
}
if (!user) {
diag.warn('Authentication failed, no user found:', info);
res.status(401).json(info);
return;
}
diag.info('User authenticated successfully:', user);
req.user = user;
next();
})(req, res, next);
Expand Down
20 changes: 11 additions & 9 deletions backend/lib/passportConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const passport = require("passport");
const { diag } = require('@opentelemetry/api');
const Strategy = require("passport-local").Strategy;

const passportJWT = require("passport-jwt");
Expand All @@ -15,6 +16,7 @@ const filterJson = (obj, unwantedKeys) => {
filteredObj[key] = obj[key];
}
});
diag.debug('Filtered object:', filteredObj);
return filteredObj;
};

Expand All @@ -25,12 +27,14 @@ passport.use(
passReqToCallback: true,
},
(req, email, password, done, res) => {
// console.log(email, password);
diag.debug('Authenticating user with email:', email);
User.findOne({ email: email }, (err, user) => {
if (err) {
diag.error('Error finding user:', err);
return done(err);
}
if (!user) {
diag.warn('User not found for email:', email);
return done(null, false, {
message: "User does not exist",
});
Expand All @@ -39,17 +43,12 @@ passport.use(
user
.login(password)
.then(() => {
// let userSecure = {};
// const unwantedKeys = ["password", "__v"];
// Object.keys(user["_doc"]).forEach((key) => {
// if (unwantedKeys.indexOf(key) === -1) {
// userSecure[key] = user[key];
// }
// });
diag.debug('User logged in successfully:', user._id);
user["_doc"] = filterJson(user["_doc"], ["password", "__v"]);
return done(null, user);
})
.catch((err) => {
diag.error('Login failed for user:', user._id, 'Error:', err);
return done(err, false, {
message: "Password is incorrect.",
});
Expand All @@ -66,10 +65,12 @@ passport.use(
secretOrKey: authKeys.jwtSecretKey,
},
(jwt_payload, done) => {
diag.debug('Authenticating JWT payload:', jwt_payload);
User.findById(jwt_payload._id)
.then((user) => {
console.log(Object.keys(jwt_payload));
diag.debug('User found for JWT payload:', jwt_payload._id);
if (!user) {
diag.warn('User not found for JWT payload:', jwt_payload._id);
return done(null, false, {
message: "JWT Token does not exist",
});
Expand All @@ -78,6 +79,7 @@ passport.use(
return done(null, user);
})
.catch((err) => {
diag.error('Error processing JWT payload:', jwt_payload, 'Error:', err);
return done(err, false, {
message: "Incorrect Token",
});
Expand Down
7 changes: 6 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/sdk-node": "^0.27.0",
"@opentelemetry/auto-instrumentations-node": "^0.27.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.27.0",
"@opentelemetry/exporter-metrics-otlp-http": "^0.27.0"
}
}
Loading