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/checkKnownOrg.js
2022-03-30 09:46:43 -05:00

35 lines
926 B
JavaScript

const { Organization } = require('../models/index');
async function GetOrg(req, res, next) {
const user = req.user;
const orgID = req.params.orgID || req.query.orgID || req.body.orgID;
const result = await Organization.findByPk(orgID, {
include: [
{
association: 'orgOwner',
where: { userID: user.userID },
required: false,
},
{
association: 'orgMember',
where: { userID: user.userID },
required: false,
},
],
});
if (!result) {
return res.status(500).send('There is no organization with this id.');
}
if (!result.orgOwner && result.orgMember.length == 0) {
return res
.status(403)
.send('You do not know any organizations with this id.');
}
return next();
}
module.exports = GetOrg;