Skip to content

Commit

Permalink
fixed category search
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Jul 3, 2024
1 parent 6770309 commit 1e04c64
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const convertFilterToCategoryMatchingFilter = async (

return {
...filter,
pipelineCategoryIds: allCategoryIds,
/*
pipelineOr: [
...(filter.pipelineOr || []),
{
Expand All @@ -74,5 +76,6 @@ export const convertFilterToCategoryMatchingFilter = async (
},
},
],
*/
}
}
10 changes: 1 addition & 9 deletions src/adverts/repository/category-search/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ describe('convertFilterToCategoryMatchingFilter', () => {
})
).toMatchObject({
search: 'I can haz cheezburger',
pipelineOr: [
{
fields: {
category: {
in: ['c1'],
},
},
},
],
pipelineCategoryIds: ['c1'],
})
})
})
34 changes: 23 additions & 11 deletions src/adverts/repository/mongo/filters/map-search.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import type { Filter } from 'mongodb'
import type { MongoAdvert } from '../types'
import { escapeRegExp } from './filter-utils'
import { combineOr, escapeRegExp } from './filter-utils'

export const mapSearch = (
search?: string
): Filter<MongoAdvert> | null | undefined =>
[search]
.map(s => (s || '').trim())
.filter(s => s)
.map(s => ({
$or: [
const srch = (s: string) =>
s
? [
{ 'advert.title': { $regex: escapeRegExp(s), $options: 'i' } },
{ 'advert.description': { $regex: escapeRegExp(s), $options: 'i' } },
{ 'advert.reference': { $regex: escapeRegExp(s), $options: 'i' } },
],
}))[0] || null
]
: []

const cat = (categoryIds: string[]) =>
categoryIds.length
? [
{
'advert.category': {
$in: categoryIds,
},
},
]
: []

export const mapSearch = (
search?: string,
categoryIds?: string[]
): Filter<MongoAdvert> | null | undefined =>
combineOr(...[...srch((search || '').trim()), ...cat(categoryIds || [])])
5 changes: 4 additions & 1 deletion src/adverts/repository/mongo/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const mapAdvertFilterInputToMongoQuery = (
): Filter<MongoAdvert> =>
combineAnd(
combineOr(
combineAnd(mapSearch(filter?.search), mapFields(filter?.fields)),
combineAnd(
mapSearch(filter?.search, filter?.pipelineCategoryIds),
mapFields(filter?.fields)
),
...(filter?.pipelineOr?.map(({ fields }) => mapFields(fields)) || [])
),
mapRestrictions(user, filter?.restrictions)
Expand Down
1 change: 1 addition & 0 deletions src/adverts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export interface AdvertFilterInput {
paging?: AdvertPagingInput

// decorators/pipelines can attach additional criterias
pipelineCategoryIds?: string[]
pipelineOr?: {
fields: AdvertFieldsFilterInput
}[]
Expand Down

0 comments on commit 1e04c64

Please sign in to comment.