Skip to content

Commit

Permalink
bug: some fetch requests do not work (For instance, the employee time…
Browse files Browse the repository at this point in the history
…line job fetching in the employee tab), among others. Still trying to debug and fix this author: William Lam<williamnhut.lam@mail.utoronto.ca>
  • Loading branch information
williamntlam committed Oct 19, 2024
1 parent d914072 commit f269619
Show file tree
Hide file tree
Showing 12 changed files with 8,293 additions and 2,505 deletions.
10,646 changes: 8,211 additions & 2,435 deletions client/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function ClientTimelineEntriesComponent({
try {
setLoading(true);
const response = await getFilteredClientTimelineEntries(
queryParams.toString(),
queryParams.toString()
);
if (response.ok) {
const clientTimelineEntries = await response.json();
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/login-component/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function LoginComponent({ setIsAuthenticated, loginUser }) {
if (!email.trim() || !password.trim()) {
setErrorMessage(
// eslint-disable-next-line
"Please fill in your email and password before proceeding",
"Please fill in your email and password before proceeding"
);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ app.use(
httpOnly: true,
secure: false,
},
}),
})
);
app.use(passport.authenticate("session"));

Expand Down
4 changes: 3 additions & 1 deletion server/src/configs/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const sequelize = new Sequelize(process.env.SUPABASE_DATABASE_URI, {
});

// Authenticate the connection
sequelize.authenticate().then(() => {});
sequelize.authenticate().then(() => {
// return sequelize.sync({ force: true }); // Use { force: true } if you want to recreate the tables
});

module.exports = {
sequelize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ const getAllClientTimelineEntriesRequestHandler = async (req, res) => {
const { type, client, search_query } = req.query;

const query = {};

// Use `Op.eq` for `type` since it's an ENUM field
if (type) {
query.type = { [Op.like]: `%${type}%` };
query.type = { [Op.eq]: type };
}

// Use `Op.eq` for `client` since it's an integer field
if (client) {
query.client = client;
query.client = { [Op.eq]: client };
}

// Use `LIKE` for string fields like `title` and `body` for search queries
if (search_query) {
query[Op.or] = [
{ title: { [Op.like]: `%${search_query}%` } },
Expand Down Expand Up @@ -59,7 +63,7 @@ const getAllClientTimelineEntriesRequestHandler = async (req, res) => {
job_lead_details: jobLeadDetails ? jobLeadDetails.toJSON() : null,
client_details: clientDetails ? clientDetails.toJSON() : null,
};
}),
})
);

