-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added shared notification types & services * updated notification tests
- Loading branch information
Showing
9 changed files
with
205 additions
and
49 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
18 changes: 18 additions & 0 deletions
18
apps/chat-backend/src/services/notification/notification.service.spec.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { NotificationService } from './notification.service'; | ||
|
||
describe('NotificationService', () => { | ||
let service: NotificationService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [NotificationService] | ||
}).compile(); | ||
|
||
service = module.get<NotificationService>(NotificationService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
apps/chat-backend/src/services/notification/notification.service.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { NotificationFormat, NotificationTypes } from 'shared-sdk'; | ||
|
||
@Injectable() | ||
export class NotificationService { | ||
buildNotificationMessage({ | ||
type, | ||
room, | ||
data | ||
}: { | ||
type: NotificationTypes; | ||
room: string; | ||
data?: { | ||
[key: string]: string; | ||
}; | ||
}): NotificationFormat { | ||
return { | ||
type, | ||
room, | ||
timestamp: new Date().toISOString(), | ||
data | ||
}; | ||
} | ||
} |
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
55 changes: 45 additions & 10 deletions
55
apps/chat-frontend/src/app/services/notification/notification-service.service.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,20 +1,55 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NbGlobalPhysicalPosition, NbToastrService } from '@nebular/theme'; | ||
import { Socket } from 'ngx-socket-io'; | ||
import { EventTypes } from 'shared-sdk'; | ||
|
||
export interface NotificationMessageFormat { | ||
type: string; | ||
room: string; | ||
uid: string; | ||
timestamp: string; | ||
} | ||
import { tap } from 'rxjs'; | ||
import { EventTypes, NotificationFormat, NotificationMapping, NotificationTypes } from 'shared-sdk'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NotificationServiceService { | ||
constructor(private socket: Socket) {} | ||
constructor( | ||
private socket: Socket, | ||
private toastrService: NbToastrService | ||
) {} | ||
|
||
subscribeToNotifications() { | ||
return this.socket.fromEvent<NotificationMessageFormat>(EventTypes.NOTIFICATION); | ||
return this.socket.fromEvent<NotificationFormat>(EventTypes.NOTIFICATION).pipe( | ||
tap((notification) => { | ||
//TODO implement additional data object at some point | ||
this.showNotification(notification.type); | ||
}) | ||
); | ||
} | ||
|
||
showNotification(notificationType: NotificationTypes) { | ||
const notificationData = NotificationMapping[notificationType]; | ||
const { title, message, type } = notificationData; | ||
switch (type) { | ||
case 'info': | ||
return this.showInformationalNotification(message, title); | ||
case 'danger': | ||
return this.showErrorNotification(message, title); | ||
case 'warn': | ||
return this.showWarningotification(message, title); | ||
} | ||
} | ||
|
||
private showInformationalNotification(message: string, title?: string) { | ||
this.toastrService.info(message, title, { | ||
position: NbGlobalPhysicalPosition.TOP_RIGHT | ||
}); | ||
} | ||
|
||
private showErrorNotification(message: string, title?: string) { | ||
this.toastrService.danger(message, title, { | ||
position: NbGlobalPhysicalPosition.TOP_RIGHT | ||
}); | ||
} | ||
|
||
private showWarningotification(message: string, title?: string) { | ||
this.toastrService.warning(message, title, { | ||
position: NbGlobalPhysicalPosition.TOP_RIGHT | ||
}); | ||
} | ||
} |
Oops, something went wrong.