Skip to content

Commit d7e94e4

Browse files
committed
feat: inject Discord webhook URL into SongWebhookService and refactor usage
1 parent 568a2b1 commit d7e94e4

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

server/src/song/song-webhook/song-webhook.service.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
1+
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
22
import { InjectModel } from '@nestjs/mongoose';
33
import { Model } from 'mongoose';
44

@@ -12,6 +12,8 @@ export class SongWebhookService implements OnModuleInit {
1212
constructor(
1313
@InjectModel(SongEntity.name)
1414
private songModel: Model<SongEntity>,
15+
@Inject('DISCORD_WEBHOOK_URL')
16+
private readonly discordWebhookUrl: string | undefined,
1517
) {}
1618

1719
async onModuleInit() {
@@ -28,7 +30,7 @@ export class SongWebhookService implements OnModuleInit {
2830
* @throws {Error} If the Discord webhook URL is not found.
2931
* @throws {Error} If there is an error sending the webhook message.
3032
*/
31-
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
33+
const webhookUrl = this.discordWebhookUrl;
3234

3335
if (!webhookUrl) {
3436
this.logger.error('Discord webhook URL not found');
@@ -71,7 +73,7 @@ export class SongWebhookService implements OnModuleInit {
7173
throw new Error('Song does not have a webhook message');
7274
}
7375

74-
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
76+
const webhookUrl = this.discordWebhookUrl;
7577

7678
if (!webhookUrl) {
7779
this.logger.error('Discord webhook URL not found');
@@ -110,7 +112,7 @@ export class SongWebhookService implements OnModuleInit {
110112
throw new Error('Song does not have a webhook message');
111113
}
112114

113-
const webhookUrl = process.env.DISCORD_WEBHOOK_URL;
115+
const webhookUrl = this.discordWebhookUrl;
114116

115117
if (!webhookUrl) {
116118
this.logger.error('Discord webhook URL not found');
@@ -175,7 +177,7 @@ export class SongWebhookService implements OnModuleInit {
175177
.sort({ createdAt: 1 })
176178
.populate('uploader', 'username profileImage -_id');
177179

178-
for await (const songDocument of songQuery) {
180+
for (const songDocument of await songQuery) {
179181
const webhookMessageId = await this.syncSongWebhook(
180182
songDocument as unknown as SongWithUser,
181183
);

server/src/song/song.module.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SongUploadService } from './song-upload/song-upload.service';
1111
import { SongController } from './song.controller';
1212
import { SongService } from './song.service';
1313
import { SongWebhookService } from './song-webhook/song-webhook.service';
14+
import { ConfigService } from '@nestjs/config';
1415

1516
@Module({
1617
imports: [
@@ -19,7 +20,17 @@ import { SongWebhookService } from './song-webhook/song-webhook.service';
1920
UserModule,
2021
FileModule.forRootAsync(),
2122
],
22-
providers: [SongService, SongUploadService, SongWebhookService],
23+
providers: [
24+
SongService,
25+
SongUploadService,
26+
SongWebhookService,
27+
{
28+
provide: 'DISCORD_WEBHOOK_URL',
29+
useFactory: (configService: ConfigService) =>
30+
configService.getOrThrow('DISCORD_WEBHOOK_URL'),
31+
inject: [ConfigService],
32+
},
33+
],
2334
controllers: [SongController, MySongsController],
2435
exports: [SongService],
2536
})

0 commit comments

Comments
 (0)