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
@@ -21,5 +21,6 @@ export const InventoryModule = {
},
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}
@@ -65,5 +65,6 @@ export const InventoryStockLocationLink = {
foreignKeyData?: string
) => {}
),
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}
@@ -71,5 +71,6 @@ export const ProductInventoryLinkModule = {
foreignKeyData?: string
) => {}
),
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}
@@ -18,5 +18,6 @@ export const ProductModule = {
alias: [],
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}
@@ -18,5 +18,6 @@ export const StockLocationModule = {
alias: [],
},
list: jest.fn(async () => []),
softDelete: jest.fn(() => {}),
}
+40 -44
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