-
Notifications
You must be signed in to change notification settings - Fork 10k
/
sendEmailTranscriptOnClose.ts
71 lines (56 loc) · 1.98 KB
/
sendEmailTranscriptOnClose.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { LivechatRooms } from '@rocket.chat/models';
import { callbacks } from '../../../../lib/callbacks';
import { Livechat } from '../lib/LivechatTyped';
import type { CloseRoomParams } from '../lib/LivechatTyped.d';
type LivechatCloseCallbackParams = {
room: IOmnichannelRoom;
options: CloseRoomParams['options'];
};
const sendEmailTranscriptOnClose = async (params: LivechatCloseCallbackParams): Promise<LivechatCloseCallbackParams> => {
const { room, options } = params;
if (!isOmnichannelRoom(room)) {
return params;
}
const { _id: rid, v: { token } = {} } = room;
if (!token) {
return params;
}
const transcriptData = resolveTranscriptData(room, options);
if (!transcriptData) {
return params;
}
const { email, subject, requestedBy: user } = transcriptData;
await Promise.all([
Livechat.sendTranscript({ token, rid, email, subject, user }),
LivechatRooms.unsetEmailTranscriptRequestedByRoomId(rid),
]);
delete room.transcriptRequest;
return {
room,
options,
};
};
const resolveTranscriptData = (
room: IOmnichannelRoom,
options: LivechatCloseCallbackParams['options'] = {},
): IOmnichannelRoom['transcriptRequest'] | undefined => {
const { transcriptRequest: roomTranscriptRequest } = room;
const { emailTranscript: optionsTranscriptRequest } = options;
// Note: options.emailTranscript will override the room.transcriptRequest check
// If options.emailTranscript is not set, then the room.transcriptRequest will be checked
if (optionsTranscriptRequest === undefined) {
return roomTranscriptRequest;
}
if (!optionsTranscriptRequest.sendToVisitor) {
return undefined;
}
return optionsTranscriptRequest.requestData;
};
callbacks.add(
'livechat.closeRoom',
(params: LivechatCloseCallbackParams) => Promise.await(sendEmailTranscriptOnClose(params)),
callbacks.priority.HIGH,
'livechat-send-email-transcript-on-close-room',
);