2021-02-15 11:24:28 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { pick, isNil } = require('lodash/fp');
|
|
|
|
const { getNonLocalizedFields } = require('./content-types');
|
|
|
|
|
|
|
|
const addLocalizations = async (entry, { model }) => {
|
|
|
|
if (isNil(entry.localizations)) {
|
|
|
|
const localizations = [{ locale: entry.locale, id: entry.id }];
|
|
|
|
await strapi.query(model.uid).update({ id: entry.id }, { localizations });
|
|
|
|
|
|
|
|
Object.assign(entry, { localizations });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateNonLocalizedFields = async (entry, { model }) => {
|
|
|
|
const fieldsToUpdate = pick(getNonLocalizedFields(model), entry);
|
|
|
|
|
|
|
|
if (Array.isArray(entry.localizations)) {
|
2021-02-16 12:23:51 +01:00
|
|
|
const updateQueries = entry.localizations
|
2021-02-15 11:24:28 +01:00
|
|
|
.filter(({ id }) => id != entry.id)
|
|
|
|
.map(({ id }) => strapi.query(model.uid).update({ id }, fieldsToUpdate));
|
|
|
|
|
2021-02-16 12:23:51 +01:00
|
|
|
await Promise.all(updateQueries);
|
2021-02-15 11:24:28 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addLocalizations,
|
|
|
|
updateNonLocalizedFields,
|
|
|
|
getNonLocalizedFields,
|
|
|
|
};
|