Merge pull request #15742 from strapi/fix/default-locale-createMany

Set default locale when using Query Engine function createMany
This commit is contained in:
Pierre Noël 2023-02-13 12:38:41 +01:00 committed by GitHub
commit 4fe8f2f8b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 11 deletions

View File

@ -8,10 +8,16 @@ const registerModelsHooks = () => {
.map((contentType) => contentType.uid);
if (i18nModelUIDs.length > 0) {
// TODO V5 : to remove ?
// Should this code exist? It's putting business logic on the query engine
// whereas it should maybe stay on the entity service layer ?
strapi.db.lifecycles.subscribe({
models: i18nModelUIDs,
async beforeCreate(event) {
await getService('localizations').assignDefaultLocale(event.params.data);
await getService('localizations').assignDefaultLocaleToEntries(event.params.data);
},
async beforeCreateMany(event) {
await getService('localizations').assignDefaultLocaleToEntries(event.params.data);
},
});
}

View File

@ -1,6 +1,6 @@
'use strict';
const { assignDefaultLocale, syncLocalizations, syncNonLocalizedAttributes } =
const { assignDefaultLocaleToEntries, syncLocalizations, syncNonLocalizedAttributes } =
require('../localizations')();
const locales = require('../locales')();
@ -74,16 +74,16 @@ const setGlobalStrapi = () => {
};
describe('localizations service', () => {
describe('assignDefaultLocale', () => {
test('Does not change the input if locale is already defined', async () => {
describe('assignDefaultLocaleToEntries', () => {
test('Does not change the input if locale is already defined (single entry)', async () => {
setGlobalStrapi();
const input = { locale: 'myLocale' };
await assignDefaultLocale(input);
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'myLocale' });
});
test('Use default locale to set the locale on the input data', async () => {
test('Use default locale to set the locale on the input data (single entry)', async () => {
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
@ -91,11 +91,33 @@ describe('localizations service', () => {
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = {};
await assignDefaultLocale(input);
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual({ locale: 'defaultLocale' });
expect(getDefaultLocaleMock).toHaveBeenCalled();
});
test('Does not change the input if locale is already defined (multiple entries)', async () => {
setGlobalStrapi();
const input = [{ locale: 'myLocale' }, { locale: 'mylocalebis' }];
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual([{ locale: 'myLocale' }, { locale: 'mylocalebis' }]);
});
test('Use default locale to set the locale on the entries missing it (multiple entries)', async () => {
setGlobalStrapi();
const getDefaultLocaleMock = jest.fn(() => 'defaultLocale');
global.strapi.plugins.i18n.services.locales.getDefaultLocale = getDefaultLocaleMock;
const input = [{ locale: 'mylocale' }, {}];
await assignDefaultLocaleToEntries(input);
expect(input).toStrictEqual([{ locale: 'mylocale' }, { locale: 'defaultLocale' }]);
expect(getDefaultLocaleMock).toHaveBeenCalled();
});
});
describe('syncLocalizations', () => {

View File

@ -1,6 +1,6 @@
'use strict';
const { prop, isNil, isEmpty } = require('lodash/fp');
const { prop, isNil, isEmpty, isArray } = require('lodash/fp');
const { forEachAsync } = require('@strapi/utils');
const { getService } = require('../utils');
@ -11,10 +11,15 @@ const isDialectMySQL = () => strapi.db.dialect.client === 'mysql';
* Adds the default locale to an object if it isn't defined yet
* @param {Object} data a data object before being persisted into db
*/
const assignDefaultLocale = async (data) => {
const assignDefaultLocaleToEntries = async (data) => {
const { getDefaultLocale } = getService('locales');
if (isNil(data.locale)) {
if (isArray(data) && data.some((entry) => !entry.locale)) {
const defaultLocale = await getDefaultLocale();
data.forEach((entry) => {
entry.locale = entry.locale || defaultLocale;
});
} else if (!isArray(data) && isNil(data.locale)) {
data.locale = await getDefaultLocale();
}
};
@ -70,7 +75,7 @@ const syncNonLocalizedAttributes = async (entry, { model }) => {
};
module.exports = () => ({
assignDefaultLocale,
assignDefaultLocaleToEntries,
syncLocalizations,
syncNonLocalizedAttributes,
});