refactored ws module
This commit is contained in:
72
src/dolls/dolls-notification.service.ts
Normal file
72
src/dolls/dolls-notification.service.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { WsNotificationService } from '../ws/state/ws-notification.service';
|
||||
import { UserSocketService } from '../ws/state/user-socket.service';
|
||||
import { WS_EVENT } from '../ws/state/ws-events';
|
||||
import type {
|
||||
DollCreatedEvent,
|
||||
DollUpdatedEvent,
|
||||
DollDeletedEvent,
|
||||
} from './events/doll.events';
|
||||
import { DollEvents } from './events/doll.events';
|
||||
|
||||
@Injectable()
|
||||
export class DollsNotificationService {
|
||||
private readonly logger = new Logger(DollsNotificationService.name);
|
||||
|
||||
constructor(
|
||||
private readonly wsNotificationService: WsNotificationService,
|
||||
private readonly userSocketService: UserSocketService,
|
||||
) {}
|
||||
|
||||
@OnEvent(DollEvents.DOLL_CREATED)
|
||||
async handleDollCreated(payload: DollCreatedEvent) {
|
||||
const { userId, doll } = payload;
|
||||
await this.wsNotificationService.emitToFriends(
|
||||
[userId],
|
||||
WS_EVENT.FRIEND_DOLL_CREATED,
|
||||
{
|
||||
friendId: userId,
|
||||
doll: {
|
||||
id: doll.id,
|
||||
name: doll.name,
|
||||
configuration: doll.configuration,
|
||||
createdAt: doll.createdAt,
|
||||
updatedAt: doll.updatedAt,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@OnEvent(DollEvents.DOLL_UPDATED)
|
||||
async handleDollUpdated(payload: DollUpdatedEvent) {
|
||||
const { userId, doll } = payload;
|
||||
await this.wsNotificationService.emitToFriends(
|
||||
[userId],
|
||||
WS_EVENT.FRIEND_DOLL_UPDATED,
|
||||
{
|
||||
friendId: userId,
|
||||
doll: {
|
||||
id: doll.id,
|
||||
name: doll.name,
|
||||
configuration: doll.configuration,
|
||||
createdAt: doll.createdAt,
|
||||
updatedAt: doll.updatedAt,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@OnEvent(DollEvents.DOLL_DELETED)
|
||||
async handleDollDeleted(payload: DollDeletedEvent) {
|
||||
const { userId, dollId } = payload;
|
||||
await this.wsNotificationService.emitToFriends(
|
||||
[userId],
|
||||
WS_EVENT.FRIEND_DOLL_DELETED,
|
||||
{
|
||||
friendId: userId,
|
||||
dollId,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { Module, forwardRef } from '@nestjs/common';
|
||||
import { DollsService } from './dolls.service';
|
||||
import { DollsController } from './dolls.controller';
|
||||
import { DollsNotificationService } from './dolls-notification.service';
|
||||
import { DatabaseModule } from '../database/database.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { WsModule } from '../ws/ws.module';
|
||||
|
||||
@Module({
|
||||
imports: [DatabaseModule, AuthModule],
|
||||
imports: [DatabaseModule, AuthModule, forwardRef(() => WsModule)],
|
||||
controllers: [DollsController],
|
||||
providers: [DollsService],
|
||||
providers: [DollsService, DollsNotificationService],
|
||||
exports: [DollsService],
|
||||
})
|
||||
export class DollsModule {}
|
||||
|
||||
Reference in New Issue
Block a user