Skip to content

Commit

Permalink
Implemented nested global fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sunil-lakshman committed Jan 3, 2025
1 parent 51428a6 commit 6d67aa7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
30 changes: 27 additions & 3 deletions packages/contentstack-import/src/import/modules/content-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import BaseClass, { ApiOptions } from './base-class';
import { updateFieldRules } from '../../utils/content-type-helper';
import { sanitizePath } from '@contentstack/cli-utilities';

type GlobalFieldData = {
uid: string;
title: string;
schema: Array<{ data_type: string }>;
};
const nestedGlobalFieldsVersion = '3.2';

export default class ContentTypesImport extends BaseClass {
private cTsMapperPath: string;
private cTsFolderPath: string;
Expand Down Expand Up @@ -232,7 +239,7 @@ export default class ContentTypesImport extends BaseClass {
log(this.importConfig, `Updated the global field ${uid} with content type references`, 'info');
};
const onReject = ({ error, apiData: { uid } = undefined }: any) => {
log(this.importConfig, `failed to update the global field '${uid}' ${formatError(error)}`, 'error');
log(this.importConfig, `Failed to update the global field '${uid}' ${formatError(error)}`, 'error');
};
return await this.makeConcurrentCall({
processName: 'Update pending global fields',
Expand Down Expand Up @@ -260,18 +267,35 @@ export default class ContentTypesImport extends BaseClass {
apiData: { uid },
} = apiOptions;
const globalField = find(this.gFs, { uid });
const hasNested = this.hasNestedGlobalFields(globalField as GlobalFieldData);
lookupExtension(
this.importConfig,
globalField.schema,
this.importConfig.preserveStackVersion,
this.installedExtensions,
);
apiOptions.apiData = globalField;
const globalFieldPayload = this.stack.globalField(uid);
const globalFieldPayload = this.stack.globalField(
uid,
hasNested ? { api_version: nestedGlobalFieldsVersion } : undefined,
);
Object.assign(globalFieldPayload, cloneDeep(globalField));
apiOptions.apiData = globalFieldPayload;
return apiOptions;
}

/**
* Check if a global field has nested global fields
* @param {GlobalFieldData} globalField The global field data
* @returns {boolean} True if nested global fields are present, otherwise false
*/
hasNestedGlobalFields(globalField: GlobalFieldData): boolean {
if (!globalField || !globalField.schema) {
return false;
}
// Check for nested global fields in the schema
return globalField.schema.some((field: any) => field.data_type === 'global_field');
}


async updatePendingExtensions(): Promise<any> {
let apiContent = fsUtil.readFile(this.extPendingPath) as Record<string, any>[];
Expand Down
25 changes: 20 additions & 5 deletions packages/contentstack-import/src/import/modules/global-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GlobalFieldData } from '@contentstack/management/types/stack/globalFiel
import { fsUtil, log, formatError, fileHelper, lookupExtension, removeReferenceFields } from '../../utils';
import { ImportConfig, ModuleClassParams } from '../../types';
import BaseClass, { ApiOptions } from './base-class';

const nestedGlobalFieldsVersion = '3.2';
export default class ImportGlobalFields extends BaseClass {
private gFsMapperPath: string;
private gFsFolderPath: string;
Expand Down Expand Up @@ -140,17 +140,18 @@ export default class ImportGlobalFields extends BaseClass {
if (flag.supressed) {
this.pendingGFs.push(globalField.uid);
}
const hasNested = this.hasNestedGlobalFields(globalField as GlobalFieldData);
return this.stack
.globalField()
.globalField(null, hasNested ? { api_version: nestedGlobalFieldsVersion} : undefined)
.create({ global_field: globalField as GlobalFieldData })
.then((response) => {
.then((response: GlobalFieldData) => {
apiParams.resolve({
response,
apiData: globalField,
});
resolve(true);
})
.catch((error) => {
.catch((error: unknown) => {
apiParams.reject({
error,
apiData: globalField,
Expand Down Expand Up @@ -192,14 +193,28 @@ export default class ImportGlobalFields extends BaseClass {
);
}

/**
* Check if a global field has nested global fields
* @param {GlobalFieldData} globalField The global field data
* @returns {boolean} True if nested global fields are present, otherwise false
*/
hasNestedGlobalFields(globalField: GlobalFieldData): boolean {
if (!globalField || !globalField.schema) {
return false;
}
// Check for nested global fields in the schema
return globalField.schema.some((field: any) => field.data_type === 'global_field');
}

/**
* @method serializeUpdateGFs
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeReplaceGFs(apiOptions: ApiOptions): ApiOptions {
const { apiData: globalField } = apiOptions;
const globalFieldPayload = this.stack.globalField(globalField.uid);
const hasNested = this.hasNestedGlobalFields(apiOptions.apiData as GlobalFieldData)
const globalFieldPayload = this.stack.globalField(globalField.uid, hasNested ? { api_version: nestedGlobalFieldsVersion } : undefined);
Object.assign(globalFieldPayload, cloneDeep(globalField), {
stackHeaders: globalFieldPayload.stackHeaders,
});
Expand Down

0 comments on commit 6d67aa7

Please sign in to comment.