feat: add hasMany flag to enforce in app link uniqueness (#12039)

* feat: add createMultiple flag to enforce inApp link uniqueness

* changes

* mocks

* default

* many to many

---------

Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com>
This commit is contained in:
Harminder Virk
2025-04-02 14:16:51 +05:30
committed by GitHub
parent f441362f4a
commit d3e725a907
25 changed files with 316 additions and 70 deletions

View File

@@ -46,7 +46,7 @@ medusaIntegrationTestRunner({
entity: "Currency",
primaryKey: "code",
foreignKey: "currency_code",
isList: true,
hasMany: false,
alias: "currency",
args: {
methodSuffix: "Currencies",
@@ -59,7 +59,7 @@ medusaIntegrationTestRunner({
primaryKey: "id",
foreignKey: "region_id",
alias: "region",
isList: false,
hasMany: false,
args: {
methodSuffix: "Regions",
},
@@ -90,9 +90,9 @@ medusaIntegrationTestRunner({
serviceName: "region",
entity: "Region",
fieldAlias: {
currencies: {
currency: {
path: "currency_link.currency",
isList: true,
isList: false,
forwardArgumentsOnPath: ["currency_link.currency"],
},
},
@@ -102,7 +102,7 @@ medusaIntegrationTestRunner({
primaryKey: "region_id",
foreignKey: "id",
alias: "currency_link",
isList: true,
isList: false,
},
},
],
@@ -147,7 +147,7 @@ medusaIntegrationTestRunner({
entity: "ProductVariant",
primaryKey: "id",
foreignKey: "product_variant_id",
isList: true,
hasMany: false,
alias: "product_variant",
args: {
methodSuffix: "ProductVariants",
@@ -159,7 +159,7 @@ medusaIntegrationTestRunner({
entity: "Region",
primaryKey: "id",
foreignKey: "region_id",
isList: false,
hasMany: false,
alias: "region",
args: {
methodSuffix: "Regions",
@@ -191,9 +191,9 @@ medusaIntegrationTestRunner({
serviceName: "region",
entity: "Region",
fieldAlias: {
product_variants: {
product_variant: {
path: "product_variant_link.product_variant",
isList: true,
isList: false,
forwardArgumentsOnPath: [
"product_variant_link.product_variant",
],
@@ -205,7 +205,7 @@ medusaIntegrationTestRunner({
primaryKey: "region_id",
foreignKey: "id",
alias: "product_variant_link",
isList: true,
isList: false,
},
},
],
@@ -253,7 +253,7 @@ medusaIntegrationTestRunner({
entity: "Currency",
primaryKey: "code",
foreignKey: "currency_code",
isList: true,
hasMany: false,
alias: "currency",
args: {
methodSuffix: "Currencies",
@@ -265,7 +265,7 @@ medusaIntegrationTestRunner({
entity: "Region",
primaryKey: "id",
foreignKey: "region_id",
isList: false,
hasMany: false,
alias: "region",
args: {
methodSuffix: "Regions",
@@ -297,9 +297,9 @@ medusaIntegrationTestRunner({
serviceName: "region",
entity: "Region",
fieldAlias: {
currencies: {
currency: {
path: "currency_link.currency",
isList: true,
isList: false,
forwardArgumentsOnPath: ["currency_link.currency"],
},
},
@@ -309,7 +309,7 @@ medusaIntegrationTestRunner({
primaryKey: "region_id",
foreignKey: "id",
alias: "currency_link",
isList: true,
isList: false,
},
},
],
@@ -353,7 +353,7 @@ medusaIntegrationTestRunner({
entity: "Currency",
primaryKey: "code",
foreignKey: "currency_code",
isList: true,
hasMany: false,
alias: "currency",
args: {
methodSuffix: "Currencies",
@@ -365,7 +365,113 @@ medusaIntegrationTestRunner({
entity: "Region",
primaryKey: "id",
foreignKey: "region_id",
isList: true,
hasMany: true,
alias: "region",
args: {
methodSuffix: "Regions",
},
deleteCascade: false,
},
],
extends: [
{
serviceName: "currency",
entity: "Currency",
fieldAlias: {
regions: {
path: "region_link.region",
isList: true,
forwardArgumentsOnPath: ["region_link.region"],
},
},
relationship: {
serviceName: "CurrencyCurrencyRegionRegionLink",
entity: "LinkCurrencyCurrencyRegionRegion",
primaryKey: "currency_code",
foreignKey: "code",
alias: "region_link",
isList: true,
},
},
{
serviceName: "region",
entity: "Region",
fieldAlias: {
currency: {
path: "currency_link.currency",
isList: false,
forwardArgumentsOnPath: ["currency_link.currency"],
},
},
relationship: {
serviceName: "CurrencyCurrencyRegionRegionLink",
entity: "LinkCurrencyCurrencyRegionRegion",
primaryKey: "region_id",
foreignKey: "id",
alias: "currency_link",
isList: false,
},
},
],
})
})
it("should generate a proper link with both sides using explicit isList=true", async () => {
const currencyLinks = CurrencyModule.linkable
const regionLinks = RegionModule.linkable
const link = defineLink(
{
linkable: currencyLinks.currency,
isList: true,
},
{
linkable: regionLinks.region,
isList: true,
}
)
const linkDefinition = MedusaModule.getCustomLinks()
.map((linkDefinition: any) => {
const definition = linkDefinition(
MedusaModule.getAllJoinerConfigs()
)
return definition.serviceName === link.serviceName && definition
})
.filter(Boolean)[0]
expect(link.serviceName).toEqual("CurrencyCurrencyRegionRegionLink")
expect(linkDefinition).toEqual({
serviceName: "CurrencyCurrencyRegionRegionLink",
isLink: true,
alias: [
{
name: ["currency_region"],
args: {
entity: "LinkCurrencyCurrencyRegionRegion",
},
},
],
primaryKeys: ["id", "currency_code", "region_id"],
relationships: [
{
serviceName: "currency",
entity: "Currency",
primaryKey: "code",
foreignKey: "currency_code",
hasMany: true,
alias: "currency",
args: {
methodSuffix: "Currencies",
},
deleteCascade: false,
},
{
serviceName: "region",
entity: "Region",
primaryKey: "id",
foreignKey: "region_id",
hasMany: true,
alias: "region",
args: {
methodSuffix: "Regions",
@@ -415,6 +521,112 @@ medusaIntegrationTestRunner({
],
})
})
it("should generate a proper link with both sides using explicit isList=false", async () => {
const currencyLinks = CurrencyModule.linkable
const regionLinks = RegionModule.linkable
const link = defineLink(
{
linkable: currencyLinks.currency,
isList: false,
},
{
linkable: regionLinks.region,
isList: false,
}
)
const linkDefinition = MedusaModule.getCustomLinks()
.map((linkDefinition: any) => {
const definition = linkDefinition(
MedusaModule.getAllJoinerConfigs()
)
return definition.serviceName === link.serviceName && definition
})
.filter(Boolean)[0]
expect(link.serviceName).toEqual("CurrencyCurrencyRegionRegionLink")
expect(linkDefinition).toEqual({
serviceName: "CurrencyCurrencyRegionRegionLink",
isLink: true,
alias: [
{
name: ["currency_region"],
args: {
entity: "LinkCurrencyCurrencyRegionRegion",
},
},
],
primaryKeys: ["id", "currency_code", "region_id"],
relationships: [
{
serviceName: "currency",
entity: "Currency",
primaryKey: "code",
foreignKey: "currency_code",
hasMany: false,
alias: "currency",
args: {
methodSuffix: "Currencies",
},
deleteCascade: false,
},
{
serviceName: "region",
entity: "Region",
primaryKey: "id",
foreignKey: "region_id",
hasMany: false,
alias: "region",
args: {
methodSuffix: "Regions",
},
deleteCascade: false,
},
],
extends: [
{
serviceName: "currency",
entity: "Currency",
fieldAlias: {
region: {
path: "region_link.region",
isList: false,
forwardArgumentsOnPath: ["region_link.region"],
},
},
relationship: {
serviceName: "CurrencyCurrencyRegionRegionLink",
entity: "LinkCurrencyCurrencyRegionRegion",
primaryKey: "currency_code",
foreignKey: "code",
alias: "region_link",
isList: false,
},
},
{
serviceName: "region",
entity: "Region",
fieldAlias: {
currency: {
path: "currency_link.currency",
isList: false,
forwardArgumentsOnPath: ["currency_link.currency"],
},
},
relationship: {
serviceName: "CurrencyCurrencyRegionRegionLink",
entity: "LinkCurrencyCurrencyRegionRegion",
primaryKey: "region_id",
foreignKey: "id",
alias: "currency_link",
isList: false,
},
},
],
})
})
})
},
})

View File

@@ -21,5 +21,6 @@ export const InventoryModule = {
},
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}

View File

@@ -65,5 +65,6 @@ export const InventoryStockLocationLink = {
foreignKeyData?: string
) => {}
),
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}

View File

@@ -71,5 +71,6 @@ export const ProductInventoryLinkModule = {
foreignKeyData?: string
) => {}
),
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}

View File

@@ -18,5 +18,6 @@ export const ProductModule = {
alias: [],
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}

View File

@@ -18,5 +18,6 @@ export const StockLocationModule = {
alias: [],
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}

View File

@@ -389,7 +389,7 @@ export class Link {
{
linksToCreate: [string | string[], string, Record<string, unknown>?][]
linksToValidateForUniqueness: {
filters: { [key: string]: string }[]
filters: { [key: string]: any }[]
services: string[]
}
}
@@ -418,55 +418,51 @@ export class Link {
*/
linksToValidateForUniqueness: {
filters: [],
services: [],
services: relationships?.map((r) => r.serviceName) ?? [],
},
})
}
relationships?.forEach((relationship) => {
const linksToValidateForUniqueness = serviceLinks.get(
service.__definition.key
)!.linksToValidateForUniqueness!
/**
* When isList is set on false on the relationship, then it means
* we have a one-to-one or many-to-one relationship with the
* other side and we have limit duplicate entries from other
* entity. For example:
*
* - A brand has a many to one relationship with a product.
* - A product can have only one brand. Aka (brand.isList = false)
* - A brand can have multiple products. Aka (products.isList = true)
*
* A result of this, we have to ensure that a product_id can only appear
* once in the pivot table that is used for tracking "brand<>products"
* relationship.
*/
const linksToValidateForUniqueness = serviceLinks.get(
service.__definition.key
)!.linksToValidateForUniqueness!
linksToValidateForUniqueness.services.push(relationship.serviceName)
/**
* When isList is set on false on the relationship, then it means
* we have a one-to-one or many-to-one relationship with the
* other side and we have limit duplicate entries from other
* entity. For example:
*
* - A brand has a many to one relationship with a product.
* - A product can have only one brand. Aka (brand.isList = false)
* - A brand can have multiple products. Aka (products.isList = true)
*
* A result of this, we have to ensure that a product_id can only appear
* once in the pivot table that is used for tracking "brand<>products"
* relationship.
*/
if (relationship.isList === false) {
const otherSide = relationships.find(
(other) => other.foreignKey !== relationship.foreignKey
)
if (!otherSide) {
return
}
if (moduleBKey === otherSide.foreignKey) {
linksToValidateForUniqueness.filters.push({
[otherSide.foreignKey]: link[moduleB][moduleBKey],
})
} else {
primaryKeys.forEach((pk) => {
if (pk === otherSide.foreignKey) {
linksToValidateForUniqueness.filters.push({
[otherSide.foreignKey]: link[moduleA][pk],
})
}
})
}
const modA = relationships?.[0]!
const modB = relationships?.[1]!
if (!modA.hasMany || !modB.hasMany) {
if (!modA.hasMany && !modB.hasMany) {
linksToValidateForUniqueness.filters.push({
$or: [
{ [modA.foreignKey]: link[moduleA][modA.foreignKey] },
{ [modB.foreignKey]: link[moduleB][modB.foreignKey] },
],
})
} else if (!modA.hasMany) {
linksToValidateForUniqueness.filters.push({
[modA.foreignKey]: { $ne: link[moduleA][modA.foreignKey] },
[modB.foreignKey]: link[moduleB][modB.foreignKey],
})
} else if (!modB.hasMany) {
linksToValidateForUniqueness.filters.push({
[modB.foreignKey]: { $ne: link[moduleB][modB.foreignKey] },
[modA.foreignKey]: link[moduleA][modA.foreignKey],
})
}
})
}
const pkValue =
primaryKeys.length === 1

View File

@@ -209,7 +209,7 @@ export type ModuleJoinerConfig = Omit<
isList?: boolean
}
> // alias for deeper nested relationships (e.g. { 'price': 'prices.calculated_price_set.amount' })
relationship: ModuleJoinerRelationship
relationship: Omit<ModuleJoinerRelationship, "hasMany">
}[]
serviceName?: string
primaryKeys?: string[]
@@ -248,6 +248,11 @@ export declare type ModuleJoinerRelationship = JoinerRelationship & {
* If true, the link joiner will cascade deleting the relationship
*/
deleteCascade?: boolean
/**
* Allow multiple relationships to exist for this
* entity
*/
hasMany?: boolean
}
export type ModuleExports<T = Constructor<any>> = {

View File

@@ -85,6 +85,7 @@ type ModuleLinkableKeyConfig = {
deleteCascade?: boolean
primaryKey: string
alias: string
hasMany?: boolean
shortcut?: Shortcut | Shortcut[]
}
@@ -125,8 +126,7 @@ function buildFieldAlias(fieldAliases?: Shortcut | Shortcut[]) {
}
function prepareServiceConfig(
input: DefineLinkInputSource | DefineReadOnlyLinkInputSource,
defaultOptions?: { isList?: boolean }
input: DefineLinkInputSource | DefineReadOnlyLinkInputSource
) {
let serviceConfig = {} as ModuleLinkableKeyConfig
@@ -138,7 +138,8 @@ function prepareServiceConfig(
alias: source.alias ?? camelToSnakeCase(source.field ?? ""),
field: input.field ?? source.field,
primaryKey: source.primaryKey,
isList: defaultOptions?.isList ?? false,
isList: false,
hasMany: false,
deleteCascade: false,
module: source.serviceName,
entity: source.entity,
@@ -148,12 +149,15 @@ function prepareServiceConfig(
? input.linkable.toJSON()
: input.linkable
const hasMany = !!input.isList
serviceConfig = {
key: source.linkable,
alias: source.alias ?? camelToSnakeCase(source.field ?? ""),
field: input.field ?? source.field,
primaryKey: source.primaryKey,
isList: input.isList ?? defaultOptions?.isList ?? false,
isList: input.isList ?? false,
hasMany,
deleteCascade: input.deleteCascade ?? false,
module: source.serviceName,
entity: source.entity,
@@ -184,8 +188,8 @@ export function defineLink(
rightService: DefineLinkInputSource | DefineReadOnlyLinkInputSource,
linkServiceOptions?: ExtraOptions | ReadOnlyExtraOptions
): DefineLinkExport {
const serviceAObj = prepareServiceConfig(leftService, { isList: true })
const serviceBObj = prepareServiceConfig(rightService, { isList: false })
const serviceAObj = prepareServiceConfig(leftService)
const serviceBObj = prepareServiceConfig(rightService)
if (linkServiceOptions?.readOnly) {
return defineReadOnlyLink(
@@ -374,7 +378,7 @@ ${serviceBObj.module}: {
methodSuffix: serviceAMethodSuffix,
},
deleteCascade: serviceAObj.deleteCascade,
isList: serviceAObj.isList,
hasMany: serviceAObj.hasMany,
},
{
serviceName: serviceBObj.module,
@@ -386,7 +390,7 @@ ${serviceBObj.module}: {
methodSuffix: serviceBMethodSuffix,
},
deleteCascade: serviceBObj.deleteCascade,
isList: serviceBObj.isList,
hasMany: serviceBObj.hasMany,
},
],
extends: [

View File

@@ -25,6 +25,7 @@ export const CartPromotion: ModuleJoinerConfig = {
args: {
methodSuffix: "Carts",
},
hasMany: true,
},
{
serviceName: Modules.PROMOTION,
@@ -35,6 +36,7 @@ export const CartPromotion: ModuleJoinerConfig = {
args: {
methodSuffix: "Promotions",
},
hasMany: true,
},
],
extends: [

View File

@@ -35,6 +35,7 @@ export const CustomerAccountHolder: ModuleJoinerConfig = {
args: {
methodSuffix: "AccountHolders",
},
hasMany: true,
},
],
extends: [

View File

@@ -23,6 +23,7 @@ export const LocationFulfillmentProvider: ModuleJoinerConfig = {
foreignKey: "stock_location_id",
alias: "location",
args: { methodSuffix: "StockLocations" },
hasMany: true,
},
{
serviceName: Modules.FULFILLMENT,
@@ -31,6 +32,7 @@ export const LocationFulfillmentProvider: ModuleJoinerConfig = {
foreignKey: "fulfillment_provider_id",
alias: "fulfillment_provider",
args: { methodSuffix: "FulfillmentProviders" },
hasMany: true,
},
],
extends: [

View File

@@ -36,6 +36,7 @@ export const LocationFulfillmentSet: ModuleJoinerConfig = {
methodSuffix: "FulfillmentSets",
},
deleteCascade: true,
hasMany: true,
},
],
extends: [

View File

@@ -38,6 +38,7 @@ export const OrderClaimPaymentCollection: ModuleJoinerConfig = {
args: {
methodSuffix: "PaymentCollections",
},
hasMany: true,
},
],
extends: [

View File

@@ -38,6 +38,7 @@ export const OrderExchangePaymentCollection: ModuleJoinerConfig = {
args: {
methodSuffix: "PaymentCollections",
},
hasMany: true,
},
],
extends: [

View File

@@ -35,6 +35,7 @@ export const OrderFulfillment: ModuleJoinerConfig = {
args: {
methodSuffix: "Fulfillments",
},
hasMany: true,
},
],
extends: [

View File

@@ -36,6 +36,7 @@ export const OrderPaymentCollection: ModuleJoinerConfig = {
methodSuffix: "PaymentCollections",
},
deleteCascade: true,
hasMany: true,
},
],
extends: [

View File

@@ -25,6 +25,7 @@ export const OrderPromotion: ModuleJoinerConfig = {
args: {
methodSuffix: "Orders",
},
hasMany: true,
},
{
serviceName: Modules.PROMOTION,
@@ -35,6 +36,7 @@ export const OrderPromotion: ModuleJoinerConfig = {
args: {
methodSuffix: "Promotions",
},
hasMany: true,
},
],
extends: [

View File

@@ -35,6 +35,7 @@ export const ReturnFulfillment: ModuleJoinerConfig = {
args: {
methodSuffix: "Fulfillments",
},
hasMany: true,
},
],
extends: [

View File

@@ -27,6 +27,7 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
args: {
methodSuffix: "Products",
},
hasMany: true,
},
{
serviceName: Modules.SALES_CHANNEL,
@@ -37,6 +38,7 @@ export const ProductSalesChannel: ModuleJoinerConfig = {
args: {
methodSuffix: "SalesChannels",
},
hasMany: true,
},
],
extends: [

View File

@@ -27,6 +27,7 @@ export const ProductShippingProfile: ModuleJoinerConfig = {
args: {
methodSuffix: "Products",
},
hasMany: true,
},
{
serviceName: Modules.FULFILLMENT,

View File

@@ -34,6 +34,7 @@ export const ProductVariantInventoryItem: ModuleJoinerConfig = {
args: {
methodSuffix: "ProductVariants",
},
hasMany: true,
},
{
serviceName: Modules.INVENTORY,
@@ -44,6 +45,7 @@ export const ProductVariantInventoryItem: ModuleJoinerConfig = {
args: {
methodSuffix: "InventoryItems",
},
hasMany: true,
},
],
extends: [

View File

@@ -27,6 +27,7 @@ export const PublishableApiKeySalesChannel: ModuleJoinerConfig = {
args: {
methodSuffix: "ApiKeys",
},
hasMany: true,
},
{
serviceName: Modules.SALES_CHANNEL,
@@ -37,6 +38,7 @@ export const PublishableApiKeySalesChannel: ModuleJoinerConfig = {
args: {
methodSuffix: "SalesChannels",
},
hasMany: true,
},
],
extends: [

View File

@@ -25,6 +25,7 @@ export const RegionPaymentProvider: ModuleJoinerConfig = {
args: {
methodSuffix: "Regions",
},
hasMany: true,
},
{
serviceName: Modules.PAYMENT,
@@ -33,6 +34,7 @@ export const RegionPaymentProvider: ModuleJoinerConfig = {
foreignKey: "payment_provider_id",
alias: "payment_provider",
args: { methodSuffix: "PaymentProviders" },
hasMany: true,
},
],
extends: [

View File

@@ -25,6 +25,7 @@ export const SalesChannelLocation: ModuleJoinerConfig = {
args: {
methodSuffix: "SalesChannels",
},
hasMany: true,
},
{
serviceName: Modules.STOCK_LOCATION,
@@ -35,6 +36,7 @@ export const SalesChannelLocation: ModuleJoinerConfig = {
args: {
methodSuffix: "StockLocations",
},
hasMany: true,
},
],
extends: [