Feat(link-module, core-flows, medusa): Add sales channel location management (#6905)

* add locations with sales channels

* init, not working location based management

* working adding sales channel stock locations

* remote sales channels from location

* remove console log

* rm allowedFields

* redo errorhandler

* make validation take an array

* add changeset

* fix build

* Update packages/core-flows/src/sales-channel/workflows/remove-locations-from-sales-channel.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* add default cases to return early

* cleanup

* add sc test and remove associations

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2024-04-03 08:38:53 +02:00
committed by GitHub
co-authored by Oli Juhl
parent 27f4f0d724
commit edafe7db47
19 changed files with 483 additions and 43 deletions
@@ -100,16 +100,19 @@ export default class LinkModuleService<TLink> implements ILinkModule {
return this.primaryKey_.concat(this.foreignKey_).includes(name)
}
private validateFields(data: any) {
const keys = Object.keys(data)
if (!keys.every((k) => this.isValidKeyName(k))) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Invalid field name provided. Valid field names are ${this.primaryKey_.concat(
this.foreignKey_
)}`
)
}
private validateFields(data: any | any[]) {
const dataToValidate = Array.isArray(data) ? data : [data]
dataToValidate.forEach((d) => {
const keys = Object.keys(d)
if (keys.some((k) => !this.isValidKeyName(k))) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Invalid field name provided. Valid field names are ${this.primaryKey_.concat(
this.foreignKey_
)}`
)
}
})
}
@InjectManager("baseRepository_")
@@ -276,10 +279,12 @@ export default class LinkModuleService<TLink> implements ILinkModule {
{ returnLinkableKeys }: SoftDeleteReturn = {},
@MedusaContext() sharedContext: Context = {}
): Promise<Record<string, unknown[]> | void> {
this.validateFields(data)
const inputArray = Array.isArray(data) ? data : [data]
this.validateFields(inputArray)
let [deletedEntities, cascadedEntitiesMap] = await this.softDelete_(
data,
inputArray,
sharedContext
)
@@ -324,7 +329,7 @@ export default class LinkModuleService<TLink> implements ILinkModule {
@InjectTransactionManager(shouldForceTransaction, "baseRepository_")
protected async softDelete_(
data: any,
data: any[],
@MedusaContext() sharedContext: Context = {}
): Promise<[object[], Record<string, string[]>]> {
return await this.linkService_.softDelete(data, sharedContext)
@@ -335,10 +340,11 @@ export default class LinkModuleService<TLink> implements ILinkModule {
{ returnLinkableKeys }: RestoreReturn = {},
@MedusaContext() sharedContext: Context = {}
): Promise<Record<string, unknown[]> | void> {
this.validateFields(data)
const inputArray = Array.isArray(data) ? data : [data]
this.validateFields(inputArray)
let [restoredEntities, cascadedEntitiesMap] = await this.restore_(
data,
inputArray,
sharedContext
)
+27 -9
View File
@@ -87,15 +87,24 @@ export default class LinkService<TEntity> {
@InjectTransactionManager(doNotForceTransaction, "linkRepository_")
async softDelete(
data: any,
data: any[],
@MedusaContext() sharedContext: Context = {}
): Promise<[object[], Record<string, string[]>]> {
const filter = {}
for (const key in data) {
filter[key] = { $in: Array.isArray(data[key]) ? data[key] : [data[key]] }
const deleteFilters = {
$or: data.map((dataEntry) => {
const filter = {}
for (const key in dataEntry) {
filter[key] = {
$in: Array.isArray(dataEntry[key])
? dataEntry[key]
: [dataEntry[key]],
}
}
return filter
}),
}
return await this.linkRepository_.softDelete(filter, {
return await this.linkRepository_.softDelete(deleteFilters, {
transactionManager: sharedContext.transactionManager,
})
}
@@ -105,12 +114,21 @@ export default class LinkService<TEntity> {
data: any,
@MedusaContext() sharedContext: Context = {}
): Promise<[object[], Record<string, string[]>]> {
const filter = {}
for (const key in data) {
filter[key] = { $in: Array.isArray(data[key]) ? data[key] : [data[key]] }
const restoreFilters = {
$or: data.map((dataEntry) => {
const filter = {}
for (const key in dataEntry) {
filter[key] = {
$in: Array.isArray(dataEntry[key])
? dataEntry[key]
: [dataEntry[key]],
}
}
return filter
}),
}
return await this.linkRepository_.restore(data, {
return await this.linkRepository_.restore(restoreFilters, {
transactionManager: sharedContext.transactionManager,
})
}