Adding auth and bootstrap message

This commit is contained in:
Collin Duncan 2023-09-13 20:52:25 +02:00
parent 62a4b8c9e6
commit bd634f9742
No known key found for this signature in database
2 changed files with 17 additions and 2 deletions

View file

@ -14,10 +14,12 @@ export class NtfyClient {
) { ) {
const host = this.configService.getOrThrow<string>('NTFY_HOST') const host = this.configService.getOrThrow<string>('NTFY_HOST')
this.topic = this.configService.getOrThrow<string>('NTFY_TOPIC') this.topic = this.configService.getOrThrow<string>('NTFY_TOPIC')
const token = this.configService.getOrThrow<string>('NTFY_TOKEN')
this.httpClient = new Axios({ this.httpClient = new Axios({
baseURL: `https://${host}`, baseURL: `https://${host}`,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
}, },
}) })
} }

View file

@ -1,5 +1,5 @@
import { InjectQueue, Process, Processor } from '@nestjs/bull' import { InjectQueue, Process, Processor } from '@nestjs/bull'
import { Inject, Injectable } from '@nestjs/common' import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'
import { Job, JobOptions, Queue } from 'bull' import { Job, JobOptions, Queue } from 'bull'
import { Dayjs } from 'dayjs' import { Dayjs } from 'dayjs'
@ -13,7 +13,7 @@ import {
@Processor(NTFY_PUBLISH_QUEUE_NAME) @Processor(NTFY_PUBLISH_QUEUE_NAME)
@Injectable() @Injectable()
export class NtfyProvider { export class NtfyProvider implements OnApplicationBootstrap {
constructor( constructor(
@Inject(NtfyClient) @Inject(NtfyClient)
private readonly ntfyClient: NtfyClient, private readonly ntfyClient: NtfyClient,
@ -22,6 +22,10 @@ export class NtfyProvider {
private readonly publishQueue: Queue, private readonly publishQueue: Queue,
) {} ) {}
async onApplicationBootstrap() {
await this.sendBootstrappedNotification()
}
@Process() @Process()
async handlePublishJob(job: Job<Omit<MessageConfig, 'topic'>>) { async handlePublishJob(job: Job<Omit<MessageConfig, 'topic'>>) {
await this.ntfyClient.publish({ await this.ntfyClient.publish({
@ -44,6 +48,15 @@ export class NtfyProvider {
] ]
} }
async sendBootstrappedNotification() {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Autobaan up and running',
tags: [MessageTags.badminton],
}),
)
}
async sendCronStartNotification(title: string) { async sendCronStartNotification(title: string) {
await this.publishQueue.add( await this.publishQueue.add(
...NtfyProvider.defaultJob({ ...NtfyProvider.defaultJob({