feat: 为文档库/相关问题页面添加类型筛选功能 #92
@ -10,6 +10,13 @@ query GetDocumentList($locale: String!) {
|
||||
id
|
||||
title
|
||||
}
|
||||
type {
|
||||
id
|
||||
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
products {
|
||||
id
|
||||
products_id {
|
||||
|
||||
@ -1,6 +1,51 @@
|
||||
import { describe, test, expect } from 'vitest';
|
||||
import { toProductDocumentView, toDocumentListView } from './documentMapper';
|
||||
import {
|
||||
toProductDocumentView,
|
||||
toDocumentListView,
|
||||
toDocumentTypeView,
|
||||
} from './documentMapper';
|
||||
|
||||
/**
|
||||
* 单元测试: toDocumentTypeView
|
||||
*/
|
||||
describe('toDocumentTypeView', () => {
|
||||
const baseData: DocumentType = {
|
||||
id: 1,
|
||||
translations: [{ id: 1, name: 'Type Name' }],
|
||||
};
|
||||
|
||||
test('convert raw data to DocumentTypeView correctly', () => {
|
||||
const rawData: DocumentType = {
|
||||
...baseData,
|
||||
};
|
||||
|
||||
expect(toDocumentTypeView(rawData)).toEqual({
|
||||
id: '1',
|
||||
name: 'Type Name',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert raw data with missing translations', () => {
|
||||
const rawData: DocumentType = {
|
||||
...baseData,
|
||||
translations: [],
|
||||
};
|
||||
|
||||
expect(toDocumentTypeView(rawData)).toEqual({
|
||||
id: '1',
|
||||
name: '',
|
||||
});
|
||||
});
|
||||
|
||||
test('convert null input to default DocumentTypeView', () => {
|
||||
const rawData: DocumentType | null = null;
|
||||
|
||||
expect(toDocumentTypeView(rawData)).toEqual({
|
||||
id: '-1',
|
||||
name: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
/**
|
||||
* 单元测试: toProductDocumentView
|
||||
*/
|
||||
@ -65,6 +110,15 @@ describe('toProductDocumentView', () => {
|
||||
describe('toDocumentListView', () => {
|
||||
const baseData: ProductDocument = {
|
||||
id: 1,
|
||||
type: {
|
||||
id: 1,
|
||||
translations: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Type A',
|
||||
},
|
||||
],
|
||||
},
|
||||
file: {
|
||||
id: 'rand-om__-uuid-1234',
|
||||
filename_download: 'document.pdf',
|
||||
@ -94,6 +148,10 @@ describe('toDocumentListView', () => {
|
||||
title: 'Document Title',
|
||||
url: '/api/assets/rand-om__-uuid-1234',
|
||||
size: 2048,
|
||||
type: {
|
||||
id: '1',
|
||||
name: 'Type A',
|
||||
},
|
||||
products: [
|
||||
{
|
||||
id: '1',
|
||||
|
||||
@ -1,5 +1,31 @@
|
||||
import { isObject } from '../../server/utils/object';
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 DocumentType 数据转换为 DocumentTypeView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 DocumentType 数据
|
||||
* @returns 转换后的 DocumentTypeView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toDocumentTypeView(rawDocumentType);
|
||||
*/
|
||||
export function toDocumentTypeView(
|
||||
raw: DocumentType | string | null
|
||||
): DocumentTypeView {
|
||||
if (typeof raw === 'string' || raw === null) {
|
||||
return {
|
||||
id: '-1',
|
||||
name: '',
|
||||
} satisfies DocumentTypeView;
|
||||
}
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
return {
|
||||
id: raw.id.toString(),
|
||||
name: trans?.name ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Directus 返回的 Document 数据转换为 ProductDocumentView 视图模型
|
||||
*
|
||||
@ -40,6 +66,8 @@ export function toProductDocumentView(
|
||||
export function toDocumentListView(raw: ProductDocument): DocumentListView {
|
||||
const trans = raw.translations?.[0];
|
||||
|
||||
const type = toDocumentTypeView(raw.type ?? null);
|
||||
|
||||
const file = isObject<DirectusFile>(raw.file) ? raw.file : undefined;
|
||||
const fileId = file?.id ?? '';
|
||||
|
||||
@ -73,6 +101,7 @@ export function toDocumentListView(raw: ProductDocument): DocumentListView {
|
||||
title: trans?.title ?? '',
|
||||
url: url,
|
||||
size: file?.filesize ?? 0,
|
||||
type: type,
|
||||
products: related_products,
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,3 +1,14 @@
|
||||
/**
|
||||
* 文档类型视图模型
|
||||
* 用于在文档库中提供类型筛选功能
|
||||
*/
|
||||
export interface DocumentTypeView {
|
||||
/** 唯一标识符 **/
|
||||
id: string;
|
||||
/** 类型名 **/
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文档关联产品类型模型
|
||||
* 用于在文档库中提供产品筛选功能
|
||||
@ -40,6 +51,9 @@ export interface DocumentListView {
|
||||
/** 文档链接 **/
|
||||
url: string;
|
||||
|
||||
/** 文档类型 **/
|
||||
type: DocumentTypeView;
|
||||
|
||||
/** 相关产品 **/
|
||||
products: DocumentListProduct[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user