2021-03-08 15:39:09 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
const { get } = require('lodash/fp');
|
2021-10-20 17:30:05 +02:00
|
|
|
const { ApplicationError } = require('@strapi/utils').errors;
|
2021-06-08 10:39:45 +02:00
|
|
|
const { getService } = require('../utils');
|
2021-03-08 15:39:09 +01:00
|
|
|
|
|
|
|
const validateLocaleCreation = async (ctx, next) => {
|
|
|
|
const { model } = ctx.params;
|
|
|
|
const { query, body } = ctx.request;
|
|
|
|
|
2021-03-23 11:03:19 +01:00
|
|
|
const {
|
|
|
|
getValidLocale,
|
|
|
|
getNewLocalizationsFrom,
|
2021-04-07 11:43:46 +02:00
|
|
|
isLocalizedContentType,
|
2021-03-23 11:03:19 +01:00
|
|
|
getAndValidateRelatedEntity,
|
|
|
|
fillNonLocalizedAttributes,
|
|
|
|
} = getService('content-types');
|
2021-03-16 17:33:10 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
const modelDef = strapi.getModel(model);
|
|
|
|
|
2021-04-07 11:43:46 +02:00
|
|
|
if (!isLocalizedContentType(modelDef)) {
|
2021-03-14 11:23:13 +01:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2021-03-12 19:56:42 +01:00
|
|
|
const locale = get('plugins.i18n.locale', query);
|
|
|
|
const relatedEntityId = get('plugins.i18n.relatedEntityId', query);
|
2021-03-14 11:23:13 +01:00
|
|
|
// cleanup to avoid creating duplicates in singletypes
|
|
|
|
ctx.request.query = {};
|
|
|
|
|
|
|
|
let entityLocale;
|
|
|
|
try {
|
|
|
|
entityLocale = await getValidLocale(locale);
|
|
|
|
} catch (e) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError("This locale doesn't exist");
|
2021-03-14 11:23:13 +01:00
|
|
|
}
|
2021-03-12 19:56:42 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
body.locale = entityLocale;
|
2021-03-08 15:39:09 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
if (modelDef.kind === 'singleType') {
|
2021-09-21 17:18:24 +02:00
|
|
|
const entity = await strapi.entityService.findMany(modelDef.uid, { locale: entityLocale });
|
2021-03-08 15:39:09 +01:00
|
|
|
|
2021-07-19 16:47:24 +02:00
|
|
|
ctx.request.query.locale = body.locale;
|
2021-03-11 19:40:42 +01:00
|
|
|
|
2021-03-14 11:23:13 +01:00
|
|
|
// updating
|
|
|
|
if (entity) {
|
|
|
|
return next();
|
2021-03-11 19:40:42 +01:00
|
|
|
}
|
2021-03-14 11:23:13 +01:00
|
|
|
}
|
2021-03-11 19:40:42 +01:00
|
|
|
|
2021-03-23 11:03:19 +01:00
|
|
|
let relatedEntity;
|
2021-03-14 11:23:13 +01:00
|
|
|
try {
|
2021-05-26 09:00:56 +02:00
|
|
|
relatedEntity = await getAndValidateRelatedEntity(relatedEntityId, model, entityLocale);
|
2021-03-14 11:23:13 +01:00
|
|
|
} catch (e) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError(
|
2021-03-14 11:23:13 +01:00
|
|
|
"The related entity doesn't exist or the entity already exists in this locale"
|
|
|
|
);
|
2021-03-08 15:39:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 11:03:19 +01:00
|
|
|
fillNonLocalizedAttributes(body, relatedEntity, { model });
|
|
|
|
const localizations = await getNewLocalizationsFrom(relatedEntity);
|
|
|
|
body.localizations = localizations;
|
|
|
|
|
2021-03-08 15:39:09 +01:00
|
|
|
return next();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = validateLocaleCreation;
|