62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
export const TASK_DETAIL_PREVIEW_TYPES = {
|
|
main: 'main-preview',
|
|
sku: 'sku-preview',
|
|
detail: 'detail-preview',
|
|
} as const
|
|
|
|
export type TaskDetailEditGroup = keyof typeof TASK_DETAIL_PREVIEW_TYPES
|
|
|
|
export function getTaskDetailEditGroup(assetType: string | undefined): TaskDetailEditGroup | null {
|
|
if (assetType === TASK_DETAIL_PREVIEW_TYPES.main) return 'main'
|
|
if (assetType === TASK_DETAIL_PREVIEW_TYPES.sku) return 'sku'
|
|
if (assetType === TASK_DETAIL_PREVIEW_TYPES.detail) return 'detail'
|
|
return null
|
|
}
|
|
|
|
export class TaskDetailAssetEditError extends Error {
|
|
constructor(message: string, readonly statusCode: 400 | 403 | 404 | 409 = 400) {
|
|
super(message)
|
|
this.name = 'TaskDetailAssetEditError'
|
|
}
|
|
}
|
|
|
|
type GroupableImageEdit =
|
|
| { kind: 'group_images' | 'append'; group: TaskDetailEditGroup }
|
|
| { kind: 'insert'; afterAssetId: string; beforeAssetId: string }
|
|
| { kind: 'delete' | 'rename' | 'replace'; assetId: string }
|
|
|
|
export function groupTaskDetailImageEdits<Edit extends GroupableImageEdit>(
|
|
edits: Edit[],
|
|
assets: Array<{ id: string; assetType: string }>,
|
|
) {
|
|
const groups = new Map<TaskDetailEditGroup, Edit[]>()
|
|
const assetGroups = new Map(assets.map((asset) => [asset.id, getTaskDetailEditGroup(asset.assetType)]))
|
|
for (const edit of edits) {
|
|
let group: TaskDetailEditGroup | null | undefined
|
|
if ('assetId' in edit) {
|
|
group = assetGroups.get(edit.assetId)
|
|
if (!group) {
|
|
throw new TaskDetailAssetEditError('要编辑的图片不存在或不属于该素材', 404)
|
|
}
|
|
} else if (edit.kind === 'insert') {
|
|
if (assetGroups.get(edit.afterAssetId) !== 'detail' || assetGroups.get(edit.beforeAssetId) !== 'detail') {
|
|
throw new TaskDetailAssetEditError('详情图插入位置不存在或不属于该素材', 404)
|
|
}
|
|
group = 'detail'
|
|
} else {
|
|
group = edit.group
|
|
}
|
|
|
|
const current = groups.get(group)
|
|
if (current) {
|
|
if (edit.kind === 'group_images' || current[0].kind === 'group_images') {
|
|
throw new TaskDetailAssetEditError('同一图片分组的整组变更不能与该组的其他变更同时提交')
|
|
}
|
|
current.push(edit)
|
|
} else {
|
|
groups.set(group, [edit])
|
|
}
|
|
}
|
|
return groups
|
|
}
|