This repository has been archived on 2025-11-04. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
know-it-all/server/middleware/checkKnownUser.js

53 lines
1.4 KiB
JavaScript

const { User } = require('../db/models/index');
async function checkKnownUser(req, res, next) {
const user = req.user;
const userID = req.params.userID || req.query.userID || req.body.userID;
if (user.id == userID) {
return next();
}
const result = await User.findByPk(userID, {
include: [
{
association: 'ownedOrg',
include: {
association: 'member',
where: { id: user.id },
},
},
{
association: 'memberOrg',
include: [
{
association: 'owner',
where: { id: user.id },
required: false,
},
{
association: 'member',
where: { id: user.id },
required: false,
},
],
},
],
});
if (!result) {
return res.status(500).send('No user with that ID exists.');
}
const filteredResult = result.memberOrg.filter(
(org) => org.member.length != 0 || org.owner
);
if (filteredResult.length == 0 && result.ownedOrg) {
return res.status(403).send('You do not have contact with this user.');
}
return next();
}
module.exports = checkKnownUser;