From 90e6ca0e9e6267f460b1512812d87cd56fabd2c9 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 21 Jun 2024 12:36:54 +0200 Subject: [PATCH] chore: Internal medusa service proper typings with DML (#7792) --- packages/core/types/src/bundles.ts | 1 + .../dml/types.ts => types/src/dml/index.ts} | 23 +++++- packages/core/types/src/index.ts | 1 + .../modules-sdk/medusa-internal-service.ts | 54 ++++++++---- packages/core/utils/package.json | 2 +- .../src/dml/__tests__/base-property.spec.ts | 2 +- packages/core/utils/src/dml/entity-builder.ts | 2 +- packages/core/utils/src/dml/entity.ts | 11 +-- .../dml/helpers/create-mikro-orm-entity.ts | 2 +- .../core/utils/src/dml/properties/base.ts | 2 +- .../core/utils/src/dml/properties/nullable.ts | 2 +- packages/core/utils/src/dml/relations/base.ts | 2 +- .../utils/src/dml/relations/belongs-to.ts | 1 - .../core/utils/src/dml/relations/has-many.ts | 1 - .../core/utils/src/dml/relations/has-one.ts | 1 - .../utils/src/dml/relations/many-to-many.ts | 1 - .../core/utils/src/dml/relations/nullable.ts | 2 +- .../modules-sdk/medusa-internal-service.ts | 82 +++++++++++++------ .../src/services/inventory-level.ts | 6 +- .../src/services/inventory-module.ts | 4 +- .../src/services/order-change-service.ts | 26 +++--- .../src/services/order-module-service.ts | 8 +- .../order/src/services/order-service.ts | 12 ++- .../pricing/src/services/price-list.ts | 18 ++-- .../pricing/src/services/pricing-module.ts | 8 +- .../modules/pricing/src/services/rule-type.ts | 20 ++--- .../product/src/services/product-category.ts | 24 +++--- .../src/services/product-module-service.ts | 8 +- .../modules/product/src/services/product.ts | 14 ++-- 29 files changed, 198 insertions(+), 142 deletions(-) rename packages/core/{utils/src/dml/types.ts => types/src/dml/index.ts} (80%) diff --git a/packages/core/types/src/bundles.ts b/packages/core/types/src/bundles.ts index 9dc4e933e4..8c096c7c02 100644 --- a/packages/core/types/src/bundles.ts +++ b/packages/core/types/src/bundles.ts @@ -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" diff --git a/packages/core/utils/src/dml/types.ts b/packages/core/types/src/dml/index.ts similarity index 80% rename from packages/core/utils/src/dml/types.ts rename to packages/core/types/src/dml/index.ts index 44726b7e0a..87efd1132c 100644 --- a/packages/core/utils/src/dml/types.ts +++ b/packages/core/types/src/dml/index.ts @@ -1,4 +1,11 @@ -import { DmlEntity } from "./entity" +export const IsDmlEntity = Symbol.for("isDmlEntity") + +export interface IDmlEntity< + Schema extends Record | RelationshipType> +> { + [IsDmlEntity]: true + schema: Schema +} /** * The supported data types @@ -92,7 +99,7 @@ export interface EntityConstructor extends Function { /** * Helper to infer the schema type of a DmlEntity */ -export type Infer = T extends DmlEntity +export type Infer = T extends IDmlEntity ? EntityConstructor<{ [K in keyof Schema]: Schema[K]["$dataType"] extends () => infer R ? Infer @@ -123,3 +130,15 @@ export type ExtractEntityRelations< export type EntityCascades = { delete?: Relationships } + +/** + * Helper to infer the instance type of a DmlEntity once converted as an Entity + */ +export type InferTypeOf> = InstanceType> + +/** + * Used in the module sdk internal service to infer propert entity typings from DML + */ +export type ExtractEntityType = T extends IDmlEntity + ? InferTypeOf + : T diff --git a/packages/core/types/src/index.ts b/packages/core/types/src/index.ts index 2295a4a1be..d294c40c31 100644 --- a/packages/core/types/src/index.ts +++ b/packages/core/types/src/index.ts @@ -41,3 +41,4 @@ export * from "./user" export * from "./workflow" export * from "./workflows" export * from "./workflows-sdk" +export * from "./dml" diff --git a/packages/core/types/src/modules-sdk/medusa-internal-service.ts b/packages/core/types/src/modules-sdk/medusa-internal-service.ts index 4434db597e..c4e7830652 100644 --- a/packages/core/types/src/modules-sdk/medusa-internal-service.ts +++ b/packages/core/types/src/modules-sdk/medusa-internal-service.ts @@ -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, sharedContext?: Context - ): Promise + ): Promise> retrieve( idOrObject: object, config?: FindConfig, sharedContext?: Context - ): Promise + ): Promise> list( filters?: FilterQuery | BaseFilterable>, config?: FindConfig, sharedContext?: Context - ): Promise + ): Promise[]> listAndCount( filters?: FilterQuery | BaseFilterable>, config?: FindConfig, sharedContext?: Context - ): Promise<[TEntity[], number]> + ): Promise<[ExtractEntityType[], number]> - create(data: any[], sharedContext?: Context): Promise - create(data: any, sharedContext?: Context): Promise + create( + data: any[], + sharedContext?: Context + ): Promise[]> + create( + data: any, + sharedContext?: Context + ): Promise> - update(data: any[], sharedContext?: Context): Promise - update(data: any, sharedContext?: Context): Promise + update( + data: any[], + sharedContext?: Context + ): Promise[]> + update( + data: any, + sharedContext?: Context + ): Promise> update( selectorAndData: { selector: FilterQuery | BaseFilterable> data: any }, sharedContext?: Context - ): Promise + ): Promise[]> update( selectorAndData: { selector: FilterQuery | BaseFilterable> data: any }[], sharedContext?: Context - ): Promise + ): Promise[]> delete(idOrSelector: string, sharedContext?: Context): Promise delete(idOrSelector: string[], sharedContext?: Context): Promise @@ -75,19 +88,28 @@ export interface IMedusaInternalService< | InternalFilterQuery | InternalFilterQuery[], sharedContext?: Context - ): Promise<[TEntity[], Record]> + ): Promise<[ExtractEntityType[], Record]> restore( idsOrFilter: string[] | InternalFilterQuery, sharedContext?: Context - ): Promise<[TEntity[], Record]> + ): Promise<[ExtractEntityType[], Record]> - upsert(data: any[], sharedContext?: Context): Promise - upsert(data: any, sharedContext?: Context): Promise + upsert( + data: any[], + sharedContext?: Context + ): Promise[]> + upsert( + data: any, + sharedContext?: Context + ): Promise> upsertWithReplace( data: any[], - config?: UpsertWithReplaceConfig, + config?: UpsertWithReplaceConfig>, sharedContext?: Context - ): Promise<{ entities: TEntity[]; performedActions: PerformedActions }> + ): Promise<{ + entities: ExtractEntityType[] + performedActions: PerformedActions + }> } diff --git a/packages/core/utils/package.json b/packages/core/utils/package.json index f5642c827f..37a0ed9a20 100644 --- a/packages/core/utils/package.json +++ b/packages/core/utils/package.json @@ -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", diff --git a/packages/core/utils/src/dml/__tests__/base-property.spec.ts b/packages/core/utils/src/dml/__tests__/base-property.spec.ts index d171789ef5..f882f218c0 100644 --- a/packages/core/utils/src/dml/__tests__/base-property.spec.ts +++ b/packages/core/utils/src/dml/__tests__/base-property.spec.ts @@ -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", () => { diff --git a/packages/core/utils/src/dml/entity-builder.ts b/packages/core/utils/src/dml/entity-builder.ts index 1dd97850a3..a84b9b455c 100644 --- a/packages/core/utils/src/dml/entity-builder.ts +++ b/packages/core/utils/src/dml/entity-builder.ts @@ -13,7 +13,7 @@ import type { PropertyType, RelationshipOptions, RelationshipType, -} from "./types" +} from "@medusajs/types" import { NullableModifier } from "./properties/nullable" import { IdProperty } from "./properties/id" diff --git a/packages/core/utils/src/dml/entity.ts b/packages/core/utils/src/dml/entity.ts index c654608667..1850fb8dc5 100644 --- a/packages/core/utils/src/dml/entity.ts +++ b/packages/core/utils/src/dml/entity.ts @@ -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 | RelationshipType> -> { - [IsDmlEntity] = true +> implements IDmlEntity +{ + [IsDmlEntity]: true = true #cascades: EntityCascades = {} constructor(public name: string, public schema: Schema) {} diff --git a/packages/core/utils/src/dml/helpers/create-mikro-orm-entity.ts b/packages/core/utils/src/dml/helpers/create-mikro-orm-entity.ts index dc026a9633..5a7bfffc00 100644 --- a/packages/core/utils/src/dml/helpers/create-mikro-orm-entity.ts +++ b/packages/core/utils/src/dml/helpers/create-mikro-orm-entity.ts @@ -34,7 +34,7 @@ import type { PropertyType, RelationshipMetadata, RelationshipType, -} from "../types" +} from "@medusajs/types" /** * DML entity data types to PostgreSQL data types via diff --git a/packages/core/utils/src/dml/properties/base.ts b/packages/core/utils/src/dml/properties/base.ts index 7f808391b0..ed0b356e15 100644 --- a/packages/core/utils/src/dml/properties/base.ts +++ b/packages/core/utils/src/dml/properties/base.ts @@ -1,4 +1,4 @@ -import { PropertyMetadata, PropertyType } from "../types" +import { PropertyMetadata, PropertyType } from "@medusajs/types" import { NullableModifier } from "./nullable" /** diff --git a/packages/core/utils/src/dml/properties/nullable.ts b/packages/core/utils/src/dml/properties/nullable.ts index d13a2dd03d..0b80d6702a 100644 --- a/packages/core/utils/src/dml/properties/nullable.ts +++ b/packages/core/utils/src/dml/properties/nullable.ts @@ -1,4 +1,4 @@ -import { PropertyType } from "../types" +import { PropertyType } from "@medusajs/types" /** * Nullable modifier marks a schema node as nullable diff --git a/packages/core/utils/src/dml/relations/base.ts b/packages/core/utils/src/dml/relations/base.ts index ca905c41b1..906554909d 100644 --- a/packages/core/utils/src/dml/relations/base.ts +++ b/packages/core/utils/src/dml/relations/base.ts @@ -3,7 +3,7 @@ import { RelationshipOptions, RelationshipType, RelationshipTypes, -} from "../types" +} from "@medusajs/types" /** * The BaseRelationship encapsulates the repetitive parts of defining diff --git a/packages/core/utils/src/dml/relations/belongs-to.ts b/packages/core/utils/src/dml/relations/belongs-to.ts index d513faf70a..ca84a4956b 100644 --- a/packages/core/utils/src/dml/relations/belongs-to.ts +++ b/packages/core/utils/src/dml/relations/belongs-to.ts @@ -1,5 +1,4 @@ import { BaseRelationship } from "./base" -import { RelationshipTypes } from "../types" import { NullableModifier } from "./nullable" export class BelongsTo extends BaseRelationship { diff --git a/packages/core/utils/src/dml/relations/has-many.ts b/packages/core/utils/src/dml/relations/has-many.ts index 7f15e656ae..2afff2be55 100644 --- a/packages/core/utils/src/dml/relations/has-many.ts +++ b/packages/core/utils/src/dml/relations/has-many.ts @@ -1,5 +1,4 @@ import { BaseRelationship } from "./base" -import { RelationshipTypes } from "../types" /** * HasMany relationship defines a relationship between two entities diff --git a/packages/core/utils/src/dml/relations/has-one.ts b/packages/core/utils/src/dml/relations/has-one.ts index 80c77a6433..766cb8b7fe 100644 --- a/packages/core/utils/src/dml/relations/has-one.ts +++ b/packages/core/utils/src/dml/relations/has-one.ts @@ -1,6 +1,5 @@ import { BaseRelationship } from "./base" import { NullableModifier } from "./nullable" -import { RelationshipTypes } from "../types" /** * HasOne relationship defines a relationship between two entities diff --git a/packages/core/utils/src/dml/relations/many-to-many.ts b/packages/core/utils/src/dml/relations/many-to-many.ts index 5d58c8ebf7..19154071e9 100644 --- a/packages/core/utils/src/dml/relations/many-to-many.ts +++ b/packages/core/utils/src/dml/relations/many-to-many.ts @@ -1,5 +1,4 @@ import { BaseRelationship } from "./base" -import { RelationshipTypes } from "../types" /** * ManyToMany relationship defines a relationship between two entities diff --git a/packages/core/utils/src/dml/relations/nullable.ts b/packages/core/utils/src/dml/relations/nullable.ts index 70fbdd659e..80e56dade0 100644 --- a/packages/core/utils/src/dml/relations/nullable.ts +++ b/packages/core/utils/src/dml/relations/nullable.ts @@ -1,4 +1,4 @@ -import { RelationshipType, PropertyType } from "../types" +import { RelationshipType } from "@medusajs/types" /** * Nullable modifier marks a schema node as nullable diff --git a/packages/core/utils/src/modules-sdk/medusa-internal-service.ts b/packages/core/utils/src/modules-sdk/medusa-internal-service.ts index b859c404cf..6860b36bbb 100644 --- a/packages/core/utils/src/modules-sdk/medusa-internal-service.ts +++ b/packages/core/utils/src/modules-sdk/medusa-internal-service.ts @@ -1,6 +1,7 @@ import { BaseFilterable, Context, + ExtractEntityType, FilterQuery, FilterQuery as InternalFilterQuery, FindConfig, @@ -100,9 +101,9 @@ export function MedusaInternalService( @InjectManager(propertyRepositoryName) async retrieve( idOrObject: string | object, - config: FindConfig = {}, + config: FindConfig> = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise> { const primaryKeys = AbstractService_.retrievePrimaryKeys(model) if ( @@ -162,7 +163,7 @@ export function MedusaInternalService( filters: FilterQuery | BaseFilterable> = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { AbstractService_.applyDefaultOrdering(config) AbstractService_.applyFreeTextSearchFilter(filters, config) @@ -179,7 +180,7 @@ export function MedusaInternalService( filters: FilterQuery | BaseFilterable> = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise<[TEntity[], number]> { + ): Promise<[ExtractEntityType[], number]> { AbstractService_.applyDefaultOrdering(config) AbstractService_.applyFreeTextSearchFilter(filters, config) @@ -191,16 +192,24 @@ export function MedusaInternalService( ) } - create(data: any, sharedContext?: Context): Promise - create(data: any[], sharedContext?: Context): Promise + create( + data: any, + sharedContext?: Context + ): Promise> + create( + data: any[], + sharedContext?: Context + ): Promise[]> @InjectTransactionManager(shouldForceTransaction, propertyRepositoryName) async create( data: any | any[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise | ExtractEntityType[]> { 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 + | ExtractEntityType[] } const data_ = Array.isArray(data) ? data : [data] @@ -212,24 +221,32 @@ export function MedusaInternalService( return Array.isArray(data) ? entities : entities[0] } - update(data: any[], sharedContext?: Context): Promise - update(data: any, sharedContext?: Context): Promise + update( + data: any[], + sharedContext?: Context + ): Promise[]> + update( + data: any, + sharedContext?: Context + ): Promise> update( selectorAndData: SelectorAndData, sharedContext?: Context - ): Promise + ): Promise[]> update( selectorAndData: SelectorAndData[], sharedContext?: Context - ): Promise + ): Promise[]> @InjectTransactionManager(shouldForceTransaction, propertyRepositoryName) async update( input: any | any[] | SelectorAndData | SelectorAndData[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise | ExtractEntityType[]> { 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 + | ExtractEntityType[] } const primaryKeys = AbstractService_.retrievePrimaryKeys(model) @@ -294,7 +311,8 @@ export function MedusaInternalService( // 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).name ?? model + const entityName = + (model as EntityClass>).name ?? model const compositeKeysValuesForFoundEntities = new Set( entitiesToUpdate.map((entity) => { @@ -447,7 +465,7 @@ export function MedusaInternalService( | InternalFilterQuery | InternalFilterQuery[], @MedusaContext() sharedContext: Context = {} - ): Promise<[TEntity[], Record]> { + ): Promise<[ExtractEntityType[], Record]> { if ( (Array.isArray(idsOrFilter) && !idsOrFilter.length) || (!Array.isArray(idsOrFilter) && !idsOrFilter) @@ -465,21 +483,27 @@ export function MedusaInternalService( async restore( idsOrFilter: string[] | InternalFilterQuery, @MedusaContext() sharedContext: Context = {} - ): Promise<[TEntity[], Record]> { + ): Promise<[ExtractEntityType[], Record]> { return await this[propertyRepositoryName].restore( idsOrFilter, sharedContext ) } - upsert(data: any[], sharedContext?: Context): Promise - upsert(data: any, sharedContext?: Context): Promise + upsert( + data: any[], + sharedContext?: Context + ): Promise[]> + upsert( + data: any, + sharedContext?: Context + ): Promise> @InjectTransactionManager(propertyRepositoryName) async upsert( data: any | any[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise | ExtractEntityType[]> { const data_ = Array.isArray(data) ? data : [data] const entities = await this[propertyRepositoryName].upsert( data_, @@ -490,24 +514,30 @@ export function MedusaInternalService( upsertWithReplace( data: any[], - config?: UpsertWithReplaceConfig, + config?: UpsertWithReplaceConfig>, sharedContext?: Context - ): Promise<{ entities: TEntity[]; performedActions: PerformedActions }> + ): Promise<{ + entities: ExtractEntityType[] + performedActions: PerformedActions + }> upsertWithReplace( data: any, - config?: UpsertWithReplaceConfig, + config?: UpsertWithReplaceConfig>, sharedContext?: Context - ): Promise<{ entities: TEntity; performedActions: PerformedActions }> + ): Promise<{ + entities: ExtractEntityType + performedActions: PerformedActions + }> @InjectTransactionManager(propertyRepositoryName) async upsertWithReplace( data: any | any[], - config: UpsertWithReplaceConfig = { + config: UpsertWithReplaceConfig> = { relations: [], }, @MedusaContext() sharedContext: Context = {} ): Promise<{ - entities: TEntity | TEntity[] + entities: ExtractEntityType | TEntity[] performedActions: PerformedActions }> { const data_ = Array.isArray(data) ? data : [data] diff --git a/packages/modules/inventory-next/src/services/inventory-level.ts b/packages/modules/inventory-next/src/services/inventory-level.ts index c9673f0965..99fbf7b1f1 100644 --- a/packages/modules/inventory-next/src/services/inventory-level.ts +++ b/packages/modules/inventory-next/src/services/inventory-level.ts @@ -8,11 +8,9 @@ type InjectedDependencies = { inventoryLevelRepository: InventoryLevelRepository } -export default class InventoryLevelService< - TEntity extends InventoryLevel = InventoryLevel -> extends ModulesSdkUtils.MedusaInternalService( +export default class InventoryLevelService extends ModulesSdkUtils.MedusaInternalService( InventoryLevel -) { +) { protected readonly inventoryLevelRepository: InventoryLevelRepository constructor(container: InjectedDependencies) { diff --git a/packages/modules/inventory-next/src/services/inventory-module.ts b/packages/modules/inventory-next/src/services/inventory-module.ts index e3c8a2519f..1a375baee5 100644 --- a/packages/modules/inventory-next/src/services/inventory-module.ts +++ b/packages/modules/inventory-next/src/services/inventory-module.ts @@ -30,7 +30,7 @@ import { IInventoryService } from "@medusajs/types/dist/inventory" type InjectedDependencies = { baseRepository: DAL.RepositoryService inventoryItemService: ModulesSdkTypes.IMedusaInternalService - inventoryLevelService: InventoryLevelService + inventoryLevelService: InventoryLevelService reservationItemService: ModulesSdkTypes.IMedusaInternalService } @@ -59,7 +59,7 @@ export default class InventoryModuleService protected readonly inventoryItemService_: ModulesSdkTypes.IMedusaInternalService protected readonly reservationItemService_: ModulesSdkTypes.IMedusaInternalService - protected readonly inventoryLevelService_: InventoryLevelService + protected readonly inventoryLevelService_: InventoryLevelService constructor( { diff --git a/packages/modules/order/src/services/order-change-service.ts b/packages/modules/order/src/services/order-change-service.ts index 205305dd9f..f8e49244d3 100644 --- a/packages/modules/order/src/services/order-change-service.ts +++ b/packages/modules/order/src/services/order-change-service.ts @@ -20,12 +20,10 @@ type InjectedDependencies = { orderChangeRepository: DAL.RepositoryService } -export default class OrderChangeService< - TEntity extends OrderChange = OrderChange -> extends ModulesSdkUtils.MedusaInternalService( +export default class OrderChangeService extends ModulesSdkUtils.MedusaInternalService( OrderChange -) { - protected readonly orderChangeRepository_: RepositoryService +) { + protected readonly orderChangeRepository_: RepositoryService constructor(container: InjectedDependencies) { // @ts-ignore @@ -38,7 +36,7 @@ export default class OrderChangeService< orderId: string | string[], config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { 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( + const queryConfig = ModulesSdkUtils.buildQuery( { id: lastChanges, order: { @@ -104,20 +102,20 @@ export default class OrderChangeService< } async create( - data: Partial[], + data: Partial[], sharedContext?: Context - ): Promise + ): Promise async create( - data: Partial, + data: Partial, sharedContext?: Context - ): Promise + ): Promise @InjectTransactionManager("orderChangeRepository_") async create( - data: Partial[] | Partial, + data: Partial[] | Partial, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { const dataArr = Array.isArray(data) ? data : [data] const activeOrderEdit = await this.listCurrentOrderChange( dataArr.map((d) => d.order_id!), diff --git a/packages/modules/order/src/services/order-module-service.ts b/packages/modules/order/src/services/order-module-service.ts index 4ce51f43a3..410751d30a 100644 --- a/packages/modules/order/src/services/order-module-service.ts +++ b/packages/modules/order/src/services/order-module-service.ts @@ -78,7 +78,7 @@ import OrderService from "./order-service" type InjectedDependencies = { baseRepository: DAL.RepositoryService - orderService: OrderService + orderService: OrderService addressService: ModulesSdkTypes.IMedusaInternalService lineItemService: ModulesSdkTypes.IMedusaInternalService shippingMethodAdjustmentService: ModulesSdkTypes.IMedusaInternalService @@ -87,7 +87,7 @@ type InjectedDependencies = { lineItemTaxLineService: ModulesSdkTypes.IMedusaInternalService shippingMethodTaxLineService: ModulesSdkTypes.IMedusaInternalService transactionService: ModulesSdkTypes.IMedusaInternalService - orderChangeService: OrderChangeService + orderChangeService: OrderChangeService orderChangeActionService: ModulesSdkTypes.IMedusaInternalService orderItemService: ModulesSdkTypes.IMedusaInternalService orderSummaryService: ModulesSdkTypes.IMedusaInternalService @@ -169,7 +169,7 @@ export default class OrderModuleService< implements IOrderModuleService { protected baseRepository_: DAL.RepositoryService - protected orderService_: OrderService + protected orderService_: OrderService protected addressService_: ModulesSdkTypes.IMedusaInternalService protected lineItemService_: ModulesSdkTypes.IMedusaInternalService protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService @@ -178,7 +178,7 @@ export default class OrderModuleService< protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService protected transactionService_: ModulesSdkTypes.IMedusaInternalService - protected orderChangeService_: OrderChangeService + protected orderChangeService_: OrderChangeService protected orderChangeActionService_: ModulesSdkTypes.IMedusaInternalService protected orderItemService_: ModulesSdkTypes.IMedusaInternalService protected orderSummaryService_: ModulesSdkTypes.IMedusaInternalService diff --git a/packages/modules/order/src/services/order-service.ts b/packages/modules/order/src/services/order-service.ts index fec0f7354b..c5c36c9460 100644 --- a/packages/modules/order/src/services/order-service.ts +++ b/packages/modules/order/src/services/order-service.ts @@ -17,12 +17,10 @@ type InjectedDependencies = { orderRepository: DAL.RepositoryService } -export default class OrderService< - TEntity extends Order = Order -> extends ModulesSdkUtils.MedusaInternalService( +export default class OrderService extends ModulesSdkUtils.MedusaInternalService( Order -) { - protected readonly orderRepository_: RepositoryService +) { + protected readonly orderRepository_: RepositoryService constructor(container: InjectedDependencies) { // @ts-ignore @@ -36,8 +34,8 @@ export default class OrderService< version: number, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { - const queryConfig = ModulesSdkUtils.buildQuery( + ): Promise { + const queryConfig = ModulesSdkUtils.buildQuery( { id, items: { version } }, { ...config, take: 1 } ) diff --git a/packages/modules/pricing/src/services/price-list.ts b/packages/modules/pricing/src/services/price-list.ts index 403377d8ef..d8680f23c3 100644 --- a/packages/modules/pricing/src/services/price-list.ts +++ b/packages/modules/pricing/src/services/price-list.ts @@ -7,11 +7,9 @@ type InjectedDependencies = { priceListRepository: DAL.RepositoryService } -export default class PriceListService< - TEntity extends PriceList = PriceList -> extends ModulesSdkUtils.MedusaInternalService( +export default class PriceListService extends ModulesSdkUtils.MedusaInternalService( PriceList -) { +) { constructor(container: InjectedDependencies) { // @ts-ignore super(...arguments) @@ -20,32 +18,32 @@ export default class PriceListService< create( data: ServiceTypes.CreatePriceListDTO[], sharedContext?: Context - ): Promise + ): Promise create( data: ServiceTypes.CreatePriceListDTO, sharedContext?: Context - ): Promise + ): Promise async create( data: ServiceTypes.CreatePriceListDTO | ServiceTypes.CreatePriceListDTO[], sharedContext?: Context - ): Promise { + ): Promise { 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 + update(data: any[], sharedContext?: Context): Promise // @ts-ignore - update(data: any, sharedContext?: Context): Promise + update(data: any, sharedContext?: Context): Promise // TODO: Add support for selector? and then rm ts ignore // @ts-ignore async update( data: ServiceTypes.UpdatePriceListDTO | ServiceTypes.UpdatePriceListDTO[], sharedContext?: Context - ): Promise { + ): Promise { const data_ = Array.isArray(data) ? data : [data] const priceLists = this.normalizePriceListDate(data_) return await super.update(priceLists, sharedContext) diff --git a/packages/modules/pricing/src/services/pricing-module.ts b/packages/modules/pricing/src/services/pricing-module.ts index 5841277085..e5a70b9fb5 100644 --- a/packages/modules/pricing/src/services/pricing-module.ts +++ b/packages/modules/pricing/src/services/pricing-module.ts @@ -58,11 +58,11 @@ type InjectedDependencies = { baseRepository: DAL.RepositoryService pricingRepository: PricingRepositoryService priceSetService: ModulesSdkTypes.IMedusaInternalService - ruleTypeService: RuleTypeService + ruleTypeService: RuleTypeService priceRuleService: ModulesSdkTypes.IMedusaInternalService priceSetRuleTypeService: ModulesSdkTypes.IMedusaInternalService priceService: ModulesSdkTypes.IMedusaInternalService - priceListService: PriceListService + priceListService: PriceListService priceListRuleService: ModulesSdkTypes.IMedusaInternalService priceListRuleValueService: ModulesSdkTypes.IMedusaInternalService } @@ -99,12 +99,12 @@ export default class PricingModuleService { protected baseRepository_: DAL.RepositoryService protected readonly pricingRepository_: PricingRepositoryService - protected readonly ruleTypeService_: RuleTypeService + protected readonly ruleTypeService_: RuleTypeService protected readonly priceSetService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceRuleService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceSetRuleTypeService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceService_: ModulesSdkTypes.IMedusaInternalService - protected readonly priceListService_: PriceListService + protected readonly priceListService_: PriceListService protected readonly priceListRuleService_: ModulesSdkTypes.IMedusaInternalService protected readonly priceListRuleValueService_: ModulesSdkTypes.IMedusaInternalService diff --git a/packages/modules/pricing/src/services/rule-type.ts b/packages/modules/pricing/src/services/rule-type.ts index b7ec25baa3..26480f97b6 100644 --- a/packages/modules/pricing/src/services/rule-type.ts +++ b/packages/modules/pricing/src/services/rule-type.ts @@ -11,12 +11,10 @@ type InjectedDependencies = { ruleTypeRepository: DAL.RepositoryService } -export default class RuleTypeService< - TEntity extends RuleType = RuleType -> extends ModulesSdkUtils.MedusaInternalService( +export default class RuleTypeService extends ModulesSdkUtils.MedusaInternalService( RuleType -) { - protected readonly ruleTypeRepository_: DAL.RepositoryService +) { + protected readonly ruleTypeRepository_: DAL.RepositoryService constructor({ ruleTypeRepository }: InjectedDependencies) { // @ts-ignore @@ -27,17 +25,17 @@ export default class RuleTypeService< create( data: PricingTypes.CreateRuleTypeDTO, sharedContext: Context - ): Promise + ): Promise create( data: PricingTypes.CreateRuleTypeDTO[], sharedContext: Context - ): Promise + ): Promise @InjectTransactionManager("ruleTypeRepository_") async create( data: PricingTypes.CreateRuleTypeDTO | PricingTypes.CreateRuleTypeDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { 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 + ): Promise // @ts-ignore update( data: PricingTypes.UpdateRuleTypeDTO, sharedContext: Context - ): Promise + ): Promise @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 { + ): Promise { const data_ = Array.isArray(data) ? data : [data] validateRuleAttributes(data_.map((d) => d.rule_attribute)) return await super.update(data, sharedContext) diff --git a/packages/modules/product/src/services/product-category.ts b/packages/modules/product/src/services/product-category.ts index 4ba1b685ce..87305ee2c6 100644 --- a/packages/modules/product/src/services/product-category.ts +++ b/packages/modules/product/src/services/product-category.ts @@ -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 = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { 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 = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { 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 = {}, @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 { + ): Promise { 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 { + ): Promise { return (await ( this.productCategoryRepository_ as unknown as ProductCategoryRepository - ).update(data, sharedContext)) as TEntity[] + ).update(data, sharedContext)) as ProductCategory[] } @InjectTransactionManager("productCategoryRepository_") diff --git a/packages/modules/product/src/services/product-module-service.ts b/packages/modules/product/src/services/product-module-service.ts index 47c7fb25b7..f7acad89cf 100644 --- a/packages/modules/product/src/services/product-module-service.ts +++ b/packages/modules/product/src/services/product-module-service.ts @@ -55,10 +55,10 @@ import { entityNameToLinkableKeysMap, joinerConfig } from "./../joiner-config" type InjectedDependencies = { baseRepository: DAL.RepositoryService - productService: ProductService + productService: ProductService productVariantService: ModulesSdkTypes.IMedusaInternalService productTagService: ModulesSdkTypes.IMedusaInternalService - productCategoryService: ProductCategoryService + productCategoryService: ProductCategoryService productCollectionService: ModulesSdkTypes.IMedusaInternalService productImageService: ModulesSdkTypes.IMedusaInternalService productTypeService: ModulesSdkTypes.IMedusaInternalService @@ -105,9 +105,9 @@ export default class ProductModuleService implements ProductTypes.IProductModuleService { protected baseRepository_: DAL.RepositoryService - protected readonly productService_: ProductService + protected readonly productService_: ProductService protected readonly productVariantService_: ModulesSdkTypes.IMedusaInternalService - protected readonly productCategoryService_: ProductCategoryService + protected readonly productCategoryService_: ProductCategoryService protected readonly productTagService_: ModulesSdkTypes.IMedusaInternalService protected readonly productCollectionService_: ModulesSdkTypes.IMedusaInternalService protected readonly productImageService_: ModulesSdkTypes.IMedusaInternalService diff --git a/packages/modules/product/src/services/product.ts b/packages/modules/product/src/services/product.ts index c5f4799a78..1470f21828 100644 --- a/packages/modules/product/src/services/product.ts +++ b/packages/modules/product/src/services/product.ts @@ -18,12 +18,10 @@ type NormalizedFilterableProductProps = ProductTypes.FilterableProductProps & { } } -export default class ProductService< - TEntity extends Product = Product -> extends ModulesSdkUtils.MedusaInternalService( +export default class ProductService extends ModulesSdkUtils.MedusaInternalService( Product -) { - protected readonly productRepository_: DAL.RepositoryService +) { + protected readonly productRepository_: DAL.RepositoryService constructor({ productRepository }: InjectedDependencies) { // @ts-ignore @@ -36,9 +34,9 @@ export default class ProductService< @InjectManager("productRepository_") async list( filters: ProductTypes.FilterableProductProps = {}, - config: FindConfig = {}, + config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise { return await super.list( ProductService.normalizeFilters(filters), config, @@ -51,7 +49,7 @@ export default class ProductService< filters: ProductTypes.FilterableProductProps = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} - ): Promise<[TEntity[], number]> { + ): Promise<[Product[], number]> { return await super.listAndCount( ProductService.normalizeFilters(filters), config,