fix(utils): build query withDeleted remove auto detection (#12788)
**What**
Currently, filtering data providing a `deleted_at` value will automatically apply the `withDeleted` flag which in turns remove the default constraint apply to all queries `deleted_at: null`. The problem is that it does not account for the value assign to `deleted_at` leading to inconsistent behaviour depending on the value. e.g filtering with `deleted_at: { $eq: null }` where the expectation is to only filter the non deleted record will end up returning deleted record as well by applying the `withDeleted` filters.
This pr revert this auto detection if favor of the user providing `withDeleted` explicitly, as it is already supported , plus the filters.
Further more, some integration tests demonstrate how to filter deleted records (e.g product) from the api. While the api did not properly support it, this pr adds support to pass with_deleted flags to the query and being handled accordingly to our api support. Validators have been updated and product list end point benefit from it. Also, the list config type was already accepting such value which I have translated to the remote query config.
Also, since the previous pr was adjusting the product types, I ve adjusted them to match the expectation
This commit is contained in:
committed by
GitHub
parent
9d61bb7e71
commit
a833c3c98c
@@ -78,7 +78,7 @@ describe("buildQuery", () => {
|
||||
|
||||
test("should handle withDeleted flag", () => {
|
||||
const filters = { deleted_at: "some-value" }
|
||||
const result = buildQuery(filters)
|
||||
const result = buildQuery(filters, { withDeleted: true })
|
||||
expect(result.options.filters).toHaveProperty(SoftDeletableFilterKey)
|
||||
expect(result.options.filters?.[SoftDeletableFilterKey]).toEqual({
|
||||
withDeleted: true,
|
||||
|
||||
@@ -3,13 +3,6 @@ import { deduplicate, isObject } from "../common"
|
||||
|
||||
import { SoftDeletableFilterKey } from "../dal/mikro-orm/mikro-orm-soft-deletable-filter"
|
||||
|
||||
// Following convention here is fine, we can make it configurable if needed.
|
||||
const DELETED_AT_FIELD_NAME = "deleted_at"
|
||||
|
||||
type FilterFlags = {
|
||||
withDeleted?: boolean
|
||||
}
|
||||
|
||||
export function buildQuery<const T = any>(
|
||||
filters: Record<string, any> = {},
|
||||
config: FindConfig<InferRepositoryReturnType<T>> & {
|
||||
@@ -17,8 +10,7 @@ export function buildQuery<const T = any>(
|
||||
} = {}
|
||||
): Required<DAL.FindOptions<T>> {
|
||||
const where = {} as DAL.FilterQuery<T>
|
||||
const filterFlags: FilterFlags = {}
|
||||
buildWhere(filters, where, filterFlags)
|
||||
buildWhere(filters, where)
|
||||
|
||||
delete config.primaryKeyFields
|
||||
|
||||
@@ -41,7 +33,7 @@ export function buildQuery<const T = any>(
|
||||
>["options"]["orderBy"]
|
||||
}
|
||||
|
||||
if (config.withDeleted || filterFlags.withDeleted) {
|
||||
if (config.withDeleted) {
|
||||
findOptions.filters ??= {}
|
||||
findOptions.filters[SoftDeletableFilterKey] = {
|
||||
withDeleted: true,
|
||||
@@ -63,16 +55,8 @@ export function buildQuery<const T = any>(
|
||||
return { where, options: findOptions } as Required<DAL.FindOptions<T>>
|
||||
}
|
||||
|
||||
function buildWhere(
|
||||
filters: Record<string, any> = {},
|
||||
where = {},
|
||||
flags: FilterFlags = {}
|
||||
) {
|
||||
function buildWhere(filters: Record<string, any> = {}, where = {}) {
|
||||
for (let [prop, value] of Object.entries(filters)) {
|
||||
if (prop === DELETED_AT_FIELD_NAME) {
|
||||
flags.withDeleted = true
|
||||
}
|
||||
|
||||
if (["$or", "$and"].includes(prop)) {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(`Expected array for ${prop} but got ${value}`)
|
||||
@@ -80,7 +64,7 @@ function buildWhere(
|
||||
|
||||
where[prop] = value.map((val) => {
|
||||
const deepWhere = {}
|
||||
buildWhere(val, deepWhere, flags)
|
||||
buildWhere(val, deepWhere)
|
||||
return deepWhere
|
||||
})
|
||||
continue
|
||||
@@ -93,7 +77,7 @@ function buildWhere(
|
||||
|
||||
if (isObject(value)) {
|
||||
where[prop] = {}
|
||||
buildWhere(value, where[prop], flags)
|
||||
buildWhere(value, where[prop])
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user