Skip to content

Commit dcdc88c

Browse files
Fixed issue for groups and blocks
1 parent 6f41c23 commit dcdc88c

File tree

1 file changed

+117
-11
lines changed

1 file changed

+117
-11
lines changed

packages/contentstack-bulk-publish/src/producer/add-fields.js

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,87 @@ function removeUnwanted(entry, unwantedkeys) {
6262
return entry;
6363
}
6464

65+
function isLinkObject(obj, keyName) {
66+
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
67+
return false;
68+
}
69+
70+
const linkKeyNames = ['link', 'card_link'];
71+
if (linkKeyNames.includes(keyName)) {
72+
return true;
73+
}
74+
75+
const hasTitle = 'title' in obj && obj.title !== undefined;
76+
const hasUrl = 'url' in obj && obj.url !== undefined;
77+
const hasHref = 'href' in obj && obj.href !== undefined;
78+
79+
return hasTitle && (hasUrl || hasHref);
80+
}
81+
82+
function ensureHrefIsString(linkObj) {
83+
if (linkObj.href === undefined || linkObj.href === null) {
84+
linkObj.href = '';
85+
} else if (typeof linkObj.href !== 'string') {
86+
linkObj.href = String(linkObj.href);
87+
}
88+
}
89+
90+
function isValidJsonRte(obj) {
91+
return obj !== null &&
92+
typeof obj === 'object' &&
93+
!Array.isArray(obj) &&
94+
typeof obj.type === 'string' &&
95+
obj.type !== '';
96+
}
97+
98+
function cleanJsonFields(obj) {
99+
if (obj === null || obj === undefined || typeof obj !== 'object') {
100+
return obj;
101+
}
102+
103+
if (Array.isArray(obj)) {
104+
return obj.map((item) => cleanJsonFields(item));
105+
}
106+
107+
const cleaned = {};
108+
for (const key in obj) {
109+
if (!obj.hasOwnProperty(key)) {
110+
continue;
111+
}
112+
let value = obj[key];
113+
const isJsonField = key.endsWith('_rte') || key === 'json_rte';
114+
const isAccessibilityField = key.endsWith('_accessibility') || key === 'image_preset_accessibility';
115+
116+
if (isJsonField) {
117+
if (value === '' || value === null || value === undefined) {
118+
continue;
119+
}
120+
if (typeof value === 'object' && !Array.isArray(value)) {
121+
const keyCount = Object.keys(value).length;
122+
if (keyCount === 0) {
123+
continue;
124+
}
125+
if (!isValidJsonRte(value)) {
126+
continue;
127+
}
128+
cleaned[key] = value;
129+
} else {
130+
continue;
131+
}
132+
} else if (isAccessibilityField && value === '') {
133+
cleaned[key] = {};
134+
} else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
135+
value = cleanJsonFields(value);
136+
if (value !== null && typeof value === 'object') {
137+
cleaned[key] = value;
138+
}
139+
} else {
140+
cleaned[key] = value;
141+
}
142+
}
143+
return cleaned;
144+
}
145+
65146
function convertUrlToHref(obj) {
66147
if (obj === null || obj === undefined) {
67148
return obj;
@@ -74,15 +155,23 @@ function convertUrlToHref(obj) {
74155
if (typeof obj === 'object') {
75156
const converted = {};
76157
for (const key in obj) {
77-
if (key === 'link' && typeof obj[key] === 'object' && obj[key] !== null) {
78-
// Convert url to href in link objects
79-
converted[key] = { ...obj[key] };
158+
const value = obj[key];
159+
160+
if (isLinkObject(value, key)) {
161+
converted[key] = { ...value };
80162
if (converted[key].url !== undefined && converted[key].href === undefined) {
81-
converted[key].href = converted[key].url;
163+
if (typeof converted[key].url === 'string') {
164+
converted[key].href = converted[key].url;
165+
} else if (converted[key].url === null || converted[key].url === undefined) {
166+
converted[key].href = '';
167+
} else {
168+
converted[key].href = String(converted[key].url);
169+
}
82170
delete converted[key].url;
83171
}
172+
ensureHrefIsString(converted[key]);
84173
} else {
85-
converted[key] = convertUrlToHref(obj[key]);
174+
converted[key] = convertUrlToHref(value);
86175
}
87176
}
88177
return converted;
@@ -135,6 +224,11 @@ function addFields(contentType, entry) {
135224
}
136225
} else if (schema.enum) {
137226
entry[schema.uid] = null;
227+
} else if (schema.data_type === 'json') {
228+
const isJsonRteField = schema.uid && (schema.uid.endsWith('_rte') || schema.uid === 'json_rte');
229+
if (!isJsonRteField) {
230+
entry[schema.uid] = {};
231+
}
138232
} else if (Object.prototype.hasOwnProperty.call(defaults, schema.data_type)) {
139233
entry[schema.uid] = defaults[schema.data_type];
140234
} else {
@@ -155,15 +249,20 @@ function addFields(contentType, entry) {
155249

156250
if (schema.data_type === 'group' && !schema.multiple) {
157251
addFields(schema.schema, entry[schema.uid]);
252+
if (entry[schema.uid]) {
253+
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
254+
}
158255
}
159256
if (schema.data_type === 'group' && schema.multiple) {
160257
entry[schema.uid].forEach((field) => {
161258
addFields(schema.schema, field);
162259
});
260+
if (entry[schema.uid]) {
261+
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
262+
}
163263
}
164264
if (schema.data_type === 'global_field' && !schema.multiple) {
165265
addFields(schema.schema, entry[schema.uid]);
166-
// Convert url to href in link fields within global fields
167266
if (entry[schema.uid]) {
168267
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
169268
}
@@ -172,7 +271,6 @@ function addFields(contentType, entry) {
172271
entry[schema.uid].forEach((field) => {
173272
addFields(schema.schema, field);
174273
});
175-
// Convert url to href in link fields within global fields
176274
if (entry[schema.uid]) {
177275
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
178276
}
@@ -193,6 +291,9 @@ function addFields(contentType, entry) {
193291
if (filterBlockFields.length > 0) {
194292
filterBlockFields.forEach((bfield) => {
195293
addFields(block.schema, bfield[block.uid]);
294+
if (bfield[block.uid]) {
295+
bfield[block.uid] = convertUrlToHref(bfield[block.uid]);
296+
}
196297
});
197298
} else {
198299
entry[schema.uid].push({ [block.uid]: {} });
@@ -206,6 +307,9 @@ function addFields(contentType, entry) {
206307
if (filterBlockFields.length > 0) {
207308
filterBlockFields.forEach((bfield) => {
208309
addFields(block.schema, bfield[block.uid]);
310+
if (bfield[block.uid]) {
311+
bfield[block.uid] = convertUrlToHref(bfield[block.uid]);
312+
}
209313
});
210314
}
211315
}
@@ -258,9 +362,13 @@ async function getEntries(
258362
for (let index = 0; index < entriesResponse.items.length; index++) {
259363
let updatedEntry = addFields(schema, entries[index]);
260364
if (updatedEntry.changedFlag || forceUpdate) {
261-
updatedEntry = removeUnwanted(entries[index], deleteFields);
365+
let entryData = JSON.parse(JSON.stringify(updatedEntry.entry));
366+
entryData = removeUnwanted(entryData, deleteFields);
367+
entryData = cleanJsonFields(entryData);
368+
entryData = convertUrlToHref(entryData);
369+
entryData = cleanJsonFields(entryData);
262370
const entry = stack.contentType(contentType).entry(entries[index].uid);
263-
Object.assign(entry, updatedEntry);
371+
Object.assign(entry, entryData);
264372
const flag = await updateEntry(entry, locale);
265373
if (flag) {
266374
if (bulkPublish) {
@@ -392,8 +500,6 @@ async function start(
392500
}
393501
}
394502

395-
// start()
396-
397503
module.exports = {
398504
start,
399505
getContentTypeSchema,

0 commit comments

Comments
 (0)