Skip to content

Commit

Permalink
๐ŸŽ…๐Ÿฟ Add nawc volunteer role (#458)
Browse files Browse the repository at this point in the history
* Add new role, remove intern role

* Add new file
  • Loading branch information
jackieo5023 authored May 21, 2021
1 parent 763b910 commit 2456aec
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 126 deletions.
111 changes: 0 additions & 111 deletions api/src/api/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ const { errorWrap } = require('../middleware');
const Category = require('../models/category');
const Resource = require('../models/resource');
const HomePage = require('../models/homepage');
const Translation = require('../models/translation');
const VerifiedTranslation = require('../models/verifiedTranslation');
const {
deleteTranslatedText,
translateAndSaveText,
} = require('../utils/translate');
const extractLongLat = require('../utils/extractLongLat');
const {
getVerifiedAggregation,
getNestedVerifiedAggregation,
getVerificationDetails,
} = require('../utils/verification');

const imageHelper = async (image) => {
const imageResponse = await fetch('https://api.imgur.com/3/image', {
Expand Down Expand Up @@ -437,108 +430,4 @@ router.delete(
}),
);

// Get verified translations for table
router.get(
'/verified',
errorWrap(async (req, res) => {
const { language } = req.query;
const resourceInfo = await Resource.aggregate(
getVerifiedAggregation(language, 'resourceID', 'resource'),
);
const categoryInfo = await Category.aggregate(
getVerifiedAggregation(language, 'categoryID', 'category'),
);
const subcategoryInfo = await Category.aggregate(
getNestedVerifiedAggregation(
language,
'subcategoryID',
'subcategory',
'subcategories',
'name',
),
);
const testimonialInfo = await HomePage.aggregate(
getNestedVerifiedAggregation(
language,
'testimonialID',
'testimonial',
'testimonials',
'person',
),
);

res.json({
code: 200,
message: 'Successfully returned verified translation table info',
success: true,
result: resourceInfo.concat(
categoryInfo,
subcategoryInfo,
testimonialInfo,
),
});
}),
);

// Get verified translations for an ID
router.get(
'/verified/:id',
errorWrap(async (req, res) => {
const { id } = req.params;
const { language, type } = req.query;

const verificationDetails = await getVerificationDetails(
type,
language,
id,
);
const { messages } = await Translation.findOne({
language: { $eq: language },
});
verificationDetails.forEach((verificationObject) => {
const key = Object.keys(verificationObject)[0];
verificationObject[key][language] = messages.get(key);
});

res.json({
code: 200,
message: 'Successfully returned verified translation info',
success: true,
result: verificationDetails,
});
}),
);

// Update verified translations for an ID
router.put(
'/verified',
errorWrap(async (req, res) => {
const { language, type } = req.query;
const { translations } = req.body;

const translationsToUpdate = await Translation.findOne({
language: { $eq: language },
});
translations.forEach(async (translation) => {
const key = Object.keys(translation)[0];
translationsToUpdate.messages.set(key, translation[key][language]);
const verifiedTranslation = await VerifiedTranslation.updateOne(
{
translationID: key,
language,
},
{ $set: { verified: translation[key].verified } },
);
});
await translationsToUpdate.save();

res.json({
code: 200,
message: 'Successfully updated verified translation info',
success: true,
result: null,
});
}),
);

module.exports = router;
2 changes: 2 additions & 0 deletions api/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const category = require('./category');
const resource = require('./resource');
const homepage = require('./homepage');
const translation = require('./translation');
const volunteer = require('./volunteer');

module.exports = {
admin,
category,
resource,
homepage,
translation,
volunteer,
};
120 changes: 120 additions & 0 deletions api/src/api/volunteer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const express = require('express');
const router = express.Router();
const { errorWrap } = require('../middleware');

const Category = require('../models/category');
const Resource = require('../models/resource');
const HomePage = require('../models/homepage');
const Translation = require('../models/translation');
const VerifiedTranslation = require('../models/verifiedTranslation');
const {
getVerifiedAggregation,
getNestedVerifiedAggregation,
getVerificationDetails,
} = require('../utils/verification');

// Get verified translations for table
router.get(
'/verified',
errorWrap(async (req, res) => {
const { language } = req.query;
const resourceInfo = await Resource.aggregate(
getVerifiedAggregation(language, 'resourceID', 'resource'),
);
const categoryInfo = await Category.aggregate(
getVerifiedAggregation(language, 'categoryID', 'category'),
);
const subcategoryInfo = await Category.aggregate(
getNestedVerifiedAggregation(
language,
'subcategoryID',
'subcategory',
'subcategories',
'name',
),
);
const testimonialInfo = await HomePage.aggregate(
getNestedVerifiedAggregation(
language,
'testimonialID',
'testimonial',
'testimonials',
'person',
),
);

res.json({
code: 200,
message: 'Successfully returned verified translation table info',
success: true,
result: resourceInfo.concat(
categoryInfo,
subcategoryInfo,
testimonialInfo,
),
});
}),
);

// Get verified translations for an ID
router.get(
'/verified/:id',
errorWrap(async (req, res) => {
const { id } = req.params;
const { language, type } = req.query;

const verificationDetails = await getVerificationDetails(
type,
language,
id,
);
const { messages } = await Translation.findOne({
language: { $eq: language },
});
verificationDetails.forEach((verificationObject) => {
const key = Object.keys(verificationObject)[0];
verificationObject[key][language] = messages.get(key);
});

res.json({
code: 200,
message: 'Successfully returned verified translation info',
success: true,
result: verificationDetails,
});
}),
);

// Update verified translations for an ID
router.put(
'/verified',
errorWrap(async (req, res) => {
const { language, type } = req.query;
const { translations } = req.body;

const translationsToUpdate = await Translation.findOne({
language: { $eq: language },
});
translations.forEach(async (translation) => {
const key = Object.keys(translation)[0];
translationsToUpdate.messages.set(key, translation[key][language]);
const verifiedTranslation = await VerifiedTranslation.updateOne(
{
translationID: key,
language,
},
{ $set: { verified: translation[key].verified } },
);
});
await translationsToUpdate.save();

res.json({
code: 200,
message: 'Successfully updated verified translation info',
success: true,
result: null,
});
}),
);

module.exports = router;
6 changes: 5 additions & 1 deletion api/src/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const authAdmin = async (req, res, next) => {
auth(req, res, next, ['admin']);
};

const authVolunteer = async (req, res, next) => {
auth(req, res, next, ['nawc volunteer']);
};

const authGeneral = async (req, res, next) => {
auth(req, res, next, []);
};
Expand Down Expand Up @@ -46,4 +50,4 @@ const auth = async (req, res, next, roles) => {
}
};

module.exports = { authAdmin, authGeneral };
module.exports = { authAdmin, authVolunteer, authGeneral };
12 changes: 10 additions & 2 deletions api/src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const express = require('express');

const router = express.Router();
const { admin, category, resource, homepage, translation } = require('../api');
const { authAdmin, authGeneral } = require('../middleware/auth');
const {
admin,
category,
resource,
homepage,
translation,
volunteer,
} = require('../api');
const { authAdmin, authVolunteer } = require('../middleware/auth');

router.use('/api/categories', category);
router.use('/api/resources', resource);
router.use('/api/admin', authAdmin, admin);
router.use('/api/homepage', homepage);
router.use('/api/translation', translation);
router.use('/api/volunteer', authVolunteer, volunteer);

module.exports = router;
4 changes: 2 additions & 2 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Made from [H4i Infrastructure Authentication Server](https://github.com/hack4imp

**admin** can promote or demote:

- _intern_
- _nawc volunteer_
- _public_

**intern** can promote or demote:
**nawc volunteer** can promote or demote:

- _public_

Expand Down
6 changes: 3 additions & 3 deletions auth/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
roles:
admin:
- intern
- nawc volunteer
- public
intern:
nawc volunteer:
- public
public: null
security_questions:
Expand All @@ -12,4 +12,4 @@ security_questions:
useGoogleAuth: false
security_question: true
gmail: false
expire_after_hrs: 1
expire_after_hrs: 2
7 changes: 5 additions & 2 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ const App = (): React$Element<React$FragmentType> => {
if (authRoleIsEquivalentTo('admin')) {
return <Redirect to="/admin" />;
}
if (authRoleIsEquivalentTo('nawc volunteer')) {
return <Redirect to="/translations" />;
}

return <Redirect to="/" />;
}
Expand Down Expand Up @@ -139,12 +142,12 @@ const App = (): React$Element<React$FragmentType> => {
path="/translations"
component={Translations}
exact
minRole="admin"
minRole="nawc volunteer"
/>
<PrivateRoute
path="/translations/:id"
component={EditTranslations}
minRole="admin"
minRole="nawc volunteer"
/>
<Route
path="/saved"
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const NavDesktop = (props: NavigationProps) => {
</Menu.Item>
)}

{authRoleIsEquivalentTo('admin') && (
{authRoleIsEquivalentTo('nawc volunteer') && (
<Menu.Item key="translations">
<NavLink to="/translations" activeClassName="navbar-active-style">
Translations
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/RoleApproval.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const RoleApproval = () => {
const RoleMenu = (email) => (
<Menu onClick={(e) => setNewRoleAndUser(e.key, email)}>
<Menu.Item key="public">public</Menu.Item>
<Menu.Item key="intern">intern</Menu.Item>
<Menu.Item key="nawc volunteer">NAWC volunteer</Menu.Item>
<Menu.Item key="admin">admin</Menu.Item>
</Menu>
);
Expand Down
Loading

0 comments on commit 2456aec

Please sign in to comment.