Fixed Product serializer lookup of original include

no-issue

The Frame object colocates the query, params & options data under a
single options property, this is not the case for the "original" data
however, which means that we need to explicitly check individual
"original" properties. We do not expect the `include` option to be used
as a param so that has been left out for now.

This reverts commit ea9a83d444.
This commit is contained in:
Fabien O'Carroll 2021-06-08 10:53:19 +01:00
parent a028f7e706
commit 09a817da16

View File

@ -20,14 +20,13 @@ module.exports = {
* @returns {{products: SerializedProduct[], meta: PageMeta}} * @returns {{products: SerializedProduct[], meta: PageMeta}}
*/ */
function paginatedProducts(page, _apiConfig, frame) { function paginatedProducts(page, _apiConfig, frame) {
const requestedIncludes = frame.original && frame.original.options && frame.original.options.include || []; const requestedQueryIncludes = frame.original && frame.original.query && frame.original.query.include && frame.original.query.include.split(',') || [];
const defaultIncludes = ['monthly_price', 'yearly_price']; const requestedOptionsIncludes = frame.original && frame.original.options && frame.original.options.include || [];
return { return {
products: page.data.map((model) => { products: page.data.map((model) => {
return cleanIncludes( return cleanIncludes(
allowedIncludes, allowedIncludes,
requestedIncludes, requestedQueryIncludes || requestedOptionsIncludes,
defaultIncludes,
serializeProduct(model, frame.options, frame.apiType) serializeProduct(model, frame.options, frame.apiType)
); );
}), }),
@ -43,14 +42,13 @@ function paginatedProducts(page, _apiConfig, frame) {
* @returns {{products: SerializedProduct[]}} * @returns {{products: SerializedProduct[]}}
*/ */
function singleProduct(model, _apiConfig, frame) { function singleProduct(model, _apiConfig, frame) {
const requestedIncludes = frame.original && frame.original.options && frame.original.options.include || []; const requestedQueryIncludes = frame.original && frame.original.query && frame.original.query.include && frame.original.query.include.split(',') || [];
const defaultIncludes = ['monthly_price', 'yearly_price']; const requestedOptionsIncludes = frame.original && frame.original.options && frame.original.options.include || [];
return { return {
products: [ products: [
cleanIncludes( cleanIncludes(
allowedIncludes, allowedIncludes,
requestedIncludes, requestedQueryIncludes || requestedOptionsIncludes,
defaultIncludes,
serializeProduct(model, frame.options, frame.apiType) serializeProduct(model, frame.options, frame.apiType)
) )
] ]
@ -123,17 +121,15 @@ function serializeStripePrice(data, hideStripeData) {
* *
* @returns {Data} * @returns {Data}
*/ */
function cleanIncludes(allowed, requested, defaults, data) { function cleanIncludes(allowed, requested, data) {
const cleaned = { const cleaned = {
...data ...data
}; };
for (const include of allowed) { for (const include of allowed) {
if (!requested.includes(include) && !defaults.includes(include)) { if (!requested.includes(include)) {
delete cleaned[include]; delete cleaned[include];
} }
} }
return cleaned; return cleaned;
} }