feat: notifications (#172)

The Notifications API allows plugins to register Notification Providers which have `sendNotification` and `resendNotification`.

Each plugin can listen to any events transmittet over the event bus and the result of the notification send will be persisted in the database to allow for clear communications timeline + ability to resend notifications.
This commit is contained in:
Sebastian Rindom
2021-02-15 11:59:37 +01:00
committed by GitHub
parent 4229e241d0
commit 7308946e56
46 changed files with 1538 additions and 254 deletions
@@ -0,0 +1,10 @@
import { Entity, Column, PrimaryColumn } from "typeorm"
@Entity()
export class NotificationProvider {
@PrimaryColumn()
id: string
@Column({ default: true })
is_installed: boolean
}
@@ -0,0 +1,80 @@
import {
Entity,
BeforeInsert,
Column,
CreateDateColumn,
UpdateDateColumn,
Index,
PrimaryColumn,
OneToMany,
ManyToOne,
JoinColumn,
} from "typeorm"
import { ulid } from "ulid"
import { Customer } from "./customer"
import { NotificationProvider } from "./notification-provider"
@Entity()
export class Notification {
@PrimaryColumn()
id: string
@Column({ nullable: true })
event_name: string
@Index()
@Column()
resource_type: string
@Index()
@Column()
resource_id: string
@Index()
@Column({ nullable: true })
customer_id: string
@ManyToOne(() => Customer)
@JoinColumn({ name: "customer_id" })
customer: Customer
@Column()
to: string
@Column({ type: "jsonb" })
data: any
@Column({ nullable: true })
parent_id: string
@ManyToOne(() => Notification)
@JoinColumn({ name: "parent_id" })
parent_notification: Notification
@OneToMany(
() => Notification,
noti => noti.parent_notification
)
resends: Notification[]
@Column({ nullable: true })
provider_id: string
@ManyToOne(() => NotificationProvider)
@JoinColumn({ name: "provider_id" })
provider: NotificationProvider
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@UpdateDateColumn({ type: "timestamptz" })
updated_at: Date
@BeforeInsert()
private beforeInsert() {
if (this.id) return
const id = ulid()
this.id = `noti_${id}`
}
}