2021-02-15 11:24:28 +01:00
|
|
|
'use strict';
|
|
|
|
|
2023-02-08 11:04:06 +01:00
|
|
|
const { prop, isNil, isEmpty, isArray } = require('lodash/fp');
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2023-02-22 15:01:10 +01:00
|
|
|
const { mapAsync } = require('@strapi/utils');
|
2021-02-17 13:00:49 +01:00
|
|
|
const { getService } = require('../utils');
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2023-02-06 15:39:47 +01:00
|
|
|
const isDialectMySQL = () => strapi.db.dialect.client === 'mysql';
|
|
|
|
|
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
|
|
|
|
*/
|
2023-02-10 11:10:13 +01:00
|
|
|
const assignDefaultLocaleToEntries = async (data) => {
|
2021-03-14 11:23:13 +01:00
|
|
|
const { getDefaultLocale } = getService('locales');
|
|
|
|
|
2023-02-08 11:04:06 +01:00
|
|
|
if (isArray(data) && data.some((entry) => !entry.locale)) {
|
|
|
|
const defaultLocale = await getDefaultLocale();
|
|
|
|
data.forEach((entry) => {
|
2023-02-10 11:10:13 +01:00
|
|
|
entry.locale = entry.locale || defaultLocale;
|
2023-02-08 11:04:06 +01:00
|
|
|
});
|
2023-02-10 11:10:13 +01:00
|
|
|
} else if (!isArray(data) && isNil(data.locale)) {
|
2021-03-14 11:23:13 +01:00
|
|
|
data.locale = await getDefaultLocale();
|
2021-02-17 13:00:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-09 11:31:29 +02:00
|
|
|
* Synchronize 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 }) => {
|
2023-04-05 16:06:22 +02:00
|
|
|
if (Array.isArray(entry?.localizations)) {
|
2021-03-09 11:12:09 +01:00
|
|
|
const newLocalizations = [entry.id, ...entry.localizations.map(prop('id'))];
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const updateLocalization = (id) => {
|
|
|
|
const localizations = newLocalizations.filter((localizationId) => localizationId !== id);
|
2021-03-08 16:39:39 +01:00
|
|
|
|
2021-07-19 16:47:24 +02:00
|
|
|
return strapi.query(model.uid).update({ where: { id }, data: { localizations } });
|
2021-03-08 16:39:39 +01:00
|
|
|
};
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2023-02-06 15:39:47 +01:00
|
|
|
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
2023-02-22 15:01:10 +01:00
|
|
|
await mapAsync(entry.localizations, (localization) => updateLocalization(localization.id), {
|
2023-02-06 19:11:58 +01:00
|
|
|
concurrency: isDialectMySQL() ? 1 : Infinity,
|
2023-02-06 15:39:47 +01:00
|
|
|
});
|
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-03-14 11:23:13 +01:00
|
|
|
const syncNonLocalizedAttributes = async (entry, { model }) => {
|
2021-04-02 15:13:23 +02:00
|
|
|
const { copyNonLocalizedAttributes } = getService('content-types');
|
2021-03-14 11:23:13 +01:00
|
|
|
|
2023-04-05 16:06:22 +02:00
|
|
|
if (Array.isArray(entry?.localizations)) {
|
2021-04-02 15:13:23 +02:00
|
|
|
const nonLocalizedAttributes = copyNonLocalizedAttributes(model, entry);
|
2021-02-15 11:24:28 +01:00
|
|
|
|
2021-04-02 15:13:23 +02:00
|
|
|
if (isEmpty(nonLocalizedAttributes)) {
|
2021-03-08 16:39:39 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const updateLocalization = (id) => {
|
2021-08-18 16:57:18 +02:00
|
|
|
return strapi.entityService.update(model.uid, id, { data: nonLocalizedAttributes });
|
2021-07-19 16:47:24 +02:00
|
|
|
};
|
2021-02-17 13:00:49 +01:00
|
|
|
|
2023-02-06 15:39:47 +01:00
|
|
|
// MySQL/MariaDB can cause deadlocks here if concurrency higher than 1
|
2023-02-22 15:01:10 +01:00
|
|
|
await mapAsync(entry.localizations, (localization) => updateLocalization(localization.id), {
|
2023-02-06 19:11:58 +01:00
|
|
|
concurrency: isDialectMySQL() ? 1 : Infinity,
|
2023-02-06 15:39:47 +01:00
|
|
|
});
|
2021-02-17 13:00:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-08 10:39:45 +02:00
|
|
|
module.exports = () => ({
|
2023-02-10 11:10:13 +01:00
|
|
|
assignDefaultLocaleToEntries,
|
2021-03-08 16:39:39 +01:00
|
|
|
syncLocalizations,
|
2021-03-14 11:23:13 +01:00
|
|
|
syncNonLocalizedAttributes,
|
2021-06-08 10:39:45 +02:00
|
|
|
});
|