Skip to content

Commit

Permalink
feat: correction retour
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-goupil committed Jul 26, 2024
1 parent ae7df41 commit a64a1c4
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 82 deletions.
171 changes: 89 additions & 82 deletions front/example-app/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,96 +22,103 @@ const durationFormat: Record<DurationKey, string> = {
export function getProductFilters(
activeFilters: IActiveFilters
): IProductFieldFilterInput[] {
const data: IProductFieldFilterInput[] = []
activeFilters.forEach((activeFilter) => {
const precAcc = data.find(
(value) => Object.keys(value)[0] === activeFilter.filter.field
)
const acc: IProductFieldFilterInput = precAcc || {}
if (activeFilter.filter.type === AggregationType.CATEGORY) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
eq: activeFilter.value,
} as ICategoryTypeDefaultFilterInputType
} else if (activeFilter.filter.type === AggregationType.BOOLEAN) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
eq: true,
} as IStockTypeDefaultFilterInputType
} else if (activeFilter.filter.type === AggregationType.SLIDER) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
lte: Number(activeFilter.value),
} as IEntityIntegerTypeFilterInput
} else if (activeFilter.filter.type === AggregationType.CHECKBOX) {
if (!(activeFilter.filter.field in acc)) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
in: [],
}
}
;(
acc[
activeFilter.filter.field as keyof IProductFieldFilterInput
] as ISelectTypeDefaultFilterInputType
).in.push(activeFilter.value)
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
return activeFilters.reduce<IProductFieldFilterInput[]>(
(data, activeFilter) => {
const field = activeFilter.filter.field as keyof IProductFieldFilterInput
const date = parse(
activeFilter.value,
activeFilter.filter.date_format,
new Date()
)

const incrementString = activeFilter.filter.date_range_interval
const incrementNumber = Number(
incrementString.substring(0, incrementString.length - 1)
)
const incrementType = incrementString.substring(
incrementString.length - 1
) as DurationKey
let acc = data.find((value) => Object.keys(value)[0] === field)

const gt = format(date, activeFilter.filter.date_format)
const lt = format(
add(date, {
[durationFormat[incrementType]]: incrementNumber,
}),
activeFilter.filter.date_format
)

acc.boolFilter._should.push({
[field]: {
gt,
lt,
},
})
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
if (!acc) {
acc = {} as IProductFieldFilterInput
data.push(acc)
}
const field = activeFilter.filter.field as keyof IProductFieldFilterInput
const arrayValue = activeFilter.value.split('-')
const [firstValue, secondValue] = arrayValue

if (arrayValue.length > 1) {
const isFirstValueIsAsterisk = firstValue === '*'
const isSecondValueIsAsterisk = secondValue === '*'
let data: Record<string, number> = {}

if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { lt: Number(secondValue) }
} else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { gte: Number(firstValue), lte: Number(secondValue) }
} else {
data = { gt: Number(firstValue) }
if (activeFilter.filter.type === AggregationType.CATEGORY) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
eq: activeFilter.value,
} as ICategoryTypeDefaultFilterInputType
} else if (activeFilter.filter.type === AggregationType.BOOLEAN) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
eq: true,
} as IStockTypeDefaultFilterInputType
} else if (activeFilter.filter.type === AggregationType.SLIDER) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
lte: Number(activeFilter.value),
} as IEntityIntegerTypeFilterInput
} else if (activeFilter.filter.type === AggregationType.CHECKBOX) {
if (!(activeFilter.filter.field in acc)) {
acc[activeFilter.filter.field as keyof IProductFieldFilterInput] = {
in: [],
}
}
;(
acc[
activeFilter.filter.field as keyof IProductFieldFilterInput
] as ISelectTypeDefaultFilterInputType
).in.push(activeFilter.value)
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM_DATE) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const date = parse(
activeFilter.value,
activeFilter.filter.date_format,
new Date()
)

const incrementString = activeFilter.filter.date_range_interval
const incrementNumber = Number(
incrementString.substring(0, incrementString.length - 1)
)
const incrementType = incrementString.substring(
incrementString.length - 1
) as DurationKey

const gt = format(date, activeFilter.filter.date_format)
const lt = format(
add(date, {
[durationFormat[incrementType]]: incrementNumber,
}),
activeFilter.filter.date_format
)

acc.boolFilter._should.push({
[field]: data,
[field]: {
gt,
lt,
},
})
} else if (activeFilter.filter.type === AggregationType.HISTOGRAM) {
if (!acc.boolFilter?._should) {
acc.boolFilter = { _should: [] }
}
const field = activeFilter.filter
.field as keyof IProductFieldFilterInput
const arrayValue = activeFilter.value.split('-')
const [firstValue, secondValue] = arrayValue

if (arrayValue.length > 1) {
const isFirstValueIsAsterisk = firstValue === '*'
const isSecondValueIsAsterisk = secondValue === '*'
let data: Record<string, number> = {}

if (isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { lt: Number(secondValue) }
} else if (!isFirstValueIsAsterisk && !isSecondValueIsAsterisk) {
data = { gte: Number(firstValue), lte: Number(secondValue) }
} else {
data = { gt: Number(firstValue) }
}

acc.boolFilter._should.push({
[field]: data,
})
}
}
}
if (!precAcc) data.push(acc)
})

return data
return data
},
[]
)
}
68 changes: 68 additions & 0 deletions front/pwa/playwright-report/index.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions front/pwa/test-results/.last-run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "passed",
"failedTests": []
}

0 comments on commit a64a1c4

Please sign in to comment.