forked from Sofie-Automation/sofie-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluations.ts
More file actions
92 lines (83 loc) · 2.93 KB
/
evaluations.ts
File metadata and controls
92 lines (83 loc) · 2.93 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { EvaluationBase } from '@sofie-automation/meteor-lib/dist/collections/Evaluations'
import { getRandomId, getSofieHostUrl } from '@sofie-automation/corelib/dist/lib'
import { getCurrentTime } from '../lib/lib'
import { deferAsync } from '../lib/lib'
import { logger } from '../logging'
import { Meteor } from 'meteor/meteor'
import _ from 'underscore'
import { fetchStudioLight } from '../optimizations'
import { sendSlackMessageToWebhook } from './integration/slack'
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist/RundownPlaylist'
import { Evaluations, RundownPlaylists } from '../collections'
import { applyAndValidateOverrides } from '@sofie-automation/corelib/dist/settings/objectWithOverrides'
import { VerifiedRundownPlaylistForUserAction } from '../security/check'
export async function saveEvaluation(
_playlist: VerifiedRundownPlaylistForUserAction,
evaluation: EvaluationBase
): Promise<void> {
await Evaluations.insertAsync({
...evaluation,
_id: getRandomId(),
userId: null,
timestamp: getCurrentTime(),
})
logger.info({
message: 'evaluation',
evaluation: evaluation,
})
deferAsync(async () => {
const studio = await fetchStudioLight(evaluation.studioId)
if (!studio) throw new Meteor.Error(500, `Studio ${evaluation.studioId} not found!`)
const studioSettings = applyAndValidateOverrides(studio.settingsWithOverrides).obj
const webhookUrls = _.compact((studioSettings.slackEvaluationUrls || '').split(','))
if (webhookUrls.length) {
// Only send notes if not everything is OK
const evaluationLevel = _.find(evaluation.answers, (_answer, key) => {
return key === 'q0'
})
const evaluationMessage = _.find(evaluation.answers, (_answer, key) => {
return key === 'q1'
})
const evaluationProducer = _.find(evaluation.answers, (_answer, key) => {
return key === 'q2'
})
let slackMessage = 'Evaluation!'
switch (evaluationLevel) {
case 'nothing':
slackMessage = ':heavy_check_mark: Hey! Fra '
break
case 'minor':
slackMessage = ':grey_question: Ehm! Fra '
break
case 'major':
slackMessage = ':warning: Uh-oh! Fra '
break
}
// only send message for evaluations with content
if (evaluationMessage) {
const playlist = (await RundownPlaylists.findOneAsync(evaluation.playlistId, {
projection: {
_id: 1,
name: 1,
},
})) as Pick<DBRundownPlaylist, '_id' | 'name'>
const hostUrl = getSofieHostUrl()
slackMessage +=
'rundown ' +
(hostUrl && playlist
? '*<' + hostUrl + '/rundown/' + playlist._id + '|' + playlist.name + '>*'
: playlist?.name || 'N/A') +
(hostUrl ? ' in ' + hostUrl.replace(/http:\/\/|https:\/\//, '') : '') +
'\n' +
evaluationMessage +
'\n' +
'_' +
evaluationProducer +
'_'
await Promise.all(
webhookUrls.map(async (webhookUrl) => sendSlackMessageToWebhook(slackMessage, webhookUrl))
)
}
}
})
}