2020-04-01 22:45:43 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2020-10-29 07:56:28 -04:00
|
|
|
const privileges = require('../../privileges');
|
2020-04-01 22:45:43 -04:00
|
|
|
const categories = require('../../categories');
|
2020-10-16 22:41:38 -04:00
|
|
|
const api = require('../../api');
|
2020-04-01 22:45:43 -04:00
|
|
|
|
|
|
|
|
const helpers = require('../helpers');
|
|
|
|
|
|
|
|
|
|
const Categories = module.exports;
|
|
|
|
|
|
2020-10-29 07:56:28 -04:00
|
|
|
const hasAdminPrivilege = async (uid) => {
|
|
|
|
|
const ok = await privileges.admin.can(`admin:categories`, uid);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-29 10:31:53 -05:00
|
|
|
Categories.get = async (req, res) => {
|
|
|
|
|
helpers.formatApiResponse(200, res, await api.categories.get(req, req.params));
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-01 22:45:43 -04:00
|
|
|
Categories.create = async (req, res) => {
|
2020-10-29 07:56:28 -04:00
|
|
|
await hasAdminPrivilege(req.uid);
|
|
|
|
|
|
2020-10-16 22:41:38 -04:00
|
|
|
const response = await api.categories.create(req, req.body);
|
|
|
|
|
helpers.formatApiResponse(200, res, response);
|
2020-04-01 22:45:43 -04:00
|
|
|
};
|
2020-04-02 20:44:04 -04:00
|
|
|
|
|
|
|
|
Categories.update = async (req, res) => {
|
2020-10-29 07:56:28 -04:00
|
|
|
await hasAdminPrivilege(req.uid);
|
|
|
|
|
|
2020-04-02 20:44:04 -04:00
|
|
|
const payload = {};
|
|
|
|
|
payload[req.params.cid] = req.body;
|
2020-10-16 22:41:38 -04:00
|
|
|
await api.categories.update(req, payload);
|
2020-04-02 20:44:04 -04:00
|
|
|
const categoryObjs = await categories.getCategories([req.params.cid]);
|
|
|
|
|
helpers.formatApiResponse(200, res, categoryObjs[0]);
|
|
|
|
|
};
|
2020-04-03 11:37:41 -04:00
|
|
|
|
|
|
|
|
Categories.delete = async (req, res) => {
|
2020-10-29 07:56:28 -04:00
|
|
|
await hasAdminPrivilege(req.uid);
|
|
|
|
|
|
2020-10-16 22:41:38 -04:00
|
|
|
await api.categories.delete(req, { cid: req.params.cid });
|
2020-04-03 11:37:41 -04:00
|
|
|
helpers.formatApiResponse(200, res);
|
|
|
|
|
};
|
2021-03-04 12:46:31 -05:00
|
|
|
|
|
|
|
|
Categories.getPrivileges = async (req, res) => {
|
|
|
|
|
if (!await privileges.admin.can('admin:privileges', req.uid)) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const privilegeSet = await api.categories.getPrivileges(req, req.params.cid);
|
|
|
|
|
helpers.formatApiResponse(200, res, privilegeSet);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Categories.setPrivilege = async (req, res) => {
|
|
|
|
|
if (!await privileges.admin.can('admin:privileges', req.uid)) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await api.categories.setPrivilege(req, {
|
|
|
|
|
...req.params,
|
|
|
|
|
member: req.body.member,
|
|
|
|
|
set: req.method === 'PUT',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const privilegeSet = await api.categories.getPrivileges(req, req.params.cid);
|
|
|
|
|
helpers.formatApiResponse(200, res, privilegeSet);
|
|
|
|
|
};
|