-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathisAgentAvailableToTakeContactInquiry.ts
More file actions
41 lines (34 loc) · 1.91 KB
/
isAgentAvailableToTakeContactInquiry.ts
File metadata and controls
41 lines (34 loc) · 1.91 KB
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
import type { ILivechatContact, ILivechatVisitor, IOmnichannelSource } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatContacts } from '@rocket.chat/models';
import { isAgentAvailableToTakeContactInquiry } from '../../../app/livechat/server/lib/contacts/isAgentAvailableToTakeContactInquiry';
import { isVerifiedChannelInSource } from '../../../app/livechat/server/lib/contacts/isVerifiedChannelInSource';
import { settings } from '../../../app/settings/server';
// If the contact is unknown and the setting to block unknown contacts is on, we must not allow the agent to take this inquiry
// if the contact is not verified in this channel and the block unverified contacts setting is on, we should not allow the inquiry to be taken
// otherwise, the contact is allowed to be taken
export const runIsAgentAvailableToTakeContactInquiry = async (
_next: any,
visitorId: ILivechatVisitor['_id'],
source: IOmnichannelSource,
contactId: ILivechatContact['_id'],
): Promise<{ error: string; value: false } | { value: true }> => {
const contact = await LivechatContacts.findOneEnabledById<Pick<ILivechatContact, '_id' | 'unknown' | 'channels'>>(contactId, {
projection: {
unknown: 1,
channels: 1,
},
});
if (!contact) {
return { value: false, error: 'error-invalid-contact' };
}
if (contact.unknown && settings.get<boolean>('Livechat_Block_Unknown_Contacts')) {
return { value: false, error: 'error-unknown-contact' };
}
const isContactVerified = (contact.channels.filter((channel) => isVerifiedChannelInSource(channel, visitorId, source)) || []).length > 0;
if (!isContactVerified && settings.get<boolean>('Livechat_Block_Unverified_Contacts')) {
return { value: false, error: 'error-unverified-contact' };
}
return { value: true };
};
isAgentAvailableToTakeContactInquiry.patch(runIsAgentAvailableToTakeContactInquiry, () => License.hasModule('contact-id-verification'));