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:
@ -1,106 +0,0 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试: toProductDocumentView
|
|
||||||
*/
|
|
||||||
describe('toProductDocumentView', () => {
|
|
||||||
test('convert raw data with fileId to ProductDocumentView correctly', () => {
|
|
||||||
const rawData: ProductDocument = {
|
|
||||||
id: 1,
|
|
||||||
file: 'rand-om__-uuid-1234',
|
|
||||||
translations: [{ id: 10, title: 'Document Title' }],
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(toProductDocumentView(rawData)).toEqual({
|
|
||||||
id: 1,
|
|
||||||
fileId: 'rand-om__-uuid-1234',
|
|
||||||
filename: undefined,
|
|
||||||
title: 'Document Title',
|
|
||||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
|
||||||
size: undefined,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('convert raw data with fileMeta to ProductDocumentView correctly', () => {
|
|
||||||
const rawData: ProductDocument = {
|
|
||||||
id: 1,
|
|
||||||
file: {
|
|
||||||
id: 'rand-om__-uuid-1234',
|
|
||||||
filename_download: 'document.pdf',
|
|
||||||
filesize: 2048,
|
|
||||||
},
|
|
||||||
translations: [{ id: 10, title: 'Document Title' }],
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(toProductDocumentView(rawData)).toEqual({
|
|
||||||
id: 1,
|
|
||||||
fileId: 'rand-om__-uuid-1234',
|
|
||||||
filename: 'document.pdf',
|
|
||||||
title: 'Document Title',
|
|
||||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
|
||||||
size: 2048,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
|
||||||
const rawData: ProductDocument = {
|
|
||||||
id: 1,
|
|
||||||
file: 'rand-om__-uuid-1234',
|
|
||||||
translations: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(toProductDocumentView(rawData)).toEqual({
|
|
||||||
id: 1,
|
|
||||||
fileId: 'rand-om__-uuid-1234',
|
|
||||||
filename: undefined,
|
|
||||||
title: '',
|
|
||||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
|
||||||
size: undefined,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单元测试: toDocumentListView
|
|
||||||
*/
|
|
||||||
describe('toProductDocumentView', () => {
|
|
||||||
test('convert raw data with fileId to DocumentListView correctly', () => {
|
|
||||||
const rawData: ProductDocument = {
|
|
||||||
id: 1,
|
|
||||||
file: 'rand-om__-uuid-1234',
|
|
||||||
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' }],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(toDocumentListView(rawData)).toEqual({
|
|
||||||
id: 1,
|
|
||||||
fileId: 'rand-om__-uuid-1234',
|
|
||||||
filename: undefined,
|
|
||||||
title: 'Document Title',
|
|
||||||
url: 'http://localhost:8055/assets/rand-om__-uuid-1234',
|
|
||||||
size: undefined,
|
|
||||||
products: [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
name: 'Product A',
|
|
||||||
type: {
|
|
||||||
id: 1,
|
|
||||||
name: 'Type A',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -1,79 +0,0 @@
|
|||||||
/**
|
|
||||||
* 将 Directus 返回的 Document 数据转换为 ProductDocumentView 视图模型
|
|
||||||
*
|
|
||||||
* @param raw: 原始的 Document 数据
|
|
||||||
* @returns 转换后的 ProductDocumentView 对象
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const view = toProductDocumentView(rawDocument);
|
|
||||||
*/
|
|
||||||
export function toProductDocumentView(
|
|
||||||
raw: ProductDocument
|
|
||||||
): ProductDocumentView {
|
|
||||||
const trans = raw.translations?.[0] ?? {
|
|
||||||
title: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
const fileId = typeof raw.file === 'string' ? raw.file : raw.file?.id;
|
|
||||||
const file = raw.file as DirectusFile;
|
|
||||||
|
|
||||||
const { getFileUrl } = useDirectusFiles();
|
|
||||||
|
|
||||||
const url = getFileUrl(fileId);
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: raw.id,
|
|
||||||
fileId: fileId,
|
|
||||||
filename: file.filename_download,
|
|
||||||
title: trans.title,
|
|
||||||
url: url,
|
|
||||||
size: file.filesize,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将 Directus 返回的 Document 数据转换为 DocumentListView 视图模型
|
|
||||||
*
|
|
||||||
* @param raw: 原始的 Document 数据
|
|
||||||
* @returns 转换后的 DocumentListView 对象
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const view = toDocumentListView(rawDocument);
|
|
||||||
*/
|
|
||||||
export function toDocumentListView(raw: ProductDocument): DocumentListView {
|
|
||||||
const trans = raw.translations?.[0] ?? { title: '' };
|
|
||||||
|
|
||||||
const fileId = typeof raw.file === 'string' ? raw.file : raw.file?.id;
|
|
||||||
const file = raw.file as DirectusFile;
|
|
||||||
|
|
||||||
const { getFileUrl } = useDirectusFiles();
|
|
||||||
const url = getFileUrl(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);
|
|
||||||
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,
|
|
||||||
products: related_products,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -4,7 +4,7 @@
|
|||||||
<app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
|
<app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<markdown-renderer :content="content.content || ''" />
|
<markdown-renderer :content="companyProfile.content || ''" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<el-divider content-position="left">{{ $t('learn-more') }}</el-divider>
|
<el-divider content-position="left">{{ $t('learn-more') }}</el-divider>
|
||||||
@ -32,9 +32,7 @@
|
|||||||
{ label: $t('navigation.home'), to: localePath('/') },
|
{ label: $t('navigation.home'), to: localePath('/') },
|
||||||
{ label: $t('navigation.about-us') },
|
{ label: $t('navigation.about-us') },
|
||||||
];
|
];
|
||||||
const { data, pending, error } = await useCompanyProfile();
|
const { data: companyProfile, pending, error } = await useCompanyProfile();
|
||||||
|
|
||||||
const content = computed(() => toCompanyProfileView(data.value));
|
|
||||||
|
|
||||||
watch(error, (value) => {
|
watch(error, (value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|||||||
@ -1,24 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="homepage">
|
<div class="homepage">
|
||||||
<homepage-carousel :homepage-data="homepageData" :pending="pending" />
|
<homepage-carousel :homepage-data="data" :pending="pending" />
|
||||||
<homepage-product-section
|
<homepage-product-section :homepage-data="data" :pending="pending" />
|
||||||
:homepage-data="homepageData"
|
<homepage-solution-section :homepage-data="data" :pending="pending" />
|
||||||
:pending="pending"
|
|
||||||
/>
|
|
||||||
<homepage-solution-section
|
|
||||||
:homepage-data="homepageData"
|
|
||||||
:pending="pending"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const { data, pending, error } = await useHomepage();
|
const { data, pending, error } = await useHomepage();
|
||||||
|
|
||||||
const homepageData = computed(() => {
|
|
||||||
return toHomepageView(data.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
const pageTilte = $t('page-title.homepage');
|
const pageTilte = $t('page-title.homepage');
|
||||||
|
|
||||||
watch(error, (value) => {
|
watch(error, (value) => {
|
||||||
|
|||||||
@ -37,16 +37,7 @@
|
|||||||
// 获取路由参数
|
// 获取路由参数
|
||||||
const id = route.params.slug as string;
|
const id = route.params.slug as string;
|
||||||
|
|
||||||
const { data, pending, error } = await useProduct(id);
|
const { data: product, pending, error } = await useProduct(id);
|
||||||
|
|
||||||
const rawProduct = computed(() => data.value ?? null);
|
|
||||||
|
|
||||||
const product = computed(() => {
|
|
||||||
if (rawProduct.value === null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return toProductView(rawProduct.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
const breadcrumbItems = computed(() => [
|
const breadcrumbItems = computed(() => [
|
||||||
{ label: $t('navigation.home'), to: localePath('/') },
|
{ label: $t('navigation.home'), to: localePath('/') },
|
||||||
|
|||||||
@ -37,7 +37,7 @@
|
|||||||
const localePath = useLocalePath();
|
const localePath = useLocalePath();
|
||||||
const { getImageUrl } = useDirectusImage();
|
const { getImageUrl } = useDirectusImage();
|
||||||
|
|
||||||
const { data, pending, error } = await useProductList();
|
const { data: products, pending, error } = await useProductList();
|
||||||
|
|
||||||
const activeNames = ref<string[]>([]);
|
const activeNames = ref<string[]>([]);
|
||||||
|
|
||||||
@ -46,14 +46,6 @@
|
|||||||
{ label: $t('navigation.products') },
|
{ label: $t('navigation.products') },
|
||||||
];
|
];
|
||||||
|
|
||||||
const productsRaw = computed(() => data.value ?? []);
|
|
||||||
|
|
||||||
const products = computed(() =>
|
|
||||||
productsRaw.value.map((item) => toProductListView(item))
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.debug('产品列表数据: ', products.value);
|
|
||||||
|
|
||||||
// 按类型分组
|
// 按类型分组
|
||||||
const groupedProducts = computed(() => {
|
const groupedProducts = computed(() => {
|
||||||
const groups: Record<string, { data: ProductListView[]; sort: number }> =
|
const groups: Record<string, { data: ProductListView[]; sort: number }> =
|
||||||
|
|||||||
@ -29,14 +29,7 @@
|
|||||||
// 获取路由参数
|
// 获取路由参数
|
||||||
const id = route.params.slug as string;
|
const id = route.params.slug as string;
|
||||||
|
|
||||||
const { data, pending, error } = await useSolution(id);
|
const { data: solution, pending, error } = await useSolution(id);
|
||||||
|
|
||||||
const solution = computed(() => {
|
|
||||||
if (!data.value) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return toSolutionView(data.value);
|
|
||||||
});
|
|
||||||
|
|
||||||
const breadcrumbItems = computed(() => [
|
const breadcrumbItems = computed(() => [
|
||||||
{ label: $t('navigation.home'), to: localePath('/') },
|
{ label: $t('navigation.home'), to: localePath('/') },
|
||||||
|
|||||||
@ -52,12 +52,7 @@
|
|||||||
{ label: $t('navigation.solutions') },
|
{ label: $t('navigation.solutions') },
|
||||||
];
|
];
|
||||||
|
|
||||||
const { data, pending, error } = await useSolutionList();
|
const { data: solutions, pending, error } = await useSolutionList();
|
||||||
|
|
||||||
const solutionsRaw = computed(() => data.value ?? []);
|
|
||||||
const solutions = computed(() =>
|
|
||||||
solutionsRaw.value.map((item) => toSolutionListView(item))
|
|
||||||
);
|
|
||||||
|
|
||||||
const activeName = ref<string>('all');
|
const activeName = ref<string>('all');
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="!pending" class="page-content">
|
<div v-if="!pending" class="page-content">
|
||||||
<markdown-renderer :content="content.content || ''" />
|
<markdown-renderer :content="contactInfo.content || ''" />
|
||||||
</div>
|
</div>
|
||||||
<div v-else class="loading">
|
<div v-else class="loading">
|
||||||
<el-skeleton :rows="5" animated />
|
<el-skeleton :rows="5" animated />
|
||||||
@ -22,9 +22,7 @@
|
|||||||
{ label: $t('navigation.support'), to: localePath('/support') },
|
{ label: $t('navigation.support'), to: localePath('/support') },
|
||||||
{ label: $t('navigation.contact-info') },
|
{ label: $t('navigation.contact-info') },
|
||||||
];
|
];
|
||||||
const { data, pending, error } = await useContactInfo();
|
const { data: contactInfo, pending, error } = await useContactInfo();
|
||||||
|
|
||||||
const content = computed(() => toContactInfoView(data.value));
|
|
||||||
|
|
||||||
watch(error, (value) => {
|
watch(error, (value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|||||||
@ -35,11 +35,7 @@
|
|||||||
keyword: '',
|
keyword: '',
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data, pending, error } = await useDocumentList();
|
const { data: documents, pending, error } = await useDocumentList();
|
||||||
|
|
||||||
const documents = computed(
|
|
||||||
() => data?.value.map((item) => toDocumentListView(item)) ?? []
|
|
||||||
);
|
|
||||||
|
|
||||||
const productTypeOptions = computed(() => {
|
const productTypeOptions = computed(() => {
|
||||||
const types: DocumentListProductType[] = [];
|
const types: DocumentListProductType[] = [];
|
||||||
|
|||||||
@ -38,11 +38,7 @@
|
|||||||
{ label: $t('navigation.faq') },
|
{ label: $t('navigation.faq') },
|
||||||
];
|
];
|
||||||
|
|
||||||
const { data, pending, error } = await useQuestionList();
|
const { data: questions, pending, error } = await useQuestionList();
|
||||||
|
|
||||||
const questions = computed(
|
|
||||||
() => data.value.map((item) => toQuestionListView(item)) ?? null
|
|
||||||
);
|
|
||||||
|
|
||||||
const productTypeOptions = computed(() => {
|
const productTypeOptions = computed(() => {
|
||||||
const types: QuestionListProductType[] = [];
|
const types: QuestionListProductType[] = [];
|
||||||
|
|||||||
7
server/api/cms/companyProfile.get.ts
Normal file
7
server/api/cms/companyProfile.get.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { companyProfileService } from '~~/server/services/cms/companyProfileService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return companyProfileService.getCompanyProfile(locale);
|
||||||
|
});
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/companyProfile.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ company_profile: CompanyProfile }>(
|
|
||||||
query,
|
|
||||||
{
|
|
||||||
locale: locale,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const companyProfile = data?.company_profile;
|
|
||||||
|
|
||||||
return companyProfile;
|
|
||||||
});
|
|
||||||
7
server/api/cms/contactInfo.get.ts
Normal file
7
server/api/cms/contactInfo.get.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { contactInfoService } from '~~/server/services/cms/contactInfoService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return contactInfoService.getContactInfo(locale);
|
||||||
|
});
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/contactInfo.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ contact_info: ContactInfo }>(query, {
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const contactInfo = data?.contact_info;
|
|
||||||
|
|
||||||
return contactInfo;
|
|
||||||
});
|
|
||||||
7
server/api/cms/documentList.get.ts
Normal file
7
server/api/cms/documentList.get.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { documentService } from '~~/server/services/cms/documentService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return documentService.getDocumentList(locale);
|
||||||
|
});
|
||||||
@ -1,24 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/documentList.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ product_documents: ProductDocument[] }>(
|
|
||||||
query,
|
|
||||||
{
|
|
||||||
locale: locale,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const documents = data?.product_documents;
|
|
||||||
|
|
||||||
return documents;
|
|
||||||
});
|
|
||||||
7
server/api/cms/homepage.get.ts
Normal file
7
server/api/cms/homepage.get.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { homepageService } from '~~/server/services/cms/homepageService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return homepageService.getHomepage(locale);
|
||||||
|
});
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/homepage.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ homepage: Homepage }>(query, {
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const homepage = data?.homepage;
|
|
||||||
|
|
||||||
return homepage;
|
|
||||||
});
|
|
||||||
13
server/api/cms/product/[id].get.ts
Normal file
13
server/api/cms/product/[id].get.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { productService } from '~~/server/services/cms/productService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const id = getRouterParam(event, 'id');
|
||||||
|
if (!id)
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Product ID is required',
|
||||||
|
});
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return productService.getProductById(id, locale);
|
||||||
|
});
|
||||||
@ -1,33 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const id = getRouterParam(event, 'id');
|
|
||||||
if (!id)
|
|
||||||
throw createError({
|
|
||||||
statusCode: 400,
|
|
||||||
statusMessage: 'Product ID is required',
|
|
||||||
});
|
|
||||||
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets/server',
|
|
||||||
'graphql/product.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ products_by_id: Product }>(query, {
|
|
||||||
id: id,
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const product = data?.products_by_id;
|
|
||||||
|
|
||||||
if (!product || product.status === 'archived') {
|
|
||||||
throw createError({ statusCode: 404, statusMessage: 'Product not found' });
|
|
||||||
}
|
|
||||||
|
|
||||||
return product;
|
|
||||||
});
|
|
||||||
6
server/api/cms/productList.get.ts
Normal file
6
server/api/cms/productList.get.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { productService } from '~~/server/services/cms/productService';
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return productService.getProductList(locale);
|
||||||
|
});
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/productList.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ products: Product[] }>(query, {
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const products = data?.products || [];
|
|
||||||
|
|
||||||
return products;
|
|
||||||
});
|
|
||||||
6
server/api/cms/questionList.get.ts
Normal file
6
server/api/cms/questionList.get.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { questionService } from '~~/server/services/cms/questionService';
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return questionService.getQuestionList(locale);
|
||||||
|
});
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/questionList.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ questions: Question[] }>(query, {
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const questions = data?.questions || [];
|
|
||||||
|
|
||||||
return questions;
|
|
||||||
});
|
|
||||||
14
server/api/cms/solution/[id].get.ts
Normal file
14
server/api/cms/solution/[id].get.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { solutionService } from '~~/server/services/cms/solutionService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const id = getRouterParam(event, 'id');
|
||||||
|
if (!id)
|
||||||
|
throw createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Solution ID is required',
|
||||||
|
});
|
||||||
|
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return solutionService.getSolutionById(id, locale);
|
||||||
|
});
|
||||||
@ -1,33 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const id = getRouterParam(event, 'id');
|
|
||||||
if (!id)
|
|
||||||
throw createError({
|
|
||||||
statusCode: 400,
|
|
||||||
statusMessage: 'Solution ID is required',
|
|
||||||
});
|
|
||||||
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/solution.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
|
||||||
id: id,
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const solution = data?.solutions_by_id;
|
|
||||||
|
|
||||||
if (!solution || solution.status === 'archived') {
|
|
||||||
throw createError({ statusCode: 404, statusMessage: 'Solution not found' });
|
|
||||||
}
|
|
||||||
|
|
||||||
return solution;
|
|
||||||
});
|
|
||||||
7
server/api/cms/solutionList.get.ts
Normal file
7
server/api/cms/solutionList.get.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { solutionService } from '~~/server/services/cms/solutionService';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||||
|
|
||||||
|
return solutionService.getSolutionList(locale);
|
||||||
|
});
|
||||||
@ -1,21 +0,0 @@
|
|||||||
import { directus } from '~~/server/utils/directus';
|
|
||||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
|
||||||
const query = await loadAssetAsString(
|
|
||||||
'assets:server',
|
|
||||||
'graphql/solutionList.graphql'
|
|
||||||
);
|
|
||||||
if (!query)
|
|
||||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
|
||||||
|
|
||||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
|
||||||
|
|
||||||
const data = await directus.query<{ solutions: Solution[] }>(query, {
|
|
||||||
locale: locale,
|
|
||||||
});
|
|
||||||
|
|
||||||
const solutions = data?.solutions || [];
|
|
||||||
|
|
||||||
return solutions;
|
|
||||||
});
|
|
||||||
@ -1,15 +1,18 @@
|
|||||||
import { expect, test, describe } from 'vitest';
|
import { expect, test, describe } from 'vitest';
|
||||||
|
import { toCompanyProfileView } from './companyProfileMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: companyProfileMapper
|
* 单元测试: companyProfileMapper
|
||||||
*/
|
*/
|
||||||
describe('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', () => {
|
test('convert raw data to CompanyProfileView correctly', () => {
|
||||||
const rawData: CompanyProfile = {
|
const rawData: CompanyProfile = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [
|
|
||||||
{ id: 10, content: 'This is raw data of company profile' },
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(toCompanyProfileView(rawData)).toEqual({
|
expect(toCompanyProfileView(rawData)).toEqual({
|
||||||
@ -19,7 +22,7 @@ describe('companyProfileMapper', () => {
|
|||||||
});
|
});
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: CompanyProfile = {
|
const rawData: CompanyProfile = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -8,10 +8,10 @@
|
|||||||
* const view = toCompanyProfileView(rawCompanyProfile);
|
* const view = toCompanyProfileView(rawCompanyProfile);
|
||||||
*/
|
*/
|
||||||
export function toCompanyProfileView(raw: CompanyProfile): CompanyProfileView {
|
export function toCompanyProfileView(raw: CompanyProfile): CompanyProfileView {
|
||||||
const trans = raw.translations?.[0] ?? { content: '' };
|
const trans = raw.translations?.[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
content: trans.content,
|
content: trans?.content ?? '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,14 +1,18 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { toContactInfoView } from './contactInfoMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: contactInfoMapper
|
* 单元测试: contactInfoMapper
|
||||||
*/
|
*/
|
||||||
describe('contactInfoMapper', () => {
|
describe('contactInfoMapper', () => {
|
||||||
test('convert raw data to ContactInfoView correctly', () => {
|
const baseData: ContactInfo = {
|
||||||
const rawData: ContactInfo = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [{ id: 10, content: 'This is raw data of contact info' }],
|
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({
|
expect(toContactInfoView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -18,7 +22,7 @@ describe('contactInfoMapper', () => {
|
|||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: ContactInfo = {
|
const rawData: ContactInfo = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -8,10 +8,10 @@
|
|||||||
* const view = toContactInfoView(rawContactInfo);
|
* const view = toContactInfoView(rawContactInfo);
|
||||||
*/
|
*/
|
||||||
export function toContactInfoView(raw: ContactInfo): ContactInfoView {
|
export function toContactInfoView(raw: ContactInfo): ContactInfoView {
|
||||||
const trans = raw.translations?.[0] ?? { content: '' };
|
const trans = raw.translations?.[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
content: trans.content,
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { toHomepageView } from './homepageMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: toHomepageView
|
* 单元测试: toHomepageView
|
||||||
@ -25,19 +26,25 @@ describe('toHomepageView', () => {
|
|||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [{ id: 1, name: 'Product 1', summary: 'Summary 1' }],
|
translations: [{ id: 1, name: 'Product 1', summary: 'Summary 1' }],
|
||||||
cover: 'cover-file-uuid-1',
|
cover: {
|
||||||
|
id: 'cover-file-uuid-1',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
translations: [{ id: 2, name: 'Product 2', summary: 'Summary 2' }],
|
translations: [{ id: 2, name: 'Product 2', summary: 'Summary 2' }],
|
||||||
cover: { id: 'cover-file-uuid-2' },
|
cover: {
|
||||||
|
id: 'cover-file-uuid-2',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
recommend_solutions: [
|
recommend_solutions: [
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [{ id: 1, title: 'Solution 1', summary: 'Summary 1' }],
|
translations: [{ id: 1, title: 'Solution 1', summary: 'Summary 1' }],
|
||||||
cover: 'cover-file-uuid-1',
|
cover: {
|
||||||
|
id: 'cover-file-uuid-1',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
@ -81,8 +88,8 @@ describe('toHomepageView', () => {
|
|||||||
expect(toHomepageView(rawData)).toEqual({
|
expect(toHomepageView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
carousel: [],
|
carousel: [],
|
||||||
recommendProducts: [{ id: 1, name: '', summary: '', cover: null }],
|
recommendProducts: [{ id: 1, name: '', summary: '', cover: '' }],
|
||||||
recommendSolutions: [{ id: 1, title: '', summary: '', cover: null }],
|
recommendSolutions: [{ id: 1, title: '', summary: '', cover: '' }],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import { isObject } from '../utils/object';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 Directus 返回的 Homepage 数据转换为 HomepageView 视图模型
|
* 将 Directus 返回的 Homepage 数据转换为 HomepageView 视图模型
|
||||||
*
|
*
|
||||||
@ -17,14 +19,14 @@ export function toHomepageView(raw: Homepage): HomepageView {
|
|||||||
const products = (raw.recommend_products ?? [])
|
const products = (raw.recommend_products ?? [])
|
||||||
.filter(isObject<Product>)
|
.filter(isObject<Product>)
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
const cover = isObject<DirectusFile>(item.cover)
|
const trans = item.translations?.[0];
|
||||||
? item.cover.id
|
|
||||||
: item.cover;
|
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||||
const trans = item.translations?.[0] ?? { name: '', summary: '' };
|
|
||||||
return {
|
return {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
name: trans.name,
|
name: trans?.name ?? '',
|
||||||
summary: trans.summary,
|
summary: trans?.summary ?? '',
|
||||||
cover: cover,
|
cover: cover,
|
||||||
} satisfies HomepageProductView;
|
} satisfies HomepageProductView;
|
||||||
});
|
});
|
||||||
@ -32,15 +34,13 @@ export function toHomepageView(raw: Homepage): HomepageView {
|
|||||||
const solutions = (raw.recommend_solutions ?? [])
|
const solutions = (raw.recommend_solutions ?? [])
|
||||||
.filter(isObject<Solution>)
|
.filter(isObject<Solution>)
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
const cover = isObject<DirectusFile>(item.cover)
|
const trans = item.translations?.[0];
|
||||||
? item.cover.id
|
const cover = isObject<DirectusFile>(item.cover) ? item.cover.id : '';
|
||||||
: item.cover;
|
|
||||||
const trans = item.translations?.[0] ?? { title: '', summary: '' };
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
title: trans.title,
|
title: trans?.title ?? '',
|
||||||
summary: trans.summary,
|
summary: trans?.summary ?? '',
|
||||||
cover: cover,
|
cover: cover,
|
||||||
} satisfies HomepageSolutionView;
|
} satisfies HomepageSolutionView;
|
||||||
});
|
});
|
||||||
@ -1,4 +1,10 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import {
|
||||||
|
toProductListView,
|
||||||
|
toProductSpecView,
|
||||||
|
toProductSpecGroupView,
|
||||||
|
toProductView,
|
||||||
|
} from './productMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: toProductListView
|
* 单元测试: toProductListView
|
||||||
@ -1,3 +1,6 @@
|
|||||||
|
import { toProductQuestionView } from './questionMapper';
|
||||||
|
import { toProductDocumentView } from './documentMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
* 将Directus返回的ProductType数据转换为ProductTypeView视图模型
|
||||||
*
|
*
|
||||||
@ -27,19 +30,23 @@ export function toProductTypeView(raw: ProductType): ProductTypeView {
|
|||||||
* const view = toProductListView(rawProduct);
|
* const view = toProductListView(rawProduct);
|
||||||
*/
|
*/
|
||||||
export function toProductListView(raw: Product): ProductListView {
|
export function toProductListView(raw: Product): ProductListView {
|
||||||
const trans = raw.translations?.[0] ?? { name: '', summary: '' };
|
const trans = raw.translations?.[0];
|
||||||
|
|
||||||
const type = isObject<ProductType>(raw.product_type)
|
const type = isObject<ProductType>(raw.product_type)
|
||||||
? toProductTypeView(raw.product_type)
|
? toProductTypeView(raw.product_type)
|
||||||
: undefined;
|
: ({
|
||||||
|
id: -1,
|
||||||
|
name: '',
|
||||||
|
sort: 999,
|
||||||
|
} satisfies ProductTypeView);
|
||||||
|
|
||||||
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
const cover = isObject<DirectusFile>(raw.cover) ? raw.cover.id : '';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
product_type: type,
|
product_type: type,
|
||||||
name: trans.name,
|
name: trans?.name ?? '',
|
||||||
summary: trans.summary,
|
summary: trans?.summary ?? '',
|
||||||
cover: cover,
|
cover: cover,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -56,14 +63,14 @@ export function toProductListView(raw: Product): ProductListView {
|
|||||||
export function toProductSpecGroupView(
|
export function toProductSpecGroupView(
|
||||||
raw: ProductSpecGroup
|
raw: ProductSpecGroup
|
||||||
): ProductSpecGroupView {
|
): ProductSpecGroupView {
|
||||||
const trans = raw.translations?.[0] ?? {
|
const trans = raw.translations?.[0];
|
||||||
name: '',
|
|
||||||
};
|
const specs = raw.specs ?? [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
name: trans.name,
|
name: trans?.name ?? '',
|
||||||
specs: raw.specs
|
specs: specs
|
||||||
.filter(isObject<ProductSpec>)
|
.filter(isObject<ProductSpec>)
|
||||||
.map((item) => toProductSpecView(item)),
|
.map((item) => toProductSpecView(item)),
|
||||||
};
|
};
|
||||||
@ -79,15 +86,12 @@ export function toProductSpecGroupView(
|
|||||||
* const view = toProductSpecView(rawSpecGroup);
|
* const view = toProductSpecView(rawSpecGroup);
|
||||||
*/
|
*/
|
||||||
export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
||||||
const trans = raw.translations?.[0] ?? {
|
const trans = raw.translations?.[0];
|
||||||
key: '',
|
|
||||||
value: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
key: trans.key,
|
key: trans?.key ?? '',
|
||||||
value: trans.value,
|
value: trans?.value ?? '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,11 +105,7 @@ export function toProductSpecView(raw: ProductSpec): ProductSpecView {
|
|||||||
* const view = toProductView(rawProduct);
|
* const view = toProductView(rawProduct);
|
||||||
*/
|
*/
|
||||||
export function toProductView(raw: Product): ProductView {
|
export function toProductView(raw: Product): ProductView {
|
||||||
const trans = raw.translations?.[0] ?? {
|
const trans = raw.translations?.[0];
|
||||||
name: '',
|
|
||||||
summary: '',
|
|
||||||
description: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
const images = (raw.images ?? [])
|
const images = (raw.images ?? [])
|
||||||
.filter(isObject<ProductsProductImage>)
|
.filter(isObject<ProductsProductImage>)
|
||||||
@ -120,6 +120,10 @@ export function toProductView(raw: Product): ProductView {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const type = isObject<ProductType>(raw.product_type)
|
||||||
|
? (raw.product_type.translations?.[0]?.name ?? '')
|
||||||
|
: '';
|
||||||
|
|
||||||
const specs = (raw.specs ?? [])
|
const specs = (raw.specs ?? [])
|
||||||
.filter(isObject<ProductSpecGroup>)
|
.filter(isObject<ProductSpecGroup>)
|
||||||
.map((item) => toProductSpecGroupView(item));
|
.map((item) => toProductSpecGroupView(item));
|
||||||
@ -138,17 +142,11 @@ export function toProductView(raw: Product): ProductView {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
|
product_type: type,
|
||||||
product_type:
|
name: trans?.name ?? '',
|
||||||
typeof raw.product_type === 'string'
|
summary: trans?.summary ?? '',
|
||||||
? raw.product_type
|
|
||||||
: typeof raw.product_type === 'object' && raw.product_type
|
|
||||||
? raw.product_type.translations[0].name || ''
|
|
||||||
: '',
|
|
||||||
name: trans.name,
|
|
||||||
summary: trans.summary,
|
|
||||||
images: images,
|
images: images,
|
||||||
description: trans.description,
|
description: trans?.description ?? '',
|
||||||
specs: specs,
|
specs: specs,
|
||||||
faqs: faqs,
|
faqs: faqs,
|
||||||
documents: documents,
|
documents: documents,
|
||||||
@ -1,16 +1,20 @@
|
|||||||
import { describe, expect, test } from 'vitest';
|
import { describe, expect, test } from 'vitest';
|
||||||
|
import { toProductQuestionView, toQuestionListView } from './questionMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: toProductQuestionView
|
* 单元测试: toProductQuestionView
|
||||||
*/
|
*/
|
||||||
describe('toProductQuestionView', () => {
|
describe('toProductQuestionView', () => {
|
||||||
test('convert raw data to ProductQuestionView correctly', () => {
|
const baseData: Question = {
|
||||||
const rawData: Question = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [
|
translations: [
|
||||||
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
test('convert raw data to ProductQuestionView correctly', () => {
|
||||||
|
const rawData: Question = {
|
||||||
|
...baseData,
|
||||||
|
};
|
||||||
|
|
||||||
expect(toProductQuestionView(rawData)).toEqual({
|
expect(toProductQuestionView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -21,7 +25,7 @@ describe('toProductQuestionView', () => {
|
|||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: Question = {
|
const rawData: Question = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -37,8 +41,7 @@ describe('toProductQuestionView', () => {
|
|||||||
* 单元测试: toQuestionListView
|
* 单元测试: toQuestionListView
|
||||||
*/
|
*/
|
||||||
describe('toQuestionListView', () => {
|
describe('toQuestionListView', () => {
|
||||||
test('convert raw data to QuestionListView correctly', () => {
|
const baseData: Question = {
|
||||||
const rawData: Question = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [
|
translations: [
|
||||||
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
{ id: 1, title: 'Question Title', content: 'Question Answer' },
|
||||||
@ -58,6 +61,11 @@ describe('toQuestionListView', () => {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
test('convert raw data to QuestionListView correctly', () => {
|
||||||
|
const rawData: Question = {
|
||||||
|
...baseData,
|
||||||
|
};
|
||||||
|
|
||||||
expect(toQuestionListView(rawData)).toEqual({
|
expect(toQuestionListView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
title: 'Question Title',
|
title: 'Question Title',
|
||||||
@ -77,7 +85,7 @@ describe('toQuestionListView', () => {
|
|||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: Question = {
|
const rawData: Question = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
products: [
|
products: [
|
||||||
{
|
{
|
||||||
@ -8,12 +8,12 @@
|
|||||||
* const view = toProductQuestionView(rawQuestion);
|
* const view = toProductQuestionView(rawQuestion);
|
||||||
*/
|
*/
|
||||||
export function toProductQuestionView(raw: Question): ProductQuestionView {
|
export function toProductQuestionView(raw: Question): ProductQuestionView {
|
||||||
const trans = raw.translations?.[0] ?? { title: '', content: '' };
|
const trans = raw.translations?.[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
title: trans.title,
|
title: trans?.title ?? '',
|
||||||
content: trans.content,
|
content: trans?.content ?? '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,33 +27,36 @@ export function toProductQuestionView(raw: Question): ProductQuestionView {
|
|||||||
* const view = toQuestionListView(rawQuestion);
|
* const view = toQuestionListView(rawQuestion);
|
||||||
*/
|
*/
|
||||||
export function toQuestionListView(raw: Question): QuestionListView {
|
export function toQuestionListView(raw: Question): QuestionListView {
|
||||||
const trans = raw.translations?.[0] ?? { title: '', content: '' };
|
const trans = raw.translations?.[0];
|
||||||
|
|
||||||
const related_products: QuestionListProduct[] = (raw.products ?? [])
|
const related_products: QuestionListProduct[] = (raw.products ?? [])
|
||||||
.filter(isObject<ProductsQuestion>)
|
.filter(isObject<ProductsQuestion>)
|
||||||
.map((item) => item.products_id)
|
.map((item) => item.products_id)
|
||||||
.filter(isObject<Product>)
|
.filter(isObject<Product>)
|
||||||
.map((item) => {
|
.map((item) => {
|
||||||
const translations = item.translations[0] ?? { name: '' };
|
const translations = item.translations?.[0];
|
||||||
|
|
||||||
const product_type =
|
const product_type = isObject<ProductType>(item.product_type)
|
||||||
isObject<ProductType>(item.product_type) &&
|
? ({
|
||||||
({
|
|
||||||
id: item.product_type.id,
|
id: item.product_type.id,
|
||||||
name: item.product_type.translations[0]?.name ?? '',
|
name: item.product_type.translations?.[0]?.name ?? '',
|
||||||
|
} satisfies QuestionListProductType)
|
||||||
|
: ({
|
||||||
|
id: -1,
|
||||||
|
name: '',
|
||||||
} satisfies QuestionListProductType);
|
} satisfies QuestionListProductType);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: item.id,
|
id: item.id,
|
||||||
name: translations.name,
|
name: translations?.name ?? '',
|
||||||
type: product_type,
|
type: product_type,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
title: trans.title,
|
title: trans?.title ?? '',
|
||||||
content: trans.content,
|
content: trans?.content ?? '',
|
||||||
products: related_products,
|
products: related_products,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -12,5 +12,5 @@ export function toSearchItemView<T extends MeiliSearchItemType>(
|
|||||||
type: T
|
type: T
|
||||||
): SearchItemView {
|
): SearchItemView {
|
||||||
const converter = converters[type];
|
const converter = converters[type];
|
||||||
return converter ? converter(item) : null;
|
return converter(item);
|
||||||
}
|
}
|
||||||
@ -1,11 +1,11 @@
|
|||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
|
import { toSolutionListView, toSolutionView } from './solutionMapper';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单元测试: toSolutionListView
|
* 单元测试: toSolutionListView
|
||||||
*/
|
*/
|
||||||
describe('toSolutionListView', () => {
|
describe('toSolutionListView', () => {
|
||||||
test('convert raw data to SolutionListView correctly', () => {
|
const baseData: Solution = {
|
||||||
const rawData: Solution = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [
|
translations: [
|
||||||
{ id: 1, title: 'Solution Title', summary: 'Solution Summary' },
|
{ id: 1, title: 'Solution Title', summary: 'Solution Summary' },
|
||||||
@ -16,6 +16,8 @@ describe('toSolutionListView', () => {
|
|||||||
sort: 1,
|
sort: 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
test('convert raw data to SolutionListView correctly', () => {
|
||||||
|
const rawData: Solution = { ...baseData };
|
||||||
|
|
||||||
expect(toSolutionListView(rawData)).toEqual({
|
expect(toSolutionListView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
@ -26,18 +28,14 @@ describe('toSolutionListView', () => {
|
|||||||
name: 'Type Name',
|
name: 'Type Name',
|
||||||
sort: 1,
|
sort: 1,
|
||||||
},
|
},
|
||||||
|
cover: '',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: Solution = {
|
const rawData: Solution = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
type: {
|
|
||||||
id: 1,
|
|
||||||
translations: [],
|
|
||||||
sort: null,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(toSolutionListView(rawData)).toEqual({
|
expect(toSolutionListView(rawData)).toEqual({
|
||||||
@ -46,9 +44,10 @@ describe('toSolutionListView', () => {
|
|||||||
summary: '',
|
summary: '',
|
||||||
solution_type: {
|
solution_type: {
|
||||||
id: 1,
|
id: 1,
|
||||||
name: '',
|
name: 'Type Name',
|
||||||
sort: 999,
|
sort: 1,
|
||||||
},
|
},
|
||||||
|
cover: '',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -57,8 +56,7 @@ describe('toSolutionListView', () => {
|
|||||||
* 单元测试: toSolutionView
|
* 单元测试: toSolutionView
|
||||||
*/
|
*/
|
||||||
describe('toSolutionView', () => {
|
describe('toSolutionView', () => {
|
||||||
test('convert raw data to SolutionView correctly', () => {
|
const baseData: Solution = {
|
||||||
const rawData: Solution = {
|
|
||||||
id: 1,
|
id: 1,
|
||||||
translations: [
|
translations: [
|
||||||
{
|
{
|
||||||
@ -71,6 +69,11 @@ describe('toSolutionView', () => {
|
|||||||
create_at: '2023-01-01T00:00:00Z',
|
create_at: '2023-01-01T00:00:00Z',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
test('convert raw data to SolutionView correctly', () => {
|
||||||
|
const rawData: Solution = {
|
||||||
|
...baseData,
|
||||||
|
};
|
||||||
|
|
||||||
expect(toSolutionView(rawData)).toEqual({
|
expect(toSolutionView(rawData)).toEqual({
|
||||||
id: 1,
|
id: 1,
|
||||||
title: 'Solution Title',
|
title: 'Solution Title',
|
||||||
@ -82,9 +85,8 @@ describe('toSolutionView', () => {
|
|||||||
|
|
||||||
test('convert raw data with missing translations', () => {
|
test('convert raw data with missing translations', () => {
|
||||||
const rawData: Solution = {
|
const rawData: Solution = {
|
||||||
id: 1,
|
...baseData,
|
||||||
translations: [],
|
translations: [],
|
||||||
create_at: '2023-01-01T00:00:00Z',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
expect(toSolutionView(rawData)).toEqual({
|
expect(toSolutionView(rawData)).toEqual({
|
||||||
@ -10,11 +10,11 @@
|
|||||||
* const view = toSolutionTypeView(rawSolutionType);
|
* const view = toSolutionTypeView(rawSolutionType);
|
||||||
*/
|
*/
|
||||||
export function toSolutionTypeView(raw: SolutionType): SolutionTypeView {
|
export function toSolutionTypeView(raw: SolutionType): SolutionTypeView {
|
||||||
const trans = raw.translations?.[0] ?? { name: '' };
|
const trans = raw?.translations?.[0];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
name: trans.name,
|
name: trans?.name ?? '',
|
||||||
sort: raw?.sort ?? 999,
|
sort: raw?.sort ?? 999,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -31,21 +31,24 @@ export function toSolutionTypeView(raw: SolutionType): SolutionTypeView {
|
|||||||
* const view = toSolutionListView(rawSolution);
|
* const view = toSolutionListView(rawSolution);
|
||||||
*/
|
*/
|
||||||
export function toSolutionListView(raw: Solution): SolutionListView {
|
export function toSolutionListView(raw: Solution): SolutionListView {
|
||||||
const trans = raw.translations?.[0] ?? {
|
const trans = raw.translations?.[0];
|
||||||
title: '',
|
|
||||||
summary: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
const type = isObject<SolutionType>(raw.type)
|
const type = isObject<SolutionType>(raw.type)
|
||||||
? toSolutionTypeView(raw.type)
|
? toSolutionTypeView(raw.type)
|
||||||
: undefined;
|
: ({
|
||||||
|
id: -1,
|
||||||
|
name: 'uncategory',
|
||||||
|
sort: 999,
|
||||||
|
} satisfies SolutionTypeView);
|
||||||
|
|
||||||
|
const cover = isObject<DirectusFile>(raw.cover) ? (raw?.cover.id ?? '') : '';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
title: trans.title,
|
title: trans?.title ?? '',
|
||||||
summary: trans.summary,
|
summary: trans?.summary ?? '',
|
||||||
solution_type: type,
|
solution_type: type,
|
||||||
cover: isObject<DirectusFile>(raw.cover) ? raw.cover.id : raw.cover,
|
cover: cover,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,17 +64,13 @@ export function toSolutionListView(raw: Solution): SolutionListView {
|
|||||||
* const view = toSolutionView(rawSolution);
|
* const view = toSolutionView(rawSolution);
|
||||||
*/
|
*/
|
||||||
export function toSolutionView(raw: Solution): SolutionView {
|
export function toSolutionView(raw: Solution): SolutionView {
|
||||||
const trans = raw.translations?.[0] ?? {
|
const trans = raw.translations?.[0];
|
||||||
title: '',
|
|
||||||
summary: '',
|
|
||||||
content: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: raw.id,
|
id: raw.id,
|
||||||
title: trans.title,
|
title: trans?.title ?? '',
|
||||||
summary: trans.summary,
|
summary: trans?.summary ?? '',
|
||||||
content: trans.content,
|
content: trans?.content ?? '',
|
||||||
createAt: raw.create_at,
|
createAt: raw?.create_at ?? '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
25
server/services/cms/companyProfileService.ts
Normal file
25
server/services/cms/companyProfileService.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { toCompanyProfileView } from '~~/server/mappers/companyProfileMapper';
|
||||||
|
|
||||||
|
export const companyProfileService = {
|
||||||
|
async getCompanyProfile(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/companyProfile.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ company_profile: CompanyProfile }>(
|
||||||
|
query,
|
||||||
|
{
|
||||||
|
locale: locale,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return toCompanyProfileView(data?.company_profile);
|
||||||
|
},
|
||||||
|
};
|
||||||
22
server/services/cms/contactInfoService.ts
Normal file
22
server/services/cms/contactInfoService.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { toContactInfoView } from '~~/server/mappers/contactInfoMapper';
|
||||||
|
|
||||||
|
export const contactInfoService = {
|
||||||
|
async getContactInfo(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/contactInfo.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ contact_info: ContactInfo }>(query, {
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
return toContactInfoView(data?.contact_info);
|
||||||
|
},
|
||||||
|
};
|
||||||
27
server/services/cms/documentService.ts
Normal file
27
server/services/cms/documentService.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { toDocumentListView } from '~~/server/mappers/documentMapper';
|
||||||
|
|
||||||
|
export const documentService = {
|
||||||
|
async getDocumentList(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/documentList.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ product_documents: ProductDocument[] }>(
|
||||||
|
query,
|
||||||
|
{
|
||||||
|
locale: locale,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const documents = data?.product_documents || [];
|
||||||
|
|
||||||
|
return documents.map((document) => toDocumentListView(document));
|
||||||
|
},
|
||||||
|
};
|
||||||
22
server/services/cms/homepageService.ts
Normal file
22
server/services/cms/homepageService.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { toHomepageView } from '~~/server/mappers/homepageMapper';
|
||||||
|
|
||||||
|
export const homepageService = {
|
||||||
|
async getHomepage(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/homepage.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ homepage: Homepage }>(query, {
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
return toHomepageView(data?.homepage);
|
||||||
|
},
|
||||||
|
};
|
||||||
55
server/services/cms/productService.ts
Normal file
55
server/services/cms/productService.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import {
|
||||||
|
toProductView,
|
||||||
|
toProductListView,
|
||||||
|
} from '~~/server/mappers/productMapper';
|
||||||
|
|
||||||
|
export const productService = {
|
||||||
|
async getProductList(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/productList.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ products: Product[] }>(query, {
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const products = data?.products || [];
|
||||||
|
|
||||||
|
return products.map((product) => toProductListView(product));
|
||||||
|
},
|
||||||
|
async getProductById(id: string, locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/product.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ products_by_id: Product }>(query, {
|
||||||
|
id: id,
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const product = data?.products_by_id;
|
||||||
|
|
||||||
|
if (!product || product.status === 'archived') {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 404,
|
||||||
|
statusMessage: 'Product not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return toProductView(product);
|
||||||
|
},
|
||||||
|
};
|
||||||
24
server/services/cms/questionService.ts
Normal file
24
server/services/cms/questionService.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { toQuestionListView } from '~~/server/mappers/questionMapper';
|
||||||
|
|
||||||
|
export const questionService = {
|
||||||
|
async getQuestionList(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/questionList.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ questions: Question[] }>(query, {
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const questions = data?.questions || [];
|
||||||
|
|
||||||
|
return questions.map((question) => toQuestionListView(question));
|
||||||
|
},
|
||||||
|
};
|
||||||
55
server/services/cms/solutionService.ts
Normal file
55
server/services/cms/solutionService.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import {
|
||||||
|
toSolutionView,
|
||||||
|
toSolutionListView,
|
||||||
|
} from '~~/server/mappers/solutionMapper';
|
||||||
|
|
||||||
|
export const solutionService = {
|
||||||
|
async getSolutionList(locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/solutionList.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ solutions: Solution[] }>(query, {
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const solutions = data?.solutions || [];
|
||||||
|
|
||||||
|
return solutions.map((solution) => toSolutionListView(solution));
|
||||||
|
},
|
||||||
|
async getSolutionById(id: string, locale: string) {
|
||||||
|
const query = await loadAssetAsString(
|
||||||
|
'assets/server',
|
||||||
|
'graphql/solution.graphql'
|
||||||
|
);
|
||||||
|
if (!query) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 500,
|
||||||
|
message: 'GraphQL query not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
||||||
|
id: id,
|
||||||
|
locale: locale,
|
||||||
|
});
|
||||||
|
|
||||||
|
const solution = data?.solutions_by_id;
|
||||||
|
|
||||||
|
if (!solution || solution.status === 'archived') {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 404,
|
||||||
|
statusMessage: 'Solution not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return toSolutionView(solution);
|
||||||
|
},
|
||||||
|
};
|
||||||
66
server/utils/object.test.ts
Normal file
66
server/utils/object.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { expect, test, describe } from 'vitest';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试: isObject
|
||||||
|
*/
|
||||||
|
describe('isObject', () => {
|
||||||
|
test('identify plain object', () => {
|
||||||
|
expect(isObject({})).toBe(true);
|
||||||
|
expect(isObject({ key: 'value' })).toBe(true);
|
||||||
|
});
|
||||||
|
test('identify null as non objevt', () => {
|
||||||
|
expect(isObject(null)).toBe(false);
|
||||||
|
});
|
||||||
|
test('identify non-object types', () => {
|
||||||
|
expect(isObject(undefined)).toBe(false);
|
||||||
|
expect(isObject(42)).toBe(false);
|
||||||
|
expect(isObject('string')).toBe(false);
|
||||||
|
expect(isObject(Symbol('sym'))).toBe(false);
|
||||||
|
expect(isObject(true)).toBe(false);
|
||||||
|
expect(isObject(() => {})).toBe(false);
|
||||||
|
});
|
||||||
|
test('identify arrays as objects', () => {
|
||||||
|
expect(isObject([])).toBe(true);
|
||||||
|
});
|
||||||
|
test('identify narrowed object type', () => {
|
||||||
|
const value: unknown = { id: 1 };
|
||||||
|
if (isObject<{ id: number }>(value)) {
|
||||||
|
expect(value.id).toBe(1);
|
||||||
|
} else {
|
||||||
|
throw new Error('Type narrowing failed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试: isArrayOfObject
|
||||||
|
*/
|
||||||
|
describe('isArrayOfObject', () => {
|
||||||
|
test('identify array of plain objects', () => {
|
||||||
|
const arr = [{ id: 1 }, { name: 'Alice' }];
|
||||||
|
expect(isArrayOfObject<{ id?: number; name?: string }>(arr)).toBe(true);
|
||||||
|
});
|
||||||
|
test('identify array containing non-objects', () => {
|
||||||
|
expect(isArrayOfObject([1, 2, 3])).toBe(false);
|
||||||
|
expect(isArrayOfObject([{ id: 1 }, null])).toBe(false);
|
||||||
|
expect(isArrayOfObject([{ id: 1 }, 'string'])).toBe(false);
|
||||||
|
});
|
||||||
|
test('identify non-array types', () => {
|
||||||
|
expect(isArrayOfObject(null)).toBe(false);
|
||||||
|
expect(isArrayOfObject({})).toBe(false);
|
||||||
|
expect(isArrayOfObject(42)).toBe(false);
|
||||||
|
});
|
||||||
|
test('identify empty array as array of objects', () => {
|
||||||
|
expect(isArrayOfObject([])).toBe(true);
|
||||||
|
});
|
||||||
|
test('identify narrowed array of object type', () => {
|
||||||
|
const data: unknown = [{ id: 1 }, { id: 2 }];
|
||||||
|
if (isArrayOfObject<{ id: number }>(data)) {
|
||||||
|
// TS 能识别为 { id: number }[]
|
||||||
|
expect(data[0].id).toBe(1);
|
||||||
|
expect(data[1].id).toBe(2);
|
||||||
|
} else {
|
||||||
|
throw new Error('Type guard failed');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
30
server/utils/object.ts
Normal file
30
server/utils/object.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* 判断某一值是否为非null对象
|
||||||
|
*
|
||||||
|
* @template T 泛型类型,用于推断目标对象的类型
|
||||||
|
* @param value: 需要判断的值
|
||||||
|
* @returns 如果值是非null对象则返回true,否则返回false
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* if (isObject<Product>(value)) value.id
|
||||||
|
*/
|
||||||
|
export const isObject = <T extends object>(value: unknown): value is T =>
|
||||||
|
typeof value === 'object' && value !== null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断某一值是否为非null对象组成的数组
|
||||||
|
*
|
||||||
|
* @template T 泛型类型,用于推断目标对象的类型
|
||||||
|
* @param value: 需要判断的值
|
||||||
|
* @returns 如果值是非null对象组成的数组则返回true,否则返回false
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const data: unknown = [{ id: 1 }, { id: 2 }];
|
||||||
|
* if (isArrayOfObject)<{ id: number }>(data) {
|
||||||
|
* // TypeScript 知道 data 是 { id: number }[] 类型
|
||||||
|
* console.log(data[0].id);
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
export const isArrayOfObject = <T extends object>(arr: unknown): arr is T[] => {
|
||||||
|
return Array.isArray(arr) && arr.every(isObject<T>);
|
||||||
|
};
|
||||||
76
server/utils/search-converter.test.ts
Normal file
76
server/utils/search-converter.test.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import { describe, expect, test } from 'vitest';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试: converters
|
||||||
|
*/
|
||||||
|
describe('converters', () => {
|
||||||
|
test('convert product item', () => {
|
||||||
|
const item = {
|
||||||
|
id: 1,
|
||||||
|
name: 'Hydraulic Pump',
|
||||||
|
summary: 'High efficiency',
|
||||||
|
description: 'Detailed description',
|
||||||
|
type: 'pump',
|
||||||
|
};
|
||||||
|
const result = converters.products(item);
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 1,
|
||||||
|
type: 'product',
|
||||||
|
title: 'Hydraulic Pump',
|
||||||
|
summary: 'High efficiency',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert solution item', () => {
|
||||||
|
const item = {
|
||||||
|
id: 1,
|
||||||
|
title: 'Solution A',
|
||||||
|
summary: 'Effective solution',
|
||||||
|
content: 'Detailed content',
|
||||||
|
type: 'Type A',
|
||||||
|
};
|
||||||
|
const result = converters.solutions(item);
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 1,
|
||||||
|
type: 'solution',
|
||||||
|
title: 'Solution A',
|
||||||
|
summary: 'Effective solution',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert question item', () => {
|
||||||
|
const item = {
|
||||||
|
id: 1,
|
||||||
|
title: 'How to use product?',
|
||||||
|
content:
|
||||||
|
'This is a detailed explanation of how to use the product effectively.',
|
||||||
|
products: ['Product A'],
|
||||||
|
product_types: ['Type A'],
|
||||||
|
};
|
||||||
|
const result = converters.questions(item);
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 1,
|
||||||
|
title: 'How to use product?',
|
||||||
|
summary:
|
||||||
|
'This is a detailed explanation of how to use the product effectively....',
|
||||||
|
type: 'question',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert product document item', () => {
|
||||||
|
const item = {
|
||||||
|
id: 1,
|
||||||
|
title: 'User Manual',
|
||||||
|
products: ['Product A'],
|
||||||
|
product_types: ['Type A'],
|
||||||
|
fileUUID: 'TEST-UUID',
|
||||||
|
};
|
||||||
|
const result = converters.product_documents(item);
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: 'TEST-UUID',
|
||||||
|
title: 'User Manual',
|
||||||
|
summary: undefined,
|
||||||
|
type: 'document',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
36
server/utils/search-converters.ts
Normal file
36
server/utils/search-converters.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* 各索引对应的转换函数表
|
||||||
|
*/
|
||||||
|
export const converters: {
|
||||||
|
[K in keyof MeiliIndexMap]: (item: MeiliIndexMap[K]) => SearchItemView;
|
||||||
|
} = {
|
||||||
|
products: (item: MeiliIndexMap['products']): SearchItemView => ({
|
||||||
|
id: item.id,
|
||||||
|
type: 'product',
|
||||||
|
title: item.name,
|
||||||
|
summary: item.summary,
|
||||||
|
}),
|
||||||
|
|
||||||
|
solutions: (item: MeiliIndexMap['solutions']): SearchItemView => ({
|
||||||
|
id: item.id,
|
||||||
|
type: 'solution',
|
||||||
|
title: item.title,
|
||||||
|
summary: item.summary,
|
||||||
|
}),
|
||||||
|
|
||||||
|
questions: (item: MeiliIndexMap['questions']): SearchItemView => ({
|
||||||
|
id: item.id,
|
||||||
|
type: 'question',
|
||||||
|
title: item.title,
|
||||||
|
summary: item.content.slice(0, 100) + '...',
|
||||||
|
}),
|
||||||
|
|
||||||
|
product_documents: (
|
||||||
|
item: MeiliIndexMap['product_documents']
|
||||||
|
): SearchItemView => ({
|
||||||
|
id: item.fileUUID || item.id,
|
||||||
|
type: 'document',
|
||||||
|
title: item.title,
|
||||||
|
summary: '',
|
||||||
|
}),
|
||||||
|
};
|
||||||
@ -1,2 +1,3 @@
|
|||||||
export * from './directus';
|
export * from './directus';
|
||||||
export * from './meilisearch';
|
export * from './meilisearch';
|
||||||
|
export * from './views';
|
||||||
|
|||||||
13
shared/types/views/index.ts
Normal file
13
shared/types/views/index.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
export * from './solution-view';
|
||||||
|
export * from './solution-list-view';
|
||||||
|
export * from './product-view';
|
||||||
|
export * from './product-list-view';
|
||||||
|
export * from './product-spec-group-view';
|
||||||
|
export * from './product-document-view';
|
||||||
|
export * from './product-question-view';
|
||||||
|
export * from './document-list-view';
|
||||||
|
export * from './question-list-view';
|
||||||
|
export * from './company-profile-view';
|
||||||
|
export * from './contact-info-view';
|
||||||
|
export * from './homepage-view';
|
||||||
|
export * from './search-item-view';
|
||||||
@ -1,3 +1,7 @@
|
|||||||
|
import type { ProductSpecGroupView } from './product-spec-group-view';
|
||||||
|
import type { ProductQuestionView } from './product-question-view';
|
||||||
|
import type { ProductDocumentView } from './product-document-view';
|
||||||
|
|
||||||
interface ImageView {
|
interface ImageView {
|
||||||
id: number;
|
id: number;
|
||||||
image: string;
|
image: string;
|
||||||
@ -9,5 +9,5 @@ export interface SearchItemView {
|
|||||||
title: string;
|
title: string;
|
||||||
|
|
||||||
/** 条目摘要 **/
|
/** 条目摘要 **/
|
||||||
summary?: string;
|
summary: string;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user