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
@@ -141,6 +141,7 @@ export interface MedusaRequest<
|
||||
queryConfig: {
|
||||
fields: string[]
|
||||
pagination: { order?: Record<string, string>; skip: number; take?: number }
|
||||
withDeleted?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -101,7 +101,13 @@ export function prepareListQuery<T extends RequestQueryFields, TEntity>(
|
||||
defaultLimit = 50,
|
||||
isList,
|
||||
} = queryConfig
|
||||
const { order, fields, limit = defaultLimit, offset = 0 } = validated
|
||||
const {
|
||||
order,
|
||||
fields,
|
||||
limit = defaultLimit,
|
||||
offset = 0,
|
||||
with_deleted,
|
||||
} = validated
|
||||
|
||||
// e.g *product.variants meaning that we want all fields from the product.variants
|
||||
// in that case it wont be part of the select but it will be part of the relations.
|
||||
@@ -220,6 +226,7 @@ export function prepareListQuery<T extends RequestQueryFields, TEntity>(
|
||||
skip: offset,
|
||||
take: limit,
|
||||
order: finalOrder,
|
||||
withDeleted: with_deleted,
|
||||
},
|
||||
remoteQueryConfig: {
|
||||
// Add starFields that are relations only on which we want all properties with a dedicated format to the remote query
|
||||
@@ -234,6 +241,7 @@ export function prepareListQuery<T extends RequestQueryFields, TEntity>(
|
||||
order: finalOrder,
|
||||
}
|
||||
: {},
|
||||
withDeleted: with_deleted,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -255,6 +263,7 @@ export function prepareRetrieveQuery<T extends RequestQueryFields, TEntity>(
|
||||
remoteQueryConfig: {
|
||||
fields: remoteQueryConfig.fields,
|
||||
pagination: {},
|
||||
withDeleted: remoteQueryConfig.withDeleted,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ export const refetchEntities = async (
|
||||
idOrFilter: string | object,
|
||||
scope: MedusaContainer,
|
||||
fields: string[],
|
||||
pagination?: MedusaRequest["queryConfig"]["pagination"]
|
||||
pagination?: MedusaRequest["queryConfig"]["pagination"],
|
||||
withDeleted?: boolean
|
||||
) => {
|
||||
const remoteQuery = scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const filters = isString(idOrFilter) ? { id: idOrFilter } : idOrFilter
|
||||
@@ -25,7 +26,7 @@ export const refetchEntities = async (
|
||||
delete filters.context
|
||||
}
|
||||
|
||||
const variables = { filters, ...context, ...pagination }
|
||||
const variables = { filters, ...context, ...pagination, withDeleted }
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint,
|
||||
|
||||
@@ -77,9 +77,10 @@ export function validateAndTransformQuery<TEntity extends BaseEntity>(
|
||||
}
|
||||
|
||||
delete req.allowed
|
||||
const query = normalizeQuery(req)
|
||||
const query = normalizeQuery(req) as Record<string, any>
|
||||
|
||||
const validated = await zodValidator(zodSchema, query)
|
||||
|
||||
const cnf = queryConfig.isList
|
||||
? prepareListQuery(validated, { ...queryConfig, allowed, restricted })
|
||||
: prepareRetrieveQuery(validated, {
|
||||
@@ -88,7 +89,8 @@ export function validateAndTransformQuery<TEntity extends BaseEntity>(
|
||||
restricted,
|
||||
})
|
||||
|
||||
req.validatedQuery = validated
|
||||
const { with_deleted, ...validatedQueryFilters } = validated
|
||||
req.validatedQuery = validatedQueryFilters
|
||||
req.filterableFields = getFilterableFields(req.validatedQuery)
|
||||
req.queryConfig = cnf.remoteQueryConfig as any
|
||||
req.remoteQueryConfig = req.queryConfig
|
||||
|
||||
Reference in New Issue
Block a user