add online/offline api
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { Socket } from 'socket.io';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ClientConnectionDto } from './dto/clientConnection.dto';
|
||||
@@ -7,13 +9,38 @@ import { SocketConnection } from './socket.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SocketService {
|
||||
private readonly logger = new Logger(SocketService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(SocketConnection)
|
||||
private socketConnectionRepository: Repository<SocketConnection>,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
|
||||
async clientConnection(data: ClientConnectionDto) {
|
||||
const client = this.socketConnectionRepository.create(data);
|
||||
|
||||
const eventEndpoints = {
|
||||
connected: '/api/operator/online',
|
||||
disconnected: '/api/operator/offline',
|
||||
};
|
||||
|
||||
const endpoint = eventEndpoints[data.event];
|
||||
|
||||
if (endpoint) {
|
||||
const url = `${process.env.SERVICE_URL}${endpoint}?telephone_id=${client.client_id}`;
|
||||
try {
|
||||
await firstValueFrom(this.httpService.get(url));
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`❌ Failed to notify ${data.event} for client ${client.client_id}:`,
|
||||
error.message || error,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
this.logger.warn(`⚠️ Unknown event type: ${data.event}`);
|
||||
}
|
||||
|
||||
await this.socketConnectionRepository.save(client);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user