Skip to content

Commit 2695c2f

Browse files
authored
Merge pull request #1819 from contentstack/fix/DX-2339
DX - 2339 - Added Uid and content_type_uid field in reference of older format
2 parents 87a8593 + a6abc60 commit 2695c2f

File tree

3 files changed

+135
-74
lines changed

3 files changed

+135
-74
lines changed

packages/contentstack-audit/src/messages/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const auditMsg = {
4040
SCAN_ASSET_SUCCESS_MSG: `Successfully completed the scanning of Asset with UID '{uid}'.`,
4141
SCAN_ASSET_WARN_MSG: `The locale '{locale}' or environment '{environment}' are not present for asset with uid '{uid}'`,
4242
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`,
43-
CT_REFERENCE_FIELD: `The mentioned Reference Field is not Array field name 'reference_to' having display name 'display_name'`,
43+
CT_REFERENCE_FIELD: `The mentioned Reference field is not Array field reference is '{reference_to}' having display name '{display_name}''`,
4444
ASSET_NOT_EXIST: `The publish_details either does not exist or is not an array for asset uid '{uid}'`,
4545
ENTRY_PUBLISH_DETAILS_NOT_EXIST: `The publish_details either does not exist or is not an array for entry uid '{uid}'`,
4646
};

packages/contentstack-audit/src/modules/content-types.ts

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ export default class ContentType {
5353
this.gfSchema = gfSchema;
5454
this.moduleName = this.validateModules(moduleName!, this.config.moduleConfig);
5555
this.fileName = config.moduleConfig[this.moduleName].fileName;
56-
this.folderPath = resolve(sanitizePath(config.basePath), sanitizePath(config.moduleConfig[this.moduleName].dirName));
56+
this.folderPath = resolve(
57+
sanitizePath(config.basePath),
58+
sanitizePath(config.moduleConfig[this.moduleName].dirName),
59+
);
5760
}
5861

