Skip to content

Commit

Permalink
Merge pull request #1099 from contentstack/fix/CS-41904
Browse files Browse the repository at this point in the history
fixed entry overwrite issues
  • Loading branch information
shafeeqd959 authored Oct 11, 2023
2 parents e4090f8 + 32d300f commit b7d4074
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/contentstack-export/src/export/module-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ModuleExporter {
try {
this.exportConfig.branchName = branch.uid;
this.exportConfig.branchDir = path.join(this.exportConfig.exportDir, branch.uid);
log(this.exportConfig, `Exporting content from branch ${branch.uid}`, 'success');
await this.export();
log(this.exportConfig, `The content of branch ${branch.uid} has been exported successfully!`, 'success');
} catch (error) {
Expand Down
55 changes: 33 additions & 22 deletions packages/contentstack-import/src/import/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,24 @@ export default class EntriesImport extends BaseClass {
const onReject = ({ error, apiData: entry, additionalInfo }: any) => {
const { title, uid } = entry;
//Note: write existing entries into files to handler later
if (error?.errors?.title) {
if (this.importConfig.replaceExisting) {
entry.entryOldUid = uid;
entry.sourceEntryFilePath = path.join(basePath, additionalInfo.entryFileName); // stores source file path temporarily
existingEntriesFileHelper.writeIntoFile({ [uid]: entry } as any, { mapKeyVal: true });
}
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Entry '${title}' already exists`, 'info');
if (error.errorCode === 119) {
if (error?.errors?.title || error?.errors?.uid) {
if (this.importConfig.replaceExisting) {
entry.entryOldUid = uid;
entry.sourceEntryFilePath = path.join(basePath, additionalInfo.entryFileName); // stores source file path temporarily
existingEntriesFileHelper.writeIntoFile({ [uid]: entry } as any, { mapKeyVal: true });
}
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Entry '${title}' already exists`, 'info');
}
} else {
log(
this.importConfig,
`${title} entry of content type ${cTUid} in locale ${locale} failed to create`,
'error',
);
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({ content_type: cTUid, locale, entry: { uid, title } });
}
} else {
log(this.importConfig, `${title} entry of content type ${cTUid} in locale ${locale} failed to create`, 'error');
Expand Down Expand Up @@ -518,18 +528,19 @@ export default class EntriesImport extends BaseClass {
}) {
const { additionalInfo: { cTUid, locale } = {} } = apiParams;
return new Promise(async (resolve, reject) => {
const { items: [entryInStack] = [] }: any = await this.stack
.contentType(cTUid)
.entry()
.query({ query: { title: entry.title, locale } })
.findOne()
.catch((error: Error) => {
apiParams.reject({
error,
apiData: entry,
});
reject(true);
});
const { items: [entryInStack] = [] }: any =
(await this.stack
.contentType(cTUid)
.entry()
.query({ query: { title: entry.title, locale } })
.findOne()
.catch((error: Error) => {
apiParams.reject({
error,
apiData: entry,
});
reject(true);
})) || {};
if (entryInStack) {
const entryPayload = this.stack.contentType(cTUid).entry(entryInStack.uid);
Object.assign(entryPayload, entryInStack, cloneDeep(entry), {
Expand All @@ -539,7 +550,7 @@ export default class EntriesImport extends BaseClass {
_version: entryInStack._version,
});
return entryPayload
.update()
.update({ locale })
.then((response: any) => {
apiParams.resolve({
response,
Expand All @@ -556,7 +567,7 @@ export default class EntriesImport extends BaseClass {
});
} else {
apiParams.reject({
error: new Error(`Extension with title ${entry.title} not found in the stack`),
error: new Error(`Entry with title ${entry.title} not found in the stack`),
apiData: entry,
});
reject(true);
Expand Down

0 comments on commit b7d4074

Please sign in to comment.