|
|
|
@@ -5,12 +5,12 @@ import {
|
|
|
|
|
DAL,
|
|
|
|
|
FilterableStockLocationProps,
|
|
|
|
|
IEventBusService,
|
|
|
|
|
IStockLocationServiceNext,
|
|
|
|
|
IStockLocationService,
|
|
|
|
|
ModuleJoinerConfig,
|
|
|
|
|
ModulesSdkTypes,
|
|
|
|
|
StockLocationAddressInput,
|
|
|
|
|
StockLocationTypes,
|
|
|
|
|
UpdateStockLocationNextInput,
|
|
|
|
|
UpdateStockLocationInput,
|
|
|
|
|
UpsertStockLocationInput,
|
|
|
|
|
} from "@medusajs/types"
|
|
|
|
|
import {
|
|
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
|
|
InjectTransactionManager,
|
|
|
|
|
isString,
|
|
|
|
|
MedusaContext,
|
|
|
|
|
ModulesSdkUtils,
|
|
|
|
|
MedusaService,
|
|
|
|
|
promiseAll,
|
|
|
|
|
} from "@medusajs/utils"
|
|
|
|
|
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
|
|
|
@@ -31,28 +31,20 @@ type InjectedDependencies = {
|
|
|
|
|
stockLocationAddressService: ModulesSdkTypes.IMedusaInternalService<any>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const generateMethodForModels = { StockLocationAddress }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Service for managing stock locations.
|
|
|
|
|
*/
|
|
|
|
|
export default class StockLocationModuleService<
|
|
|
|
|
TEntity extends StockLocation = StockLocation,
|
|
|
|
|
TStockLocationAddress extends StockLocationAddress = StockLocationAddress
|
|
|
|
|
>
|
|
|
|
|
extends ModulesSdkUtils.MedusaService<
|
|
|
|
|
StockLocationTypes.StockLocationDTO,
|
|
|
|
|
{
|
|
|
|
|
StockLocation: { dto: StockLocationTypes.StockLocationDTO }
|
|
|
|
|
StockLocationAddress: { dto: StockLocationTypes.StockLocationAddressDTO }
|
|
|
|
|
}
|
|
|
|
|
>(StockLocation, generateMethodForModels, entityNameToLinkableKeysMap)
|
|
|
|
|
implements IStockLocationServiceNext
|
|
|
|
|
export default class StockLocationModuleService
|
|
|
|
|
extends MedusaService<{
|
|
|
|
|
StockLocation: { dto: StockLocationTypes.StockLocationDTO }
|
|
|
|
|
StockLocationAddress: { dto: StockLocationTypes.StockLocationAddressDTO }
|
|
|
|
|
}>({ StockLocation, StockLocationAddress }, entityNameToLinkableKeysMap)
|
|
|
|
|
implements IStockLocationService
|
|
|
|
|
{
|
|
|
|
|
protected readonly eventBusModuleService_: IEventBusService
|
|
|
|
|
protected baseRepository_: DAL.RepositoryService
|
|
|
|
|
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
|
|
|
|
|
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<TStockLocationAddress>
|
|
|
|
|
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<StockLocation>
|
|
|
|
|
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<StockLocationAddress>
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
{
|
|
|
|
@@ -76,23 +68,18 @@ export default class StockLocationModuleService<
|
|
|
|
|
return joinerConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create(
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
createStockLocations(
|
|
|
|
|
data: CreateStockLocationInput,
|
|
|
|
|
context: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO>
|
|
|
|
|
create(
|
|
|
|
|
createStockLocations(
|
|
|
|
|
data: CreateStockLocationInput[],
|
|
|
|
|
context: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO[]>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new stock location.
|
|
|
|
|
* @param data - The input data for creating a Stock Location.
|
|
|
|
|
* @param context
|
|
|
|
|
* @returns The created stock location.
|
|
|
|
|
*/
|
|
|
|
|
@InjectManager("baseRepository_")
|
|
|
|
|
async create(
|
|
|
|
|
async createStockLocations(
|
|
|
|
|
data: CreateStockLocationInput | CreateStockLocationInput[],
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
): Promise<
|
|
|
|
@@ -100,7 +87,7 @@ export default class StockLocationModuleService<
|
|
|
|
|
> {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
|
|
|
|
|
const created = await this.create_(input, context)
|
|
|
|
|
const created = await this.createStockLocations_(input, context)
|
|
|
|
|
|
|
|
|
|
const serialized = await this.baseRepository_.serialize<
|
|
|
|
|
| StockLocationTypes.StockLocationDTO
|
|
|
|
@@ -111,24 +98,24 @@ export default class StockLocationModuleService<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@InjectTransactionManager("baseRepository_")
|
|
|
|
|
async create_(
|
|
|
|
|
async createStockLocations_(
|
|
|
|
|
data: CreateStockLocationInput[],
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
): Promise<TEntity[]> {
|
|
|
|
|
): Promise<StockLocation[]> {
|
|
|
|
|
return await this.stockLocationService_.create(data, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async upsert(
|
|
|
|
|
async upsertStockLocations(
|
|
|
|
|
data: UpsertStockLocationInput,
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO>
|
|
|
|
|
async upsert(
|
|
|
|
|
async upsertStockLocations(
|
|
|
|
|
data: UpsertStockLocationInput[],
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO[]>
|
|
|
|
|
|
|
|
|
|
@InjectManager("baseRepository_")
|
|
|
|
|
async upsert(
|
|
|
|
|
async upsertStockLocations(
|
|
|
|
|
data: UpsertStockLocationInput | UpsertStockLocationInput[],
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
): Promise<
|
|
|
|
@@ -136,7 +123,7 @@ export default class StockLocationModuleService<
|
|
|
|
|
> {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
|
|
|
|
|
const result = await this.upsert_(input, context)
|
|
|
|
|
const result = await this.upsertStockLocations_(input, context)
|
|
|
|
|
|
|
|
|
|
return await this.baseRepository_.serialize<
|
|
|
|
|
| StockLocationTypes.StockLocationDTO[]
|
|
|
|
@@ -145,13 +132,13 @@ export default class StockLocationModuleService<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@InjectTransactionManager("baseRepository_")
|
|
|
|
|
async upsert_(
|
|
|
|
|
async upsertStockLocations_(
|
|
|
|
|
input: UpsertStockLocationInput[],
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
) {
|
|
|
|
|
const toUpdate = input.filter(
|
|
|
|
|
(location): location is UpdateStockLocationNextInput => !!location.id
|
|
|
|
|
) as UpdateStockLocationNextInput[]
|
|
|
|
|
(location): location is UpdateStockLocationInput => !!location.id
|
|
|
|
|
) as UpdateStockLocationInput[]
|
|
|
|
|
const toCreate = input.filter(
|
|
|
|
|
(location) => !location.id
|
|
|
|
|
) as CreateStockLocationInput[]
|
|
|
|
@@ -159,23 +146,24 @@ export default class StockLocationModuleService<
|
|
|
|
|
const operations: Promise<StockLocation[] | StockLocation>[] = []
|
|
|
|
|
|
|
|
|
|
if (toCreate.length) {
|
|
|
|
|
operations.push(this.create_(toCreate, context))
|
|
|
|
|
operations.push(this.createStockLocations_(toCreate, context))
|
|
|
|
|
}
|
|
|
|
|
if (toUpdate.length) {
|
|
|
|
|
operations.push(this.update_(toUpdate, context))
|
|
|
|
|
operations.push(this.updateStockLocations_(toUpdate, context))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (await promiseAll(operations)).flat()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
updateStockLocations(
|
|
|
|
|
id: string,
|
|
|
|
|
input: UpdateStockLocationNextInput,
|
|
|
|
|
input: UpdateStockLocationInput,
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO>
|
|
|
|
|
update(
|
|
|
|
|
updateStockLocations(
|
|
|
|
|
selector: FilterableStockLocationProps,
|
|
|
|
|
input: UpdateStockLocationNextInput,
|
|
|
|
|
input: UpdateStockLocationInput,
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationDTO[]>
|
|
|
|
|
/**
|
|
|
|
@@ -186,22 +174,22 @@ export default class StockLocationModuleService<
|
|
|
|
|
* @returns The updated stock location.
|
|
|
|
|
*/
|
|
|
|
|
@InjectManager("baseRepository_")
|
|
|
|
|
async update(
|
|
|
|
|
async updateStockLocations(
|
|
|
|
|
idOrSelector: string | FilterableStockLocationProps,
|
|
|
|
|
data: UpdateStockLocationNextInput | UpdateStockLocationNextInput[],
|
|
|
|
|
data: UpdateStockLocationInput | UpdateStockLocationInput[],
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
): Promise<
|
|
|
|
|
StockLocationTypes.StockLocationDTO | StockLocationTypes.StockLocationDTO[]
|
|
|
|
|
> {
|
|
|
|
|
let normalizedInput:
|
|
|
|
|
| (UpdateStockLocationNextInput & { id: string })[]
|
|
|
|
|
| (UpdateStockLocationInput & { id: string })[]
|
|
|
|
|
| { data: any; selector: FilterableStockLocationProps } = []
|
|
|
|
|
if (isString(idOrSelector)) {
|
|
|
|
|
normalizedInput = [{ id: idOrSelector, ...data }]
|
|
|
|
|
} else {
|
|
|
|
|
normalizedInput = { data, selector: idOrSelector }
|
|
|
|
|
}
|
|
|
|
|
const updated = await this.update_(normalizedInput, context)
|
|
|
|
|
const updated = await this.updateStockLocations_(normalizedInput, context)
|
|
|
|
|
|
|
|
|
|
const serialized = await this.baseRepository_.serialize<
|
|
|
|
|
| StockLocationTypes.StockLocationDTO
|
|
|
|
@@ -212,27 +200,28 @@ export default class StockLocationModuleService<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@InjectTransactionManager("baseRepository_")
|
|
|
|
|
async update_(
|
|
|
|
|
async updateStockLocations_(
|
|
|
|
|
data:
|
|
|
|
|
| UpdateStockLocationNextInput[]
|
|
|
|
|
| UpdateStockLocationNextInput
|
|
|
|
|
| UpdateStockLocationInput[]
|
|
|
|
|
| UpdateStockLocationInput
|
|
|
|
|
| { data: any; selector: FilterableStockLocationProps },
|
|
|
|
|
@MedusaContext() context: Context = {}
|
|
|
|
|
): Promise<TEntity[] | TEntity> {
|
|
|
|
|
): Promise<StockLocation[] | StockLocation> {
|
|
|
|
|
return await this.stockLocationService_.update(data, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateStockLocationAddress(
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
updateStockLocationAddresses(
|
|
|
|
|
data: StockLocationAddressInput & { id: string },
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationAddressDTO>
|
|
|
|
|
updateStockLocationAddress(
|
|
|
|
|
updateStockLocationAddresses(
|
|
|
|
|
data: (StockLocationAddressInput & { id: string })[],
|
|
|
|
|
context?: Context
|
|
|
|
|
): Promise<StockLocationTypes.StockLocationAddressDTO[]>
|
|
|
|
|
|
|
|
|
|
@InjectManager("baseRepository_")
|
|
|
|
|
async updateStockLocationAddress(
|
|
|
|
|
async updateStockLocationAddresses(
|
|
|
|
|
data:
|
|
|
|
|
| (StockLocationAddressInput & { id: string })
|
|
|
|
|
| (StockLocationAddressInput & { id: string })[],
|
|
|
|
@@ -240,7 +229,7 @@ export default class StockLocationModuleService<
|
|
|
|
|
) {
|
|
|
|
|
const input = Array.isArray(data) ? data : [data]
|
|
|
|
|
|
|
|
|
|
const updated = await this.updateStockLocationAddress_(input, context)
|
|
|
|
|
const updated = await this.updateStockLocationAddresses_(input, context)
|
|
|
|
|
|
|
|
|
|
const serialized = await this.baseRepository_.serialize<
|
|
|
|
|
| StockLocationTypes.StockLocationAddressDTO
|
|
|
|
@@ -251,7 +240,7 @@ export default class StockLocationModuleService<
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@InjectTransactionManager("baseRepository_")
|
|
|
|
|
private async updateStockLocationAddress_(
|
|
|
|
|
private async updateStockLocationAddresses_(
|
|
|
|
|
input: (StockLocationAddressInput & { id: string })[],
|
|
|
|
|
@MedusaContext() context: Context
|
|
|
|
|
) {
|
|
|
|
|