Skip to content

Commit

Permalink
Adds transitionTo prop to WorkflowStep, prepares context of languag…
Browse files Browse the repository at this point in the history
…eVariant
  • Loading branch information
Enngage committed Oct 16, 2024
1 parent 28f3ea9 commit 9db5f15
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
59 changes: 53 additions & 6 deletions lib/core/helpers/workflow-helper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { WorkflowModels } from '@kontent-ai/management-sdk';
import { WorkflowContracts, WorkflowModels } from '@kontent-ai/management-sdk';
import chalk from 'chalk';
import { findRequired } from '../utils/array.utils.js';

interface TransitionTo {
readonly codename: string;
readonly id: string;
}

export type WorkflowStep = {
readonly codename: string;
readonly id: string;
readonly transitionsTo: readonly TransitionTo[];
};

type WorkflowMatcher = {
Expand All @@ -17,12 +23,53 @@ type StepMatcher = {
};

export function workflowHelper(workflows: readonly Readonly<WorkflowModels.Workflow>[]) {
const mapContractToWorkflowSteps = (
workflow: Readonly<WorkflowModels.Workflow>,
stepsContract: readonly Readonly<WorkflowContracts.IWorkflowStepNewContract>[]
): readonly WorkflowStep[] => {
const allStepsContracts = [...workflow.steps, workflow.archivedStep, workflow.publishedStep, workflow.scheduledStep];

return stepsContract.map<WorkflowStep>((m) => {
return {
codename: m.codename,
id: m.id,
transitionsTo: m.transitions_to.map((transitionTo) => {
const transitionStep = allStepsContracts.find((step) => step.id === transitionTo.step.id);

if (!transitionStep) {
throw Error(`Could not find transition step with id '${transitionTo.step.id}' in workflow '${workflow.codename}'`);
}

return {
codename: transitionStep.codename,
id: transitionStep.id
};
})
};
});
};

const getWorkflowStep = (workflow: Readonly<WorkflowModels.Workflow>, stepMatcher: StepMatcher): WorkflowStep => {
return findRequired(
[...workflow.steps, workflow.archivedStep, workflow.publishedStep, workflow.scheduledStep],
stepMatcher.match,
stepMatcher.errorMessage
);
const workflowSteps: readonly WorkflowStep[] = [
...mapContractToWorkflowSteps(workflow, workflow.steps),
{
codename: workflow.archivedStep.codename,
id: workflow.archivedStep.id,
transitionsTo: []
},
{
codename: workflow.publishedStep.codename,
id: workflow.publishedStep.id,
transitionsTo: []
},
{
codename: workflow.scheduledStep.codename,
id: workflow.scheduledStep.id,
transitionsTo: []
}
];

return findRequired(workflowSteps, stepMatcher.match, stepMatcher.errorMessage);
};

const getWorkflow = (workflowMatcher: WorkflowMatcher): Readonly<WorkflowModels.Workflow> => {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/run.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import { match } from 'ts-pattern';
import { MapiAction, MapiType } from '../models/core.models.js';
import { LogMessage, LogSpinnerData, Logger } from '../models/log.models.js';
import { match } from 'ts-pattern';

export async function runMapiRequestAsync<TResult>(data: {
readonly logger: Logger;
Expand Down
3 changes: 2 additions & 1 deletion lib/import/importers/language-variant-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export function languageVariantImporter(config: {
migrationItem: data.migrationItem,
workflowCodename: workflow.codename,
stepCodename: step.codename,
migrationItemVersion: data.migrationItemVersion
migrationItemVersion: data.migrationItemVersion,
languageVariant: languageVariant
});

// set scheduling
Expand Down
12 changes: 7 additions & 5 deletions lib/import/importers/workflow-importer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ManagementClient, SharedModels, WorkflowModels } from '@kontent-ai/management-sdk';
import { LanguageVariantModels, ManagementClient, SharedModels, WorkflowModels } from '@kontent-ai/management-sdk';
import { match } from 'ts-pattern';
import {
Logger,
runMapiRequestAsync,
LogSpinnerData,
MigrationItem,
workflowHelper as workflowHelperInit,
MigrationItemVersion
MigrationItemVersion,
runMapiRequestAsync,
workflowHelper as workflowHelperInit
} from '../../core/index.js';
import { match } from 'ts-pattern';

export function workflowImporter(config: {
readonly logger: Logger;
Expand All @@ -19,6 +19,7 @@ export function workflowImporter(config: {
const publishLanguageVariantAsync = async (data: {
readonly logSpinner: LogSpinnerData;
readonly migrationItem: MigrationItem;
readonly languageVariant: Readonly<LanguageVariantModels.ContentItemLanguageVariant>;
}): Promise<void> => {
await runMapiRequestAsync({
logger: config.logger,
Expand Down Expand Up @@ -331,6 +332,7 @@ export function workflowImporter(config: {
readonly stepCodename: string;
readonly migrationItem: MigrationItem;
readonly migrationItemVersion: MigrationItemVersion;
readonly languageVariant: Readonly<LanguageVariantModels.ContentItemLanguageVariant>;
}): Promise<void> => {
return await match(data.stepCodename)
.returnType<Promise<void>>()
Expand Down

0 comments on commit 9db5f15

Please sign in to comment.