chore: Migrate notification module to DML (#7835)

This commit is contained in:
Stevche Radevski
2024-07-01 11:17:32 +02:00
committed by GitHub
parent c661180c44
commit 9daec5d7ac
11 changed files with 161 additions and 177 deletions

View File

@@ -6,6 +6,7 @@ import {
ModuleJoinerConfig,
ModulesSdkTypes,
NotificationTypes,
InferEntityType,
} from "@medusajs/types"
import {
InjectManager,
@@ -21,7 +22,9 @@ import NotificationProviderService from "./notification-provider"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
notificationService: ModulesSdkTypes.IMedusaInternalService<any>
notificationService: ModulesSdkTypes.IMedusaInternalService<
typeof Notification
>
notificationProviderService: NotificationProviderService
}
@@ -32,7 +35,9 @@ export default class NotificationModuleService
implements INotificationModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly notificationService_: ModulesSdkTypes.IMedusaInternalService<Notification>
protected readonly notificationService_: ModulesSdkTypes.IMedusaInternalService<
typeof Notification
>
protected readonly notificationProviderService_: NotificationProviderService
constructor(
@@ -91,7 +96,7 @@ export default class NotificationModuleService
protected async createNotifications_(
data: NotificationTypes.CreateNotificationDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<Notification[]> {
): Promise<InferEntityType<typeof Notification>[]> {
if (!data.length) {
return []
}
@@ -108,12 +113,13 @@ export default class NotificationModuleService
{ take: null },
sharedContext
)
const existsMap = new Map(
alreadySentNotifications.map((n) => [n.idempotency_key, true])
alreadySentNotifications.map((n) => [n.idempotency_key as string, true])
)
const notificationsToProcess = data.filter(
(entry) => !existsMap.has(entry.idempotency_key)
(entry) => !entry.idempotency_key || !existsMap.has(entry.idempotency_key)
)
const notificationsToCreate = await promiseAll(

View File

@@ -1,10 +1,12 @@
import { DAL, NotificationTypes } from "@medusajs/types"
import { DAL, InferEntityType, NotificationTypes } from "@medusajs/types"
import { MedusaError, ModulesSdkUtils } from "@medusajs/utils"
import { NotificationProvider } from "@models"
import { NotificationProviderRegistrationPrefix } from "@types"
type InjectedDependencies = {
notificationProviderRepository: DAL.RepositoryService
notificationProviderRepository: DAL.RepositoryService<
InferEntityType<typeof NotificationProvider>
>
[
key: `${typeof NotificationProviderRegistrationPrefix}${string}`
]: NotificationTypes.INotificationProvider
@@ -13,9 +15,14 @@ type InjectedDependencies = {
export default class NotificationProviderService extends ModulesSdkUtils.MedusaInternalService<InjectedDependencies>(
NotificationProvider
) {
protected readonly notificationProviderRepository_: DAL.RepositoryService<NotificationProvider>
protected readonly notificationProviderRepository_: DAL.RepositoryService<
InferEntityType<typeof NotificationProvider>
>
// We can store the providers in a memory since they can only be registered on startup and not changed during runtime
protected providersCache: Map<string, NotificationProvider>
protected providersCache: Map<
string,
InferEntityType<typeof NotificationProvider>
>
constructor(container: InjectedDependencies) {
super(container)
@@ -40,7 +47,7 @@ export default class NotificationProviderService extends ModulesSdkUtils.MedusaI
async getProviderForChannel(
channel: string
): Promise<NotificationProvider | undefined> {
): Promise<InferEntityType<typeof NotificationProvider> | undefined> {
if (!this.providersCache) {
const providers = await this.notificationProviderRepository_.find()
this.providersCache = new Map(
@@ -54,7 +61,7 @@ export default class NotificationProviderService extends ModulesSdkUtils.MedusaI
}
async send(
provider: NotificationProvider,
provider: InferEntityType<typeof NotificationProvider>,
notification: NotificationTypes.ProviderSendNotificationDTO
): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> {
const providerHandler = this.retrieveProviderRegistration(provider.id)