Skip to content

Commit

Permalink
fix: HttpService Injection in WebsocketService for Push Notification …
Browse files Browse the repository at this point in the history
…Sending (#37)

* fix: Fix HttpService injection in WebsocketService constructor to resolve undefined error

* test: Fix websocketTestModule added HttpModule into imports
  • Loading branch information
gabrielmatau79 authored Nov 13, 2024
1 parent bf2a6d5 commit 52992d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/websocket/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
result: 'error',
id: 1,
})
this.logger.error(`WebSocket server listening on port ${WS_PORT}`)
this.logger.log(`WebSocket server listening on port ${WS_PORT}`)
} catch (error) {
this.logger.error('Error during WebSocket server initialization', error.stack)
throw error
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/websocket/websocket.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { MongooseModule } from '@nestjs/mongoose'
import { StoreQueuedMessageSchema, StoreQueuedMessage } from './schemas/StoreQueuedMessage'
import { StoreLiveSessionSchema, StoreLiveSession } from './schemas/StoreLiveSession'
import { MessagePersister } from './services/MessagePersister'
import { HttpModule } from '@nestjs/axios'

@Module({
imports: [
MongooseModule.forFeature([
{ name: StoreQueuedMessage.name, schema: StoreQueuedMessageSchema },
{ name: StoreLiveSession.name, schema: StoreLiveSessionSchema },
]),
HttpModule,
],
providers: [WebsocketGateway, WebsocketService, MessagePersister],
})
Expand Down
15 changes: 14 additions & 1 deletion packages/server/src/websocket/websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { JsonRpcResponseSubscriber } from './interfaces/interfaces'
@Injectable()
export class WebsocketService {
private readonly logger: Logger
private readonly httpService: HttpService
private readonly redisSubscriber: Redis
private readonly redisPublisher: Redis
private server: Server
Expand All @@ -33,6 +32,7 @@ export class WebsocketService {
@InjectModel(StoreQueuedMessage.name) private queuedMessage: Model<StoreQueuedMessage>,
@InjectRedis() private readonly redis: Redis,
private configService: ConfigService,
private readonly httpService: HttpService,
) {
this.logger = new Logger(WebsocketService.name)
this.redisSubscriber = this.redis.duplicate()
Expand Down Expand Up @@ -452,6 +452,19 @@ export class WebsocketService {
// Retrieves the push notification URL from the configuration service
const pushNotificationUrl = this.configService.get<string>('appConfig.pushNotificationUrl')

if (!pushNotificationUrl) {
this.logger?.error('[sendPushNotification] Push notification URL is not defined in appConfig')
return false
}

this.logger?.debug(`[sendPushNotification] pushNotificationUrl: ${pushNotificationUrl}`)
if (!token || !messageId) {
this.logger?.error('[sendPushNotification] Invalid token or messageId')
return false
}

this.logger?.debug(`[sendPushNotification] token: ${token} --- messageId: ${messageId}`)

// Sends the push notification via HTTP POST request
const response = await lastValueFrom(
this.httpService.post(pushNotificationUrl, {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/test/WebsocketTestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { WebsocketGateway } from '../src/websocket/websocket.gateway'
import { MongooseModule } from '@nestjs/mongoose'
import { StoreQueuedMessageSchema, StoreQueuedMessage } from '../src/websocket/schemas/StoreQueuedMessage'
import { StoreLiveSessionSchema, StoreLiveSession } from '../src/websocket/schemas/StoreLiveSession'
import { HttpModule } from '@nestjs/axios'

@Module({
imports: [
MongooseModule.forFeature([
{ name: StoreQueuedMessage.name, schema: StoreQueuedMessageSchema },
{ name: StoreLiveSession.name, schema: StoreLiveSessionSchema },
]),
HttpModule,
],
providers: [WebsocketGateway, WebsocketService],
})
Expand Down

0 comments on commit 52992d4

Please sign in to comment.