Skip to content

Commit 0271527

Browse files
committed
fix: Refactor buildSchemaTree function to improve null safety and handle optional chaining for field properties
1 parent 4ac4764 commit 0271527

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

api/src/utils/content-type-creator.utils.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ function getLastSegmentNew(str: string, separator: string): string {
237237
return segments[segments.length - 1].trim();
238238
}
239239

240-
export function buildSchemaTree(fields: any[], parentUid = '', parentType = '', oldPrentUid = ''): any[] {
240+
export function buildSchemaTree(fields: any[], parentUid = '', parentType = '', oldParentUid = ''): any[] {
241241

242242
if (!Array.isArray(fields)) {
243243
console.warn('buildSchemaTree called with invalid fields:', fields);
@@ -253,25 +253,25 @@ export function buildSchemaTree(fields: any[], parentUid = '', parentType = '',
253253

254254
// Filter direct children of current parent
255255
const directChildren = fields.filter(field => {
256-
const fieldUid = field.contentstackFieldUid || '';
256+
const fieldUid = field?.contentstackFieldUid || '';
257257

258258
if (!parentUid) {
259259
// Root level - only fields without dots
260-
return fieldUid && !fieldUid.includes('.');
260+
return fieldUid && !fieldUid?.includes('.');
261261
}
262262

263263
// Check if field is a direct child of parentUid
264-
if (fieldUid.startsWith(parentUid + '.')) {
265-
const remainder = fieldUid.substring(parentUid.length + 1);
264+
if (fieldUid?.startsWith(parentUid + '.')) {
265+
const remainder = fieldUid?.substring(parentUid.length + 1);
266266
// Verify it's exactly one level deeper (no more dots in remainder)
267-
return remainder && !remainder.includes('.');
267+
return remainder && !remainder?.includes('.');
268268
}
269269

270270
// Fallback: check if field is a direct child of oldPrentUid (if provided and different)
271-
if (oldPrentUid && oldPrentUid !== parentUid && fieldUid.startsWith(oldPrentUid + '.')) {
272-
const remainder = fieldUid.substring(oldPrentUid.length + 1);
271+
if (oldParentUid && oldParentUid !== parentUid && fieldUid?.startsWith(oldParentUid + '.')) {
272+
const remainder = fieldUid?.substring(oldParentUid.length + 1);
273273
// Verify it's exactly one level deeper (no more dots in remainder)
274-
return remainder && !remainder.includes('.');
274+
return remainder && !remainder?.includes('.');
275275
}
276276

277277
// Not a direct child
@@ -280,7 +280,7 @@ export function buildSchemaTree(fields: any[], parentUid = '', parentType = '',
280280

281281
return directChildren.map(field => {
282282
const uid = getLastSegmentNew(field.contentstackFieldUid, '.');
283-
const displayName = field.display_name || getLastSegmentNew(field.contentstackField || '', '>').trim();
283+
const displayName = field?.display_name || getLastSegmentNew(field?.contentstackField || '', '>').trim();
284284

285285
// Base field structure
286286
const result: any = {
@@ -290,25 +290,25 @@ export function buildSchemaTree(fields: any[], parentUid = '', parentType = '',
290290
};
291291

292292
// Determine if field should have nested schema
293-
const fieldUid = field.contentstackFieldUid;
294-
const fieldType = field.contentstackFieldType;
295-
const oldFieldtUid = field.backupFieldUid;
293+
const fieldUid = field?.contentstackFieldUid;
294+
const fieldType = field?.contentstackFieldType;
295+
const oldFieldUid = field?.backupFieldUid;
296296

297297
// Check if this field has direct children (exactly one level deeper)
298298
const hasChildren = fields.some(f => {
299-
const fUid = f.contentstackFieldUid || '';
299+
const fUid = f?.contentstackFieldUid || '';
300300
if (!fUid) return false;
301301

302302
// Check if field starts with current fieldUid and is exactly one level deeper
303303
if (fieldUid && fUid.startsWith(fieldUid + '.')) {
304-
const remainder = fUid.substring(fieldUid.length + 1);
305-
return remainder && !remainder.includes('.');
304+
const remainder = fUid?.substring(fieldUid.length + 1);
305+
return remainder && !remainder?.includes('.');
306306
}
307307

308308
// Check if field starts with oldFieldtUid and is exactly one level deeper
309-
if (oldFieldtUid && fUid.startsWith(oldFieldtUid + '.')) {
310-
const remainder = fUid.substring(oldFieldtUid.length + 1);
311-
return remainder && !remainder.includes('.');
309+
if (oldFieldUid && fUid.startsWith(oldFieldUid + '.')) {
310+
const remainder = fUid.substring(oldFieldUid.length + 1);
311+
return remainder && !remainder?.includes('.');
312312
}
313313

314314
return false;
@@ -338,7 +338,7 @@ export function buildSchemaTree(fields: any[], parentUid = '', parentType = '',
338338
} else if (fieldType === 'group' ||
339339
(fieldType === 'modular_blocks_child' && hasChildren)) {
340340
// Recursively build schema for groups and modular block children with nested content
341-
result.schema = buildSchemaTree(fields, fieldUid, fieldType, oldFieldtUid);
341+
result.schema = buildSchemaTree(fields, fieldUid, fieldType, oldFieldUid);
342342
}
343343
}
344344

0 commit comments

Comments
 (0)