fix(content-manager): error when content type has no i18n enabled (#21026)

* fix(content-manager): error when content type has no i18n enabled

* fix(content-manager): history check if CT has D&P and i18n

* fix api tests
This commit is contained in:
Fernando Chávez 2024-08-23 13:31:42 +02:00 committed by GitHub
parent 686101fc1f
commit 45c8d25668
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 8 deletions

View File

@ -34,7 +34,15 @@ const createHistoryService = ({ strapi }: { strapi: Core.Strapi }) => {
results: HistoryVersions.HistoryVersionDataResponse[]; results: HistoryVersions.HistoryVersionDataResponse[];
pagination: HistoryVersions.Pagination; pagination: HistoryVersions.Pagination;
}> { }> {
const locale = params.query.locale || (await serviceUtils.getDefaultLocale()); const model = strapi.getModel(params.query.contentType);
const isLocalizedContentType = serviceUtils.isLocalizedContentType(model);
const defaultLocale = await serviceUtils.getDefaultLocale();
let locale = null;
if (isLocalizedContentType) {
locale = params.query.locale || defaultLocale;
}
const [{ results, pagination }, localeDictionary] = await Promise.all([ const [{ results, pagination }, localeDictionary] = await Promise.all([
query.findPage({ query.findPage({
...params.query, ...params.query,

View File

@ -1,4 +1,5 @@
import type { Core, Modules, UID } from '@strapi/types'; import type { Core, Modules, UID } from '@strapi/types';
import { contentTypes } from '@strapi/utils';
import { omit, castArray } from 'lodash/fp'; import { omit, castArray } from 'lodash/fp';
@ -131,13 +132,18 @@ const createLifecyclesService = ({ strapi }: { strapi: Core.Strapi }) => {
// All schemas related to the content type // All schemas related to the content type
const uid = context.contentType.uid; const uid = context.contentType.uid;
const schemas = getSchemas(uid); const schemas = getSchemas(uid);
const model = strapi.getModel(uid);
const isLocalizedContentType = serviceUtils.isLocalizedContentType(model);
// Find all affected entries // Find all affected entries
const localeEntries = await strapi.db.query(uid).findMany({ const localeEntries = await strapi.db.query(uid).findMany({
where: { where: {
documentId, documentId,
locale: { $in: locales }, ...(isLocalizedContentType ? { locale: { $in: locales } } : {}),
publishedAt: null, ...(contentTypes.hasDraftAndPublish(strapi.contentTypes[uid])
? { publishedAt: null }
: {}),
}, },
populate: serviceUtils.getDeepPopulate(uid, true /* use database syntax */), populate: serviceUtils.getDeepPopulate(uid, true /* use database syntax */),
}); });

View File

@ -118,9 +118,13 @@ export const createServiceUtils = ({ strapi }: { strapi: Core.Strapi }) => {
}; };
const localesService = strapi.plugin('i18n')?.service('locales'); const localesService = strapi.plugin('i18n')?.service('locales');
const i18nContentTypeService = strapi.plugin('i18n')?.service('content-types');
const getDefaultLocale = async () => (localesService ? localesService.getDefaultLocale() : null); const getDefaultLocale = async () => (localesService ? localesService.getDefaultLocale() : null);
const isLocalizedContentType = (model: Schema.ContentType) =>
i18nContentTypeService ? i18nContentTypeService.isLocalizedContentType(model) : false;
/** /**
* *
* @description * @description
@ -328,6 +332,7 @@ export const createServiceUtils = ({ strapi }: { strapi: Core.Strapi }) => {
getRelationRestoreValue, getRelationRestoreValue,
getMediaRestoreValue, getMediaRestoreValue,
getDefaultLocale, getDefaultLocale,
isLocalizedContentType,
getLocaleDictionary, getLocaleDictionary,
getRetentionDays, getRetentionDays,
getVersionStatus, getVersionStatus,

View File

@ -464,7 +464,7 @@ describeOnCondition(edition === 'EE')('History API', () => {
test('Finds many versions with pagination params', async () => { test('Finds many versions with pagination params', async () => {
const collectionType = await rq({ const collectionType = await rq({
method: 'GET', method: 'GET',
url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${collectionTypeDocumentId}&page=1&pageSize=1`, url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${collectionTypeDocumentId}&page=1&pageSize=1&locale=en`,
}); });
expect(collectionType.body.data).toHaveLength(1); expect(collectionType.body.data).toHaveLength(1);
@ -479,7 +479,7 @@ describeOnCondition(edition === 'EE')('History API', () => {
test('Finds many versions with sensitive data', async () => { test('Finds many versions with sensitive data', async () => {
const collectionType = await rq({ const collectionType = await rq({
method: 'GET', method: 'GET',
url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${collectionTypeDocumentId}&page=1&pageSize=1`, url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${collectionTypeDocumentId}&page=1&pageSize=1&locale=en`,
}); });
expect(collectionType.body.data).toHaveLength(1); expect(collectionType.body.data).toHaveLength(1);
@ -704,18 +704,20 @@ describeOnCondition(edition === 'EE')('History API', () => {
const frHistoryVersions = await rq({ const frHistoryVersions = await rq({
method: 'GET', method: 'GET',
url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${frProduct.data.documentId}`, url: `/content-manager/history-versions/?contentType=${collectionTypeUid}&documentId=${frProduct.data.documentId}&locale=fr`,
}); });
// Create + Publish = 2 versions // Create + Publish = 2 versions
expect(enHistoryVersions.body.data).toHaveLength(2); expect(enHistoryVersions.body.data).toHaveLength(2);
// First one should be the publish version // First one should be the publish version and english locale
expect(enHistoryVersions.body.data[0].status).toBe('published'); expect(enHistoryVersions.body.data[0].status).toBe('published');
expect(enHistoryVersions.body.data[0].locale.code).toBe('en');
// Create + Publish = 2 versions // Create + Publish = 2 versions
expect(frHistoryVersions.body.data).toHaveLength(2); expect(frHistoryVersions.body.data).toHaveLength(2);
// First one should be the publish version // First one should be the publish version and french locale
expect(frHistoryVersions.body.data[0].status).toBe('published'); expect(frHistoryVersions.body.data[0].status).toBe('published');
expect(frHistoryVersions.body.data[0].locale.code).toBe('fr');
}); });
}); });
}); });