2021-03-08 16:39:39 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
const { has, omit } = require('lodash/fp');
|
2021-03-08 16:39:39 +01:00
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
const { getDefaultLocale } = require('./locales');
|
|
|
|
|
const { isLocalized } = require('./content-types');
|
|
|
|
|
const { syncLocalizations, updateNonLocalizedFields } = require('./localizations');
|
|
|
|
|
|
|
|
|
|
const LOCALE_QUERY_FILTER = '_locale';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds default locale or replaces _locale by locale in query params
|
|
|
|
|
* @param {object} params - query params
|
|
|
|
|
*/
|
|
|
|
|
const wrapParams = async (params = {}) => {
|
2021-03-15 11:38:39 +01:00
|
|
|
if (params.id) {
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
if (has(LOCALE_QUERY_FILTER, params)) {
|
|
|
|
|
return {
|
|
|
|
|
...omit(LOCALE_QUERY_FILTER, params),
|
|
|
|
|
locale: params[LOCALE_QUERY_FILTER],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...params,
|
|
|
|
|
locale: await getDefaultLocale(),
|
|
|
|
|
};
|
2021-03-08 16:39:39 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
/**
|
|
|
|
|
* Decorates the entity service with I18N business logic
|
|
|
|
|
* @param {object} service - entity service
|
|
|
|
|
*/
|
|
|
|
|
const decorator = service => ({
|
|
|
|
|
/**
|
|
|
|
|
* Wraps query options. In particular will add default locale to query params
|
|
|
|
|
* @param {object} opts - Query options object (params, data, files, populate)
|
|
|
|
|
* @param {object} ctx - Query context
|
|
|
|
|
* @param {object} ctx.model - Model that is being used
|
|
|
|
|
*/
|
|
|
|
|
async wrapOptions(opts = {}, ctx) {
|
2021-03-12 19:56:42 +01:00
|
|
|
const wrappedOptions = await service.wrapOptions.apply(this, [opts, ctx]);
|
2021-03-08 18:08:47 +01:00
|
|
|
const model = strapi.db.getModel(ctx.model);
|
|
|
|
|
|
|
|
|
|
if (!isLocalized(model)) {
|
|
|
|
|
return wrappedOptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...wrappedOptions,
|
|
|
|
|
params: await wrapParams(wrappedOptions.params),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an entry & make links between it and its related localizaionts
|
|
|
|
|
* @param {object} opts - Query options object (params, data, files, populate)
|
|
|
|
|
* @param {object} ctx - Query context
|
|
|
|
|
* @param {object} ctx.model - Model that is being used
|
|
|
|
|
*/
|
|
|
|
|
async create(opts, ctx) {
|
2021-03-08 16:39:39 +01:00
|
|
|
const model = strapi.db.getModel(ctx.model);
|
2021-03-12 19:56:42 +01:00
|
|
|
const entry = await service.create.apply(this, [opts, ctx]);
|
2021-03-08 16:39:39 +01:00
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
if (isLocalized(model)) {
|
|
|
|
|
await syncLocalizations(entry, { model });
|
2021-03-08 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates an entry & update related localizations fields
|
2021-03-08 18:08:47 +01:00
|
|
|
* @param {object} opts - Query options object (params, data, files, populate)
|
|
|
|
|
* @param {object} ctx - Query context
|
|
|
|
|
* @param {object} ctx.model - Model that is being used
|
2021-03-08 16:39:39 +01:00
|
|
|
*/
|
2021-03-08 18:08:47 +01:00
|
|
|
async update(opts, ctx) {
|
2021-03-08 16:39:39 +01:00
|
|
|
const model = strapi.db.getModel(ctx.model);
|
2021-03-11 16:19:49 +01:00
|
|
|
|
|
|
|
|
const { data, ...restOptions } = opts;
|
|
|
|
|
|
2021-03-12 19:56:42 +01:00
|
|
|
const entry = await service.update.apply(this, [
|
2021-03-11 16:19:49 +01:00
|
|
|
{
|
|
|
|
|
data: omit('locale', data),
|
|
|
|
|
...restOptions,
|
|
|
|
|
},
|
2021-03-12 19:56:42 +01:00
|
|
|
ctx,
|
|
|
|
|
]);
|
2021-03-08 16:39:39 +01:00
|
|
|
|
2021-03-08 18:08:47 +01:00
|
|
|
if (isLocalized(model)) {
|
|
|
|
|
await updateNonLocalizedFields(entry, { model });
|
2021-03-08 16:39:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-03-08 18:08:47 +01:00
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
decorator,
|
|
|
|
|
wrapParams,
|
|
|
|
|
};
|