Skip to content

Commit

Permalink
consultant
Browse files Browse the repository at this point in the history
  • Loading branch information
maahipticem committed Feb 7, 2024
2 parents e3098ae + f198a28 commit e2a96da
Show file tree
Hide file tree
Showing 32 changed files with 778 additions and 59 deletions.
2 changes: 1 addition & 1 deletion backend/database/data/applicationStatus.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{ "label": "Accepté" }, { "label": "Refusé" }, { "label": "En cours" }]
[{ "label": "Accepté" }, { "label": "En cours" }, { "label": "Refusé" }]
2 changes: 1 addition & 1 deletion backend/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CREATE TABLE application (
user_id INTEGER NOT NULL,
job_id INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
status_id INTEGER NOT NULL,
status_id INTEGER NOT NULL DEFAULT 2,
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (job_id) REFERENCES job(id) ON DELETE CASCADE,
FOREIGN KEY (status_id) REFERENCES application_status(id) ON DELETE CASCADE
Expand Down
2 changes: 1 addition & 1 deletion backend/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require("dotenv").config();
// Import database client
const database = require("./database/client");
const role = require("./database/data/role.json");
const user = require("./database/data/user.json");
const company = require("./database/data/company.json");
const applicationStatus = require("./database/data/applicationStatus.json");
const job = require("./database/data/job.json");
const user = require("./database/data/user.json");

const seed = async () => {
try {
Expand Down
118 changes: 118 additions & 0 deletions backend/src/controllers/applicationControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Import access to database tables
const tables = require("../tables");

// The B of BREAD - Browse (Read All) operation
const browse = async (req, res, next) => {
try {
// Fetch all applications from the database
const applications = await tables.application.readAll();

// Respond with the applications in JSON format
res.status(200).json(applications);
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The R of BREAD - Read operation
const read = async (req, res, next) => {
try {
// Fetch a specific application from the database based on the provided ID
const application = await tables.application.read(req.params.id);

// If the application is not found, respond with HTTP 404 (Not Found)
// Otherwise, respond with the application in JSON format
if (application == null) {
res.sendStatus(404);
} else {
res.status(200).json(application);
}
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

const readProfileApplications = async (req, res, next) => {
try {
const applications = await tables.application.getProfileApplications(
req.user.id
);

if (applications == null) {
res.sendStatus(404);
} else {
res.status(200).json(applications);
}
} catch (err) {
next(err);
}
};

const readConsultantApplications = async (req, res, next) => {
try {
const applications = await tables.application.getConsultantApplications(
req.user.id
);

if (applications == null) {
res.sendStatus(404);
} else {
res.status(200).json(applications);
}
} catch (err) {
next(err);
}
};

// The E of BREAD - Edit (Update) operation
const edit = async (req, res, next) => {
// Extract the application data from the request body
const application = req.body;
try {
// Fetch a specific city from the database based on the provided ID
const result = await tables.application.update(req.params.id, application);

// If the application is not found, respond with HTTP 404 (Not Found)
if (result.affectedRows === 1) {
res.sendStatus(204);
} else {
res.sendStatus(404);
}
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The A of BREAD - Add (Create) operation
const add = async (req, res, next) => {
// Extract the application data from the request body
const application = req.body;

try {
// Insert the application into the database
const insertId = await tables.application.create(application);

// Respond with HTTP 201 (Created) and the ID of the newly inserted application
res.status(201).json({ insertId });
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The D of BREAD - Destroy (Delete) operation
// This operation is not yet implemented

// Ready to export the controller functions
module.exports = {
browse,
read,
readProfileApplications,
readConsultantApplications,
edit,
add,
// destroy,
};
21 changes: 21 additions & 0 deletions backend/src/controllers/applicationStatusControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Import access to database tables
const tables = require("../tables");

// The B of BREAD - Browse (Read All) operation
const browse = async (req, res, next) => {
try {
// Fetch all status from the database
const status = await tables.application_status.readAll();

// Respond with the status in JSON format
res.status(200).json(status);
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// Ready to export the controller functions
module.exports = {
browse,
};
5 changes: 5 additions & 0 deletions backend/src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ const destroy = async (req, res, next) => {
}
};

const logout = (req, res) => {
res.cookie("auth", "", { expires: new Date(0) }).sendStatus(200);
};

// Ready to export the controller functions
module.exports = {
getConsultant,
Expand All @@ -202,4 +206,5 @@ module.exports = {
destroy,
updateProfile,
updateProfileCV,
logout,
};
111 changes: 111 additions & 0 deletions backend/src/models/ApplicationManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const AbstractManager = require("./AbstractManager");

class ApplicationManager extends AbstractManager {
constructor() {
// Call the constructor of the parent class (AbstractManager)
// and pass the table name "application" as configuration
super({ table: "application" });
}

// The C of CRUD - Create operation

async create(application) {
// Execute the SQL INSERT query to add a new application to the "application" table
const [result] = await this.database.query(
`insert into ${this.table} (user_id, job_id) values (?, ?)`,
[application.user_id, application.job_id]
);

// Return the ID of the newly inserted application
return result.insertId;
}

// The Rs of CRUD - Read operations

async read(id) {
// Execute the SQL SELECT query to retrieve a specific application by its ID
const [rows] = await this.database.query(
`select * from ${this.table} where id = ?`,
[id]
);

// Return the first row of the result, which represents the application
return rows[0];
}

async readAll() {
// Execute the SQL SELECT query to retrieve all applications from the "application" table
const [rows] = await this.database.query(`select * from ${this.table}`);

// Return the array of applications
return rows;
}

async getProfileApplications(userId) {
const [rows] = await this.database.query(
`SELECT
application.id,
application.job_id,
application.status_id,
job.title AS job_title,
job.consultant_id,
consultant.email AS consultant_email,
application_status.label as status_label
FROM ${this.table} AS application
INNER JOIN job ON application.job_id = job.id
INNER JOIN user AS consultant ON job.consultant_id = consultant.id
INNER JOIN user ON application.user_id = user.id
INNER JOIN application_status ON application.status_id = application_status.id
WHERE user.id = ?`,
[userId]
);
return rows;
}

async getConsultantApplications(consultantId) {
const [rows] = await this.database.query(
`SELECT
application.id AS application_id,
application.job_id,
application.status_id,
job.title AS job_title,
consultant.id AS consultant_id,
consultant.email AS consultant_email,
user.id AS candidate_id,
user.email AS candidate_email,
application_status.label AS status_label,
company.name AS company_name
FROM ${this.table} AS application
INNER JOIN job ON application.job_id = job.id
INNER JOIN user AS consultant ON job.consultant_id = consultant.id
INNER JOIN user ON application.user_id = user.id
INNER JOIN application_status ON application.status_id = application_status.id
INNER JOIN company ON job.company_id = company.id
WHERE consultant.id = ?
ORDER BY company.name`,
[consultantId]
);
return rows;
}

// The U of CRUD - Update operation
async update(id, application) {
// Execute the SQL SELECT query to retrieve a specific application by its ID
const [result] = await this.database.query(
`UPDATE ${this.table} set ? WHERE id = ?`,
[application, id]
);

// Return the first row of the result, which represents the item
return result;
}

// The D of CRUD - Delete operation
// TODO: Implement the delete operation to remove an application by its ID

// async delete(id) {
// ...
// }
}

module.exports = ApplicationManager;
19 changes: 19 additions & 0 deletions backend/src/models/ApplicationStatusManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const AbstractManager = require("./AbstractManager");

class ApplicationStatusManager extends AbstractManager {
constructor() {
// Call the constructor of the parent class (AbstractManager)
// and pass the table name "application_status" as configuration
super({ table: "application_status" });
}

async readAll() {
// Execute the SQL SELECT query to retrieve all status from the "application_status" table
const [rows] = await this.database.query(`select * from ${this.table}`);

// Return the array of status
return rows;
}
}

module.exports = ApplicationStatusManager;
31 changes: 30 additions & 1 deletion backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const userControllers = require("./controllers/userControllers");
const jobControllers = require("./controllers/jobControllers");
const companyControllers = require("./controllers/companyControllers");
const roleControllers = require("./controllers/roleControllers");
const applicationControllers = require("./controllers/applicationControllers");
const applicationStatusControllers = require("./controllers/applicationStatusControllers");

const checkCredentials = require("./middleware/checkCredentials");
const checkAdmin = require("./middleware/checkAdmin");
Expand All @@ -23,11 +25,17 @@ const validateCompany = require("./validators/validateCompany");
const validateCV = require("./validators/validateCV");
const validateJob = require("./validators/validateJob");

// ROUTES GET
router.get("/jobs", jobControllers.browse);
router.get("/locations", jobControllers.getLocations);
router.get("/languages", jobControllers.getLanguages);
router.get("/companies", companyControllers.browse);
router.get("/consultants", userControllers.getConsultant);
router.get(
"/consultants",
checkCredentials,
checkConsultant,
userControllers.getConsultant
);
router.get("/roles", checkCredentials, checkAdmin, roleControllers.browse);
router.get(
"/candidates",
Expand All @@ -36,8 +44,26 @@ router.get(
userControllers.getCandidates
);
router.get("/profile", checkCredentials, userControllers.getProfile);
router.get(
"/profile/applications",
checkCredentials,
applicationControllers.readProfileApplications
);
router.get(
"/applications/consultant",
checkCredentials,
checkConsultant,
applicationControllers.readConsultantApplications
);
router.get("/jobs/all/latest", jobControllers.browseLatest);
router.get(
"/applicationStatus",
checkCredentials,
checkConsultant,
applicationStatusControllers.browse
);

// ROUTES GET BY ID
router.get("/jobs/:id", jobControllers.read);
router.get(
"/companies/:id",
Expand Down Expand Up @@ -76,7 +102,9 @@ router.post(
validateCompany,
companyControllers.add
);
router.post("/application", checkCredentials, applicationControllers.add);

// ROUTES DELETE
router.delete(
"/jobs/:id",
checkCredentials,
Expand All @@ -96,6 +124,7 @@ router.delete(
userControllers.destroy
);

// ROUTES PUT
router.put(
"/companies/:id",
checkCredentials,
Expand Down
Loading

0 comments on commit e2a96da

Please sign in to comment.