diff --git a/packages/contentstack-audit/src/messages/index.ts b/packages/contentstack-audit/src/messages/index.ts index b135c9f700..fe07b04cb5 100644 --- a/packages/contentstack-audit/src/messages/index.ts +++ b/packages/contentstack-audit/src/messages/index.ts @@ -40,7 +40,7 @@ const auditMsg = { SCAN_ASSET_SUCCESS_MSG: `Successfully completed the scanning of Asset with UID '{uid}'.`, SCAN_ASSET_WARN_MSG: `The locale '{locale}' or environment '{environment}' are not present for asset with uid '{uid}'`, ENTRY_PUBLISH_DETAILS: `Removing the publish detials for entry '{uid}' of ct '{ctuid}' in locale '{locale}' as locale '{publocale}' or environment '{environment}' does not exist`, - CT_REFERENCE_FIELD: `The mentioned Reference Field is not Array field name 'reference_to' having display name 'display_name'`, + CT_REFERENCE_FIELD: `The mentioned Reference field is not Array field reference is '{reference_to}' having display name '{display_name}''`, ASSET_NOT_EXIST: `The publish_details either does not exist or is not an array for asset uid '{uid}'`, ENTRY_PUBLISH_DETAILS_NOT_EXIST: `The publish_details either does not exist or is not an array for entry uid '{uid}'`, }; diff --git a/packages/contentstack-audit/src/modules/content-types.ts b/packages/contentstack-audit/src/modules/content-types.ts index 299baf981e..23d18548a6 100644 --- a/packages/contentstack-audit/src/modules/content-types.ts +++ b/packages/contentstack-audit/src/modules/content-types.ts @@ -53,14 +53,20 @@ export default class ContentType { this.gfSchema = gfSchema; this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig); this.fileName = config.moduleConfig[this.moduleName].fileName; - this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName)); + this.folderPath = resolve( + sanitizePath(config.basePath), + sanitizePath(config.moduleConfig[this.moduleName].dirName), + ); } - validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record): keyof typeof auditConfig.moduleConfig { + validateModules( + moduleName: keyof typeof auditConfig.moduleConfig, + moduleConfig: Record, + ): keyof typeof auditConfig.moduleConfig { if (Object.keys(moduleConfig).includes(moduleName)) { return moduleName; } - return 'content-types' + return 'content-types'; } /** * The `run` function checks if a folder path exists, sets the schema based on the module name, @@ -121,7 +127,7 @@ export default class ContentType { if (existsSync(extensionPath)) { try { this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8'))); - } catch (error) { } + } catch (error) {} } if (existsSync(marketplacePath)) { @@ -134,7 +140,7 @@ export default class ContentType { ) as string[]; this.extensions.push(...metaData); } - } catch (error) { } + } catch (error) {} } } @@ -270,19 +276,19 @@ export default class ContentType { return missingRefs.length ? [ - { - tree, - data_type, - missingRefs, - display_name, - ct_uid: this.currentUid, - name: this.currentTitle, - treeStr: tree - .map(({ name }) => name) - .filter((val) => val) - .join(' ➜ '), - }, - ] + { + tree, + data_type, + missingRefs, + display_name, + ct_uid: this.currentUid, + name: this.currentTitle, + treeStr: tree + .map(({ name }) => name) + .filter((val) => val) + .join(' ➜ '), + }, + ] : []; } @@ -383,37 +389,46 @@ export default class ContentType { const missingRefs: string[] = []; let { reference_to, display_name, data_type } = field; - if(!Array.isArray(reference_to)) { - this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' }); - } - for (const reference of reference_to ?? []) { - // NOTE Can skip specific references keys (Ex, system defined keys can be skipped) - if (this.config.skipRefs.includes(reference)) { - continue; + if (!Array.isArray(reference_to)) { + this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), 'error'); + this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info'); + if (!this.config.skipRefs.includes(reference_to)) { + const refExist = find(this.ctSchema, { uid: reference_to }); + + if (!refExist) { + missingRefs.push(reference_to); + } } + } else { + for (const reference of reference_to ?? []) { + // NOTE Can skip specific references keys (Ex, system defined keys can be skipped) + if (this.config.skipRefs.includes(reference)) { + continue; + } - const refExist = find(this.ctSchema, { uid: reference }); + const refExist = find(this.ctSchema, { uid: reference }); - if (!refExist) { - missingRefs.push(reference); + if (!refExist) { + missingRefs.push(reference); + } } } return missingRefs.length ? [ - { - tree, - data_type, - missingRefs, - display_name, - ct_uid: this.currentUid, - name: this.currentTitle, - treeStr: tree - .map(({ name }) => name) - .filter((val) => val) - .join(' ➜ '), - }, - ] + { + tree, + data_type, + missingRefs, + display_name, + ct_uid: this.currentUid, + name: this.currentTitle, + treeStr: tree + .map(({ name }) => name) + .filter((val) => val) + .join(' ➜ '), + }, + ] : []; } @@ -638,19 +653,30 @@ export default class ContentType { let fixStatus; const missingRefs: string[] = []; const { reference_to, data_type, display_name } = field; - if(!Array.isArray(reference_to)) { - this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' }); - } - for (const reference of reference_to ?? []) { - // NOTE Can skip specific references keys (Ex, system defined keys can be skipped) - if (this.config.skipRefs.includes(reference)) { - continue; + if (!Array.isArray(reference_to)) { + this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'error'); + this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info'); + if (!this.config.skipRefs.includes(reference_to)) { + const refExist = find(this.ctSchema, { uid: reference_to }); + + if (!refExist) { + missingRefs.push(reference_to); + } } - const refExist = find(this.ctSchema, { uid: reference }); + field.reference_to = [reference_to]; + } else { + for (const reference of reference_to ?? []) { + // NOTE Can skip specific references keys (Ex, system defined keys can be skipped) + if (this.config.skipRefs.includes(reference)) { + continue; + } - if (!refExist) { - missingRefs.push(reference); + const refExist = find(this.ctSchema, { uid: reference }); + + if (!refExist) { + missingRefs.push(reference); + } } } diff --git a/packages/contentstack-audit/src/modules/entries.ts b/packages/contentstack-audit/src/modules/entries.ts index 0db5a483fe..0f92f6d8bd 100644 --- a/packages/contentstack-audit/src/modules/entries.ts +++ b/packages/contentstack-audit/src/modules/entries.ts @@ -175,7 +175,7 @@ export default class Entries { const localKey = this.locales.map((locale: any) => locale.code); - if(this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) { + if (this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) { this.log($t(auditMsg.ENTRY_PUBLISH_DETAILS_NOT_EXIST, { uid: entryUid }), { color: 'red' }); } @@ -194,11 +194,23 @@ export default class Entries { { color: 'red' }, ); if (!Object.keys(this.missingEnvLocale).includes(entryUid)) { - this.missingEnvLocale[entryUid] = [{ entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code }]; + this.missingEnvLocale[entryUid] = [ + { + entry_uid: entryUid, + publish_locale: pd.locale, + publish_environment: pd.environment, + ctUid: ctSchema.uid, + ctLocale: code, + }, + ]; } else { - this.missingEnvLocale[entryUid].push( - { entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code }, - ); + this.missingEnvLocale[entryUid].push({ + entry_uid: entryUid, + publish_locale: pd.locale, + publish_environment: pd.environment, + ctUid: ctSchema.uid, + ctLocale: code, + }); } return false; } @@ -332,7 +344,6 @@ export default class Entries { field: ContentTypeStruct | GlobalFieldDataType | ModularBlockType | GroupFieldDataType, entry: EntryFieldType, ) { - if (this.fix) { entry = this.runFixOnSchema(tree, field.schema as ContentTypeSchemaType[], entry); } @@ -631,18 +642,29 @@ export default class Entries { if (this.fix) return []; const missingRefs: Record[] = []; - const { uid: data_type, display_name } = fieldStructure; + const { uid: data_type, display_name, reference_to } = fieldStructure; for (const index in field ?? []) { - const reference = field[index]; + const reference: any = field[index]; const { uid } = reference; + if (!uid && reference.startsWith('blt')) { + const refExist = find(this.entryMetaData, { uid: reference }); + if (!refExist) { + if(Array.isArray(reference_to) && reference_to.length===1) { + missingRefs.push({uid:reference, _content_type_uid: reference_to[0]}); + } else { + missingRefs.push(reference); + } + } + } // NOTE Can skip specific references keys (Ex, system defined keys can be skipped) // if (this.config.skipRefs.includes(reference)) continue; + else { + const refExist = find(this.entryMetaData, { uid }); - const refExist = find(this.entryMetaData, { uid }); - - if (!refExist) { - missingRefs.push(reference); + if (!refExist) { + missingRefs.push(reference); + } } } @@ -847,7 +869,7 @@ export default class Entries { * @returns */ fixSelectField(tree: Record[], field: SelectFeildStruct, entry: any) { - if(!this.config.fixSelectField) { + if (!this.config.fixSelectField) { return entry; } const { enum: selectOptions, multiple, min_instance, display_type, display_name, uid } = field; @@ -1197,16 +1219,29 @@ export default class Entries { entry = JSON.parse(stringReference); } entry = entry - ?.map((reference) => { + ?.map((reference: any) => { const { uid } = reference; - const refExist = find(this.entryMetaData, { uid }); - - if (!refExist) { - missingRefs.push(reference); - return null; + const { reference_to } = field; + if (!uid && reference.startsWith('blt')) { + const refExist = find(this.entryMetaData, { uid: reference }); + if (!refExist) { + if(Array.isArray(reference_to) && reference_to.length===1) { + missingRefs.push({uid:reference, _content_type_uid: reference_to[0]}); + } else { + missingRefs.push(reference); + } + } else { + return { uid: reference, _content_type_uid: refExist.ctUid }; + } + } else { + const refExist = find(this.entryMetaData, { uid }); + if (!refExist) { + missingRefs.push(reference); + return null; + } else { + return reference; + } } - - return reference; }) .filter((val) => val) as EntryReferenceFieldDataType[]; @@ -1364,7 +1399,7 @@ export default class Entries { `error`, ); } - this.entryMetaData.push({ uid: entryUid, title }); + this.entryMetaData.push({ uid: entryUid, title, ctUid:uid }); } } }