2023-09-06 11:23:21 +02:00
|
|
|
import { InjectQueue, Process, Processor } from '@nestjs/bull'
|
2023-09-13 20:52:25 +02:00
|
|
|
import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'
|
2023-09-20 11:03:20 +02:00
|
|
|
import { ConfigService } from '@nestjs/config'
|
2023-09-06 11:23:21 +02:00
|
|
|
import { Job, JobOptions, Queue } from 'bull'
|
|
|
|
|
import { Dayjs } from 'dayjs'
|
2023-09-05 09:12:04 +02:00
|
|
|
|
|
|
|
|
import { NtfyClient } from './client'
|
2023-09-23 23:01:24 +02:00
|
|
|
import { MessageConfig, MessageTags, NTFY_PUBLISH_QUEUE_NAME } from './types'
|
2023-09-05 09:12:04 +02:00
|
|
|
|
2023-09-06 11:23:21 +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(
|
2023-09-20 11:03:20 +02:00
|
|
|
@Inject(ConfigService)
|
|
|
|
|
private readonly configService: ConfigService,
|
|
|
|
|
|
2023-09-05 09:12:04 +02:00
|
|
|
@Inject(NtfyClient)
|
|
|
|
|
private readonly ntfyClient: NtfyClient,
|
2023-09-06 11:23:21 +02:00
|
|
|
|
|
|
|
|
@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()
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 11:23:21 +02:00
|
|
|
@Process()
|
|
|
|
|
async handlePublishJob(job: Job<Omit<MessageConfig, 'topic'>>) {
|
2023-09-05 09:12:04 +02:00
|
|
|
await this.ntfyClient.publish({
|
2023-09-06 11:23:21 +02:00
|
|
|
...job.data,
|
2023-09-05 09:12:04 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 11:23:21 +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() {
|
2023-09-20 11:03:20 +02:00
|
|
|
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',
|
2023-09-20 11:03:20 +02:00
|
|
|
message: `Version ${gitCommit}`,
|
2023-09-13 20:52:25 +02:00
|
|
|
tags: [MessageTags.badminton],
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 11:23:21 +02:00
|
|
|
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
|
|
|
) {
|
2023-09-06 11:23:21 +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
|
|
|
}
|
2023-09-20 09:26:41 +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
|
|
|
}
|