return res.status(200).json({
Expand All @@ -72,7 +76,7 @@ const getAllClientTimelineEntriesRequestHandler = async (req, res) => {
logger.error(`Unexpected server error: ${err}`);
return res.status(500).json({
status: "error",
message: "An unexpected server error occured.",
message: "An unexpected server error occurred.",
});
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const logger = require("pino")();
const { Op } = require("sequelize");
const EmployerTimelineEntry = require("../../models/employer_timeline_entry.model");
const JobLeadTimelineEntry = require("../../models/job_lead_timeline_entry.model");
const JobLead = require("../../models/job_lead.model");
const Employer = require("../../models/employer.model");
const Client = require("../../models/client.model");
Expand All @@ -16,14 +15,26 @@ const getAllEmployerTimelineEntriesRequestHandler = async (req, res) => {
const { type, employer, search_query } = req.query;

const query = {};

// Use `Op.eq` for `type` since it's an ENUM
if (type) {
query.type = { [Op.like]: `%${type}%` };
query.type = { [Op.eq]: type };
}

// Use `Op.eq` for `employer` since it's an integer
if (employer) {
query.employer = { [Op.like]: `%${employer}%` };
const employerId = parseInt(employer, 10);
if (!isNaN(employerId)) {
query.employer = { [Op.eq]: employerId };
} else {
return res.status(400).json({
status: "error",
message: "Invalid employer ID",
});
}
}

// Use `LIKE` for string fields like `title` and `body`
if (search_query) {
query[Op.or] = [
{ title: { [Op.like]: `%${search_query}%` } },
Expand Down Expand Up @@ -66,7 +77,7 @@ const getAllEmployerTimelineEntriesRequestHandler = async (req, res) => {
client_details: clientDetails ? clientDetails.toJSON() : null,
employer_details: employerDetails ? employerDetails.toJSON() : null,
};
}),
})
);

return res.status(200).json({
Expand All @@ -79,7 +90,7 @@ const getAllEmployerTimelineEntriesRequestHandler = async (req, res) => {
logger.error(`Unexpected server error: ${err}`);
return res.status(500).json({
status: "error",
message: "An unexpected server error occured.",
message: "An unexpected server error occurred.",
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ const getAllJobLeadTimelineEntriesRequestHandler = async (req, res) => {
const { type, job_lead, search_query } = req.query;

const query = {};

// Use `Op.eq` for `type` since it's an ENUM
if (type) {
query.type = { [Op.like]: `%${type}%` };
query.type = { [Op.eq]: type };
}

// Use `Op.eq` for `job_lead` since it's an integer
if (job_lead) {
query.job_lead = job_lead;
query.job_lead = { [Op.eq]: job_lead };
}

// Use `LIKE` for string fields like `title` and `body` for search queries
if (search_query) {
query[Op.or] = [
{ title: { [Op.like]: `%${search_query}%` } },
{ body: { [Op.like]: `%${search_query}%` } },
{ title: { [Op.like]: `%${search_query}%` } }, // Assuming title is a string
{ body: { [Op.like]: `%${search_query}%` } }, // Assuming body is a string
];
}

Expand Down Expand Up @@ -59,7 +63,7 @@ const getAllJobLeadTimelineEntriesRequestHandler = async (req, res) => {
job_lead_details: jobLeadDetails ? jobLeadDetails.toJSON() : null,
client_details: clientDetails ? clientDetails.toJSON() : null,
};
}),
})
);

return res.status(200).json({
Expand All @@ -72,7 +76,7 @@ const getAllJobLeadTimelineEntriesRequestHandler = async (req, res) => {
logger.error(`Unexpected server error: ${err}`);
return res.status(500).json({
status: "error",
message: "An unexpected server error occured.",
message: "An unexpected server error occurred.",
});
}
};
Expand Down
20 changes: 11 additions & 9 deletions server/src/models/client.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const Client = sequelize.define(
allowNull: true,
},
status: {
type: DataTypes.ENUM(["active", "r_and_i", "closed"]),
type: DataTypes.ENUM(["Active", "r_and_i", "closed"]),
allowNull: true,
},
closure_date: {
Expand Down Expand Up @@ -93,7 +93,7 @@ const Client = sequelize.define(
},
job_lead_placement: {
type: DataTypes.INTEGER,
allowNull: false,
allowNull: true,
references: {
model: JobLead,
key: "id",
Expand All @@ -110,16 +110,18 @@ const Client = sequelize.define(
{
hooks: {
async beforeValidate(instance) {
// Check if employer contact exists
const jobLeadExists = await JobLead.findByPk(
instance.job_lead_placement,
);
if (!jobLeadExists && instance.job_lead_placement !== -1) {
throw new Error("Job lead placement does not exist");
if (instance.job_lead_placement !== null) {
// Check if employer contact exists
const jobLeadExists = await JobLead.findByPk(
instance.job_lead_placement
);
if (!jobLeadExists && instance.job_lead_placement !== null) {
throw new Error("Job lead placement does not exist");
}
}
},
},
},
}
);

module.exports = Client;
6 changes: 3 additions & 3 deletions server/src/models/client_timeline_entry.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ const ClientTimelineEntry = sequelize.define(
model: JobLead,
key: "id",
},
defaultValue: -1,
defaultValue: null,
},
user: {
type: DataTypes.INTEGER,
references: {
model: User,
key: "id",
},
defaultValue: -1,
// defaultValue: -1,
},
},
{
timestamps: false,
tableName: "client_timeline_entries",
},
}
);

module.exports = ClientTimelineEntry;
55 changes: 22 additions & 33 deletions server/src/models/employer_timeline_entry.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ const EmployerTimelineEntry = sequelize.define(
},
contact: {
type: DataTypes.INTEGER,
allowNull: false,
allowNull: true,
references: {
model: User, // to be replaced when Contact api is done
model: User, // to be replaced when Contact API is done
key: "id",
},
defaultValue: -1,
defaultValue: null,
},
employer: {
type: DataTypes.INTEGER,
allowNull: false,
allowNull: true,
references: {
model: Employer,
key: "id",
},
defaultValue: -1,
defaultValue: null,
},
client: {
type: DataTypes.INTEGER,
allowNull: false,
allowNull: true,
references: {
model: Client,
key: "id",
},
defaultValue: -1,
defaultValue: null,
},
job_lead: {
type: DataTypes.INTEGER,
Expand All @@ -65,51 +65,40 @@ const EmployerTimelineEntry = sequelize.define(
model: JobLead,
key: "id",
},
defaultValue: -1,
// validate: {
// isInJobLead(value) {
// if (!JobLead.findByPk(value)) {
// throw new Error("Job Lead does not exist");
// }
// },
// },
defaultValue: null,
},
user: {
type: DataTypes.INTEGER,
references: {
model: User,
key: "id",
},
allowNull: false,
// validate: {
// isInUser(value) {
// if (!User.findByPk(value)) {
// throw new Error("User does not exist");
// }
// },
// },
allowNull: true,
defaultValue: null,
},
},
{
hooks: {
async beforeValidate(instance) {
// Check if employer contact exists
const jobLeadExists = await JobLead.findByPk(instance.job_lead);
if (!jobLeadExists) {
throw new Error("Job lead does not exist");
// Only check if job_lead is not null
if (instance.job_lead !== null) {
const jobLeadExists = await JobLead.findByPk(instance.job_lead);
if (!jobLeadExists) {
throw new Error("Job lead does not exist");
}
}

const userExists = await User.findByPk(instance.user);
if (!userExists) {
throw new Error("User does not exist");
if (instance.user !== null) {
const userExists = await User.findByPk(instance.user);
if (!userExists) {
throw new Error("User does not exist");
}
}
},
},
},
{
timestamps: false,
tableName: "employer_timeline_entries",
},
}
);

module.exports = EmployerTimelineEntry;
Loading

0 comments on commit f269619

Please sign in to comment.