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

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

View File

@@ -1,46 +1,11 @@
import { generateEntityId } from "@medusajs/utils"
import {
ArrayType,
BeforeCreate,
Collection,
Entity,
OnInit,
OneToMany,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Notification from "./notification"
import { model } from "@medusajs/utils"
import { Notification } from "./notification"
@Entity()
export default class NotificationProvider {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
handle: string
@Property({ columnType: "text" })
name: string
@Property({ columnType: "boolean", defaultRaw: "true" })
is_enabled: boolean = true
@Property({ type: ArrayType })
channels: string[]
@OneToMany({
entity: () => Notification,
mappedBy: (notification) => notification.provider_id,
})
notifications = new Collection<Notification>(this)
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "notpro")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "notpro")
}
}
export const NotificationProvider = model.define("notificationProvider", {
id: model.id({ prefix: "notpro" }),
handle: model.text(),
name: model.text(),
is_enabled: model.boolean().default(true),
channels: model.array().default([]),
notifications: model.hasMany(() => Notification, { mappedBy: "provider" }),
})

View File

@@ -1,109 +1,30 @@
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Entity,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import NotificationProvider from "./notification-provider"
const NotificationProviderIdIndex = createPsqlIndexStatementHelper({
tableName: "notification",
columns: "provider_id",
})
const NotificationIdempotencyKeyIndex = createPsqlIndexStatementHelper({
tableName: "notification",
columns: "idempotency_key",
})
const NotificationReceiverIdIndex = createPsqlIndexStatementHelper({
tableName: "notification",
columns: "receiver_id",
})
// We don't need to support soft deletes here as this information is mainly used for auditing purposes.
// Instead, we probably want to have a TTL for each entry, so we don't bloat the DB (and also for GDPR reasons if TTL < 30 days).
@NotificationProviderIdIndex.MikroORMIndex()
@NotificationIdempotencyKeyIndex.MikroORMIndex()
@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 Notification {
@PrimaryKey({ columnType: "text" })
id: string
import { model } from "@medusajs/utils"
import { NotificationProvider } from "./notification-provider"
// We probably want to have a TTL for each entry, so we don't bloat the DB (and also for GDPR reasons if TTL < 30 days).
export const Notification = model.define("notification", {
id: model.id({ prefix: "noti" }),
// This can be an email, phone number, or username, depending on the channel.
@Property({ columnType: "text" })
to: string
@Property({ columnType: "text" })
channel: string
to: model.text(),
channel: model.text(),
// The template name in the provider's system.
@Property({ columnType: "text" })
template: string
template: model.text(),
// The data that gets passed over to the provider for rendering the notification.
@Property({ columnType: "jsonb", nullable: true })
data: Record<string, unknown> | null
data: model.json().nullable(),
// This can be the event name, the workflow, or anything else that can help to identify what triggered the notification.
@Property({ columnType: "text", nullable: true })
trigger_type?: string | null
trigger_type: model.text().nullable(),
// The ID of the resource this notification is for, if applicable. Useful for displaying relevant information in the UI
@Property({ columnType: "text", nullable: true })
resource_id?: string | null
resource_id: model.text().nullable(),
// The typeame of the resource this notification is for, if applicable, eg. "order"
@Property({ columnType: "text", nullable: true })
resource_type?: string | null
resource_type: model.text().nullable(),
// The ID of the receiver of the notification, if applicable. This can be a customer, user, a company, or anything else.
@Property({ columnType: "text", nullable: true })
receiver_id?: string | null
receiver_id: model.text().index().nullable(),
// The original notification, in case this is a retried notification.
@Property({ columnType: "text", nullable: true })
original_notification_id?: string | null
@Property({ columnType: "text", nullable: true })
idempotency_key?: string | null
original_notification_id: model.text().nullable(),
idempotency_key: model.text().unique().nullable(),
// The ID of the notification in the external system, if applicable
@Property({ columnType: "text", nullable: true })
external_id?: string | null
@ManyToOne(() => NotificationProvider, {
columnType: "text",
fieldName: "provider_id",
mapToPk: true,
nullable: true,
onDelete: "set null",
})
provider_id: string
@ManyToOne(() => NotificationProvider, { persist: false })
provider: NotificationProvider
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@BeforeCreate()
@OnInit()
onCreate() {
this.id = generateEntityId(this.id, "noti")
this.provider_id ??= this.provider_id ?? this.provider?.id
}
}
external_id: model.text().nullable(),
provider: model
.belongsTo(() => NotificationProvider, { mappedBy: "notifications" })
.nullable(),
})