Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: Adds support for archived language variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Jul 14, 2022
1 parent 2465f64 commit 0175776
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/core/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type ItemType =
| 'workflow'
| 'binaryFile';

export type ActionType = ItemType | 'publish' | 'changeWorkflowStep';
export type ActionType = ItemType | 'archive' | 'publish' | 'changeWorkflowStep';

export type ValidImportModel =
| ContentTypeModels.ContentType
Expand Down
48 changes: 47 additions & 1 deletion lib/import/import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class ImportService {
sourceData.importData.languageVariants,
sourceData.importData.workflows
);
}
}

if (this.config.workflowIdForImportedItems) {
await this.moveLanguageVariantsToCustomWorkflowStepAsync(
Expand Down Expand Up @@ -693,6 +693,7 @@ export class ImportService {
}

const isPublished = this.isLanguageVariantPublished(languageVariant, workflows);
const isArchived = this.isLanguageVariantArchived(languageVariant, workflows);

if (isPublished) {
await this.client
Expand All @@ -705,6 +706,26 @@ export class ImportService {
this.processItem(`${itemCodename} (${languageCodename})`, 'publish', response.data);
})
.catch((error) => this.handleImportError(error));
} else if (isArchived) {
const defaultWorkflow = this.getDefaultWorkflow(workflows);

await this.client
.changeWorkflowOfLanguageVariant()
.byItemCodename(itemCodename)
.byLanguageCodename(languageCodename)
.withData({
step_identifier: {
codename: defaultWorkflow.archived_step.codename
},
workflow_identifier: {
codename: defaultWorkflow.codename
}
})
.toPromise()
.then((response) => {
this.processItem(`${itemCodename} (${languageCodename})`, 'archive', response.data);
})
.catch((error) => this.handleImportError(error));
} else {
const workflowData = this.getWorkflowAndStepOfLanguageVariant(languageVariant, workflows);

Expand Down Expand Up @@ -750,6 +771,19 @@ export class ImportService {
return false;
}

private isLanguageVariantArchived(
languageVariant: LanguageVariantContracts.ILanguageVariantModelContract,
workflows: WorkflowContracts.IWorkflowContract[]
): boolean {
for (const workflow of workflows) {
if (workflow.archived_step.codename === languageVariant.workflow_step.codename) {
return true;
}
}

return false;
}

private getWorkflowAndStepOfLanguageVariant(
languageVariant: LanguageVariantContracts.ILanguageVariantModelContract,
workflows: WorkflowContracts.IWorkflowContract[]
Expand All @@ -773,6 +807,18 @@ export class ImportService {
return undefined;
}

private getDefaultWorkflow(workflows: WorkflowContracts.IWorkflowContract[]): WorkflowContracts.IWorkflowContract {
const defaultWorkflow = workflows.find(
(m) => m.codename.toLowerCase() === defaultWorkflowCodename.toLowerCase()
);

if (!defaultWorkflow) {
throw Error(`Missing default workflow`);
}

return defaultWorkflow;
}

private async moveLanguageVariantsToCustomWorkflowStepAsync(
workflowStepId: string,
languageVariants: LanguageVariantContracts.ILanguageVariantModelContract[]
Expand Down

0 comments on commit 0175776

Please sign in to comment.