mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
* feat(api-v4): new /faq route added * refactor(server): change of function name in libs/content.js
This commit is contained in:
77
website/server/controllers/api-v4/faq.js
Normal file
77
website/server/controllers/api-v4/faq.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import _ from 'lodash';
|
||||
import { query } from 'express-validator/check';
|
||||
import { langCodes } from '../../libs/i18n';
|
||||
import apiError from '../../libs/apiError';
|
||||
import common from '../../../common';
|
||||
import { localizeContentData } from '../../libs/content';
|
||||
|
||||
const { content } = common;
|
||||
const { faq } = content;
|
||||
|
||||
const api = {};
|
||||
|
||||
function _deleteProperties (obj, keysToDelete, platform) {
|
||||
// if there is no description for specified platform, use 'web' description by default
|
||||
if (obj[platform] === undefined) {
|
||||
delete obj.ios;
|
||||
delete obj.android;
|
||||
return;
|
||||
}
|
||||
|
||||
keysToDelete.forEach(key => delete obj[key]);
|
||||
}
|
||||
|
||||
function _deleteOtherPlatformsAnswers (faqObject, platform) {
|
||||
const faqCopy = _.cloneDeep(faqObject);
|
||||
const keysToDelete = _.without(['web', 'ios', 'android'], platform);
|
||||
|
||||
_deleteProperties(faqCopy.stillNeedHelp, keysToDelete, platform);
|
||||
faqCopy.questions.forEach(question => {
|
||||
_deleteProperties(question, keysToDelete, platform);
|
||||
});
|
||||
|
||||
return faqCopy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /api/v4/faq Get faq in json format
|
||||
* @apiDescription Does not require authentication.
|
||||
* @apiName FaqGet
|
||||
* @apiGroup Content
|
||||
*
|
||||
* @apiParam (Query) {String="bg","cs","da","de",
|
||||
* "en","en@pirate","en_GB",
|
||||
* "es","es_419","fr","he","hu",
|
||||
* "id","it","ja","nl","pl","pt","pt_BR",
|
||||
* "ro","ru","sk","sr","sv",
|
||||
* "uk","zh","zh_TW"} [language=en] Language code used for the items'
|
||||
* strings. If the authenticated user makes
|
||||
* the request, the content will return with
|
||||
* the user's configured language.
|
||||
*
|
||||
*
|
||||
* @apiSuccess {Object} data FAQ in a json format
|
||||
*/
|
||||
api.faq = {
|
||||
method: 'GET',
|
||||
url: '/faq',
|
||||
middlewares: [
|
||||
query('platform')
|
||||
.optional()
|
||||
.isIn(['web', 'android', 'ios']).withMessage(apiError('invalidPlatform')),
|
||||
],
|
||||
async handler (req, res) {
|
||||
const validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
const proposedLang = req.query.language && req.query.language.toString();
|
||||
const language = langCodes.includes(proposedLang) ? proposedLang : 'en';
|
||||
|
||||
const { platform } = req.query;
|
||||
|
||||
const dataToLocalize = platform ? _deleteOtherPlatformsAnswers(faq, platform) : faq;
|
||||
res.respond(200, localizeContentData(dataToLocalize, language));
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user