59-
validateModules(moduleName: keyof typeof auditConfig.moduleConfig, moduleConfig: Record<string, unknown>): keyof typeof auditConfig.moduleConfig {
62+
validateModules(
63+
moduleName: keyof typeof auditConfig.moduleConfig,
64+
moduleConfig: Record<string, unknown>,
65+
): keyof typeof auditConfig.moduleConfig {
6066
if (Object.keys(moduleConfig).includes(moduleName)) {
6167
return moduleName;
6268
}
63-
return 'content-types'
69+
return 'content-types';
6470
}
6571
/**
6672
* 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 {
121127
if (existsSync(extensionPath)) {
122128
try {
123129
this.extensions = Object.keys(JSON.parse(readFileSync(extensionPath, 'utf8')));
124-
} catch (error) { }
130+
} catch (error) {}
125131
}
126132

127133
if (existsSync(marketplacePath)) {
@@ -134,7 +140,7 @@ export default class ContentType {
134140
) as string[];
135141
this.extensions.push(...metaData);
136142
}
137-
} catch (error) { }
143+
} catch (error) {}
138144
}
139145
}
140146

@@ -270,19 +276,19 @@ export default class ContentType {
270276

271277
return missingRefs.length
272278
? [
273-
{
274-
tree,
275-
data_type,
276-
missingRefs,
277-
display_name,
278-
ct_uid: this.currentUid,
279-
name: this.currentTitle,
280-
treeStr: tree
281-
.map(({ name }) => name)
282-
.filter((val) => val)
283-
.join(' ➜ '),
284-
},
285-
]
279+
{
280+
tree,
281+
data_type,
282+
missingRefs,
283+
display_name,
284+
ct_uid: this.currentUid,
285+
name: this.currentTitle,
286+
treeStr: tree
287+
.map(({ name }) => name)
288+
.filter((val) => val)
289+
.join(' ➜ '),
290+
},
291+
]
286292
: [];
287293
}
288294

@@ -383,37 +389,46 @@ export default class ContentType {
383389
const missingRefs: string[] = [];
384390
let { reference_to, display_name, data_type } = field;
385391

386-
if(!Array.isArray(reference_to)) {
387-
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' });
388-
}
389-
for (const reference of reference_to ?? []) {
390-
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
391-
if (this.config.skipRefs.includes(reference)) {
392-
continue;
392+
if (!Array.isArray(reference_to)) {
393+
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), 'error');
394+
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info');
395+
if (!this.config.skipRefs.includes(reference_to)) {
396+
const refExist = find(this.ctSchema, { uid: reference_to });
397+
398+
if (!refExist) {
399+
missingRefs.push(reference_to);
400+
}
393401
}
402+
} else {
403+
for (const reference of reference_to ?? []) {
404+
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
405+
if (this.config.skipRefs.includes(reference)) {
406+
continue;
407+
}
394408

395-
const refExist = find(this.ctSchema, { uid: reference });
409+
const refExist = find(this.ctSchema, { uid: reference });
396410

397-
if (!refExist) {
398-
missingRefs.push(reference);
411+
if (!refExist) {
412+
missingRefs.push(reference);
413+
}
399414
}
400415
}
401416

402417
return missingRefs.length
403418
? [
404-
{
405-
tree,
406-
data_type,
407-
missingRefs,
408-
display_name,
409-
ct_uid: this.currentUid,
410-
name: this.currentTitle,
411-
treeStr: tree
412-
.map(({ name }) => name)
413-
.filter((val) => val)
414-
.join(' ➜ '),
415-
},
416-
]
419+
{
420+
tree,
421+
data_type,
422+
missingRefs,
423+
display_name,
424+
ct_uid: this.currentUid,
425+
name: this.currentTitle,
426+
treeStr: tree
427+
.map(({ name }) => name)
428+
.filter((val) => val)
429+
.join(' ➜ '),
430+
},
431+
]
417432
: [];
418433
}
419434

@@ -638,19 +653,30 @@ export default class ContentType {
638653
let fixStatus;
639654
const missingRefs: string[] = [];
640655
const { reference_to, data_type, display_name } = field;
641-
if(!Array.isArray(reference_to)) {
642-
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, data_type, display_name }), { color: 'green' });
643-
}
644-
for (const reference of reference_to ?? []) {
645-
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
646-
if (this.config.skipRefs.includes(reference)) {
647-
continue;
656+
if (!Array.isArray(reference_to)) {
657+
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'error');
658+
this.log($t(auditMsg.CT_REFERENCE_FIELD, { reference_to, display_name }), 'info');
659+
if (!this.config.skipRefs.includes(reference_to)) {
660+
const refExist = find(this.ctSchema, { uid: reference_to });
661+
662+
if (!refExist) {
663+
missingRefs.push(reference_to);
664+
}
648665
}
649666

650-
const refExist = find(this.ctSchema, { uid: reference });
667+
field.reference_to = [reference_to];
668+
} else {
669+
for (const reference of reference_to ?? []) {
670+
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
671+
if (this.config.skipRefs.includes(reference)) {
672+
continue;
673+
}
651674

652-
if (!refExist) {
653-
missingRefs.push(reference);
675+
const refExist = find(this.ctSchema, { uid: reference });
676+
677+
if (!refExist) {
678+
missingRefs.push(reference);
679+
}
654680
}
655681
}
656682

packages/contentstack-audit/src/modules/entries.ts

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export default class Entries {
175175

176176
const localKey = this.locales.map((locale: any) => locale.code);
177177

178-
if(this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) {
178+
if (this.entries[entryUid]?.publish_details && !Array.isArray(this.entries[entryUid].publish_details)) {
179179
this.log($t(auditMsg.ENTRY_PUBLISH_DETAILS_NOT_EXIST, { uid: entryUid }), { color: 'red' });
180180
}
181181

@@ -194,11 +194,23 @@ export default class Entries {
194194
{ color: 'red' },
195195
);
196196
if (!Object.keys(this.missingEnvLocale).includes(entryUid)) {
197-
this.missingEnvLocale[entryUid] = [{ entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code }];
197+
this.missingEnvLocale[entryUid] = [
198+
{
199+
entry_uid: entryUid,
200+
publish_locale: pd.locale,
201+
publish_environment: pd.environment,
202+
ctUid: ctSchema.uid,
203+
ctLocale: code,
204+
},
205+
];
198206
} else {
199-
this.missingEnvLocale[entryUid].push(
200-
{ entry_uid: entryUid, publish_locale: pd.locale, publish_environment: pd.environment, ctUid: ctSchema.uid, ctLocale: code },
201-
);
207+
this.missingEnvLocale[entryUid].push({
208+
entry_uid: entryUid,
209+
publish_locale: pd.locale,
210+
publish_environment: pd.environment,
211+
ctUid: ctSchema.uid,
212+
ctLocale: code,
213+
});
202214
}
203215
return false;
204216
}
@@ -332,7 +344,6 @@ export default class Entries {
332344
field: ContentTypeStruct | GlobalFieldDataType | ModularBlockType | GroupFieldDataType,
333345
entry: EntryFieldType,
334346
) {
335-
336347
if (this.fix) {
337348
entry = this.runFixOnSchema(tree, field.schema as ContentTypeSchemaType[], entry);
338349
}
@@ -631,18 +642,29 @@ export default class Entries {
631642
if (this.fix) return [];
632643

633644
const missingRefs: Record<string, any>[] = [];
634-
const { uid: data_type, display_name } = fieldStructure;
645+
const { uid: data_type, display_name, reference_to } = fieldStructure;
635646

636647
for (const index in field ?? []) {
637-
const reference = field[index];
648+
const reference: any = field[index];
638649
const { uid } = reference;
650+
if (!uid && reference.startsWith('blt')) {
651+
const refExist = find(this.entryMetaData, { uid: reference });
652+
if (!refExist) {
653+
if(Array.isArray(reference_to) && reference_to.length===1) {
654+
missingRefs.push({uid:reference, _content_type_uid: reference_to[0]});
655+
} else {
656+
missingRefs.push(reference);
657+
}
658+
}
659+
}
639660
// NOTE Can skip specific references keys (Ex, system defined keys can be skipped)
640661
// if (this.config.skipRefs.includes(reference)) continue;
662+
else {
663+
const refExist = find(this.entryMetaData, { uid });
641664

642-
const refExist = find(this.entryMetaData, { uid });
643-
644-
if (!refExist) {
645-
missingRefs.push(reference);
665+
if (!refExist) {
666+
missingRefs.push(reference);
667+
}
646668
}
647669
}
648670

@@ -847,7 +869,7 @@ export default class Entries {
847869
* @returns
848870
*/
849871
fixSelectField(tree: Record<string, unknown>[], field: SelectFeildStruct, entry: any) {
850-
if(!this.config.fixSelectField) {
872+
if (!this.config.fixSelectField) {
851873
return entry;
852874
}
853875
const { enum: selectOptions, multiple, min_instance, display_type, display_name, uid } = field;
@@ -1197,16 +1219,29 @@ export default class Entries {
11971219
entry = JSON.parse(stringReference);
11981220
}
11991221
entry = entry
1200-
?.map((reference) => {
1222+
?.map((reference: any) => {
12011223
const { uid } = reference;
1202-
const refExist = find(this.entryMetaData, { uid });
1203-
1204-
if (!refExist) {
1205-
missingRefs.push(reference);
1206-
return null;
1224+
const { reference_to } = field;
1225+
if (!uid && reference.startsWith('blt')) {
1226+
const refExist = find(this.entryMetaData, { uid: reference });
1227+
if (!refExist) {
1228+
if(Array.isArray(reference_to) && reference_to.length===1) {
1229+
missingRefs.push({uid:reference, _content_type_uid: reference_to[0]});
1230+
} else {
1231+
missingRefs.push(reference);
1232+
}
1233+
} else {
1234+
return { uid: reference, _content_type_uid: refExist.ctUid };
1235+
}
1236+
} else {
1237+
const refExist = find(this.entryMetaData, { uid });
1238+
if (!refExist) {
1239+
missingRefs.push(reference);
1240+
return null;
1241+
} else {
1242+
return reference;
1243+
}
12071244
}
1208-
1209-
return reference;
12101245
})
12111246
.filter((val) => val) as EntryReferenceFieldDataType[];
12121247

@@ -1364,7 +1399,7 @@ export default class Entries {
13641399
`error`,
13651400
);
13661401
}
1367-
this.entryMetaData.push({ uid: entryUid, title });
1402+
this.entryMetaData.push({ uid: entryUid, title, ctUid:uid });
13681403
}
13691404
}
13701405
}

0 commit comments

Comments
 (0)