revises the way documents are created so that they can be created directly in a specific locale

This commit is contained in:
James
2020-04-13 11:57:41 -04:00
parent ffb5f97756
commit 286ea9caff
7 changed files with 37 additions and 11 deletions

View File

@@ -4,10 +4,11 @@ const executePolicy = require('../../auth/executePolicy');
const create = async (options) => {
try {
const {
model,
model: Model,
data,
config,
user,
locale,
} = options;
const policy = config && config.policies && config.policies.create;
@@ -18,7 +19,14 @@ const create = async (options) => {
// Await pre-hook here
const doc = await model.create(data);
const doc = new Model();
if (locale && doc.setLocale) {
doc.setLocale(locale);
}
Object.assign(doc, data);
await doc.save();
// Await post hook here

View File

@@ -9,6 +9,7 @@ const update = async (options) => {
data,
config,
user,
locale,
} = options;
const policy = config && config.policies && config.policies.update;
@@ -20,6 +21,11 @@ const update = async (options) => {
// Await pre-hook here
const doc = await model.findOne({ _id: id });
if (locale && doc.setLocale) {
doc.setLocale(locale);
}
Object.assign(doc, data);
await doc.save();

View File

@@ -10,6 +10,8 @@ const createHandler = async (req, res) => {
config: req.collection,
user: req.user,
data: req.body,
locale: req.locale,
fallbackLocale: req.fallbackLocale,
});
return res.status(httpStatus.CREATED).json({

View File

@@ -15,7 +15,7 @@ const findHandler = async (req, res) => {
},
depth: req.query.depth,
locale: req.locale,
fallbackLocale: req.query['fallback-locale'],
fallbackLocale: req.fallbackLocale,
};
if (req.query.where) options.query.where = req.query.where;

View File

@@ -9,7 +9,7 @@ const findByIDHandler = async (req, res) => {
user: req.user,
id: req.params.id,
locale: req.locale,
fallbackLocale: req.query['fallback-locale'],
fallbackLocale: req.fallbackLocale,
depth: req.query.depth,
};

View File

@@ -12,6 +12,7 @@ const updateHandler = async (req, res) => {
user: req.user,
id: req.params.id,
data: req.body,
locale: req.locale,
});
if (!doc) return res.status(httpStatus.NOT_FOUND).json(formatErrorResponse(new NotFound(), 'APIError'));

View File

@@ -6,19 +6,28 @@
*/
module.exports = function localizationMiddleware(localization) {
const middleware = (req, res, next) => {
if (req.query.locale === '*' || req.query.locale === 'all') {
req.locale = 'all';
return next();
const validLocales = [...localization.locales, 'all'];
const validFallbackLocales = [...localization.locales, 'null'];
let requestedLocale = req.query.locale;
let requestedFallbackLocale = req.query['fallback-locale'];
if (req.body) {
if (req.body.locale) requestedLocale = req.body.locale;
if (req.body['fallback-locale']) requestedFallbackLocale = req.body['fallback-locale'];
}
let requestedLocale = req.body && req.body.locale;
if (req.query.locale) requestedLocale = req.query.locale;
if (requestedFallbackLocale === 'none') requestedFallbackLocale = 'null';
if (requestedLocale === '*' || requestedLocale === 'all') requestedLocale = 'all';
if (localization.locales.find(search => search === requestedLocale)) {
if (validLocales.find(locale => locale === requestedLocale)) {
req.locale = requestedLocale;
}
if (!req.locale) req.locale = localization.defaultLocale;
if (validFallbackLocales.find(locale => locale === requestedFallbackLocale)) {
req.fallbackLocale = requestedFallbackLocale;
}
return next();
};