-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
447b997
commit d22c32a
Showing
22 changed files
with
457 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 27 additions & 18 deletions
45
server/src/common/notifications/notifications.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import { Controller, HttpException, Sse, UseGuards, Get, Res } from '@nestjs/common'; | ||
import { | ||
Controller, | ||
HttpException, | ||
Sse, | ||
UseGuards, | ||
Get, | ||
Res, | ||
} from '@nestjs/common'; | ||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; | ||
import { AccessTokenGuard } from '../guards/accessToken.guard'; | ||
import { NotificationsService } from './notifications.service'; | ||
import { Observable } from 'rxjs'; | ||
|
||
|
||
import { Observable, fromEvent, map } from 'rxjs'; | ||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||
import { NotificationEvent } from '@yacht/types'; | ||
|
||
@ApiTags('Notifications') | ||
@ApiBearerAuth() | ||
@UseGuards(AccessTokenGuard) | ||
@Controller('notifications') | ||
export class NotificationsController { | ||
constructor(private readonly notificationsService: NotificationsService) { } | ||
constructor( | ||
private readonly notificationsService: NotificationsService, | ||
private eventEmitter: EventEmitter2, | ||
) {} | ||
|
||
@Sse() | ||
async streamNotifications(): Promise<Observable<any>> { | ||
try { | ||
return this.notificationsService.getStream() | ||
} catch (err) { | ||
// Error Handling | ||
if (err.statusCode) { | ||
throw new HttpException(err.message, err.statusCode); | ||
} else { | ||
throw new HttpException(err.message, 500); | ||
} | ||
} | ||
@Sse() | ||
async streamNotifications(): Promise<Observable<any>> { | ||
try { | ||
return fromEvent(this.eventEmitter, 'message'); | ||
} catch (err) { | ||
// Error Handling | ||
if (err.statusCode) { | ||
throw new HttpException(err.message, err.statusCode); | ||
} else { | ||
throw new HttpException(err.message, 500); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
import { fromEvent, map } from 'rxjs'; | ||
import { Injectable } from "@nestjs/common" | ||
import { fromEvent } from 'rxjs'; | ||
import { Injectable, Scope } from '@nestjs/common'; | ||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||
import { NotificationEvent } from '@yacht/types' | ||
|
||
@Injectable() | ||
@Injectable({ scope: Scope.TRANSIENT }) | ||
export class NotificationsService { | ||
constructor(private notifications: EventEmitter2) { | ||
constructor(private eventEmitter: EventEmitter2) { | ||
} | ||
debug(message: string) { | ||
if (process.env.DEBUG === '1') { | ||
this.notifications.emit('message', { data: { message: message, level: 'debug', timeout: -1 } } as NotificationEvent) | ||
this.eventEmitter.emit('message', { data: { message: message, level: 'debug', timeout: -1 } } as NotificationEvent) | ||
} | ||
} | ||
info(message: string) { | ||
this.notifications.emit('message', { data: { message: message, level: 'info', timeout: -1 } } as NotificationEvent) | ||
this.eventEmitter.emit('message', { data: { message: message, level: 'info', timeout: -1 } } as NotificationEvent) | ||
} | ||
warn(message: string) { | ||
this.notifications.emit('message', { data: { message: message, level: 'warn', timeout: -1 } } as NotificationEvent) | ||
this.eventEmitter.emit('message', { data: { message: message, level: 'warn', timeout: -1 } } as NotificationEvent) | ||
} | ||
error(message: string) { | ||
this.notifications.emit('message', { data: { message: message, level: 'error', timeout: -1 } } as NotificationEvent) | ||
this.eventEmitter.emit('message', { data: { message: message, level: 'error', timeout: -1 } } as NotificationEvent) | ||
} | ||
success(message: string) { | ||
this.notifications.emit('message', { data: { message: message, level: 'success', timeout: -1 } } as NotificationEvent) | ||
} | ||
getStream() { | ||
return fromEvent(this.notifications, 'message') | ||
this.eventEmitter.emit('message', { data: { message: message, level: 'success', timeout: -1 } } as NotificationEvent) | ||
} | ||
// getStream() { | ||
// return fromEvent(this.eventEmitter, 'message') | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.