2021-02-15 11:24:28 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-03-09 11:12:09 +01:00
|
|
|
const { pick, prop, isNil } = require('lodash/fp');
|
2021-02-17 13:00:49 +01:00
|
|
|
|
|
|
|
const { getService } = require('../utils');
|
2021-02-15 11:24:28 +01:00
|
|
|
const { getNonLocalizedFields } = require('./content-types');
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
/**
|
2021-02-17 18:40:45 +01:00
|
|
|
* Adds the default locale to an object if it isn't defined yet
|
2021-02-17 13:00:49 +01:00
|
|
|
* @param {Object} data a data object before being persisted into db
|
|
|
|
*/
|
|
|
|
const assignDefaultLocale = async data => {
|
|
|
|
if (isNil(data.locale)) {
|
|
|
|
data.locale = await getService('locales').getDefaultLocale();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-03-08 16:39:39 +01:00
|
|
|
* Syncronize related localizations from a root one
|
2021-02-17 13:00:49 +01:00
|
|
|
* @param {Object} entry entry to update
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.model corresponding model
|
|
|
|
*/
|
2021-03-08 16:39:39 +01:00
|
|
|
const syncLocalizations = async (entry, { model }) => {
|
|
|
|
if (Array.isArray(entry.localizations)) {
|
2021-03-09 11:12:09 +01:00
|
|
|
const newLocalizations = [entry.id, ...entry.localizations.map(prop('id'))];
|
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
const updateLocalization = id => {
|
2021-03-09 11:12:09 +01:00
|
|
|
const localizations = newLocalizations.filter(localizationId => localizationId !== id);
|
2021-03-08 16:39:39 +01:00
|
|
|
|
|
|
|
return strapi.query(model.uid).update({ id }, { localizations });
|
|
|
|
};
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
await Promise.all(entry.localizations.map(({ id }) => updateLocalization(id)));
|
2021-02-15 11:24:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-17 13:00:49 +01:00
|
|
|
/**
|
|
|
|
* Update non localized fields of all the related localizations of an entry with the entry values
|
|
|
|
* @param {Object} entry entry to update
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.model corresponding model
|
|
|
|
*/
|
2021-02-15 11:24:28 +01:00
|
|
|
const updateNonLocalizedFields = async (entry, { model }) => {
|
|
|
|
if (Array.isArray(entry.localizations)) {
|
2021-03-08 16:39:39 +01:00
|
|
|
const nonLocalizedFields = getNonLocalizedFields(model);
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
if (nonLocalizedFields.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
const fieldsToUpdate = pick(nonLocalizedFields, entry);
|
|
|
|
const updateLocalization = id => strapi.query(model.uid).update({ id }, fieldsToUpdate);
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2021-03-08 16:39:39 +01:00
|
|
|
await Promise.all(entry.localizations.map(({ id }) => updateLocalization(id)));
|
2021-02-17 13:00:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-15 11:24:28 +01:00
|
|
|
module.exports = {
|
2021-02-17 13:00:49 +01:00
|
|
|
assignDefaultLocale,
|
2021-03-08 16:39:39 +01:00
|
|
|
syncLocalizations,
|
2021-02-15 11:24:28 +01:00
|
|
|
updateNonLocalizedFields,
|
|
|
|
};
|