Skip to content

Commit 39aa0c6

Browse files
cs-rajnetrajpatel
authored andcommitted
Handled case when the entry for content type is not present in the given language
1 parent 59f1caa commit 39aa0c6

1 file changed

Lines changed: 120 additions & 105 deletions

File tree

  • packages/contentstack-import/src/lib/import

packages/contentstack-import/src/lib/import/entries.js

Lines changed: 120 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ module.exports = class ImportEntries {
103103
}
104104
}
105105
} catch (error) {
106-
addlogs(this.config, `Failed to read the content types to import entries ${util.formatError(error)}`, 'error');
106+
addlogs(
107+
this.config,
108+
`Failed to read the content types to import entries ${util.formatError(error)}`,
109+
'error',
110+
);
107111
process.exit(0);
108112
}
109113
}
@@ -165,7 +169,7 @@ module.exports = class ImportEntries {
165169
addlogs(self.config, "Successfully imported '" + lang + "' entries!", 'success');
166170
counter++;
167171
} else {
168-
addlogs(self.config,`'${lang}' has not been configured for import, thus skipping it`, 'success');
172+
addlogs(self.config, `'${lang}' has not been configured for import, thus skipping it`, 'success');
169173
counter++;
170174
}
171175
},
@@ -191,7 +195,7 @@ module.exports = class ImportEntries {
191195
addlogs(
192196
self.config,
193197
`Failed to update the field rules for content type '${schema.uid}' ${util.formatError(error)}`,
194-
'error'
198+
'error',
195199
);
196200
}
197201
}
@@ -594,7 +598,7 @@ module.exports = class ImportEntries {
594598
addlogs(
595599
this.config,
596600
`Failed to update the entry ${uid} references while reposting ${util.formatError(error)}`,
597-
'error'
601+
'error',
598602
);
599603
}
600604
});
@@ -675,7 +679,7 @@ module.exports = class ImportEntries {
675679
})
676680
.catch((error) => {
677681
// error while executing entry in batch
678-
addlogs(this.config, `Failed re-post entries at batch no: '${(index + 1)}`, 'error');
682+
addlogs(this.config, `Failed re-post entries at batch no: '${index + 1}`, 'error');
679683
addlogs(this.config, util.formatError(error), 'error');
680684
// throw error;
681685
});
@@ -694,11 +698,7 @@ module.exports = class ImportEntries {
694698
})
695699
.catch((error) => {
696700
// error while updating entries with references
697-
addlogs(
698-
this.config,
699-
`Failed re-post entries of content type ${ctUid} locale ${lang}`,
700-
'error',
701-
);
701+
addlogs(this.config, `Failed re-post entries of content type ${ctUid} locale ${lang}`, 'error');
702702
addlogs(this.config, util.formatError(error), 'error');
703703
// throw error;
704704
});
@@ -1034,105 +1034,119 @@ module.exports = class ImportEntries {
10341034
async (ctUid) => {
10351035
let eFilePath = path.resolve(this.ePath, ctUid, lang + '.json');
10361036
let entries = await helper.readLargeFile(eFilePath);
1037-
1038-
let eUids = Object.keys(entries);
1039-
let batches = [];
1040-
let batchSize;
1041-
1042-
if (eUids.length > 0) {
1043-
let entryBatchLimit = this.eConfig.batchLimit || 10;
1044-
batchSize = Math.round(entryBatchLimit / 3);
1045-
// Run entry creation in batches
1046-
for (let i = 0; i < eUids.length; i += batchSize) {
1047-
batches.push(eUids.slice(i, i + batchSize));
1048-
}
1037+
if (entries === undefined) {
1038+
addlogs(
1039+
this.config,
1040+
`There are no entries belonging to Content Type UID' ${ctUid} in language ${lang}`,
1041+
'info',
1042+
);
10491043
} else {
1050-
return;
1051-
}
1052-
1053-
return Promise.map(
1054-
batches,
1055-
async (batch, index) => {
1056-
return Promise.map(
1057-
batch,
1058-
async (eUid) => {
1059-
let entry = entries[eUid];
1060-
let envId = [];
1061-
let locales = [];
1062-
if (entry.publish_details && entry.publish_details.length > 0) {
1063-
_.forEach(entries[eUid].publish_details, (pubObject) => {
1064-
if (
1065-
self.environment.hasOwnProperty(pubObject.environment) &&
1066-
_.indexOf(envId, self.environment[pubObject.environment].name) === -1
1067-
) {
1068-
envId.push(self.environment[pubObject.environment].name);
1069-
}
1070-
if (pubObject.locale) {
1071-
let idx = _.indexOf(locales, pubObject.locale);
1072-
if (idx === -1) {
1073-
locales.push(pubObject.locale);
1044+
let eUids = Object.keys(entries);
1045+
let batches = [];
1046+
let batchSize;
1047+
1048+
if (eUids.length > 0) {
1049+
let entryBatchLimit = this.eConfig.batchLimit || 10;
1050+
batchSize = Math.round(entryBatchLimit / 3);
1051+
// Run entry creation in batches
1052+
for (let i = 0; i < eUids.length; i += batchSize) {
1053+
batches.push(eUids.slice(i, i + batchSize));
1054+
}
1055+
} else {
1056+
return;
1057+
}
1058+
1059+
return Promise.map(
1060+
batches,
1061+
async (batch, index) => {
1062+
return Promise.map(
1063+
batch,
1064+
async (eUid) => {
1065+
let entry = entries[eUid];
1066+
let envId = [];
1067+
let locales = [];
1068+
if (entry.publish_details && entry.publish_details.length > 0) {
1069+
_.forEach(entries[eUid].publish_details, (pubObject) => {
1070+
if (
1071+
self.environment.hasOwnProperty(pubObject.environment) &&
1072+
_.indexOf(envId, self.environment[pubObject.environment].name) === -1
1073+
) {
1074+
envId.push(self.environment[pubObject.environment].name);
10741075
}
1075-
}
1076-
});
1076+
if (pubObject.locale) {
1077+
let idx = _.indexOf(locales, pubObject.locale);
1078+
if (idx === -1) {
1079+
locales.push(pubObject.locale);
1080+
}
1081+
}
1082+
});
10771083

1078-
let entryUid = entryMapper[eUid];
1079-
if (entryUid) {
1080-
requestObject.entry.environments = envId;
1081-
requestObject.entry.locales = locales;
1082-
return new Promise((resolveEntryPublished, rejectEntryPublished) => {
1083-
self.stackAPIClient
1084-
.contentType(ctUid)
1085-
.entry(entryUid)
1086-
.publish({ publishDetails: requestObject.entry, locale: lang })
1087-
// eslint-disable-next-line max-nested-callbacks
1088-
.then((result) => {
1089-
// addlogs(this.config, 'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type', 'success')
1090-
addlogs(
1091-
this.config,
1092-
`Entry '${eUid}' published successfully in '${ctUid}' content type`,
1093-
'success',
1094-
);
1095-
return resolveEntryPublished(result);
1084+
let entryUid = entryMapper[eUid];
1085+
if (entryUid) {
1086+
requestObject.entry.environments = envId;
1087+
requestObject.entry.locales = locales;
1088+
return new Promise((resolveEntryPublished, rejectEntryPublished) => {
1089+
self.stackAPIClient
1090+
.contentType(ctUid)
1091+
.entry(entryUid)
1092+
.publish({ publishDetails: requestObject.entry, locale: lang })
10961093
// eslint-disable-next-line max-nested-callbacks
1097-
})
1098-
.catch((err) => {
1099-
addlogs(
1100-
this.config,
1101-
`failed to publish entry '${eUid}' content type '${ctUid}' ${util.formatError(err)}`,
1102-
'error'
1103-
);
1104-
return resolveEntryPublished('');
1105-
});
1106-
});
1094+
.then((result) => {
1095+
// addlogs(this.config, 'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type', 'success')
1096+
addlogs(
1097+
this.config,
1098+
`Entry '${eUid}' published successfully in '${ctUid}' content type`,
1099+
'success',
1100+
);
1101+
return resolveEntryPublished(result);
1102+
// eslint-disable-next-line max-nested-callbacks
1103+
})
1104+
.catch((err) => {
1105+
addlogs(
1106+
this.config,
1107+
`failed to publish entry '${eUid}' content type '${ctUid}' ${util.formatError(
1108+
err,
1109+
)}`,
1110+
'error',
1111+
);
1112+
return resolveEntryPublished('');
1113+
});
1114+
});
1115+
}
1116+
} else {
1117+
return {};
11071118
}
1108-
} else {
1109-
return {};
1110-
}
1111-
},
1112-
{
1113-
concurrency: 1,
1114-
},
1115-
)
1116-
.then(() => {
1117-
// empty function
1118-
})
1119-
.catch((error) => {
1120-
// error while executing entry in batch
1121-
addlogs(this.config, util.formatError(error), 'error');
1122-
addlogs(this.config, error, 'error');
1123-
});
1124-
},
1125-
{
1126-
concurrency: 1,
1127-
},
1128-
)
1129-
.then(() => {
1130-
// addlogs(this.config, 'Entries published successfully in ' + ctUid + ' content type', 'success')
1131-
addlogs(this.config, `Entries published successfully in '${ctUid}' content type`, 'info');
1132-
})
1133-
.catch((error) => {
1134-
addlogs(this.config, `failed to publish entry in content type '${ctUid}' ${util.formatError(error)}`, 'error');
1135-
});
1119+
},
1120+
{
1121+
concurrency: 1,
1122+
},
1123+
)
1124+
.then(() => {
1125+
// empty function
1126+
})
1127+
.catch((error) => {
1128+
// error while executing entry in batch
1129+
addlogs(this.config, util.formatError(error), 'error');
1130+
addlogs(this.config, error, 'error');
1131+
});
1132+
},
1133+
{
1134+
concurrency: 1,
1135+
},
1136+
)
1137+
.then(() => {
1138+
// addlogs(this.config, 'Entries published successfully in ' + ctUid + ' content type', 'success')
1139+
addlogs(this.config, `Entries published successfully in '${ctUid}' content type`, 'info');
1140+
})
1141+
.catch((error) => {
1142+
console.log(error);
1143+
addlogs(
1144+
this.config,
1145+
`failed to publish entry in content type '${ctUid}' ${util.formatError(error)}`,
1146+
'error',
1147+
);
1148+
});
1149+
}
11361150
},
11371151
{
11381152
concurrency: 1,
@@ -1143,6 +1157,7 @@ module.exports = class ImportEntries {
11431157
// addlogs('Published entries successfully in ' +);
11441158
})
11451159
.catch((error) => {
1160+
console.log(error);
11461161
addlogs(this.config, `Failed to publish few entries in '${lang}' ${util.formatError(error)}`, 'error');
11471162
});
11481163
},
@@ -1154,7 +1169,7 @@ module.exports = class ImportEntries {
11541169
return resolve();
11551170
})
11561171
.catch((error) => {
1157-
addlogs(this.config,`Failed to publish entries. ${util.formatError(error)}`, 'error');
1172+
addlogs(this.config, `Failed to publish entries. ${util.formatError(error)}`, 'error');
11581173
});
11591174
});
11601175
}

0 commit comments

Comments
 (0)