All files / src/features/gateway gateway.gateway.ts

100% Statements 7/7
50% Branches 2/4
100% Functions 4/4
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                              1x       1x         2x 2x 2x       1x 1x        
import {
    WebSocketGateway,
    SubscribeMessage,
    MessageBody,
    ConnectedSocket,
    OnGatewayConnection,
    OnGatewayDisconnect,
    WebSocketServer,
} from '@nestjs/websockets'
import type { Server, Socket } from 'socket.io'
import { DonationEventPayload } from './interfaces/donation-event.payload'
 
@WebSocketGateway({
    cors: {
        origin: process.env.CORS_ORIGIN ? process.env.CORS_ORIGIN.split(',') : '*',
        credentials: true,
    },
})
export class GatewayGateway implements OnGatewayConnection, OnGatewayDisconnect {
    @WebSocketServer()
    server!: Server
 
    handleConnection(client: Socket) {
        console.log(`Client connected: ${client.id}`)
    }
 
    handleDisconnect(client: Socket) {
        console.log(`Client disconnected: ${client.id}`)
    }
 
    @SubscribeMessage('joinEvent')
    handleJoinEvent(@MessageBody() eventId: string, @ConnectedSocket() client: Socket) {
        void client.join(eventId)
        console.log(`Client ${client.id} joined event: ${eventId}`)
        return { event: 'joined', eventId }
    }
 
    emitDonation(donation: DonationEventPayload) {
        Eif (donation.eventId) {
            this.server.to(donation.eventId).emit('donation.created', donation)
        }
    }
}