Set default locale when using Query Engine function createMany

This commit is contained in:
Pierre Noël 2023-02-08 11:04:06 +01:00
parent 4e53160dcf
commit 3c79572705
2 changed files with 12 additions and 2 deletions

View File

@ -13,6 +13,9 @@ const registerModelsHooks = () => {
async beforeCreate(event) {
await getService('localizations').assignDefaultLocale(event.params.data);
},
async beforeCreateMany(event) {
await getService('localizations').assignDefaultLocale(event.params.data);
},
});
}

View File

@ -1,6 +1,6 @@
'use strict';
const { prop, isNil, isEmpty } = require('lodash/fp');
const { prop, isNil, isEmpty, isArray } = require('lodash/fp');
const { getService } = require('../utils');
@ -11,7 +11,14 @@ const { getService } = require('../utils');
const assignDefaultLocale = 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) => {
if (isNil(entry.locale)) {
entry.locale = defaultLocale;
}
});
} else if (isNil(data.locale)) {
data.locale = await getDefaultLocale();
}
};