59 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-03-02 17:09:35 +01:00
'use strict';
2021-04-29 13:51:12 +02:00
const { isScalarAttribute } = require('@strapi/utils').contentTypes;
const { pick, prop, map, intersection, isEmpty, orderBy, pipe, every } = require('lodash/fp');
2021-06-09 18:10:44 +02:00
const { getService } = require('../../utils');
2021-03-02 17:09:35 +01:00
2021-03-11 10:41:35 +01:00
const shouldBeProcessed = processedLocaleCodes => entry => {
2021-03-02 17:09:35 +01:00
return (
2021-03-11 10:41:35 +01:00
entry.localizations.length > 0 &&
2021-03-02 17:09:35 +01:00
intersection(entry.localizations.map(prop('locale')), processedLocaleCodes).length === 0
);
};
2021-04-09 11:09:34 +02:00
const getUpdatesInfo = ({ entriesToProcess, attributesToMigrate }) => {
2021-03-02 17:09:35 +01:00
const updates = [];
for (const entry of entriesToProcess) {
2021-04-09 11:09:34 +02:00
const attributesValues = pick(attributesToMigrate, entry);
2021-03-11 10:41:35 +01:00
const entriesIdsToUpdate = entry.localizations.map(prop('id'));
2021-03-02 17:09:35 +01:00
updates.push({ entriesIdsToUpdate, attributesValues });
}
return updates;
};
const getSortedLocales = async ({ transacting } = {}) => {
const localeService = getService('locales');
let defaultLocale;
try {
const storeRes = await strapi
2021-06-22 17:13:11 +02:00
.query('strapi::core-store')
.findOne({ key: 'plugin_i18n_default_locale' }, null, { transacting });
defaultLocale = JSON.parse(storeRes.value);
} catch (e) {
throw new Error("Could not migrate because the default locale doesn't exist");
}
const locales = await localeService.find({}, null, { transacting });
if (isEmpty(locales)) {
throw new Error('Could not migrate because no locale exist');
}
// Put default locale first
return pipe(
map(locale => ({ code: locale.code, isDefault: locale.code === defaultLocale })),
orderBy(['isDefault', 'code'], ['desc', 'asc']),
map(prop('code'))
)(locales);
};
2021-04-09 11:09:34 +02:00
const areScalarAttributesOnly = ({ model, attributes }) =>
pipe(pick(attributes), every(isScalarAttribute))(model.attributes);
2021-03-02 17:09:35 +01:00
module.exports = {
2021-03-11 10:41:35 +01:00
shouldBeProcessed,
2021-03-02 17:09:35 +01:00
getUpdatesInfo,
getSortedLocales,
2021-04-09 11:09:34 +02:00
areScalarAttributesOnly,
2021-03-02 17:09:35 +01:00
};