refactor: remove meteor call from livechat units (server) (#35860)
Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>pull/35949/head^2
parent
5861af4727
commit
cefafb0589
@ -0,0 +1,16 @@ |
||||
import { LivechatUnit } from '@rocket.chat/models'; |
||||
|
||||
import type { CheckUnitsFromUser } from '../../../../../app/livechat/server/api/lib/livechat'; |
||||
import { checkUnitsFromUser } from '../../../../../app/livechat/server/api/lib/livechat'; |
||||
import { getUnitsFromUser } from '../methods/getUnitsFromUserRoles'; |
||||
|
||||
checkUnitsFromUser.patch(async (_next, { businessUnit, userId }: CheckUnitsFromUser) => { |
||||
if (!businessUnit) { |
||||
return; |
||||
} |
||||
const unitsFromUser = await getUnitsFromUser(userId); |
||||
const unit = await LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser }); |
||||
if (!unit) { |
||||
throw new Meteor.Error('error-unit-not-found', `Error! No Active Business Unit found with id: ${businessUnit}`); |
||||
} |
||||
}); |
||||
@ -1,19 +0,0 @@ |
||||
import { LivechatUnit } from '@rocket.chat/models'; |
||||
import mem from 'mem'; |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
async function hasUnits(): Promise<boolean> { |
||||
// @ts-expect-error - this prop is injected dynamically on ee license
|
||||
return (await LivechatUnit.countUnits({ type: 'u' })) > 0; |
||||
} |
||||
|
||||
// Units should't change really often, so we can cache the result
|
||||
const memoizedHasUnits = mem(hasUnits, { maxAge: process.env.TEST_MODE ? 1 : 10000 }); |
||||
|
||||
export async function getUnitsFromUser(): Promise<string[] | undefined> { |
||||
if (!(await memoizedHasUnits())) { |
||||
return; |
||||
} |
||||
|
||||
return Meteor.callAsync('livechat:getUnitsFromUser'); |
||||
} |
||||
Loading…
Reference in new issue