From 18afe0b9addb33ec2e3b285651b4eb1ef8065845 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:17:42 +0000 Subject: [PATCH] Fix(medusa, inventory, stock-location, types, utils): Modules sdk build query (#5713) * remove defaults * take 2 * works now * add changeset * pricing module and pricing service list take null updates * update handlers * update product module service with take:null where relevant * no spread * note to self:default offset should be 0, not 15 --- .changeset/gold-balloons-bow.md | 9 +++ .../price-list/prepare-create-price-list.ts | 1 + .../prepare-remove-price-list-prices.ts | 1 + .../prepare-remove-product-prices.ts | 1 + .../prepare-remove-variant-prices.ts | 1 + .../price-list/prepare-update-price-lists.ts | 1 + .../remove-price-set-price-list-prices.ts | 1 + .../update-product-variants-prepare-data.ts | 1 + .../product/update-products-prepare-data.ts | 7 +- .../src/handlers/product/update-products.ts | 1 + .../handlers/product/upsert-variant-prices.ts | 1 + packages/inventory/src/utils/build-query.ts | 4 +- packages/medusa/src/services/pricing.ts | 3 + packages/medusa/src/utils/build-query.ts | 4 +- .../pricing/src/services/pricing-module.ts | 67 ++++++++++++------- .../src/services/product-module-service.ts | 10 ++- .../stock-location/src/utils/build-query.ts | 4 +- packages/types/src/common/common.ts | 19 +++--- packages/utils/src/modules-sdk/build-query.ts | 12 +++- 19 files changed, 103 insertions(+), 45 deletions(-) create mode 100644 .changeset/gold-balloons-bow.md diff --git a/.changeset/gold-balloons-bow.md b/.changeset/gold-balloons-bow.md new file mode 100644 index 0000000000..45070304d9 --- /dev/null +++ b/.changeset/gold-balloons-bow.md @@ -0,0 +1,9 @@ +--- +"@medusajs/stock-location": patch +"@medusajs/inventory": patch +"@medusajs/medusa": patch +"@medusajs/types": patch +"@medusajs/utils": patch +--- + +fix(stock-location, inventory, medusa, types, utils): allow buildQuery to take null as an argument in order to prevent default pagination diff --git a/packages/core-flows/src/handlers/price-list/prepare-create-price-list.ts b/packages/core-flows/src/handlers/price-list/prepare-create-price-list.ts index a1430502fc..c944608b01 100644 --- a/packages/core-flows/src/handlers/price-list/prepare-create-price-list.ts +++ b/packages/core-flows/src/handlers/price-list/prepare-create-price-list.ts @@ -25,6 +25,7 @@ export async function prepareCreatePriceLists({ const variables = { variant_id: variantIds, + take: null, } const query = { diff --git a/packages/core-flows/src/handlers/price-list/prepare-remove-price-list-prices.ts b/packages/core-flows/src/handlers/price-list/prepare-remove-price-list-prices.ts index fe967abb43..649e7d5a22 100644 --- a/packages/core-flows/src/handlers/price-list/prepare-remove-price-list-prices.ts +++ b/packages/core-flows/src/handlers/price-list/prepare-remove-price-list-prices.ts @@ -30,6 +30,7 @@ export async function prepareRemovePriceListPrices({ "price_set_money_amount", "price_set_money_amount.price_list", ], + take: null, } ) diff --git a/packages/core-flows/src/handlers/price-list/prepare-remove-product-prices.ts b/packages/core-flows/src/handlers/price-list/prepare-remove-product-prices.ts index 5a08aa11a8..e3cb9a376f 100644 --- a/packages/core-flows/src/handlers/price-list/prepare-remove-product-prices.ts +++ b/packages/core-flows/src/handlers/price-list/prepare-remove-product-prices.ts @@ -19,6 +19,7 @@ export async function prepareRemoveProductPrices({ const variables = { id: product_ids, + take: null, } const query = { diff --git a/packages/core-flows/src/handlers/price-list/prepare-remove-variant-prices.ts b/packages/core-flows/src/handlers/price-list/prepare-remove-variant-prices.ts index 2da31e1f7d..9580980aad 100644 --- a/packages/core-flows/src/handlers/price-list/prepare-remove-variant-prices.ts +++ b/packages/core-flows/src/handlers/price-list/prepare-remove-variant-prices.ts @@ -19,6 +19,7 @@ export async function prepareRemoveVariantPrices({ const variables = { variant_id: variant_ids, + take: null, } const query = { diff --git a/packages/core-flows/src/handlers/price-list/prepare-update-price-lists.ts b/packages/core-flows/src/handlers/price-list/prepare-update-price-lists.ts index 5831f626f9..a5f0c5d757 100644 --- a/packages/core-flows/src/handlers/price-list/prepare-update-price-lists.ts +++ b/packages/core-flows/src/handlers/price-list/prepare-update-price-lists.ts @@ -28,6 +28,7 @@ export async function prepareUpdatePriceLists({ const variables = { variant_id: variantIds, + take: null, } const query = { diff --git a/packages/core-flows/src/handlers/price-list/remove-price-set-price-list-prices.ts b/packages/core-flows/src/handlers/price-list/remove-price-set-price-list-prices.ts index b5aeaa3b80..6e4a5d0d71 100644 --- a/packages/core-flows/src/handlers/price-list/remove-price-set-price-list-prices.ts +++ b/packages/core-flows/src/handlers/price-list/remove-price-set-price-list-prices.ts @@ -22,6 +22,7 @@ export async function removePriceListPriceSetPrices({ }, { relations: ["money_amount"], + take: null, } ) diff --git a/packages/core-flows/src/handlers/product/update-product-variants-prepare-data.ts b/packages/core-flows/src/handlers/product/update-product-variants-prepare-data.ts index 46860902f0..92bb2eca19 100644 --- a/packages/core-flows/src/handlers/product/update-product-variants-prepare-data.ts +++ b/packages/core-flows/src/handlers/product/update-product-variants-prepare-data.ts @@ -58,6 +58,7 @@ export async function updateProductVariantsPrepareData({ }, { select: ["id", "product_id"], + take: null, } ) diff --git a/packages/core-flows/src/handlers/product/update-products-prepare-data.ts b/packages/core-flows/src/handlers/product/update-products-prepare-data.ts index e7d0c33df2..87c4457f5b 100644 --- a/packages/core-flows/src/handlers/product/update-products-prepare-data.ts +++ b/packages/core-flows/src/handlers/product/update-products-prepare-data.ts @@ -53,15 +53,18 @@ export async function updateProductsPrepareData({ "collection", "sales_channels", ], + take: null, } ) + const productsMap = new Map(products.map((product) => [product.id, product])) + data.products.forEach((productInput) => { const removedChannels: string[] = [] const addedChannels: string[] = [] - const currentProduct = products.find( - (p) => p.id === productInput.id + const currentProduct = productsMap.get( + productInput.id ) as unknown as ProductWithSalesChannelsDTO if (productInput.sales_channels) { diff --git a/packages/core-flows/src/handlers/product/update-products.ts b/packages/core-flows/src/handlers/product/update-products.ts index 5684b0b31f..f8438c1299 100644 --- a/packages/core-flows/src/handlers/product/update-products.ts +++ b/packages/core-flows/src/handlers/product/update-products.ts @@ -35,6 +35,7 @@ export async function updateProducts({ // "profiles", // "sales_channels", ], + take: null, } ) } diff --git a/packages/core-flows/src/handlers/product/upsert-variant-prices.ts b/packages/core-flows/src/handlers/product/upsert-variant-prices.ts index 307a028cb3..d4de20e930 100644 --- a/packages/core-flows/src/handlers/product/upsert-variant-prices.ts +++ b/packages/core-flows/src/handlers/product/upsert-variant-prices.ts @@ -152,6 +152,7 @@ export async function upsertVariantPrices({ }, { select: ["id", "currency_code", "amount", "min_quantity", "max_quantity"], + take: null, } ) diff --git a/packages/inventory/src/utils/build-query.ts b/packages/inventory/src/utils/build-query.ts index ae4c86914f..11e9d84228 100644 --- a/packages/inventory/src/utils/build-query.ts +++ b/packages/inventory/src/utils/build-query.ts @@ -46,11 +46,11 @@ export function buildQuery( } if ("skip" in config) { - ;(query as FindManyOptions).skip = config.skip + ;(query as FindManyOptions).skip = config.skip ?? undefined } if ("take" in config) { - ;(query as FindManyOptions).take = config.take + ;(query as FindManyOptions).take = config.take ?? undefined } if (config.relations) { diff --git a/packages/medusa/src/services/pricing.ts b/packages/medusa/src/services/pricing.ts index 811bd61b3f..841e998639 100644 --- a/packages/medusa/src/services/pricing.ts +++ b/packages/medusa/src/services/pricing.ts @@ -206,6 +206,7 @@ class PricingService extends TransactionBaseService { ) { const variables = { variant_id: variantPriceData.map((pricedata) => pricedata.variantId), + take: null, } const query = { @@ -685,6 +686,7 @@ class PricingService extends TransactionBaseService { ): Promise> { const variables = { variant_id: variantIds, + take: null, } const query = { @@ -713,6 +715,7 @@ class PricingService extends TransactionBaseService { price_set_id: priceSetIds, }, { + take: null, relations: [ "money_amount", "price_list", diff --git a/packages/medusa/src/utils/build-query.ts b/packages/medusa/src/utils/build-query.ts index 42e9aebad3..c1d3b89ddf 100644 --- a/packages/medusa/src/utils/build-query.ts +++ b/packages/medusa/src/utils/build-query.ts @@ -48,11 +48,11 @@ export function buildQuery( } if ("skip" in config) { - ;(query as FindManyOptions).skip = config.skip + ;(query as FindManyOptions).skip = config.skip ?? undefined } if ("take" in config) { - ;(query as FindManyOptions).take = config.take + ;(query as FindManyOptions).take = config.take ?? undefined } if (config.relations) { diff --git a/packages/pricing/src/services/pricing-module.ts b/packages/pricing/src/services/pricing-module.ts index 62b4f11ef4..aaa5066a7c 100644 --- a/packages/pricing/src/services/pricing-module.ts +++ b/packages/pricing/src/services/pricing-module.ts @@ -288,6 +288,7 @@ export default class PricingModuleService< { id: priceSets.filter((p) => !!p).map((p) => p!.id) }, { relations: ["rule_types", "money_amounts", "price_rules"], + take: null, }, sharedContext ) @@ -310,7 +311,7 @@ export default class PricingModuleService< { rule_attribute: ruleAttributes, }, - {}, + { take: null }, sharedContext ) @@ -460,7 +461,7 @@ export default class PricingModuleService< ): Promise { const priceSets = await this.priceSetService_.list( { id: inputs.map((d) => d.priceSetId) }, - { relations: ["rule_types"] }, + { relations: ["rule_types"], take: null }, sharedContext ) @@ -492,7 +493,7 @@ export default class PricingModuleService< .map((d) => d.rules.map((r) => r.attribute)) .flat(), }, - {}, + { take: null }, sharedContext ) @@ -565,7 +566,7 @@ export default class PricingModuleService< return (await this.list( { id: input.map((d) => d.priceSetId) }, - { relations: ["money_amounts"] }, + { relations: ["money_amounts"], take: null }, sharedContext )) as unknown as PricingTypes.PriceSetDTO[] | PricingTypes.PriceSetDTO } @@ -577,7 +578,7 @@ export default class PricingModuleService< ) { const priceSets = await this.list( { id: input.map((d) => d.priceSetId) }, - { relations: ["rule_types"] }, + { relations: ["rule_types"], take: null }, sharedContext ) @@ -683,7 +684,7 @@ export default class PricingModuleService< { id: data.map((d) => d.id), }, - undefined, + { take: null }, sharedContext ) const priceSetIds = priceSets.map((ps) => ps.id) @@ -692,7 +693,7 @@ export default class PricingModuleService< { rule_attribute: data.map((d) => d.rules || []).flat(), }, - undefined, + { take: null }, sharedContext ) const ruleTypeIds = ruleTypes.map((rt) => rt.id) @@ -702,7 +703,7 @@ export default class PricingModuleService< price_set_id: priceSetIds, rule_type_id: ruleTypeIds, }, - undefined, + { take: null }, sharedContext ) @@ -713,6 +714,7 @@ export default class PricingModuleService< }, { select: ["price_set_money_amount"], + take: null, }, sharedContext ) @@ -1389,9 +1391,12 @@ export default class PricingModuleService< .map((priceListData) => Object.keys(priceListData.rules || {})) .flat() - const ruleTypes = await this.listRuleTypes({ - rule_attribute: ruleAttributes, - }) + const ruleTypes = await this.listRuleTypes( + { + rule_attribute: ruleAttributes, + }, + { take: null } + ) const ruleTypeMap: Map = new Map( ruleTypes.map((rt) => [rt.rule_attribute, rt]) @@ -1514,7 +1519,7 @@ export default class PricingModuleService< const existingPriceLists = await this.listPriceLists( { id: priceListIds }, - { relations: ["price_list_rules"] }, + { relations: ["price_list_rules"], take: null }, sharedContext ) @@ -1526,7 +1531,7 @@ export default class PricingModuleService< { id: priceListRuleIds, }, - {}, + { take: null }, sharedContext ) @@ -1541,7 +1546,7 @@ export default class PricingModuleService< { rule_attribute: ruleAttributes, }, - {}, + { take: null }, sharedContext ) @@ -1747,7 +1752,9 @@ export default class PricingModuleService< ): Promise { const priceLists = await this.listPriceLists( { id: data.map((d) => d.priceListId) }, - {}, + { + take: null, + }, sharedContext ) @@ -1807,15 +1814,23 @@ export default class PricingModuleService< ): Promise { const priceLists = await this.priceListService_.list( { id: data.map((d) => d.priceListId) }, - { relations: ["price_list_rules", "price_list_rules.rule_type"] }, + { + relations: ["price_list_rules", "price_list_rules.rule_type"], + take: null, + }, sharedContext ) const priceListMap = new Map(priceLists.map((p) => [p.id, p])) - const ruleTypes = await this.listRuleTypes({ - rule_attribute: data.map((d) => Object.keys(d.rules)).flat(), - }) + const ruleTypes = await this.listRuleTypes( + { + rule_attribute: data.map((d) => Object.keys(d.rules)).flat(), + }, + { + take: null, + } + ) const ruleTypeMap = new Map(ruleTypes.map((rt) => [rt.rule_attribute, rt])) @@ -1874,9 +1889,12 @@ export default class PricingModuleService< const [createdRules, priceListValuesToDelete] = await Promise.all([ this.priceListRuleService_.create(rulesToCreate), - this.priceListRuleValueService_.list({ - price_list_rule_id: ruleIdsToUpdate, - }), + this.priceListRuleValueService_.list( + { + price_list_rule_id: ruleIdsToUpdate, + }, + { take: null } + ), ]) const priceListRuleValuesToCreate: CreatePriceListRuleValueDTO[] = [] @@ -1936,7 +1954,10 @@ export default class PricingModuleService< ): Promise { const priceLists = await this.priceListService_.list( { id: data.map((d) => d.priceListId) }, - { relations: ["price_list_rules", "price_list_rules.rule_type"] }, + { + relations: ["price_list_rules", "price_list_rules.rule_type"], + take: null, + }, sharedContext ) diff --git a/packages/product/src/services/product-module-service.ts b/packages/product/src/services/product-module-service.ts index a74109e1ed..52b0f4dc30 100644 --- a/packages/product/src/services/product-module-service.ts +++ b/packages/product/src/services/product-module-service.ts @@ -255,7 +255,9 @@ export default class ProductModuleService< const productOptions = await this.listOptions( { id: productOptionIds }, - {}, + { + take: null, + }, sharedContext ) @@ -345,7 +347,7 @@ export default class ProductModuleService< const variantIdsToUpdate = data.map(({ id }) => id) const variants = await this.listVariants( { id: variantIdsToUpdate }, - { relations: ["options", "options.option"] }, + { relations: ["options", "options.option"], take: null }, sharedContext ) const variantsMap = new Map( @@ -1053,7 +1055,9 @@ export default class ProductModuleService< const productIds = data.map((pd) => pd.id) const existingProductVariants = await this.productVariantService_.list( { product_id: productIds }, - {}, + { + take: null, + }, sharedContext ) diff --git a/packages/stock-location/src/utils/build-query.ts b/packages/stock-location/src/utils/build-query.ts index f2d32c7ae2..1f92c76f8a 100644 --- a/packages/stock-location/src/utils/build-query.ts +++ b/packages/stock-location/src/utils/build-query.ts @@ -46,11 +46,11 @@ export function buildQuery( } if ("skip" in config) { - ;(query as FindManyOptions).skip = config.skip + ;(query as FindManyOptions).skip = config.skip ?? undefined } if ("take" in config) { - ;(query as FindManyOptions).take = config.take + ;(query as FindManyOptions).take = config.take ?? undefined } if (config.relations) { diff --git a/packages/types/src/common/common.ts b/packages/types/src/common/common.ts index a4bea500d3..99c3dd9367 100644 --- a/packages/types/src/common/common.ts +++ b/packages/types/src/common/common.ts @@ -4,11 +4,11 @@ import { FindOperator, FindOptionsSelect, FindOptionsWhere, - OrderByCondition, -} from "typeorm" + OrderByCondition +} from "typeorm"; -import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder" -import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations" +import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"; +import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations"; /** * Utility type used to remove some optional attributes (coming from K) from a type T @@ -42,8 +42,8 @@ export type Writable = { /** * @interface - * - * An object that is used to configure how an entity is retrieved from the database. It accepts as a typed parameter an `Entity` class, + * + * An object that is used to configure how an entity is retrieved from the database. It accepts as a typed parameter an `Entity` class, * which provides correct typing of field names in its properties. */ export interface FindConfig { @@ -54,17 +54,17 @@ export interface FindConfig { /** * A number indicating the number of records to skip before retrieving the results. */ - skip?: number + skip?: number | null | undefined /** * A number indicating the number of records to return in the result. */ - take?: number + take?: number | null | undefined /** * An array of strings, each being relation names of the entity to retrieve in the result. */ relations?: string[] /** - * An object used to specify how to sort the returned records. Its keys are the names of attributes of the entity, and a key's value can either be `ASC` + * An object used to specify how to sort the returned records. Its keys are the names of attributes of the entity, and a key's value can either be `ASC` * to sort retrieved records in an ascending order, or `DESC` to sort retrieved records in a descending order. */ order?: { [K: string]: "ASC" | "DESC" } @@ -165,6 +165,7 @@ export type DeleteResponse = { } export interface EmptyQueryParams {} + // TODO: Build a tree repository options from this export interface RepositoryTransformOptions {} diff --git a/packages/utils/src/modules-sdk/build-query.ts b/packages/utils/src/modules-sdk/build-query.ts index 06893b964e..36b6b316a9 100644 --- a/packages/utils/src/modules-sdk/build-query.ts +++ b/packages/utils/src/modules-sdk/build-query.ts @@ -13,8 +13,16 @@ export function buildQuery( const findOptions: DAL.OptionsQuery = { populate: deduplicate(config.relations ?? []), fields: config.select as string[], - limit: config.take ?? 15, - offset: config.skip ?? 0, + limit: + (Number.isSafeInteger(config.take) && config.take! >= 0) || + null === config.take + ? config.take ?? undefined + : 15, + offset: + (Number.isSafeInteger(config.skip) && config.skip! >= 0) || + null === config.skip + ? config.skip ?? undefined + : 0, } if (config.order) {