Skip to content

Commit 2eeb9cb

Browse files
authored
Merge pull request #907 from contentstack/pre-stage
stage
2 parents b97b545 + 98ecace commit 2eeb9cb

27 files changed

+9799
-7976
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2025 Contentstack
3+
Copyright (c) 2026 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

api/package-lock.json

Lines changed: 1120 additions & 1812 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,27 @@
2525
},
2626
"homepage": "https://github.com/contentstack/migration-v2.git#readme",
2727
"dependencies": {
28-
"@contentstack/cli": "^1.51.1",
29-
"@contentstack/cli-utilities": "^1.14.2",
28+
"@contentstack/cli": "^1.53.1",
29+
"@contentstack/cli-utilities": "^1.15.0",
3030
"@contentstack/json-rte-serializer": "^3.0.4",
31-
"@contentstack/marketplace-sdk": "^1.4.0",
31+
"@contentstack/marketplace-sdk": "^1.4.1",
3232
"axios": "^1.12.0",
3333
"chokidar": "^3.6.0",
3434
"cors": "^2.8.5",
3535
"dayjs": "^1.11.18",
3636
"dotenv": "^16.3.1",
37-
"express": "^4.21.0",
38-
"express-validator": "^7.3.0",
37+
"express": "^4.22.0",
38+
"express-validator": "^7.3.1",
3939
"express-winston": "^4.2.0",
4040
"fs-extra": "^11.2.0",
4141
"fs-readdir-recursive": "^1.1.0",
4242
"helmet": "^8.0.0",
4343
"html-to-json-parser": "^2.0.1",
4444
"http": "^0.0.1-security",
45+
"js-yaml": "^4.1.1",
4546
"jsdom": "^24.1.0",
4647
"jsonpath": "^1.1.1",
47-
"jsonwebtoken": "^9.0.2",
48+
"jsonwebtoken": "^9.0.3",
4849
"lowdb": "^7.0.1",
4950
"mkdirp": "^3.0.1",
5051
"p-limit": "^6.2.0",

api/src/services/contentMapper.service.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,26 @@ const putTestData = async (req: Request) => {
6060
if (item?.advanced) {
6161
item.advanced.initial = structuredClone(item?.advanced);
6262
}
63+
if(item?.refrenceTo) {
64+
item.initialRefrenceTo = item?.refrenceTo;
65+
}
6366
});
6467
});
6568

6669

6770

71+
const sanitizeObject = (obj: Record<string, any>) => {
72+
const blockedKeys = ['__proto__', 'prototype', 'constructor'];
73+
const safeObj: Record<string, any> = {};
74+
75+
for (const key in obj) {
76+
if (!blockedKeys.includes(key)) {
77+
safeObj[key] = obj[key];
78+
}
79+
}
80+
return safeObj;
81+
};
82+
6883
/*
6984
this code snippet iterates over an array of contentTypes and performs
7085
some operations on each element.
@@ -75,18 +90,38 @@ const putTestData = async (req: Request) => {
7590
Finally, it updates the fieldMapping property of each type in the contentTypes array with the fieldIds array.
7691
*/
7792
await FieldMapperModel.read();
78-
contentTypes.map((type: any, index: any) => {
93+
contentTypes.forEach((type: any, index: number) => {
7994
const fieldIds: string[] = [];
80-
const fields = Array?.isArray?.(type?.fieldMapping) ? type?.fieldMapping?.filter((field: any) => field)?.map?.((field: any) => {
81-
const id = field?.id ? field?.id?.replace(/[{}]/g, "")?.toLowerCase() : uuidv4();
82-
field.id = id;
83-
fieldIds.push(id);
84-
return { id, projectId, contentTypeId: type?.id, isDeleted: false, ...field };
85-
}) : [];
86-
95+
96+
const fields = Array.isArray(type?.fieldMapping) ?
97+
type.fieldMapping
98+
.filter(Boolean)
99+
.map((field: any) => {
100+
const safeField = sanitizeObject(field);
101+
102+
const id =
103+
safeField?.id ?
104+
safeField.id.replace(/[{}]/g, '').toLowerCase()
105+
: uuidv4();
106+
107+
fieldIds.push(id);
108+
109+
return {
110+
...safeField,
111+
id,
112+
projectId,
113+
contentTypeId: type?.id,
114+
isDeleted: false,
115+
};
116+
})
117+
: [];
118+
87119
FieldMapperModel.update((data: any) => {
88-
data.field_mapper = [...(data?.field_mapper ?? []), ...(fields ?? [])];
89-
});
120+
data.field_mapper = [
121+
...(Array.isArray(data?.field_mapper) ? data.field_mapper : []),
122+
...fields,
123+
];
124+
});
90125
if (
91126
Array?.isArray?.(contentType) &&
92127
Number?.isInteger?.(index) &&
@@ -277,8 +312,7 @@ const getFieldMapping = async (req: Request) => {
277312

278313
const fieldMapping: any = fieldData?.map((field: any) => {
279314
if (field?.advanced?.initial) {
280-
const { initial, ...restAdvanced } = field?.advanced;
281-
return { ...field, advanced: restAdvanced };
315+
return { ...field, advanced: field?.advanced };
282316
}
283317
return field;
284318
});
@@ -775,7 +809,6 @@ const resetToInitialMapping = async (req: Request) => {
775809
);
776810
if (fieldIndex > -1) {
777811
FieldMapperModel.update((data: any) => {
778-
779812
data.field_mapper[fieldIndex] = {
780813
...field,
781814
contentstackField: field?.otherCmsField,
@@ -784,7 +817,11 @@ const resetToInitialMapping = async (req: Request) => {
784817
advanced: {
785818
...field?.advanced?.initial,
786819
initial: field?.advanced?.initial,
787-
}
820+
},
821+
...(field?.referenceTo && {
822+
referenceTo: field?.initialRefrenceTo
823+
}),
824+
isDeleted: false,
788825
}
789826
});
790827
}

api/src/services/globalField.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ const createGlobalField = async ({
5656
}
5757
}
5858

59+
const safeFileGlobalFields = fileGlobalFields;
60+
61+
const existingUids = new Set(
62+
safeFileGlobalFields?.map?.((gf: { uid: string }) => gf?.uid)
63+
);
64+
5965
const mergedGlobalFields = [
60-
...globalfields,
61-
...(fileGlobalFields?.filter(
62-
(fileField: { uid: string }) =>
63-
!globalfields?.some((gf: { uid: string }) => gf?.uid === fileField?.uid)
64-
) || [])
66+
...globalfields.filter(
67+
(fileField: { uid: string }) => !existingUids?.has(fileField?.uid)
68+
),
69+
...safeFileGlobalFields,
6570
];
71+
6672
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
6773
await fs.promises.writeFile(filePath, JSON.stringify(mergedGlobalFields, null, 2));
6874

0 commit comments

Comments
 (0)