- 产品状态:从Directus Schema获取Product的Status字段 - 视图字段:添加Status字段用于标示产品状态 - 测试方法:为Status字段添加单元测试
244 lines
6.0 KiB
TypeScript
244 lines
6.0 KiB
TypeScript
import { isObject } from '../../server/utils/object';
|
|
import { toDocumentTypeView } from './documentMapper';
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductImage 数据转换为 ProductImageView 视图模型
|
|
*
|
|
* @param raw: 原始的 ProductsProductImage 数据
|
|
* @returns 转换后的 ProductImageView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductImageView(rawProductImage);
|
|
*/
|
|
export function toProductImageView(
|
|
raw: (ProductsProductImage | string)[]
|
|
): ProductImageView[] {
|
|
return (raw ?? []).map((item) => {
|
|
if (!isObject<ProductsProductImage>(item))
|
|
return {
|
|
id: item,
|
|
image: '',
|
|
caption: '',
|
|
} satisfies ProductImageView;
|
|
|
|
const image = item.product_images_id;
|
|
if (!isObject<ProductImage>(image))
|
|
return {
|
|
id: item.id.toString(),
|
|
image: '',
|
|
caption: '',
|
|
} satisfies ProductImageView;
|
|
|
|
const trans = image.translations?.[0];
|
|
|
|
return {
|
|
id: item.id.toString(),
|
|
image: isObject<DirectusFile>(image.image) ? image.image.id : '',
|
|
caption: trans?.caption || '',
|
|
} satisfies ProductImageView;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductSpecGroup 数据转换为 ProductSpecGroupView 视图模型
|
|
*
|
|
* @param raw: 原始的 ProductSpecGroup 数据
|
|
* @returns 转换后的 ProductSpecGroupView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductSpecGroupView(rawSpecGroup);
|
|
*/
|
|
export function toProductSpecGroupView(
|
|
raw: (ProductSpecGroup | string)[]
|
|
): ProductSpecGroupView[] {
|
|
return (raw ?? []).map((item) => {
|
|
if (!isObject<ProductSpecGroup>(item)) {
|
|
return {
|
|
id: item,
|
|
name: '',
|
|
specs: [],
|
|
} satisfies ProductSpecGroupView;
|
|
}
|
|
const trans = item.translations?.[0];
|
|
|
|
const specs = toProductSpecView(item?.specs ?? []);
|
|
|
|
return {
|
|
id: item.id.toString(),
|
|
name: trans?.name ?? '',
|
|
specs: specs,
|
|
} satisfies ProductSpecGroupView;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductSpec 数据转换为 ProductSpecView 视图模型
|
|
*
|
|
* @param raw: 原始的 ProductSpec 数据
|
|
* @returns 转换后的 ProductSpecView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductSpecView(rawSpecGroup);
|
|
*/
|
|
export function toProductSpecView(
|
|
raw: (ProductSpec | string)[]
|
|
): ProductSpecView[] {
|
|
return (raw ?? []).map((item) => {
|
|
if (!isObject<ProductSpec>(item)) {
|
|
return {
|
|
id: item,
|
|
key: '',
|
|
value: '',
|
|
} satisfies ProductSpecView;
|
|
}
|
|
const trans = item.translations?.[0];
|
|
|
|
return {
|
|
id: item.id.toString(),
|
|
key: trans?.key ?? '',
|
|
value: trans?.value ?? '',
|
|
} satisfies ProductSpecView;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductQuestion 数据转换为 ProductQuestionView 视图模型
|
|
*
|
|
* @param raw: 原始的ProductQuestion 数据
|
|
* @returns 转换后的 ProductQuestionView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductQuestionView(rawQuestion);
|
|
*/
|
|
export function toProductQuestionView(
|
|
raw: (ProductsQuestion | string)[]
|
|
): ProductQuestionView[] {
|
|
return (raw ?? []).map((item) => {
|
|
if (!isObject<ProductsQuestion>(item)) {
|
|
return {
|
|
id: item,
|
|
title: '',
|
|
content: '',
|
|
} satisfies ProductQuestionView;
|
|
}
|
|
|
|
const question = item.questions_id;
|
|
if (!isObject<Question>(question)) {
|
|
return {
|
|
id: item.id.toString(),
|
|
title: '',
|
|
content: '',
|
|
} satisfies ProductQuestionView;
|
|
}
|
|
|
|
const trans = question.translations?.[0];
|
|
|
|
return {
|
|
id: item.id.toString(),
|
|
title: trans?.title ?? '',
|
|
content: trans?.content ?? '',
|
|
} satisfies ProductQuestionView;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductDocument 数据转换为 ProductDocumentView 视图模型
|
|
*
|
|
* @param raw: 原始的ProductDocument 数据
|
|
* @returns 转换后的 ProductDocumentView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductDocumentView(rawDocument);
|
|
*/
|
|
export function toProductDocumentView(
|
|
raw: (ProductsProductDocument | string)[]
|
|
): ProductDocumentView[] {
|
|
return (raw ?? []).map((item) => {
|
|
if (!isObject<ProductsProductDocument>(item)) {
|
|
return {
|
|
id: item,
|
|
fileId: '',
|
|
filename: '',
|
|
size: 0,
|
|
title: '',
|
|
url: '',
|
|
type: {
|
|
id: '-1',
|
|
name: '',
|
|
},
|
|
} satisfies ProductDocumentView;
|
|
}
|
|
|
|
const document = item.product_documents_id;
|
|
if (!isObject<ProductDocument>(document)) {
|
|
return {
|
|
id: item.id.toString(),
|
|
fileId: '',
|
|
filename: '',
|
|
size: 0,
|
|
title: '',
|
|
url: '',
|
|
type: {
|
|
id: '-1',
|
|
name: '',
|
|
},
|
|
} satisfies ProductDocumentView;
|
|
}
|
|
|
|
const file = isObject<DirectusFile>(document.file)
|
|
? document.file
|
|
: undefined;
|
|
|
|
const url = file ? `/api/assets/${file.id}` : '';
|
|
|
|
const trans = document.translations?.[0];
|
|
|
|
const typeView = toDocumentTypeView(document.type ?? null);
|
|
|
|
return {
|
|
id: item.id.toString(),
|
|
fileId: file?.id ?? '',
|
|
filename: file?.filename_download ?? '',
|
|
size: file?.filesize ?? 0,
|
|
title: trans?.title ?? '',
|
|
url: url,
|
|
type: typeView,
|
|
} satisfies ProductDocumentView;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 Product 数据转换为 ProductView 视图模型
|
|
*
|
|
* @param raw: 原始的 Product 数据
|
|
* @returns 转换后的 ProductView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductView(rawProduct);
|
|
*/
|
|
export function toProductView(raw: Product): ProductView {
|
|
const trans = raw.translations?.[0];
|
|
|
|
const status = raw.status ?? 'discontinued';
|
|
|
|
const images = toProductImageView(raw.images ?? []);
|
|
|
|
const specs = toProductSpecGroupView(raw.specs ?? []);
|
|
|
|
const faqs = toProductQuestionView(raw.faqs ?? []);
|
|
|
|
const documents = toProductDocumentView(raw.documents ?? []);
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
status: status,
|
|
name: trans?.name ?? '',
|
|
summary: trans?.summary ?? '',
|
|
images: images,
|
|
description: trans?.description ?? '',
|
|
specs: specs,
|
|
faqs: faqs,
|
|
documents: documents,
|
|
} satisfies ProductView;
|
|
}
|