-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pbxEvents entity out of db watcher
- Loading branch information
1 parent
7d5bdde
commit 848df67
Showing
3 changed files
with
34 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { api, dbWatchersDisabled } from '@rocket.chat/core-services'; | ||
import type { IPbxEvent, IRocketChatRecord } from '@rocket.chat/core-typings'; | ||
import type { IBaseModel, IPbxEventsModel } from '@rocket.chat/model-typings'; | ||
import { PbxEvents } from '@rocket.chat/models'; | ||
import type { Filter } from 'mongodb'; | ||
|
||
type ClientAction = 'inserted' | 'updated' | 'removed'; | ||
|
||
async function getEntityDataById<T extends IRocketChatRecord, M extends IBaseModel<T>>(ids: T['_id'] | T['_id'][], model: M): Promise<T[]> { | ||
if (Array.isArray(ids)) { | ||
const query = { _id: { $in: ids } } as unknown as Filter<T>; | ||
return model.find(query).toArray(); | ||
} | ||
const item = await model.findOneById<T>(ids); | ||
return item ? [item] : []; | ||
} | ||
|
||
export async function broadcastOnPbxEventChanges<T extends IPbxEvent>(id: T['_id'], clientAction: ClientAction = 'updated'): Promise<void> { | ||
if (dbWatchersDisabled) { | ||
const item = await getEntityDataById<IPbxEvent, IPbxEventsModel>(id, PbxEvents); | ||
void api.broadcast('watch.pbxevents', { clientAction, id, data: item[0] }); | ||
} | ||
} |
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