-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): add notification subscription database entity (#910)
* chore(api): add notification subscription database entity * chore(repo): fix typo in watchtower cron job * feat(api): persist notification subscription in database --------- Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 35 additions & 5 deletions
40
services/api/src/core/notifications/domain/services/notification-subscription.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { Repository } from "typeorm"; | ||
import { PushSubscription } from "web-push"; | ||
|
||
const SUBSCRIPTIONS: Record<number, PushSubscription> = {}; | ||
import { NotificationSubscription } from "../../infrastructure/entities/notification-subscription.entity"; | ||
|
||
@Injectable() | ||
export class NotificationSubscriptionService { | ||
getSubscription(profileId: number): PushSubscription | undefined { | ||
return SUBSCRIPTIONS[profileId]; | ||
constructor( | ||
@InjectRepository(NotificationSubscription) | ||
private readonly notificationSubscriptions: Repository<NotificationSubscription>, | ||
) {} | ||
|
||
async getSubscription( | ||
profileId: number, | ||
): Promise<PushSubscription | undefined> { | ||
const notificationSubscription = | ||
await this.notificationSubscriptions.findOne({ | ||
where: { profileId }, | ||
}); | ||
|
||
if (!notificationSubscription) { | ||
return; | ||
} | ||
|
||
return notificationSubscription.subscription as unknown as PushSubscription; | ||
} | ||
|
||
saveSubscription(profileId: number, subscription: PushSubscription) { | ||
SUBSCRIPTIONS[profileId] = subscription; | ||
async saveSubscription(profileId: number, subscription: PushSubscription) { | ||
let notificationSubscription = await this.notificationSubscriptions.findOne( | ||
{ | ||
where: { profileId }, | ||
}, | ||
); | ||
|
||
if (!notificationSubscription) { | ||
notificationSubscription = new NotificationSubscription(); | ||
notificationSubscription.profileId = profileId; | ||
} | ||
|
||
notificationSubscription.subscription = subscription; | ||
await this.notificationSubscriptions.save(notificationSubscription); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...es/api/src/core/notifications/infrastructure/entities/notification-subscription.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Column, Entity, ManyToOne } from "typeorm"; | ||
|
||
import { BaseEntity } from "src/core/database"; | ||
import { Profile } from "src/core/profiles"; | ||
|
||
@Entity() | ||
export class NotificationSubscription extends BaseEntity { | ||
@ManyToOne(() => Profile, { onDelete: "CASCADE" }) | ||
profile!: Profile; | ||
|
||
@Column() | ||
profileId!: number; | ||
|
||
@Column("json") | ||
subscription!: object; | ||
} |
16 changes: 16 additions & 0 deletions
16
...core/notifications/infrastructure/migrations/1721483513933-AddNotificationSubscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class AddNotificationSubscription1721483513933 implements MigrationInterface { | ||
name = 'AddNotificationSubscription1721483513933' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "notification_subscription" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "profileId" integer NOT NULL, "subscription" json NOT NULL, CONSTRAINT "PK_d5c0b2efb91da0d584fddab9542" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`ALTER TABLE "notification_subscription" ADD CONSTRAINT "FK_9ce8c93447319240e642238bff4" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "notification_subscription" DROP CONSTRAINT "FK_9ce8c93447319240e642238bff4"`); | ||
await queryRunner.query(`DROP TABLE "notification_subscription"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters