refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
34
server/mappers/companyProfileMapper.test.ts
Normal file
34
server/mappers/companyProfileMapper.test.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { expect, test, describe } from 'vitest';
|
||||
import { toCompanyProfileView } from './companyProfileMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: companyProfileMapper
|
||||
*/
|
||||
describe('companyProfileMapper', () => {
|
||||
const baseData: CompanyProfile = {
|
||||
id: 1,
|
||||
translations: [{ id: 10, content: 'This is raw data of company profile' }],
|
||||
};
|
||||
|
||||
test('convert raw data to CompanyProfileView correctly', () => {
|
||||
const rawData: CompanyProfile = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toCompanyProfileView(rawData)).toEqual({
|
||||
id: 1,
|
||||
content: 'This is raw data of company profile',
|
||||
});
|
||||
});
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: CompanyProfile = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toCompanyProfileView(rawData)).toEqual({
|
||||
id: 1,
|
||||
content: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
17
server/mappers/companyProfileMapper.ts
Normal file
17
server/mappers/companyProfileMapper.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 将 Directus 返回的 CompanyProfile 数据转换为 CompanyProfileView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 CompanyProfile 数据
|
||||
* @returns 转换后的 CompanyProfileView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toCompanyProfileView(rawCompanyProfile);
|
||||
*/
|
||||
export function toCompanyProfileView(raw: CompanyProfile): CompanyProfileView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
content: trans?.content ?? '',
|
||||
};
|
||||
}
|
||||
34
server/mappers/contactInfoMapper.test.ts
Normal file
34
server/mappers/contactInfoMapper.test.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toContactInfoView } from './contactInfoMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: contactInfoMapper
|
||||
*/
|
||||
describe('contactInfoMapper', () => {
|
||||
const baseData: ContactInfo = {
|
||||
id: 1,
|
||||
translations: [{ id: 10, content: 'This is raw data of contact info' }],
|
||||
};
|
||||
test('convert raw data to ContactInfoView correctly', () => {
|
||||
const rawData: ContactInfo = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toContactInfoView(rawData)).toEqual({
|
||||
id: 1,
|
||||
content: 'This is raw data of contact info',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: ContactInfo = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toContactInfoView(rawData)).toEqual({
|
||||
id: 1,
|
||||
content: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
17
server/mappers/contactInfoMapper.ts
Normal file
17
server/mappers/contactInfoMapper.ts
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 将 Directus 返回的 ContactInfo 数据转换为 ContactInfoView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 ContactInfo 数据
|
||||
* @returns 转换后的 ContactInfoView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toContactInfoView(rawContactInfo);
|
||||
*/
|
||||
export function toContactInfoView(raw: ContactInfo): ContactInfoView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
content: trans?.content ?? '',
|
||||
};
|
||||
}
|
||||
109
server/mappers/documentMapper.test.ts
Normal file
109
server/mappers/documentMapper.test.ts
Normal file
@ -0,0 +1,109 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toProductDocumentView, toDocumentListView } from './documentMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toProductDocumentView
|
||||
*/
|
||||
describe('toProductDocumentView', () => {
|
||||
const baseData: ProductDocument = {
|
||||
id: 1,
|
||||
file: {
|
||||
id: 'rand-om__-uuid-1234',
|
||||
filename_download: 'document.pdf',
|
||||
filesize: 2048,
|
||||
},
|
||||
translations: [{ id: 10, title: 'Document Title' }],
|
||||
};
|
||||
|
||||
test('convert raw data with fileMeta to ProductDocumentView correctly', () => {
|
||||
const rawData: ProductDocument = { ...baseData };
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: 'document.pdf',
|
||||
title: 'Document Title',
|
||||
url: '/api/assets/rand-om__-uuid-1234',
|
||||
size: 2048,
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with fileId', () => {
|
||||
const rawData: ProductDocument = {
|
||||
...baseData,
|
||||
file: 'rand-om__-uuid-1234',
|
||||
};
|
||||
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: '',
|
||||
filename: '',
|
||||
title: 'Document Title',
|
||||
url: '/api/assets/',
|
||||
size: 0,
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: ProductDocument = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
expect(toProductDocumentView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: 'document.pdf',
|
||||
title: '',
|
||||
url: '/api/assets/rand-om__-uuid-1234',
|
||||
size: 2048,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toDocumentListView
|
||||
*/
|
||||
describe('toDocumentListView', () => {
|
||||
const baseData: ProductDocument = {
|
||||
id: 1,
|
||||
file: {
|
||||
id: 'rand-om__-uuid-1234',
|
||||
filename_download: 'document.pdf',
|
||||
filesize: 2048,
|
||||
},
|
||||
translations: [{ id: 10, title: 'Document Title' }],
|
||||
products: [
|
||||
{
|
||||
id: 10,
|
||||
products_id: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Product A' }],
|
||||
product_type: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Type A' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
test('convert raw data with fileMeta to DocumentListView correctly', () => {
|
||||
const rawData: ProductDocument = { ...baseData };
|
||||
expect(toDocumentListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
fileId: 'rand-om__-uuid-1234',
|
||||
filename: 'document.pdf',
|
||||
title: 'Document Title',
|
||||
url: '/api/assets/rand-om__-uuid-1234',
|
||||
size: 2048,
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product A',
|
||||
type: {
|
||||
id: 1,
|
||||
name: 'Type A',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
76
server/mappers/documentMapper.ts
Normal file
76
server/mappers/documentMapper.ts
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* 将 Directus 返回的 Document 数据转换为 ProductDocumentView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Document 数据
|
||||
* @returns 转换后的 ProductDocumentView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductDocumentView(rawDocument);
|
||||
*/
|
||||
export function toProductDocumentView(
|
||||
raw: ProductDocument
|
||||
): ProductDocumentView {
|
||||
const trans = raw.translations?.[0];
|
||||
const file = isObject<DirectusFile>(raw.file) ? raw.file : undefined;
|
||||
const fileId = file?.id ?? '';
|
||||
|
||||
const url = `/api/assets/${fileId}`;
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
fileId: fileId,
|
||||
filename: file?.filename_download ?? '',
|
||||
title: trans?.title ?? '',
|
||||
url: url,
|
||||
size: file?.filesize ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Document 数据转换为 DocumentListView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Document 数据
|
||||
* @returns 转换后的 DocumentListView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toDocumentListView(rawDocument);
|
||||
*/
|
||||
export function toDocumentListView(raw: ProductDocument): DocumentListView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const file = isObject<DirectusFile>(raw.file) ? raw.file : undefined;
|
||||
const fileId = file?.id ?? '';
|
||||
|
||||
const url = `/api/assets/${fileId}`;
|
||||
|
||||
const related_products: DocumentListProduct[] = (raw.products ?? [])
|
||||
.filter(isObject<ProductsProductDocument>)
|
||||
.map((item) => item.products_id)
|
||||
.filter(isObject<Product>)
|
||||
.map((item) => {
|
||||
const productType = isObject<ProductType>(item.product_type)
|
||||
? ({
|
||||
id: item.product_type.id,
|
||||
name: item.product_type.translations?.[0]?.name ?? '',
|
||||
} satisfies DocumentListProductType)
|
||||
: ({
|
||||
id: -1,
|
||||
name: '',
|
||||
} satisfies DocumentListProductType);
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.translations?.[0]?.name ?? '',
|
||||
type: productType,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
fileId: fileId,
|
||||
filename: file?.filename_download ?? '',
|
||||
title: trans?.title ?? '',
|
||||
url: url,
|
||||
size: file?.filesize ?? 0,
|
||||
products: related_products,
|
||||
};
|
||||
}
|
||||
95
server/mappers/homepageMapper.test.ts
Normal file
95
server/mappers/homepageMapper.test.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toHomepageView } from './homepageMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toHomepageView
|
||||
*/
|
||||
describe('toHomepageView', () => {
|
||||
test('convert raw data to HomepageView correctly', () => {
|
||||
const rawData: Homepage = {
|
||||
id: 1,
|
||||
carousel: [
|
||||
{
|
||||
id: 1,
|
||||
directus_files_id: {
|
||||
id: 'file-uuid-1',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
directus_files_id: {
|
||||
id: 'file-uuid-2',
|
||||
},
|
||||
},
|
||||
],
|
||||
recommend_products: [
|
||||
{
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Product 1', summary: 'Summary 1' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
translations: [{ id: 2, name: 'Product 2', summary: 'Summary 2' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-2',
|
||||
},
|
||||
},
|
||||
],
|
||||
recommend_solutions: [
|
||||
{
|
||||
id: 1,
|
||||
translations: [{ id: 1, title: 'Solution 1', summary: 'Summary 1' }],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(toHomepageView(rawData)).toEqual({
|
||||
id: 1,
|
||||
carousel: ['file-uuid-1', 'file-uuid-2'],
|
||||
recommendProducts: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product 1',
|
||||
summary: 'Summary 1',
|
||||
cover: 'cover-file-uuid-1',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Product 2',
|
||||
summary: 'Summary 2',
|
||||
cover: 'cover-file-uuid-2',
|
||||
},
|
||||
],
|
||||
recommendSolutions: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Solution 1',
|
||||
summary: 'Summary 1',
|
||||
cover: 'cover-file-uuid-1',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Homepage = {
|
||||
id: 1,
|
||||
carousel: [],
|
||||
recommend_products: [{ id: 1, translations: [], cover: null }],
|
||||
recommend_solutions: [{ id: 1, translations: [], cover: null }],
|
||||
};
|
||||
|
||||
expect(toHomepageView(rawData)).toEqual({
|
||||
id: 1,
|
||||
carousel: [],
|
||||
recommendProducts: [{ id: 1, name: '', summary: '', cover: '' }],
|
||||
recommendSolutions: [{ id: 1, title: '', summary: '', cover: '' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
54
server/mappers/homepageMapper.ts
Normal file
54
server/mappers/homepageMapper.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { isObject } from '../utils/object';
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Homepage 数据转换为 HomepageView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Homepage 数据
|
||||
* @returns 转换后的 HomepageView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toHomepageView(rawHomepage);
|
||||
*/
|
||||
export function toHomepageView(raw: Homepage): HomepageView {
|
||||
const carousel = (raw.carousel ?? [])
|
||||
.filter(isObject<HomepageFile>)
|
||||
.map((item) => item.directus_files_id)
|
||||
.filter(isObject<DirectusFile>)
|
||||
.map((file) => file.id);
|
||||
|
||||
const products = (raw.recommend_products ?? [])
|
||||
.filter(isObject<Product>)
|
||||
.map((item) => {
|
||||
const trans = item.translations?.[0];
|
||||
|
||||
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
} satisfies HomepageProductView;
|
||||
});
|
||||
|
||||
const solutions = (raw.recommend_solutions ?? [])
|
||||
.filter(isObject<Solution>)
|
||||
.map((item) => {
|
||||
const trans = item.translations?.[0];
|
||||
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
title: trans?.title ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
cover: cover,
|
||||
} satisfies HomepageSolutionView;
|
||||
});
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
carousel: carousel ?? [],
|
||||
recommendProducts: products ?? [],
|
||||
recommendSolutions: solutions ?? [],
|
||||
};
|
||||
}
|
||||
188
server/mappers/productMapper.test.ts
Normal file
188
server/mappers/productMapper.test.ts
Normal file
@ -0,0 +1,188 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import {
|
||||
toProductListView,
|
||||
toProductSpecView,
|
||||
toProductSpecGroupView,
|
||||
toProductView,
|
||||
} from './productMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toProductListView
|
||||
*/
|
||||
describe('toProductListView', () => {
|
||||
test('convert raw data to ProductListView correctly', () => {
|
||||
const rawData: Product = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{ id: 10, name: 'Product Name', summary: 'Product Summary' },
|
||||
],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1234',
|
||||
},
|
||||
product_type: {
|
||||
id: 1,
|
||||
translations: [{ id: 20, name: 'Type Name' }],
|
||||
sort: 1,
|
||||
},
|
||||
};
|
||||
|
||||
expect(toProductListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: 'Product Name',
|
||||
summary: 'Product Summary',
|
||||
cover: 'cover-file-uuid-1234',
|
||||
product_type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
sort: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Product = {
|
||||
id: 1,
|
||||
translations: [],
|
||||
cover: {
|
||||
id: 'cover-file-uuid-1234',
|
||||
},
|
||||
product_type: {
|
||||
id: 20,
|
||||
translations: [],
|
||||
sort: 1,
|
||||
},
|
||||
};
|
||||
|
||||
expect(toProductListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: '',
|
||||
summary: '',
|
||||
cover: 'cover-file-uuid-1234',
|
||||
product_type: {
|
||||
id: 20,
|
||||
name: '',
|
||||
sort: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toProductSpecView
|
||||
*/
|
||||
describe('toProductSpecView', () => {
|
||||
test('convert raw data to ProductSpecView correctly', () => {
|
||||
const rawData: ProductSpec = {
|
||||
id: 1,
|
||||
translations: [{ id: 1, key: 'Key', value: 'Value' }],
|
||||
};
|
||||
|
||||
expect(toProductSpecView(rawData)).toEqual({
|
||||
id: 1,
|
||||
key: 'Key',
|
||||
value: 'Value',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: ProductSpec = {
|
||||
id: 1,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toProductSpecView(rawData)).toEqual({
|
||||
id: 1,
|
||||
key: '',
|
||||
value: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toProductSpecGroupView
|
||||
*/
|
||||
describe('toProductSpecGroupView', () => {
|
||||
test('convert raw data to ProductSpecGroupView correctly', () => {
|
||||
const rawData: ProductSpecGroup = {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Group Name' }],
|
||||
specs: [
|
||||
{ id: 1, translations: [{ id: 1, key: 'Key1', value: 'Value1' }] },
|
||||
{ id: 2, translations: [{ id: 2, key: 'Key2', value: 'Value2' }] },
|
||||
],
|
||||
};
|
||||
|
||||
expect(toProductSpecGroupView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: 'Group Name',
|
||||
specs: [
|
||||
{ id: 1, key: 'Key1', value: 'Value1' },
|
||||
{ id: 2, key: 'Key2', value: 'Value2' },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: ProductSpecGroup = {
|
||||
id: 1,
|
||||
translations: [],
|
||||
specs: [],
|
||||
};
|
||||
|
||||
expect(toProductSpecGroupView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: '',
|
||||
specs: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toProductView
|
||||
*/
|
||||
describe('toProductView', () => {
|
||||
test('convert raw data to ProductView correctly', () => {
|
||||
const rawData: Product = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product Name',
|
||||
summary: 'Product Summary',
|
||||
description: 'Product Description',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(toProductView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: 'Product Name',
|
||||
summary: 'Product Summary',
|
||||
description: 'Product Description',
|
||||
product_type: '',
|
||||
images: [],
|
||||
documents: [],
|
||||
faqs: [],
|
||||
specs: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Product = {
|
||||
id: 1,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toProductView(rawData)).toEqual({
|
||||
id: 1,
|
||||
name: '',
|
||||
summary: '',
|
||||
description: '',
|
||||
product_type: '',
|
||||
images: [],
|
||||
documents: [],
|
||||
faqs: [],
|
||||
specs: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
154
server/mappers/productMapper.ts
Normal file
154
server/mappers/productMapper.ts
Normal file
@ -0,0 +1,154 @@
|
||||
import { toProductQuestionView } from './questionMapper';
|
||||
import { toProductDocumentView } from './documentMapper';
|
||||
|
||||
/**
|
||||
* 将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,
|
||||
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: -1,
|
||||
name: '',
|
||||
sort: 999,
|
||||
} satisfies ProductTypeView);
|
||||
|
||||
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
product_type: type,
|
||||
name: trans?.name ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
images: images,
|
||||
description: trans?.description ?? '',
|
||||
specs: specs,
|
||||
faqs: faqs,
|
||||
documents: documents,
|
||||
};
|
||||
}
|
||||
121
server/mappers/questionMapper.test.ts
Normal file
121
server/mappers/questionMapper.test.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { toProductQuestionView, toQuestionListView } from './questionMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toProductQuestionView
|
||||
*/
|
||||
describe('toProductQuestionView', () => {
|
||||
const baseData: Question = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
||||
],
|
||||
};
|
||||
test('convert raw data to ProductQuestionView correctly', () => {
|
||||
const rawData: Question = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toProductQuestionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Question Title',
|
||||
content: 'Question Answer',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Question = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toProductQuestionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
content: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toQuestionListView
|
||||
*/
|
||||
describe('toQuestionListView', () => {
|
||||
const baseData: Question = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
||||
],
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
products_id: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Product Name' }],
|
||||
product_type: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Type Name' }],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
test('convert raw data to QuestionListView correctly', () => {
|
||||
const rawData: Question = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toQuestionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Question Title',
|
||||
content: 'Question Answer',
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Product Name',
|
||||
type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Question = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
products_id: {
|
||||
id: 1,
|
||||
translations: [],
|
||||
product_type: {
|
||||
id: 1,
|
||||
translations: [],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(toQuestionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
content: '',
|
||||
products: [
|
||||
{
|
||||
id: 1,
|
||||
name: '',
|
||||
type: {
|
||||
id: 1,
|
||||
name: '',
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
62
server/mappers/questionMapper.ts
Normal file
62
server/mappers/questionMapper.ts
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 将 Directus 返回的 Question 数据转换为 ProductQuestionView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Question 数据
|
||||
* @returns 转换后的 ProductQuestionView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toProductQuestionView(rawQuestion);
|
||||
*/
|
||||
export function toProductQuestionView(raw: Question): ProductQuestionView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
title: trans?.title ?? '',
|
||||
content: trans?.content ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Question 数据转换为 QuestionListView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Question 数据
|
||||
* @returns 转换后的 QuestionListView 对象
|
||||
* ---
|
||||
* @example
|
||||
* const view = toQuestionListView(rawQuestion);
|
||||
*/
|
||||
export function toQuestionListView(raw: Question): QuestionListView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const related_products: QuestionListProduct[] = (raw.products ?? [])
|
||||
.filter(isObject<ProductsQuestion>)
|
||||
.map((item) => item.products_id)
|
||||
.filter(isObject<Product>)
|
||||
.map((item) => {
|
||||
const translations = item.translations?.[0];
|
||||
|
||||
const product_type = isObject<ProductType>(item.product_type)
|
||||
? ({
|
||||
id: item.product_type.id,
|
||||
name: item.product_type.translations?.[0]?.name ?? '',
|
||||
} satisfies QuestionListProductType)
|
||||
: ({
|
||||
id: -1,
|
||||
name: '',
|
||||
} satisfies QuestionListProductType);
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
name: translations?.name ?? '',
|
||||
type: product_type,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
title: trans?.title ?? '',
|
||||
content: trans?.content ?? '',
|
||||
products: related_products,
|
||||
};
|
||||
}
|
||||
16
server/mappers/searchItemMapper.ts
Normal file
16
server/mappers/searchItemMapper.ts
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 搜索索引转换器
|
||||
* @param hit 搜索条目
|
||||
* @returns 转换后的搜索条目视图模型
|
||||
*
|
||||
* ---
|
||||
* @example
|
||||
* const view = toSearchItemView(item, 'products');
|
||||
*/
|
||||
export function toSearchItemView<T extends MeiliSearchItemType>(
|
||||
item: MeiliIndexMap[T],
|
||||
type: T
|
||||
): SearchItemView {
|
||||
const converter = converters[type];
|
||||
return converter(item);
|
||||
}
|
||||
100
server/mappers/solutionMapper.test.ts
Normal file
100
server/mappers/solutionMapper.test.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toSolutionListView, toSolutionView } from './solutionMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toSolutionListView
|
||||
*/
|
||||
describe('toSolutionListView', () => {
|
||||
const baseData: Solution = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{ id: 1, title: 'Solution Title', summary: 'Solution Summary' },
|
||||
],
|
||||
type: {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Type Name' }],
|
||||
sort: 1,
|
||||
},
|
||||
};
|
||||
test('convert raw data to SolutionListView correctly', () => {
|
||||
const rawData: Solution = { ...baseData };
|
||||
|
||||
expect(toSolutionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
solution_type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
sort: 1,
|
||||
},
|
||||
cover: '',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toSolutionListView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
summary: '',
|
||||
solution_type: {
|
||||
id: 1,
|
||||
name: 'Type Name',
|
||||
sort: 1,
|
||||
},
|
||||
cover: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 单元测试: toSolutionView
|
||||
*/
|
||||
describe('toSolutionView', () => {
|
||||
const baseData: Solution = {
|
||||
id: 1,
|
||||
translations: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
content: 'Solution Content',
|
||||
},
|
||||
],
|
||||
create_at: '2023-01-01T00:00:00Z',
|
||||
};
|
||||
|
||||
test('convert raw data to SolutionView correctly', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toSolutionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: 'Solution Title',
|
||||
summary: 'Solution Summary',
|
||||
content: 'Solution Content',
|
||||
createAt: '2023-01-01T00:00:00Z',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: Solution = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toSolutionView(rawData)).toEqual({
|
||||
id: 1,
|
||||
title: '',
|
||||
summary: '',
|
||||
content: '',
|
||||
createAt: '2023-01-01T00:00:00Z',
|
||||
});
|
||||
});
|
||||
});
|
||||
76
server/mappers/solutionMapper.ts
Normal file
76
server/mappers/solutionMapper.ts
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* 将 Directus 返回的 SolutionType 数据转换为 SolutionTypeView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 SolutionType 数据
|
||||
* @returns 转换后的 SolutionTypeView 对象
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* @example
|
||||
* const view = toSolutionTypeView(rawSolutionType);
|
||||
*/
|
||||
export function toSolutionTypeView(raw: SolutionType): SolutionTypeView {
|
||||
const trans = raw?.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
name: trans?.name ?? '',
|
||||
sort: raw?.sort ?? 999,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Solution 数据转换为 SolutionListView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Solution 数据
|
||||
* @returns 转换后的 SolutionListView 对象
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* @example
|
||||
* const view = toSolutionListView(rawSolution);
|
||||
*/
|
||||
export function toSolutionListView(raw: Solution): SolutionListView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const type = isObject<SolutionType>(raw.type)
|
||||
? toSolutionTypeView(raw.type)
|
||||
: ({
|
||||
id: -1,
|
||||
name: 'uncategory',
|
||||
sort: 999,
|
||||
} satisfies SolutionTypeView);
|
||||
|
||||
const cover = isObject<DirectusFile>(raw.cover) ? (raw?.cover.id ?? '') : '';
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
title: trans?.title ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
solution_type: type,
|
||||
cover: cover,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Solution 数据转换为 SolutionView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Solution 数据
|
||||
* @returns 转换后的 SolutionView 对象
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* @example
|
||||
* const view = toSolutionView(rawSolution);
|
||||
*/
|
||||
export function toSolutionView(raw: Solution): SolutionView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
title: trans?.title ?? '',
|
||||
summary: trans?.summary ?? '',
|
||||
content: trans?.content ?? '',
|
||||
createAt: raw?.create_at ?? '',
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user