Skip to content

Commit

Permalink
Merge pull request #1409 from contentstack/fix/DX-591
Browse files Browse the repository at this point in the history
fix: issue with import entries arising from compact library
  • Loading branch information
aman19K authored May 15, 2024
2 parents 4b9fd62 + ed729dc commit 5d6e248
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions packages/contentstack-import/src/utils/asset-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,24 +375,24 @@ function findFileUrls(schema: any, _entry: any, assetUrls: any) {
}

function updateFileFields(
objekt: any,
object: any,
parent: any,
pos: any,
mappedAssetUids: any,
matchedUids: any,
unmatchedUids: any,
mappedAssetUrls?: any,
) {
if (_.isPlainObject(objekt) && _.has(objekt, 'filename') && _.has(objekt, 'uid')) {
if (_.isPlainObject(object) && _.has(object, 'filename') && _.has(object, 'uid')) {
if (typeof pos !== 'undefined') {
if (typeof pos === 'number' || typeof pos === 'string') {
const replacer = () => {
if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
parent[pos] = mappedAssetUids[objekt.uid];
matchedUids.push(objekt.uid);
if (mappedAssetUids.hasOwnProperty(object.uid)) {
parent[pos] = mappedAssetUids[object.uid];
matchedUids.push(object.uid);
} else {
parent[pos] = '';
unmatchedUids.push(objekt.uid);
unmatchedUids.push(object.uid);
}
};

Expand All @@ -401,7 +401,7 @@ function updateFileFields(
}

if (
objekt &&
object &&
_.isObject(parent[pos]) &&
parent[pos].uid &&
parent[pos].url &&
Expand All @@ -417,23 +417,24 @@ function updateFileFields(
parent = _.omit(parent, ['asset']);
}

if (objekt.uid && mappedAssetUids && mappedAssetUids[objekt.uid]) {
objekt.uid = mappedAssetUids[objekt.uid];
if (object.uid && mappedAssetUids && mappedAssetUids[object.uid]) {
object.uid = mappedAssetUids[object.uid];
}
if (objekt.url && mappedAssetUrls && mappedAssetUrls[objekt.url]) {
objekt.url = mappedAssetUrls[objekt.url];
if (object.url && mappedAssetUrls && mappedAssetUrls[object.url]) {
object.url = mappedAssetUrls[object.url];
}
} else {
replacer();
}
}
}
} else if (_.isPlainObject(objekt)) {
for (let key in objekt) updateFileFields(objekt[key], objekt, key, mappedAssetUids, matchedUids, unmatchedUids);
} else if (_.isArray(objekt) && objekt.length) {
for (let i = 0; i <= objekt.length; i++)
updateFileFields(objekt[i], objekt, i, mappedAssetUids, matchedUids, unmatchedUids);
} else if (_.isPlainObject(object)) {
for (let key in object) updateFileFields(object[key], object, key, mappedAssetUids, matchedUids, unmatchedUids);
} else if (_.isArray(object) && object.length) {
for (let i = 0; i <= object.length; i++)
updateFileFields(object[i], object, i, mappedAssetUids, matchedUids, unmatchedUids);

parent[pos] = _.compact(objekt);
// No need for _.compact() since you want to keep zero values
parent[pos] = _.filter(object, (value: any) => value !== undefined && value !== null);
}
}

0 comments on commit 5d6e248

Please sign in to comment.