import { Collection, Db, Document, MongoClient, WithId } from "mongodb"; import { ActiveEntity, EntityType } from "./types"; const DATABASE_URL = "mongodb://mongoadmin:mongoadmin@localhost:27017/?replicaSet=rs0&readPreference=primaryPreferred&directConnection=true"; const DATABASE_NAME = "openvidu"; const EVENTS_COLLECTION = "events"; const ACTIVE_ENTITIES_COLLECTION = "active-entities"; const entityStartEventTypesMap: Map = new Map([ [EntityType.ROOM, "ROOM_CREATED"], [EntityType.PARTICIPANT, "PARTICIPANT_ACTIVE"] ]); const entityCloseEventTypesMap: Map = new Map([ [EntityType.ROOM, "ROOM_ENDED"], [EntityType.PARTICIPANT, "PARTICIPANT_LEFT"] ]); const entityIdFieldMap: Map = new Map([ [EntityType.ROOM, "room.sid"], [EntityType.PARTICIPANT, "participant.sid"] ]); const entityNameFieldMap: Map = new Map([ [EntityType.ROOM, "room.name"], [EntityType.PARTICIPANT, "participant.identity"] ]); export class MongoService { private static instace: MongoService; private client: MongoClient; private db: Db; private eventsCollection: Collection; private activeEntitiesCollection: Collection; private constructor() { this.client = new MongoClient(DATABASE_URL); this.db = this.client.db(DATABASE_NAME); this.eventsCollection = this.db.collection(EVENTS_COLLECTION); this.activeEntitiesCollection = this.db.collection(ACTIVE_ENTITIES_COLLECTION); } public static getInstance(): MongoService { if (!MongoService.instace) { MongoService.instace = new MongoService(); } return MongoService.instace; } public async findStartEvent(entityType: EntityType, entityName: string): Promise | null> { try { const eventType = entityStartEventTypesMap.get(entityType)!; const nameField = entityNameFieldMap.get(entityType)!; return await this.eventsCollection.findOne({ type: eventType, [nameField]: entityName }); } catch (error) { console.error("Error finding start event", error); throw error; } } public async findCloseEvent(entityType: EntityType, entityId: string): Promise | null> { try { const eventType = entityCloseEventTypesMap.get(entityType)!; const idField = entityIdFieldMap.get(entityType)!; return await this.eventsCollection.findOne({ type: eventType, [idField]: entityId }); } catch (error) { console.error("Error finding close event", error); throw error; } } public async isActiveEntity(id: string): Promise { try { const result = await this.activeEntitiesCollection.findOne({ _id: id }); return !!result; } catch (error) { console.error("Error finding active entity", error); throw error; } } }