Chore/rm main entity concept (#7709)

**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2024-06-19 15:02:16 +02:00
committed by GitHub
parent 2895ccfba8
commit 48963f55ef
533 changed files with 6469 additions and 9769 deletions

View File

@@ -1,7 +1,10 @@
import { moduleDefinition } from "./module-definition"
import { ModuleExports } from "@medusajs/types"
import { NotificationModuleService } from "@services"
import loadProviders from "./loaders/providers"
export * from "./types"
export * from "./models"
export * from "./services"
export const moduleDefinition: ModuleExports = {
service: NotificationModuleService,
loaders: [loadProviders],
}
export default moduleDefinition

View File

@@ -1,33 +1,13 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { NotificationModel } from "@models"
import {
buildEntitiesNameToLinkableKeysMap,
defineJoinerConfig,
MapToConfig,
} from "@medusajs/utils"
export const LinkableKeys: Record<string, string> = {
notification_id: NotificationModel.name,
}
const entityLinkableKeysMap: MapToConfig = {}
Object.entries(LinkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
entityLinkableKeysMap[value].push({
mapTo: key,
valueFrom: key.split("_").pop()!,
})
export const joinerConfig = defineJoinerConfig(Modules.NOTIFICATION, {
entityQueryingConfig: [{ name: "Notification" }],
})
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
export const joinerConfig: ModuleJoinerConfig = {
serviceName: Modules.NOTIFICATION,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: [
{
name: ["notification", "notifications"],
args: {
entity: NotificationModel.name,
},
},
],
} as ModuleJoinerConfig
export const entityNameToLinkableKeysMap: MapToConfig =
buildEntitiesNameToLinkableKeysMap(joinerConfig.linkableKeys)

View File

@@ -1,2 +1,3 @@
export { default as NotificationModel } from "./notification"
export { default as Notification } from "./notification"
export { default as NotificationProvider } from "./notification-provider"

View File

@@ -9,7 +9,7 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import NotificationModel from "./notification"
import Notification from "./notification"
@Entity()
export default class NotificationProvider {
@@ -29,10 +29,10 @@ export default class NotificationProvider {
channels: string[]
@OneToMany({
entity: () => NotificationModel,
entity: () => Notification,
mappedBy: (notification) => notification.provider_id,
})
notifications = new Collection<NotificationModel>(this)
notifications = new Collection<Notification>(this)
@BeforeCreate()
onCreate() {

View File

@@ -35,7 +35,7 @@ const NotificationReceiverIdIndex = createPsqlIndexStatementHelper({
@NotificationReceiverIdIndex.MikroORMIndex()
@Entity({ tableName: "notification" })
// Since there is a native `Notification` type, we have to call this something else here and in a couple of other places.
export default class NotificationModel {
export default class Notification {
@PrimaryKey({ columnType: "text" })
id: string

View File

@@ -1,11 +0,0 @@
import { ModuleExports } from "@medusajs/types"
import { NotificationModuleService } from "@services"
import loadProviders from "./loaders/providers"
const service = NotificationModuleService
const loaders = [loadProviders]
export const moduleDefinition: ModuleExports = {
service,
loaders,
}

View File

@@ -12,40 +12,33 @@ import {
InjectTransactionManager,
MedusaContext,
MedusaError,
ModulesSdkUtils,
MedusaService,
promiseAll,
} from "@medusajs/utils"
import { Notification } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import NotificationProviderService from "./notification-provider"
import { NotificationModel, NotificationProvider } from "@models"
const generateMethodForModels = { NotificationProvider }
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
notificationModelService: ModulesSdkTypes.IMedusaInternalService<any>
notificationService: ModulesSdkTypes.IMedusaInternalService<any>
notificationProviderService: NotificationProviderService
}
export default class NotificationModuleService<
TEntity extends NotificationModel = NotificationModel
>
extends ModulesSdkUtils.MedusaService<
NotificationTypes.NotificationDTO,
{
NotificationProvider: { dto: NotificationTypes.NotificationProviderDTO }
}
>(NotificationModel, generateMethodForModels, entityNameToLinkableKeysMap)
export default class NotificationModuleService
extends MedusaService<{
Notification: { dto: NotificationTypes.NotificationDTO }
}>({ Notification }, entityNameToLinkableKeysMap)
implements INotificationModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly notificationService_: ModulesSdkTypes.IMedusaInternalService<TEntity>
protected readonly notificationService_: ModulesSdkTypes.IMedusaInternalService<Notification>
protected readonly notificationProviderService_: NotificationProviderService
constructor(
{
baseRepository,
notificationModelService,
notificationService,
notificationProviderService,
}: InjectedDependencies,
protected readonly moduleDeclaration: InternalModuleDeclaration
@@ -53,24 +46,26 @@ export default class NotificationModuleService<
// @ts-ignore
super(...arguments)
this.baseRepository_ = baseRepository
this.notificationService_ = notificationModelService
this.notificationService_ = notificationService
this.notificationProviderService_ = notificationProviderService
}
__joinerConfig(): ModuleJoinerConfig {
return joinerConfig
}
create(
// @ts-expect-error
createNotifications(
data: NotificationTypes.CreateNotificationDTO[],
sharedContext?: Context
): Promise<NotificationTypes.NotificationDTO[]>
create(
createNotifications(
data: NotificationTypes.CreateNotificationDTO,
sharedContext?: Context
): Promise<NotificationTypes.NotificationDTO>
@InjectManager("baseRepository_")
async create(
async createNotifications(
data:
| NotificationTypes.CreateNotificationDTO
| NotificationTypes.CreateNotificationDTO[],
@@ -80,7 +75,10 @@ export default class NotificationModuleService<
> {
const normalized = Array.isArray(data) ? data : [data]
const createdNotifications = await this.create_(normalized, sharedContext)
const createdNotifications = await this.createNotifications_(
normalized,
sharedContext
)
const serialized = await this.baseRepository_.serialize<
NotificationTypes.NotificationDTO[]
@@ -90,10 +88,10 @@ export default class NotificationModuleService<
}
@InjectTransactionManager("baseRepository_")
protected async create_(
protected async createNotifications_(
data: NotificationTypes.CreateNotificationDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity[]> {
): Promise<Notification[]> {
if (!data.length) {
return []
}