71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import type { Core } from '@strapi/strapi';
|
|||
|
|
|
||
|
|
import { LOG_PREFIX } from './constants';
|
||
|
|
|
||
|
|
const PARTNER_RELATIONS: Record<string, string[]> = {
|
||
|
|
'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<string, unknown>;
|
||
|
|
metadatas: Record<string, { edit?: Record<string, unknown>; [key: string]: unknown }>;
|
||
|
|
[key: string]: unknown;
|
||
|
|
};
|
||
|
|
|
||
|
|
type ContentManagerContentTypesService = {
|
||
|
|
findContentType(uid: string): Record<string, unknown> | undefined;
|
||
|
|
findConfiguration(contentType: Record<string, unknown>): Promise<ContentManagerConfiguration>;
|
||
|
|
updateConfiguration(
|
||
|
|
contentType: Record<string, unknown>,
|
||
|
|
configuration: ContentManagerConfiguration
|
||
|
|
): Promise<unknown>;
|
||
|
|
};
|
||
|
|
|
||
|
|
/** Always show Partner relation values by their organisation name in Content Manager. */
|
||
|
|
export const ensurePartnerRelationLabels = async (strapi: Core.Strapi): Promise<void> => {
|
||
|
|
const manager = strapi
|
||
|
|
.plugin('content-manager')
|
||
|
|
.service<ContentManagerContentTypesService>('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.`);
|
||
|
|
};
|