Skip to content

Commit

Permalink
get associated by 'session'
Browse files Browse the repository at this point in the history
  • Loading branch information
guipeeix7 committed Dec 17, 2024
1 parent 698de8f commit 5286f42
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Controllers/membershipController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const Token = require("../Models/tokenSchema");
const { generateRecoveryPasswordToken } = require("../Utils/token");
const saltRounds = 13;
const Role = require("../Models/roleSchema");
const TokenFunctions = require("../Utils/token");
const jwt = require("jsonwebtoken");

Check failure on line 10 in src/Controllers/membershipController.js

View workflow job for this annotation

GitHub Actions / lint

'jwt' is assigned a value but never used
const { SECRET } = process.env;

Check failure on line 11 in src/Controllers/membershipController.js

View workflow job for this annotation

GitHub Actions / lint

'SECRET' is assigned a value but never used

const createMembershipForm = async (req, res) => {
try {
Expand Down Expand Up @@ -83,6 +86,35 @@ const getMembershipForm = async (req, res) => {
}
};

const getLoggedMembershipForm = async (req, res) => {
try {
const sindRole = await Role.findOne({ name: "sindicalizado" });

if (!sindRole) {
console.error('Role "sindicalizado" não encontrada.');

Check warning on line 94 in src/Controllers/membershipController.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

return res
.status(404)
.send({ error: 'Role "sindicalizado" não encontrada.' });
}

const userId = await TokenFunctions.getLoggedUserId(req, res);

const { status } = req.query;
const query = {
role: sindRole._id,
_id: userId,
...(status && { status }), // Adiciona status apenas se existir
};

const membership = await Membership.findOne(query);
return res.status(200).send(membership);
} catch (error) {
console.error("Erro no getLoggedMembershipForm:", error);

Check warning on line 113 in src/Controllers/membershipController.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
return res.status(400).send({ error: error.message });
}
};

const getMembershipById = async (req, res) => {
try {
const membership = await Membership.findById(req.params.id);
Expand Down Expand Up @@ -178,6 +210,7 @@ const updateMembership = async (req, res) => {
module.exports = {
createMembershipForm,
getMembershipForm,
getLoggedMembershipForm,
deleteMembershipForm,
updateStatusMembership,
getMembershipById,
Expand Down
18 changes: 18 additions & 0 deletions src/Utils/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ const checkToken = (token) => {
}
};

const getLoggedUserId = async (req, res) => {
const token = req.headers.authorization?.split(" ")[1];

if (!token) {
return res.status(401).json({ message: "Token não fornecido" });
}
try {
const decoded = jwt.verify(token, SECRET ?? "S3T1N3L3L4");

userId = decoded.id;
} catch (err) {
console.log(err);
return -1;
}
return userId;
};

const tokenValidation = (req, res, next) => {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1]; // Separa ---> 'Bearer <token>'
Expand All @@ -49,5 +66,6 @@ module.exports = {
generateToken,
checkToken,
tokenValidation,
getLoggedUserId,
generateRecoveryPasswordToken,
};
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ routes.patch(
//
routes.post("/membership/create", MembershipForm.createMembershipForm);
routes.get("/membership", MembershipForm.getMembershipForm);
routes.get("/logged-membership", MembershipForm.getLoggedMembershipForm);

routes.delete("/membership/delete/:id", MembershipForm.deleteMembershipForm);
routes.patch(
"/membership/updateStatus/:id",
Expand Down

0 comments on commit 5286f42

Please sign in to comment.