autobaan/src/ntfy/provider.ts

135 lines
3.3 KiB
TypeScript
Raw Normal View History

import { InjectQueue, Process, Processor } from '@nestjs/bull'
2023-09-13 20:52:25 +02:00
import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Job, JobOptions, Queue } from 'bull'
import { Dayjs } from 'dayjs'
2023-09-05 09:12:04 +02:00
import { NtfyClient } from './client'
import { MessageConfig, MessageTags, NTFY_PUBLISH_QUEUE_NAME } from './types'
2023-09-05 09:12:04 +02:00
@Processor(NTFY_PUBLISH_QUEUE_NAME)
2023-09-05 09:12:04 +02:00
@Injectable()
2023-09-13 20:52:25 +02:00
export class NtfyProvider implements OnApplicationBootstrap {
2023-09-05 09:12:04 +02:00
constructor(
@Inject(ConfigService)
private readonly configService: ConfigService,
2023-09-05 09:12:04 +02:00
@Inject(NtfyClient)
private readonly ntfyClient: NtfyClient,
@InjectQueue(NTFY_PUBLISH_QUEUE_NAME)
private readonly publishQueue: Queue,
2023-09-05 09:12:04 +02:00
) {}
2023-09-13 20:52:25 +02:00
async onApplicationBootstrap() {
await this.sendBootstrappedNotification()
}
@Process()
async handlePublishJob(job: Job<Omit<MessageConfig, 'topic'>>) {
2023-09-05 09:12:04 +02:00
await this.ntfyClient.publish({
...job.data,
2023-09-05 09:12:04 +02:00
})
}
private static defaultJob(
data: Omit<MessageConfig, 'topic'>,
): [Omit<MessageConfig, 'topic'>, JobOptions] {
return [
data,
{
attempts: 3,
removeOnComplete: true,
backoff: {
type: 'exponential',
},
},
]
}
2023-09-13 20:52:25 +02:00
async sendBootstrappedNotification() {
const gitCommit = this.configService.get<string>('GIT_COMMIT')
2023-09-13 20:52:25 +02:00
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Autobaan up and running',
message: `Version ${gitCommit}`,
2023-09-13 20:52:25 +02:00
tags: [MessageTags.badminton],
}),
)
}
async sendCronStartNotification(title: string) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title,
tags: [MessageTags.alarm_clock, MessageTags.green_circle],
}),
)
}
async sendCronStopNotification(title: string, message: string) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title,
message,
tags: [MessageTags.alarm_clock, MessageTags.red_circle],
}),
)
}
async sendPerformingReservationNotification(
reservationId: string,
startTime: Dayjs,
endTime: Dayjs,
2023-09-05 09:12:04 +02:00
) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Performing reservation',
message: `${reservationId} - ${startTime.format()} to ${endTime.format()}`,
tags: [MessageTags.badminton],
}),
)
}
async sendErrorPerformingReservationNotification(
reservationId: string,
startTime: Dayjs,
endTime: Dayjs,
error: Error,
) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Error performing reservation',
message: `${reservationId} - ${startTime.format()} to ${endTime.format()} : (${
error.name
}) - ${error.message}`,
tags: [MessageTags.badminton, MessageTags.red_x],
}),
)
}
async sendReservationWaitlistedNotification(
reservationId: string,
startTime: Dayjs,
endTime: Dayjs,
) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Reservation waitlisted',
message: `${reservationId} - ${startTime.format()} to ${endTime.format()}`,
tags: [MessageTags.badminton, MessageTags.hourglass],
}),
)
2023-09-05 09:12:04 +02:00
}
async sendWaitListEmailReceivedNotification(subject: string) {
await this.publishQueue.add(
...NtfyProvider.defaultJob({
title: 'Wait listed reservation available',
message: `${subject}`,
tags: [MessageTags.badminton, MessageTags.hourglass],
}),
)
}
2023-09-05 09:12:04 +02:00
}