diff --git a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.spec.ts b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.spec.ts index 07eaf325ad..d3ae010d21 100644 --- a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.spec.ts +++ b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.spec.ts @@ -1,6 +1,8 @@ import CozyClient from 'cozy-client' +import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types' -import { normalizeSearchResult } from './normalizeSearchResult' +import { cleanFilePath, normalizeSearchResult } from './normalizeSearchResult' +import { FILES_DOCTYPE, TYPE_FILE } from '../consts' import { RawSearchResult } from '../types' const fakeFlatDomainClient = { @@ -341,3 +343,46 @@ describe('Should normalize unknown doctypes', () => { }) }) }) + +describe('cleanFilePath', () => { + it('should return the document unchanged if it is not an IOCozyFile', () => { + const doc = { fullname: 'name' } as IOCozyContact + expect(cleanFilePath(doc)).toEqual(doc) + }) + + it('should return the document unchanged if path is undefined', () => { + const doc = { _type: FILES_DOCTYPE, name: 'name' } as IOCozyFile + expect(cleanFilePath(doc)).toEqual(doc) + }) + + it('should return the document unchanged if not a file', () => { + const doc = { + _type: FILES_DOCTYPE, + name: 'name', + type: TYPE_FILE + } as IOCozyFile + expect(cleanFilePath(doc)).toEqual(doc) + }) + + it('should remove name from path if path ends with name', () => { + const doc = { + _type: FILES_DOCTYPE, + type: TYPE_FILE, + path: '/the/path/myname', + name: 'myname' + } as IOCozyFile + const expected = { ...doc, path: '/the/path' } + + expect(cleanFilePath(doc)).toEqual(expected) + }) + + it('should return the document unchanged if path does not end with name', () => { + const doc = { + _type: FILES_DOCTYPE, + type: TYPE_FILE, + path: '/the/path/othername', + name: 'name' + } as IOCozyFile + expect(cleanFilePath(doc)).toEqual(doc) + }) +}) diff --git a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts index abdfce2d55..6432caef41 100644 --- a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts +++ b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts @@ -1,7 +1,7 @@ import CozyClient, { generateWebLink, models } from 'cozy-client' import { IOCozyContact } from 'cozy-client/types/types' -import { APPS_DOCTYPE, TYPE_DIRECTORY } from '../consts' +import { APPS_DOCTYPE, TYPE_DIRECTORY, TYPE_FILE } from '../consts' import { CozyDoc, RawSearchResult, @@ -16,7 +16,7 @@ export const normalizeSearchResult = ( searchResults: RawSearchResult, query: string ): SearchResult => { - const doc = searchResults.doc + const doc = cleanFilePath(searchResults.doc) const url = buildOpenURL(client, doc) const slug = getSearchResultSlug(doc) const title = getSearchResultTitle(doc) @@ -30,6 +30,23 @@ export const normalizeSearchResult = ( return normalizedRes } +export const cleanFilePath = (doc: CozyDoc): CozyDoc => { + if (!isIOCozyFile(doc)) { + return doc + } + const { path, name, type } = doc + if (!path || type !== TYPE_FILE) { + return doc + } + let newPath = path + if (path.endsWith(`/${name}`)) { + // Remove the name from the path, which is added at indexing time to search on it + newPath = path.slice(0, -name.length - 1) + } + + return { ...doc, path: newPath } +} + const getSearchResultTitle = (doc: CozyDoc): string | null => { if (isIOCozyFile(doc)) { return doc.name