156 lines
4.0 KiB
TypeScript
156 lines
4.0 KiB
TypeScript
import { toProductQuestionView } from './questionMapper';
|
|
import { toProductDocumentView } from './documentMapper';
|
|
import { isObject } from '../../server/utils/object';
|
|
|
|
/**
|
|
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
|
*
|
|
* @param raw: 原始的ProductType数据
|
|
* @returns 转换后的ProductTypeView对象
|
|
*
|
|
* @example
|
|
* const view = toProductTypeView(rawProductType);
|
|
*/
|
|
export function toProductTypeView(raw: ProductType): ProductTypeView {
|
|
const trans = raw.translations?.[0] ?? { name: '' };
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
name: trans.name,
|
|
sort: raw?.sort ?? 999,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将 Directus返回的 Product 数据转换为 ProductListView 视图模型
|
|
*
|
|
* @param raw: 原始的 Product 数据
|
|
* @returns 转换后的 ProductListView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductListView(rawProduct);
|
|
*/
|
|
export function toProductListView(raw: Product): ProductListView {
|
|
const trans = raw.translations?.[0];
|
|
|
|
const type = isObject<ProductType>(raw.product_type)
|
|
? toProductTypeView(raw.product_type)
|
|
: ({
|
|
id: '',
|
|
name: '',
|
|
sort: 999,
|
|
} satisfies ProductTypeView);
|
|
|
|
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
product_type: type,
|
|
name: trans?.name ?? '',
|
|
summary: trans?.summary ?? '',
|
|
cover: cover,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductSpecGroup 数据转换为 ProductSpecGroupView 视图模型
|
|
*
|
|
* @param raw: 原始的 ProductSpecGroup 数据
|
|
* @returns 转换后的 ProductSpecGroupView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductSpecGroupView(rawSpecGroup);
|
|
*/
|
|
export function toProductSpecGroupView(
|
|
raw: ProductSpecGroup
|
|
): ProductSpecGroupView {
|
|
const trans = raw.translations?.[0];
|
|
|
|
const specs = raw.specs ?? [];
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
name: trans?.name ?? '',
|
|
specs: specs
|
|
.filter(isObject<ProductSpec>)
|
|
.map((item) => toProductSpecView(item)),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将 Directus 返回的 ProductSpec 数据转换为 ProductSpecView 视图模型
|
|
*
|
|
* @param raw: 原始的 ProductSpec 数据
|
|
* @returns 转换后的 ProductSpecView 对象
|
|
*
|
|
* @example
|
|
* const view = toProductSpecView(rawSpecGroup);
|
|
*/
|
|
export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
|
const trans = raw.translations?.[0];
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
key: trans?.key ?? '',
|
|
value: trans?.value ?? '',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 将 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 images = (raw.images ?? [])
|
|
.filter(isObject<ProductsProductImage>)
|
|
.map((item) => item.product_images_id)
|
|
.filter(isObject<ProductImage>)
|
|
.map((item) => {
|
|
const image = isObject<DirectusFile>(item.image) ? item.image.id : '';
|
|
return {
|
|
id: item.id.toString(),
|
|
image: image,
|
|
caption: item.translations?.[0]?.caption || '',
|
|
};
|
|
});
|
|
|
|
const type = isObject<ProductType>(raw.product_type)
|
|
? (raw.product_type.translations?.[0]?.name ?? '')
|
|
: '';
|
|
|
|
const specs = (raw.specs ?? [])
|
|
.filter(isObject<ProductSpecGroup>)
|
|
.map((item) => toProductSpecGroupView(item));
|
|
|
|
const faqs = (raw.faqs ?? [])
|
|
.filter(isObject<ProductsQuestion>)
|
|
.map((item) => item.questions_id)
|
|
.filter(isObject<Question>)
|
|
.map((item) => toProductQuestionView(item));
|
|
|
|
const documents = (raw.documents ?? [])
|
|
.filter(isObject<ProductsProductDocument>)
|
|
.map((item) => item.product_documents_id)
|
|
.filter(isObject<ProductDocument>)
|
|
.map((item) => toProductDocumentView(item));
|
|
|
|
return {
|
|
id: raw.id.toString(),
|
|
product_type: type,
|
|
name: trans?.name ?? '',
|
|
summary: trans?.summary ?? '',
|
|
images: images,
|
|
description: trans?.description ?? '',
|
|
specs: specs,
|
|
faqs: faqs,
|
|
documents: documents,
|
|
};
|
|
}
|