Skip to content

Commit

Permalink
Use discriminated union for ItemProcessingResult
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Dec 11, 2024
1 parent 0aeff7a commit a5dcc60
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 44 deletions.
20 changes: 15 additions & 5 deletions lib/core/models/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,18 @@ export interface OriginalManagementError {
};
}

export type ItemProcessingResult<InputItem, OutputItem> = {
readonly inputItem: InputItem;
readonly outputItem: OutputItem | undefined;
readonly error?: unknown;
};
export type ItemProcessingResult<InputItem, OutputItem> =
| {
readonly state: 'valid';
readonly inputItem: InputItem;
readonly outputItem: OutputItem;
}
| {
readonly state: 'error';
readonly inputItem: InputItem;
readonly error: unknown;
}
| {
readonly state: '404';
readonly inputItem: InputItem;
};
14 changes: 7 additions & 7 deletions lib/core/utils/processing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ export async function processItemsAsync<InputItem, OutputItem>(data: {
.then<ItemProcessingResult<InputItem, OutputItem>>((outputItem) => {
if (outputItem === '404') {
return {
inputItem: item,
outputItem: undefined,
error: undefined
state: '404',
inputItem: item
};
}
return {
inputItem: item,
outputItem: outputItem
outputItem: outputItem,
state: 'valid'
};
})
.catch<ItemProcessingResult<InputItem, OutputItem>>((error) => {
return {
state: 'error',
inputItem: item,
outputItem: undefined,
error: error
};
});
Expand All @@ -82,13 +82,13 @@ export async function processItemsAsync<InputItem, OutputItem>(data: {
// Only '<parallelLimit>' promises at a time
const resultItems = await Promise.all(requests);

const failedItemsCount = resultItems.filter((m) => m.error).length;
const failedItemsCount = resultItems.filter((m) => m.state === 'error').length;
const failedText = failedItemsCount ? ` Failed (${chalk.red(failedItemsCount)}) items` : ``;

logSpinner({
type: 'info',
message: `Completed '${chalk.yellow(data.action)}'. Successfully processed (${chalk.green(
resultItems.filter((m) => m.outputItem).length
resultItems.filter((m) => m.state === 'valid').length
)}) items.${failedText}`
});

Expand Down
13 changes: 6 additions & 7 deletions lib/export/context/export-context-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
findRequired,
FlattenedContentType,
is404Error,
isNotUndefined,
ItemStateInSourceEnvironmentById,
LogSpinnerData,
managementClientUtils,
Expand Down Expand Up @@ -274,8 +273,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getContentItemsByIdsAsync = async (itemIds: ReadonlySet<string>): Promise<readonly Readonly<ContentItemModels.ContentItem>[]> => {
Expand Down Expand Up @@ -311,8 +310,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getAssetsByIdsAsync = async (itemIds: ReadonlySet<string>): Promise<readonly Readonly<AssetModels.Asset>[]> => {
Expand Down Expand Up @@ -348,8 +347,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getItemStatesAsync = async (itemIds: ReadonlySet<string>): Promise<readonly ItemStateInSourceEnvironmentById[]> => {
Expand Down
4 changes: 2 additions & 2 deletions lib/export/export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ export function exportManager(config: ExportConfig) {
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

return {
Expand Down
13 changes: 6 additions & 7 deletions lib/import/context/import-context-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
WorkflowStep,
findRequired,
is404Error,
isNotUndefined,
managementClientUtils,
processItemsAsync,
runMapiRequestAsync,
Expand Down Expand Up @@ -187,8 +186,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) {
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getContentItemsByCodenamesAsync = async (
Expand Down Expand Up @@ -226,8 +225,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) {
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getAssetsByCodenamesAsync = async (assetCodenames: ReadonlySet<string>): Promise<readonly AssetModels.Asset[]> => {
Expand Down Expand Up @@ -263,8 +262,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) {
}
})
)
.map((m) => m.outputItem)
.filter(isNotUndefined);
.filter((m) => m.state === 'valid')
.map((m) => m.outputItem);
};

const getVariantState = (languageVariant: Readonly<LanguageVariantModels.ContentItemLanguageVariant>): LanguageVariantStateData => {
Expand Down
32 changes: 16 additions & 16 deletions lib/import/import-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,62 +97,62 @@ export function importManager(config: ImportConfig) {
const getReportResult = (importResult: ImportResult): ReportResult => {
return {
errorsCount:
importResult.editedAssets.filter((m) => m.error).length +
importResult.uploadedAssets.filter((m) => m.error).length +
importResult.contentItems.filter((m) => m.error).length +
importResult.languageVariants.filter((m) => m.error).length,
importResult.editedAssets.filter((m) => m.state === 'error').length +
importResult.uploadedAssets.filter((m) => m.state === 'error').length +
importResult.contentItems.filter((m) => m.state === 'error').length +
importResult.languageVariants.filter((m) => m.state === 'error').length,
assets: {
count: importResult.uploadedAssets.length + importResult.editedAssets.length,
successful: [
...importResult.uploadedAssets.filter((m) => m.outputItem).map((m) => m.inputItem.codename),
...importResult.editedAssets.filter((m) => m.outputItem).map((m) => m.inputItem.migrationAsset.codename)
...importResult.uploadedAssets.filter((m) => m.state === 'valid').map((m) => m.inputItem.codename),
...importResult.editedAssets.filter((m) => m.state === 'valid').map((m) => m.inputItem.migrationAsset.codename)
].map((m) => {
return {
codename: m
};
}),
failed: [
...importResult.uploadedAssets
.filter((m) => m.error)
.filter((m) => m.state === 'error')
.map((m) => {
return {
codename: m.inputItem.codename,
error: extractErrorData(m.error).message
error: extractErrorData(m.state === 'error').message
};
}),
...importResult.editedAssets
.filter((m) => m.error)
.filter((m) => m.state === 'error')
.map((m) => {
return {
codename: m.inputItem.migrationAsset.codename,
error: extractErrorData(m.error).message
error: extractErrorData(m.state === 'error').message
};
})
]
},
contentItems: {
count: importResult.contentItems.length,
successful: importResult.contentItems
.filter((m) => m.outputItem)
.filter((m) => m.state === 'valid')
.map((m) => {
return {
codename: m.inputItem.system.codename
};
}),
failed: importResult.contentItems
.filter((m) => m.error)
.filter((m) => m.state === 'error')
.map((m) => {
return {
codename: m.inputItem.system.codename,
type: m.inputItem.system.type,
error: extractErrorData(m.error).message
error: extractErrorData(m.state === 'error').message
};
})
},
languageVariants: {
count: importResult.languageVariants.length,
successful: importResult.languageVariants
.filter((m) => m.outputItem)
.filter((m) => m.state === 'valid')
.map((m) => {
return {
codename: m.inputItem.system.codename,
Expand All @@ -161,13 +161,13 @@ export function importManager(config: ImportConfig) {
};
}),
failed: importResult.languageVariants
.filter((m) => m.error)
.filter((m) => m.state === 'error')
.map((m) => {
return {
codename: m.inputItem.system.codename,
language: m.inputItem.system.language,
type: m.inputItem.system.type,
error: extractErrorData(m.error).message
error: extractErrorData(m.state === 'error').message
};
})
}
Expand Down

0 comments on commit a5dcc60

Please sign in to comment.