import type { Core } from '@strapi/strapi'; import { LOG_PREFIX } from './constants'; const PARTNER_RELATIONS: Record = { 'api::partner.partner': [], 'api::partner-access.partner-access': ['partner'], 'api::resource.resource': ['partnerOrganisations'], 'api::news-article.news-article': ['authorOrganisation'], 'api::activity.activity': ['implementingPartners'], 'api::event.event': ['implementingPartners'], 'api::opportunity.opportunity': ['implementingPartners'], }; type ContentManagerConfiguration = { settings: Record; metadatas: Record; [key: string]: unknown }>; [key: string]: unknown; }; type ContentManagerContentTypesService = { findContentType(uid: string): Record | undefined; findConfiguration(contentType: Record): Promise; updateConfiguration( contentType: Record, configuration: ContentManagerConfiguration ): Promise; }; /** Always show Partner relation values by their organisation name in Content Manager. */ export const ensurePartnerRelationLabels = async (strapi: Core.Strapi): Promise => { const manager = strapi .plugin('content-manager') .service('content-types'); for (const [uid, fields] of Object.entries(PARTNER_RELATIONS)) { const contentType = manager.findContentType(uid); if (!contentType) continue; const config = await manager.findConfiguration(contentType); const metadatas = { ...config.metadatas }; let metadataChanged = false; for (const field of fields) { const metadata = metadatas[field] ?? {}; if (metadata.edit?.mainField === 'name') continue; metadatas[field] = { ...metadata, edit: { ...metadata.edit, mainField: 'name' }, }; metadataChanged = true; } const settingsChanged = uid === 'api::partner.partner' && config.settings.mainField !== 'name'; if (!metadataChanged && !settingsChanged) continue; await manager.updateConfiguration(contentType, { ...config, settings: uid === 'api::partner.partner' ? { ...config.settings, mainField: 'name' } : config.settings, metadatas, }); } strapi.log.debug(`${LOG_PREFIX} partner relations use organisation names in Content Manager.`); };