chore: Internal medusa service proper typings with DML (#7792)
This commit is contained in:
committed by
GitHub
parent
944051a951
commit
90e6ca0e9e
@@ -30,3 +30,4 @@ export * as TransactionBaseTypes from "./transaction-base"
|
||||
export * as UserTypes from "./user"
|
||||
export * as WorkflowTypes from "./workflow"
|
||||
export * as WorkflowsSdkTypes from "./workflows-sdk"
|
||||
export * as DmlTypes from "./dml"
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { DmlEntity } from "./entity"
|
||||
export const IsDmlEntity = Symbol.for("isDmlEntity")
|
||||
|
||||
export interface IDmlEntity<
|
||||
Schema extends Record<string, PropertyType<any> | RelationshipType<any>>
|
||||
> {
|
||||
[IsDmlEntity]: true
|
||||
schema: Schema
|
||||
}
|
||||
|
||||
/**
|
||||
* The supported data types
|
||||
@@ -92,7 +99,7 @@ export interface EntityConstructor<Props> extends Function {
|
||||
/**
|
||||
* Helper to infer the schema type of a DmlEntity
|
||||
*/
|
||||
export type Infer<T> = T extends DmlEntity<infer Schema>
|
||||
export type Infer<T> = T extends IDmlEntity<infer Schema>
|
||||
? EntityConstructor<{
|
||||
[K in keyof Schema]: Schema[K]["$dataType"] extends () => infer R
|
||||
? Infer<R>
|
||||
@@ -123,3 +130,15 @@ export type ExtractEntityRelations<
|
||||
export type EntityCascades<Relationships> = {
|
||||
delete?: Relationships
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to infer the instance type of a DmlEntity once converted as an Entity
|
||||
*/
|
||||
export type InferTypeOf<T extends IDmlEntity<any>> = InstanceType<Infer<T>>
|
||||
|
||||
/**
|
||||
* Used in the module sdk internal service to infer propert entity typings from DML
|
||||
*/
|
||||
export type ExtractEntityType<T extends any> = T extends IDmlEntity<any>
|
||||
? InferTypeOf<T>
|
||||
: T
|
||||
@@ -41,3 +41,4 @@ export * from "./user"
|
||||
export * from "./workflow"
|
||||
export * from "./workflows"
|
||||
export * from "./workflows-sdk"
|
||||
export * from "./dml"
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
PerformedActions,
|
||||
UpsertWithReplaceConfig,
|
||||
} from "../dal"
|
||||
import { ExtractEntityType } from "../dml"
|
||||
|
||||
export interface IMedusaInternalService<
|
||||
TEntity extends {},
|
||||
@@ -18,44 +19,56 @@ export interface IMedusaInternalService<
|
||||
idOrObject: string,
|
||||
config?: FindConfig<any>,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
retrieve(
|
||||
idOrObject: object,
|
||||
config?: FindConfig<any>,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
|
||||
list(
|
||||
filters?: FilterQuery<any> | BaseFilterable<FilterQuery<any>>,
|
||||
config?: FindConfig<any>,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
listAndCount(
|
||||
filters?: FilterQuery<any> | BaseFilterable<FilterQuery<any>>,
|
||||
config?: FindConfig<any>,
|
||||
sharedContext?: Context
|
||||
): Promise<[TEntity[], number]>
|
||||
): Promise<[ExtractEntityType<TEntity>[], number]>
|
||||
|
||||
create(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
create(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
create(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
create(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
|
||||
update(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
update(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
update(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
update(
|
||||
selectorAndData: {
|
||||
selector: FilterQuery<any> | BaseFilterable<FilterQuery<any>>
|
||||
data: any
|
||||
},
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
selectorAndData: {
|
||||
selector: FilterQuery<any> | BaseFilterable<FilterQuery<any>>
|
||||
data: any
|
||||
}[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
delete(idOrSelector: string, sharedContext?: Context): Promise<void>
|
||||
delete(idOrSelector: string[], sharedContext?: Context): Promise<void>
|
||||
@@ -75,19 +88,28 @@ export interface IMedusaInternalService<
|
||||
| InternalFilterQuery
|
||||
| InternalFilterQuery[],
|
||||
sharedContext?: Context
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]>
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]>
|
||||
|
||||
restore(
|
||||
idsOrFilter: string[] | InternalFilterQuery,
|
||||
sharedContext?: Context
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]>
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]>
|
||||
|
||||
upsert(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
upsert(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
upsert(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
upsert(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
|
||||
upsertWithReplace(
|
||||
data: any[],
|
||||
config?: UpsertWithReplaceConfig<TEntity>,
|
||||
config?: UpsertWithReplaceConfig<ExtractEntityType<TEntity>>,
|
||||
sharedContext?: Context
|
||||
): Promise<{ entities: TEntity[]; performedActions: PerformedActions }>
|
||||
): Promise<{
|
||||
entities: ExtractEntityType<TEntity>[]
|
||||
performedActions: PerformedActions
|
||||
}>
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
"author": "Medusa",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@types/express": "^4.17.17",
|
||||
"cross-env": "^5.2.1",
|
||||
"expect-type": "^0.19.0",
|
||||
@@ -32,6 +31,7 @@
|
||||
"typescript": "^5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@medusajs/types": "^1.11.16",
|
||||
"@mikro-orm/core": "5.9.7",
|
||||
"@mikro-orm/migrations": "5.9.7",
|
||||
"@mikro-orm/postgresql": "5.9.7",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { expectTypeOf } from "expect-type"
|
||||
import { BaseProperty } from "../properties/base"
|
||||
import { PropertyMetadata } from "../types"
|
||||
import { PropertyMetadata } from "@medusajs/types"
|
||||
|
||||
describe("Base property", () => {
|
||||
test("create a property type from base property", () => {
|
||||
|
||||
@@ -13,7 +13,7 @@ import type {
|
||||
PropertyType,
|
||||
RelationshipOptions,
|
||||
RelationshipType,
|
||||
} from "./types"
|
||||
} from "@medusajs/types"
|
||||
import { NullableModifier } from "./properties/nullable"
|
||||
import { IdProperty } from "./properties/id"
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ import { BelongsTo } from "./relations/belongs-to"
|
||||
import {
|
||||
EntityCascades,
|
||||
ExtractEntityRelations,
|
||||
IDmlEntity,
|
||||
IsDmlEntity,
|
||||
PropertyType,
|
||||
RelationshipType,
|
||||
} from "./types"
|
||||
|
||||
const IsDmlEntity = Symbol("isDmlEntity")
|
||||
} from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* Dml entity is a representation of a DML model with a unique
|
||||
@@ -14,8 +14,9 @@ const IsDmlEntity = Symbol("isDmlEntity")
|
||||
*/
|
||||
export class DmlEntity<
|
||||
Schema extends Record<string, PropertyType<any> | RelationshipType<any>>
|
||||
> {
|
||||
[IsDmlEntity] = true
|
||||
> implements IDmlEntity<Schema>
|
||||
{
|
||||
[IsDmlEntity]: true = true
|
||||
|
||||
#cascades: EntityCascades<string[]> = {}
|
||||
constructor(public name: string, public schema: Schema) {}
|
||||
|
||||
@@ -34,7 +34,7 @@ import type {
|
||||
PropertyType,
|
||||
RelationshipMetadata,
|
||||
RelationshipType,
|
||||
} from "../types"
|
||||
} from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* DML entity data types to PostgreSQL data types via
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PropertyMetadata, PropertyType } from "../types"
|
||||
import { PropertyMetadata, PropertyType } from "@medusajs/types"
|
||||
import { NullableModifier } from "./nullable"
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PropertyType } from "../types"
|
||||
import { PropertyType } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* Nullable modifier marks a schema node as nullable
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
RelationshipOptions,
|
||||
RelationshipType,
|
||||
RelationshipTypes,
|
||||
} from "../types"
|
||||
} from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* The BaseRelationship encapsulates the repetitive parts of defining
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BaseRelationship } from "./base"
|
||||
import { RelationshipTypes } from "../types"
|
||||
import { NullableModifier } from "./nullable"
|
||||
|
||||
export class BelongsTo<T> extends BaseRelationship<T> {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BaseRelationship } from "./base"
|
||||
import { RelationshipTypes } from "../types"
|
||||
|
||||
/**
|
||||
* HasMany relationship defines a relationship between two entities
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { BaseRelationship } from "./base"
|
||||
import { NullableModifier } from "./nullable"
|
||||
import { RelationshipTypes } from "../types"
|
||||
|
||||
/**
|
||||
* HasOne relationship defines a relationship between two entities
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BaseRelationship } from "./base"
|
||||
import { RelationshipTypes } from "../types"
|
||||
|
||||
/**
|
||||
* ManyToMany relationship defines a relationship between two entities
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { RelationshipType, PropertyType } from "../types"
|
||||
import { RelationshipType } from "@medusajs/types"
|
||||
|
||||
/**
|
||||
* Nullable modifier marks a schema node as nullable
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
BaseFilterable,
|
||||
Context,
|
||||
ExtractEntityType,
|
||||
FilterQuery,
|
||||
FilterQuery as InternalFilterQuery,
|
||||
FindConfig,
|
||||
@@ -100,9 +101,9 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
@InjectManager(propertyRepositoryName)
|
||||
async retrieve(
|
||||
idOrObject: string | object,
|
||||
config: FindConfig<TEntity> = {},
|
||||
config: FindConfig<ExtractEntityType<TEntity>> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
): Promise<ExtractEntityType<TEntity>> {
|
||||
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
|
||||
|
||||
if (
|
||||
@@ -162,7 +163,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
filters: FilterQuery<any> | BaseFilterable<FilterQuery<any>> = {},
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity>[]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
AbstractService_.applyFreeTextSearchFilter(filters, config)
|
||||
|
||||
@@ -179,7 +180,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
filters: FilterQuery<any> | BaseFilterable<FilterQuery<any>> = {},
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], number]> {
|
||||
AbstractService_.applyDefaultOrdering(config)
|
||||
AbstractService_.applyFreeTextSearchFilter(filters, config)
|
||||
|
||||
@@ -191,16 +192,24 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
)
|
||||
}
|
||||
|
||||
create(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
create(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
create(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
create(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
|
||||
async create(
|
||||
data: any | any[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
if (!isDefined(data) || (Array.isArray(data) && data.length === 0)) {
|
||||
return (Array.isArray(data) ? [] : void 0) as TEntity | TEntity[]
|
||||
return (Array.isArray(data) ? [] : void 0) as
|
||||
| ExtractEntityType<TEntity>
|
||||
| ExtractEntityType<TEntity>[]
|
||||
}
|
||||
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
@@ -212,24 +221,32 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
return Array.isArray(data) ? entities : entities[0]
|
||||
}
|
||||
|
||||
update(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
update(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
update(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
update(
|
||||
selectorAndData: SelectorAndData,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
update(
|
||||
selectorAndData: SelectorAndData[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
|
||||
@InjectTransactionManager(shouldForceTransaction, propertyRepositoryName)
|
||||
async update(
|
||||
input: any | any[] | SelectorAndData | SelectorAndData[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
if (!isDefined(input) || (Array.isArray(input) && input.length === 0)) {
|
||||
return (Array.isArray(input) ? [] : void 0) as TEntity | TEntity[]
|
||||
return (Array.isArray(input) ? [] : void 0) as
|
||||
| ExtractEntityType<TEntity>
|
||||
| ExtractEntityType<TEntity>[]
|
||||
}
|
||||
|
||||
const primaryKeys = AbstractService_.retrievePrimaryKeys(model)
|
||||
@@ -294,7 +311,8 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
|
||||
// Only throw for missing entities when we dont have selectors involved as selector by design can return 0 entities
|
||||
if (entitiesToUpdate.length !== keySelectorDataMap.size) {
|
||||
const entityName = (model as EntityClass<TEntity>).name ?? model
|
||||
const entityName =
|
||||
(model as EntityClass<ExtractEntityType<TEntity>>).name ?? model
|
||||
|
||||
const compositeKeysValuesForFoundEntities = new Set(
|
||||
entitiesToUpdate.map((entity) => {
|
||||
@@ -447,7 +465,7 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
| InternalFilterQuery
|
||||
| InternalFilterQuery[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]> {
|
||||
if (
|
||||
(Array.isArray(idsOrFilter) && !idsOrFilter.length) ||
|
||||
(!Array.isArray(idsOrFilter) && !idsOrFilter)
|
||||
@@ -465,21 +483,27 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
async restore(
|
||||
idsOrFilter: string[] | InternalFilterQuery,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], Record<string, unknown[]>]> {
|
||||
): Promise<[ExtractEntityType<TEntity>[], Record<string, unknown[]>]> {
|
||||
return await this[propertyRepositoryName].restore(
|
||||
idsOrFilter,
|
||||
sharedContext
|
||||
)
|
||||
}
|
||||
|
||||
upsert(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
upsert(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
upsert(
|
||||
data: any[],
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>[]>
|
||||
upsert(
|
||||
data: any,
|
||||
sharedContext?: Context
|
||||
): Promise<ExtractEntityType<TEntity>>
|
||||
|
||||
@InjectTransactionManager(propertyRepositoryName)
|
||||
async upsert(
|
||||
data: any | any[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<ExtractEntityType<TEntity> | ExtractEntityType<TEntity>[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
const entities = await this[propertyRepositoryName].upsert(
|
||||
data_,
|
||||
@@ -490,24 +514,30 @@ export function MedusaInternalService<TContainer extends object = object>(
|
||||
|
||||
upsertWithReplace(
|
||||
data: any[],
|
||||
config?: UpsertWithReplaceConfig<TEntity>,
|
||||
config?: UpsertWithReplaceConfig<ExtractEntityType<TEntity>>,
|
||||
sharedContext?: Context
|
||||
): Promise<{ entities: TEntity[]; performedActions: PerformedActions }>
|
||||
): Promise<{
|
||||
entities: ExtractEntityType<TEntity>[]
|
||||
performedActions: PerformedActions
|
||||
}>
|
||||
upsertWithReplace(
|
||||
data: any,
|
||||
config?: UpsertWithReplaceConfig<TEntity>,
|
||||
config?: UpsertWithReplaceConfig<ExtractEntityType<TEntity>>,
|
||||
sharedContext?: Context
|
||||
): Promise<{ entities: TEntity; performedActions: PerformedActions }>
|
||||
): Promise<{
|
||||
entities: ExtractEntityType<TEntity>
|
||||
performedActions: PerformedActions
|
||||
}>
|
||||
|
||||
@InjectTransactionManager(propertyRepositoryName)
|
||||
async upsertWithReplace(
|
||||
data: any | any[],
|
||||
config: UpsertWithReplaceConfig<TEntity> = {
|
||||
config: UpsertWithReplaceConfig<ExtractEntityType<TEntity>> = {
|
||||
relations: [],
|
||||
},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<{
|
||||
entities: TEntity | TEntity[]
|
||||
entities: ExtractEntityType<TEntity> | TEntity[]
|
||||
performedActions: PerformedActions
|
||||
}> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
|
||||
@@ -8,11 +8,9 @@ type InjectedDependencies = {
|
||||
inventoryLevelRepository: InventoryLevelRepository
|
||||
}
|
||||
|
||||
export default class InventoryLevelService<
|
||||
TEntity extends InventoryLevel = InventoryLevel
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class InventoryLevelService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
InventoryLevel
|
||||
)<TEntity> {
|
||||
)<InventoryLevel> {
|
||||
protected readonly inventoryLevelRepository: InventoryLevelRepository
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
|
||||
@@ -30,7 +30,7 @@ import { IInventoryService } from "@medusajs/types/dist/inventory"
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
inventoryItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
inventoryLevelService: InventoryLevelService<any>
|
||||
inventoryLevelService: InventoryLevelService
|
||||
reservationItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ export default class InventoryModuleService
|
||||
|
||||
protected readonly inventoryItemService_: ModulesSdkTypes.IMedusaInternalService<InventoryItem>
|
||||
protected readonly reservationItemService_: ModulesSdkTypes.IMedusaInternalService<ReservationItem>
|
||||
protected readonly inventoryLevelService_: InventoryLevelService<InventoryLevel>
|
||||
protected readonly inventoryLevelService_: InventoryLevelService
|
||||
|
||||
constructor(
|
||||
{
|
||||
|
||||
@@ -20,12 +20,10 @@ type InjectedDependencies = {
|
||||
orderChangeRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class OrderChangeService<
|
||||
TEntity extends OrderChange = OrderChange
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class OrderChangeService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
OrderChange
|
||||
)<TEntity> {
|
||||
protected readonly orderChangeRepository_: RepositoryService<TEntity>
|
||||
)<OrderChange> {
|
||||
protected readonly orderChangeRepository_: RepositoryService<OrderChange>
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
@@ -38,7 +36,7 @@ export default class OrderChangeService<
|
||||
orderId: string | string[],
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<OrderChange[]> {
|
||||
const allChanges = await super.list(
|
||||
{ order_id: orderId },
|
||||
config ?? {
|
||||
@@ -67,7 +65,7 @@ export default class OrderChangeService<
|
||||
}
|
||||
}
|
||||
|
||||
let orderChange!: TEntity
|
||||
let orderChange!: OrderChange
|
||||
if (allChanges?.length > 0) {
|
||||
if (this.isActive(allChanges[0])) {
|
||||
orderChange = allChanges[0]
|
||||
@@ -81,7 +79,7 @@ export default class OrderChangeService<
|
||||
const relations = deduplicate([...(config.relations ?? []), "actions"])
|
||||
config.relations = relations
|
||||
|
||||
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
|
||||
const queryConfig = ModulesSdkUtils.buildQuery<OrderChange>(
|
||||
{
|
||||
id: lastChanges,
|
||||
order: {
|
||||
@@ -104,20 +102,20 @@ export default class OrderChangeService<
|
||||
}
|
||||
|
||||
async create(
|
||||
data: Partial<TEntity>[],
|
||||
data: Partial<OrderChange>[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<OrderChange[]>
|
||||
|
||||
async create(
|
||||
data: Partial<TEntity>,
|
||||
data: Partial<OrderChange>,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<OrderChange>
|
||||
|
||||
@InjectTransactionManager("orderChangeRepository_")
|
||||
async create(
|
||||
data: Partial<TEntity>[] | Partial<TEntity>,
|
||||
data: Partial<OrderChange>[] | Partial<OrderChange>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[] | TEntity> {
|
||||
): Promise<OrderChange[] | OrderChange> {
|
||||
const dataArr = Array.isArray(data) ? data : [data]
|
||||
const activeOrderEdit = await this.listCurrentOrderChange(
|
||||
dataArr.map((d) => d.order_id!),
|
||||
|
||||
@@ -78,7 +78,7 @@ import OrderService from "./order-service"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
orderService: OrderService<any>
|
||||
orderService: OrderService
|
||||
addressService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
lineItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
shippingMethodAdjustmentService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
@@ -87,7 +87,7 @@ type InjectedDependencies = {
|
||||
lineItemTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
shippingMethodTaxLineService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
transactionService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderChangeService: OrderChangeService<any>
|
||||
orderChangeService: OrderChangeService
|
||||
orderChangeActionService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderItemService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
orderSummaryService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
@@ -169,7 +169,7 @@ export default class OrderModuleService<
|
||||
implements IOrderModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected orderService_: OrderService<TOrder>
|
||||
protected orderService_: OrderService
|
||||
protected addressService_: ModulesSdkTypes.IMedusaInternalService<TAddress>
|
||||
protected lineItemService_: ModulesSdkTypes.IMedusaInternalService<TLineItem>
|
||||
protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodAdjustment>
|
||||
@@ -178,7 +178,7 @@ export default class OrderModuleService<
|
||||
protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TLineItemTaxLine>
|
||||
protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService<TShippingMethodTaxLine>
|
||||
protected transactionService_: ModulesSdkTypes.IMedusaInternalService<TTransaction>
|
||||
protected orderChangeService_: OrderChangeService<TOrderChange>
|
||||
protected orderChangeService_: OrderChangeService
|
||||
protected orderChangeActionService_: ModulesSdkTypes.IMedusaInternalService<TOrderChangeAction>
|
||||
protected orderItemService_: ModulesSdkTypes.IMedusaInternalService<TOrderItem>
|
||||
protected orderSummaryService_: ModulesSdkTypes.IMedusaInternalService<TOrderSummary>
|
||||
|
||||
@@ -17,12 +17,10 @@ type InjectedDependencies = {
|
||||
orderRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class OrderService<
|
||||
TEntity extends Order = Order
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class OrderService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
Order
|
||||
)<TEntity> {
|
||||
protected readonly orderRepository_: RepositoryService<TEntity>
|
||||
)<Order> {
|
||||
protected readonly orderRepository_: RepositoryService<Order>
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
@@ -36,8 +34,8 @@ export default class OrderService<
|
||||
version: number,
|
||||
config: FindConfig<TEntityMethod> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
|
||||
): Promise<Order> {
|
||||
const queryConfig = ModulesSdkUtils.buildQuery<Order>(
|
||||
{ id, items: { version } },
|
||||
{ ...config, take: 1 }
|
||||
)
|
||||
|
||||
@@ -7,11 +7,9 @@ type InjectedDependencies = {
|
||||
priceListRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class PriceListService<
|
||||
TEntity extends PriceList = PriceList
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class PriceListService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
PriceList
|
||||
)<TEntity> {
|
||||
)<PriceList> {
|
||||
constructor(container: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
@@ -20,32 +18,32 @@ export default class PriceListService<
|
||||
create(
|
||||
data: ServiceTypes.CreatePriceListDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<PriceList[]>
|
||||
create(
|
||||
data: ServiceTypes.CreatePriceListDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<PriceList>
|
||||
|
||||
async create(
|
||||
data: ServiceTypes.CreatePriceListDTO | ServiceTypes.CreatePriceListDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<PriceList | PriceList[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
const priceLists = this.normalizePriceListDate(data_)
|
||||
return await super.create(priceLists, sharedContext)
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
update(data: any[], sharedContext?: Context): Promise<TEntity[]>
|
||||
update(data: any[], sharedContext?: Context): Promise<PriceList[]>
|
||||
// @ts-ignore
|
||||
update(data: any, sharedContext?: Context): Promise<TEntity>
|
||||
update(data: any, sharedContext?: Context): Promise<PriceList>
|
||||
|
||||
// TODO: Add support for selector? and then rm ts ignore
|
||||
// @ts-ignore
|
||||
async update(
|
||||
data: ServiceTypes.UpdatePriceListDTO | ServiceTypes.UpdatePriceListDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<PriceList | PriceList[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
const priceLists = this.normalizePriceListDate(data_)
|
||||
return await super.update(priceLists, sharedContext)
|
||||
|
||||
@@ -58,11 +58,11 @@ type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
pricingRepository: PricingRepositoryService
|
||||
priceSetService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
ruleTypeService: RuleTypeService<any>
|
||||
ruleTypeService: RuleTypeService
|
||||
priceRuleService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
priceSetRuleTypeService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
priceService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
priceListService: PriceListService<any>
|
||||
priceListService: PriceListService
|
||||
priceListRuleService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
priceListRuleValueService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
}
|
||||
@@ -99,12 +99,12 @@ export default class PricingModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly pricingRepository_: PricingRepositoryService
|
||||
protected readonly ruleTypeService_: RuleTypeService<RuleType>
|
||||
protected readonly ruleTypeService_: RuleTypeService
|
||||
protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService<PriceSet>
|
||||
protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceRule>
|
||||
protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService<PriceSetRuleType>
|
||||
protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService<Price>
|
||||
protected readonly priceListService_: PriceListService<PriceList>
|
||||
protected readonly priceListService_: PriceListService
|
||||
protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService<PriceListRule>
|
||||
protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService<PriceListRuleValue>
|
||||
|
||||
|
||||
@@ -11,12 +11,10 @@ type InjectedDependencies = {
|
||||
ruleTypeRepository: DAL.RepositoryService
|
||||
}
|
||||
|
||||
export default class RuleTypeService<
|
||||
TEntity extends RuleType = RuleType
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class RuleTypeService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
RuleType
|
||||
)<TEntity> {
|
||||
protected readonly ruleTypeRepository_: DAL.RepositoryService<TEntity>
|
||||
)<RuleType> {
|
||||
protected readonly ruleTypeRepository_: DAL.RepositoryService<RuleType>
|
||||
|
||||
constructor({ ruleTypeRepository }: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
@@ -27,17 +25,17 @@ export default class RuleTypeService<
|
||||
create(
|
||||
data: PricingTypes.CreateRuleTypeDTO,
|
||||
sharedContext: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<RuleType>
|
||||
create(
|
||||
data: PricingTypes.CreateRuleTypeDTO[],
|
||||
sharedContext: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<RuleType[]>
|
||||
|
||||
@InjectTransactionManager("ruleTypeRepository_")
|
||||
async create(
|
||||
data: PricingTypes.CreateRuleTypeDTO | PricingTypes.CreateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<RuleType | RuleType[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
validateRuleAttributes(data_.map((d) => d.rule_attribute))
|
||||
return await super.create(data, sharedContext)
|
||||
@@ -47,12 +45,12 @@ export default class RuleTypeService<
|
||||
update(
|
||||
data: PricingTypes.UpdateRuleTypeDTO[],
|
||||
sharedContext: Context
|
||||
): Promise<TEntity[]>
|
||||
): Promise<RuleType[]>
|
||||
// @ts-ignore
|
||||
update(
|
||||
data: PricingTypes.UpdateRuleTypeDTO,
|
||||
sharedContext: Context
|
||||
): Promise<TEntity>
|
||||
): Promise<RuleType>
|
||||
|
||||
@InjectTransactionManager("ruleTypeRepository_")
|
||||
// TODO: add support for selector? and then rm ts ignore
|
||||
@@ -60,7 +58,7 @@ export default class RuleTypeService<
|
||||
async update(
|
||||
data: PricingTypes.UpdateRuleTypeDTO | PricingTypes.UpdateRuleTypeDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity | TEntity[]> {
|
||||
): Promise<RuleType | RuleType[]> {
|
||||
const data_ = Array.isArray(data) ? data : [data]
|
||||
validateRuleAttributes(data_.map((d) => d.rule_attribute))
|
||||
return await super.update(data, sharedContext)
|
||||
|
||||
@@ -15,9 +15,7 @@ import { UpdateCategoryInput } from "@types"
|
||||
type InjectedDependencies = {
|
||||
productCategoryRepository: DAL.TreeRepositoryService
|
||||
}
|
||||
export default class ProductCategoryService<
|
||||
TEntity extends ProductCategory = ProductCategory
|
||||
> {
|
||||
export default class ProductCategoryService {
|
||||
protected readonly productCategoryRepository_: DAL.TreeRepositoryService
|
||||
|
||||
constructor({ productCategoryRepository }: InjectedDependencies) {
|
||||
@@ -30,7 +28,7 @@ export default class ProductCategoryService<
|
||||
productCategoryId: string,
|
||||
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity> {
|
||||
): Promise<ProductCategory> {
|
||||
if (!isDefined(productCategoryId)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
@@ -64,7 +62,7 @@ export default class ProductCategoryService<
|
||||
)
|
||||
}
|
||||
|
||||
return productCategories[0] as TEntity
|
||||
return productCategories[0] as ProductCategory
|
||||
}
|
||||
|
||||
@InjectManager("productCategoryRepository_")
|
||||
@@ -72,7 +70,7 @@ export default class ProductCategoryService<
|
||||
filters: ProductTypes.FilterableProductCategoryProps = {},
|
||||
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ProductCategory[]> {
|
||||
const transformOptions = {
|
||||
includeDescendantsTree: filters?.include_descendants_tree || false,
|
||||
includeAncestorsTree: filters?.include_ancestors_tree || false,
|
||||
@@ -101,7 +99,7 @@ export default class ProductCategoryService<
|
||||
queryOptions,
|
||||
transformOptions,
|
||||
sharedContext
|
||||
)) as TEntity[]
|
||||
)) as ProductCategory[]
|
||||
}
|
||||
|
||||
@InjectManager("productCategoryRepository_")
|
||||
@@ -109,7 +107,7 @@ export default class ProductCategoryService<
|
||||
filters: ProductTypes.FilterableProductCategoryProps = {},
|
||||
config: FindConfig<ProductTypes.ProductCategoryDTO> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
): Promise<[ProductCategory[], number]> {
|
||||
const transformOptions = {
|
||||
includeDescendantsTree: filters?.include_descendants_tree || false,
|
||||
includeAncestorsTree: filters?.include_ancestors_tree || false,
|
||||
@@ -138,27 +136,27 @@ export default class ProductCategoryService<
|
||||
queryOptions,
|
||||
transformOptions,
|
||||
sharedContext
|
||||
)) as [TEntity[], number]
|
||||
)) as [ProductCategory[], number]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("productCategoryRepository_")
|
||||
async create(
|
||||
data: ProductTypes.CreateProductCategoryDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ProductCategory[]> {
|
||||
return (await (
|
||||
this.productCategoryRepository_ as unknown as ProductCategoryRepository
|
||||
).create(data, sharedContext)) as TEntity[]
|
||||
).create(data, sharedContext)) as ProductCategory[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("productCategoryRepository_")
|
||||
async update(
|
||||
data: UpdateCategoryInput[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<ProductCategory[]> {
|
||||
return (await (
|
||||
this.productCategoryRepository_ as unknown as ProductCategoryRepository
|
||||
).update(data, sharedContext)) as TEntity[]
|
||||
).update(data, sharedContext)) as ProductCategory[]
|
||||
}
|
||||
|
||||
@InjectTransactionManager("productCategoryRepository_")
|
||||
|
||||
@@ -55,10 +55,10 @@ import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config"
|
||||
|
||||
type InjectedDependencies = {
|
||||
baseRepository: DAL.RepositoryService
|
||||
productService: ProductService<any>
|
||||
productService: ProductService
|
||||
productVariantService: ModulesSdkTypes.IMedusaInternalService<any, any>
|
||||
productTagService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
productCategoryService: ProductCategoryService<any>
|
||||
productCategoryService: ProductCategoryService
|
||||
productCollectionService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
productImageService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
productTypeService: ModulesSdkTypes.IMedusaInternalService<any>
|
||||
@@ -105,9 +105,9 @@ export default class ProductModuleService
|
||||
implements ProductTypes.IProductModuleService
|
||||
{
|
||||
protected baseRepository_: DAL.RepositoryService
|
||||
protected readonly productService_: ProductService<Product>
|
||||
protected readonly productService_: ProductService
|
||||
protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService<ProductVariant>
|
||||
protected readonly productCategoryService_: ProductCategoryService<ProductCategory>
|
||||
protected readonly productCategoryService_: ProductCategoryService
|
||||
protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService<ProductTag>
|
||||
protected readonly productCollectionService_: ModulesSdkTypes.IMedusaInternalService<ProductCollection>
|
||||
protected readonly productImageService_: ModulesSdkTypes.IMedusaInternalService<ProductImage>
|
||||
|
||||
@@ -18,12 +18,10 @@ type NormalizedFilterableProductProps = ProductTypes.FilterableProductProps & {
|
||||
}
|
||||
}
|
||||
|
||||
export default class ProductService<
|
||||
TEntity extends Product = Product
|
||||
> extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
export default class ProductService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
|
||||
Product
|
||||
)<TEntity> {
|
||||
protected readonly productRepository_: DAL.RepositoryService<TEntity>
|
||||
)<Product> {
|
||||
protected readonly productRepository_: DAL.RepositoryService<Product>
|
||||
|
||||
constructor({ productRepository }: InjectedDependencies) {
|
||||
// @ts-ignore
|
||||
@@ -36,9 +34,9 @@ export default class ProductService<
|
||||
@InjectManager("productRepository_")
|
||||
async list(
|
||||
filters: ProductTypes.FilterableProductProps = {},
|
||||
config: FindConfig<TEntity> = {},
|
||||
config: FindConfig<Product> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TEntity[]> {
|
||||
): Promise<Product[]> {
|
||||
return await super.list(
|
||||
ProductService.normalizeFilters(filters),
|
||||
config,
|
||||
@@ -51,7 +49,7 @@ export default class ProductService<
|
||||
filters: ProductTypes.FilterableProductProps = {},
|
||||
config: FindConfig<any> = {},
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[TEntity[], number]> {
|
||||
): Promise<[Product[], number]> {
|
||||
return await super.listAndCount(
|
||||
ProductService.normalizeFilters(filters),
|
||||
config,
|
||||
|
||||
Reference in New Issue
Block a user